1// RUN: mlir-opt %s --sparse-compiler | \
2// RUN: TENSOR0="%mlir_integration_test_dir/data/test.tns" \
3// RUN: mlir-cpu-runner \
4// RUN:  -e entry -entry-point-result=void  \
5// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
6// RUN: FileCheck %s
7//
8// Do the same run, but now with SIMDization as well. This should not change the outcome.
9//
10// RUN: mlir-opt %s --sparse-compiler="vectorization-strategy=2 vl=4" | \
11// RUN: TENSOR0="%mlir_integration_test_dir/data/test.tns" \
12// RUN: mlir-cpu-runner \
13// RUN:  -e entry -entry-point-result=void  \
14// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
15// RUN: FileCheck %s
16
17!Filename = !llvm.ptr<i8>
18
19#SparseTensor = #sparse_tensor.encoding<{
20  dimLevelType = [ "compressed", "compressed", "compressed", "compressed",
21                   "compressed", "compressed", "compressed", "compressed" ],
22  // Note that any dimOrdering permutation should give the same results
23  // since, even though it impacts the sparse storage scheme layout,
24  // it should not change the semantics.
25  dimOrdering = affine_map<(i,j,k,l,m,n,o,p) -> (p,o,j,k,i,l,m,n)>
26}>
27
28#trait_flatten = {
29  indexing_maps = [
30    affine_map<(i,j,k,l,m,n,o,p) -> (i,j,k,l,m,n,o,p)>, // A
31    affine_map<(i,j,k,l,m,n,o,p) -> (i,j)>              // X (out)
32  ],
33  iterator_types = [ "parallel",  "parallel",  "reduction", "reduction",
34                     "reduction", "reduction", "reduction", "reduction" ],
35  doc = "X(i,j) += A(i,j,k,l,m,n,o,p)"
36}
37
38//
39// Integration test that lowers a kernel annotated as sparse to
40// actual sparse code, initializes a matching sparse storage scheme
41// from file, and runs the resulting code with the JIT compiler.
42//
43module {
44  //
45  // A kernel that flattens a rank 8 tensor into a dense matrix.
46  //
47  func.func @kernel_flatten(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>,
48                            %argx: tensor<7x3xf64>)
49		       -> tensor<7x3xf64> {
50    %0 = linalg.generic #trait_flatten
51      ins(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>)
52      outs(%argx: tensor<7x3xf64>) {
53      ^bb(%a: f64, %x: f64):
54        %0 = arith.addf %x, %a : f64
55        linalg.yield %0 : f64
56    } -> tensor<7x3xf64>
57    return %0 : tensor<7x3xf64>
58  }
59
60  func.func private @getTensorFilename(index) -> (!Filename)
61
62  //
63  // Main driver that reads tensor from file and calls the sparse kernel.
64  //
65  func.func @entry() {
66    %d0 = arith.constant 0.0 : f64
67    %c0 = arith.constant 0 : index
68    %c1 = arith.constant 1 : index
69    %c3 = arith.constant 3 : index
70    %c7 = arith.constant 7 : index
71
72    // Setup matrix memory that is initialized to zero.
73    %x = arith.constant dense<0.000000e+00> : tensor<7x3xf64>
74
75    // Read the sparse tensor from file, construct sparse storage.
76    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
77    %a = sparse_tensor.new %fileName : !Filename to tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>
78
79    // Call the kernel.
80    %0 = call @kernel_flatten(%a, %x)
81      : (tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>, tensor<7x3xf64>) -> tensor<7x3xf64>
82
83    // Print the result for verification.
84    //
85    // CHECK: ( 6.25, 0, 0 )
86    // CHECK: ( 4.224, 6.21, 0 )
87    // CHECK: ( 0, 0, 15.455 )
88    // CHECK: ( 0, 0, 0 )
89    // CHECK: ( 0, 0, 0 )
90    // CHECK: ( 0, 0, 0 )
91    // CHECK: ( 7, 0, 0 )
92    //
93    scf.for %i = %c0 to %c7 step %c1 {
94      %v = vector.transfer_read %0[%i, %c0], %d0: tensor<7x3xf64>, vector<3xf64>
95      vector.print %v : vector<3xf64>
96    }
97
98    // Release the resources.
99    bufferization.dealloc_tensor %a : tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>
100
101    return
102  }
103}
104