1// RUN: mlir-opt %s --sparse-compiler | \
2// RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \
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 \
11// RUN:   --sparse-compiler="vectorization-strategy=2 vl=4 enable-simd-index32" | \
12// RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \
13// RUN: mlir-cpu-runner \
14// RUN:  -e entry -entry-point-result=void  \
15// RUN:  -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
16// RUN: FileCheck %s
17//
18
19!Filename = type !llvm.ptr<i8>
20
21#SparseMatrix = #sparse_tensor.encoding<{
22  dimLevelType = [ "compressed", "compressed" ],
23  pointerBitWidth = 32,
24  indexBitWidth = 32
25}>
26
27#trait_sampled_dense_dense = {
28  indexing_maps = [
29    affine_map<(i,j,k) -> (i,j)>,  // S
30    affine_map<(i,j,k) -> (i,k)>,  // A
31    affine_map<(i,j,k) -> (k,j)>,  // B
32    affine_map<(i,j,k) -> (i,j)>   // X (out)
33  ],
34  iterator_types = ["parallel", "parallel", "reduction"],
35  doc = "X(i,j) += S(i,j) SUM_k A(i,k) B(k,j)"
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 computes a sampled matrix matrix multiplication.
46  //
47  func.func @sampled_dense_dense(%args: tensor<?x?xf32, #SparseMatrix>,
48                            %arga: tensor<?x?xf32>,
49                            %argb: tensor<?x?xf32>,
50                            %argx: tensor<?x?xf32> {linalg.inplaceable = true}) -> tensor<?x?xf32> {
51    %0 = linalg.generic #trait_sampled_dense_dense
52      ins(%args, %arga, %argb: tensor<?x?xf32, #SparseMatrix>, tensor<?x?xf32>, tensor<?x?xf32>)
53      outs(%argx: tensor<?x?xf32>) {
54        ^bb(%s: f32, %a: f32, %b: f32, %x: f32):
55          %0 = arith.mulf %a, %b : f32
56          %1 = arith.mulf %s, %0 : f32
57          %2 = arith.addf %x, %1 : f32
58          linalg.yield %2 : f32
59    } -> tensor<?x?xf32>
60    return %0 : tensor<?x?xf32>
61  }
62
63  func.func private @getTensorFilename(index) -> (!Filename)
64
65  //
66  // Main driver that reads matrix from file and calls the sparse kernel.
67  //
68  func.func @entry() {
69    %d0 = arith.constant 0.0 : f32
70    %c0 = arith.constant 0 : index
71    %c1 = arith.constant 1 : index
72    %c5 = arith.constant 5 : index
73    %c10 = arith.constant 10 : index
74
75    // Setup memory for the dense matrices and initialize.
76    %adata = memref.alloc(%c5, %c10) : memref<?x?xf32>
77    %bdata = memref.alloc(%c10, %c5) : memref<?x?xf32>
78    %xdata = memref.alloc(%c5,  %c5) : memref<?x?xf32>
79    scf.for %i = %c0 to %c5 step %c1 {
80      scf.for %j = %c0 to %c5 step %c1 {
81        memref.store %d0, %xdata[%i, %j] : memref<?x?xf32>
82      }
83      %p = arith.addi %i, %c1 : index
84      %q = arith.index_cast %p : index to i32
85      %d = arith.sitofp %q : i32 to f32
86      scf.for %j = %c0 to %c10 step %c1 {
87        memref.store %d, %adata[%i, %j] : memref<?x?xf32>
88        memref.store %d, %bdata[%j, %i] : memref<?x?xf32>
89      }
90    }
91    %a = bufferization.to_tensor %adata : memref<?x?xf32>
92    %b = bufferization.to_tensor %bdata : memref<?x?xf32>
93    %x = bufferization.to_tensor %xdata : memref<?x?xf32>
94
95    // Read the sparse matrix from file, construct sparse storage.
96    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
97    %s = sparse_tensor.new %fileName : !Filename to tensor<?x?xf32, #SparseMatrix>
98
99    // Call the kernel.
100    %0 = call @sampled_dense_dense(%s, %a, %b, %x)
101       : (tensor<?x?xf32, #SparseMatrix>,
102          tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32>
103
104    // Print the result for verification.
105    //
106    // CHECK: ( 10, 0, 0, 56, 0 )
107    // CHECK: ( 0, 80, 0, 0, 250 )
108    // CHECK: ( 0, 0, 270, 0, 0 )
109    // CHECK: ( 164, 0, 0, 640, 0 )
110    // CHECK: ( 0, 520, 0, 0, 1250 )
111    //
112    %r = bufferization.to_memref %0 : memref<?x?xf32>
113    scf.for %i = %c0 to %c5 step %c1 {
114      %v = vector.transfer_read %r[%i, %c0], %d0: memref<?x?xf32>, vector<5xf32>
115      vector.print %v : vector<5xf32>
116    }
117
118    // Release the resources.
119    memref.dealloc %adata : memref<?x?xf32>
120    memref.dealloc %bdata : memref<?x?xf32>
121    memref.dealloc %xdata : memref<?x?xf32>
122    sparse_tensor.release %s : tensor<?x?xf32, #SparseMatrix>
123
124    return
125  }
126}
127