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 = type !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 @kernel_flatten(%arga: tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>,
48                       %argx: tensor<7x3xf64> {linalg.inplaceable = true})
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 private @getTensorFilename(index) -> (!Filename)
61
62  //
63  // Main driver that reads tensor from file and calls the sparse kernel.
64  //
65  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    %xdata = memref.alloc() : memref<7x3xf64>
74    scf.for %i = %c0 to %c7 step %c1 {
75      scf.for %j = %c0 to %c3 step %c1 {
76        memref.store %d0, %xdata[%i, %j] : memref<7x3xf64>
77      }
78    }
79    %x = bufferization.to_tensor %xdata : memref<7x3xf64>
80
81    // Read the sparse tensor from file, construct sparse storage.
82    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
83    %a = sparse_tensor.new %fileName : !Filename to tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>
84
85    // Call the kernel.
86    %0 = call @kernel_flatten(%a, %x)
87      : (tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>, tensor<7x3xf64>) -> tensor<7x3xf64>
88
89    // Print the result for verification.
90    //
91    // CHECK: ( 6.25, 0, 0 )
92    // CHECK: ( 4.224, 6.21, 0 )
93    // CHECK: ( 0, 0, 15.455 )
94    // CHECK: ( 0, 0, 0 )
95    // CHECK: ( 0, 0, 0 )
96    // CHECK: ( 0, 0, 0 )
97    // CHECK: ( 7, 0, 0 )
98    //
99    %r = bufferization.to_memref %0 : memref<7x3xf64>
100    scf.for %i = %c0 to %c7 step %c1 {
101      %v = vector.transfer_read %r[%i, %c0], %d0: memref<7x3xf64>, vector<3xf64>
102      vector.print %v : vector<3xf64>
103    }
104
105    // Release the resources.
106    memref.dealloc %xdata : memref<7x3xf64>
107    sparse_tensor.release %a : tensor<7x3x3x3x3x3x5x3xf64, #SparseTensor>
108
109    return
110  }
111}
112