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