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.mtx" \
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 | \
13 // Do the same run, but now with SIMDization as well. This should not change the outcome.
16 // RUN: --sparsification="vectorization-strategy=2 vl=4 enable-simd-index32" --sparse-tensor-conversion \
17 // RUN: --convert-vector-to-scf --convert-scf-to-std \
18 // RUN: --func-bufferize --tensor-constant-bufferize --tensor-bufferize \
19 // RUN: --std-bufferize --finalizing-bufferize --lower-affine \
20 // RUN: --convert-vector-to-llvm --convert-memref-to-llvm --convert-std-to-llvm --reconcile-unrealized-casts | \
21 // RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \
22 // RUN: mlir-cpu-runner \
23 // RUN: -e entry -entry-point-result=void \
24 // RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \
28 !Filename = type !llvm.ptr<i8>
30 #SparseMatrix = #sparse_tensor.encoding<{
31 dimLevelType = [ "compressed", "compressed" ],
36 #trait_sampled_dense_dense = {
38 affine_map<(i,j,k) -> (i,j)>, // S
39 affine_map<(i,j,k) -> (i,k)>, // A
40 affine_map<(i,j,k) -> (k,j)>, // B
41 affine_map<(i,j,k) -> (i,j)> // X (out)
43 iterator_types = ["parallel", "parallel", "reduction"],
44 doc = "X(i,j) += S(i,j) SUM_k A(i,k) B(k,j)"
48 // Integration test that lowers a kernel annotated as sparse to
49 // actual sparse code, initializes a matching sparse storage scheme
50 // from file, and runs the resulting code with the JIT compiler.
54 // A kernel that computes a sampled matrix matrix multiplication.
56 func @sampled_dense_dense(%args: tensor<?x?xf32, #SparseMatrix>,
57 %arga: tensor<?x?xf32>,
58 %argb: tensor<?x?xf32>,
59 %argx: tensor<?x?xf32> {linalg.inplaceable = true}) -> tensor<?x?xf32> {
60 %0 = linalg.generic #trait_sampled_dense_dense
61 ins(%args, %arga, %argb: tensor<?x?xf32, #SparseMatrix>, tensor<?x?xf32>, tensor<?x?xf32>)
62 outs(%argx: tensor<?x?xf32>) {
63 ^bb(%s: f32, %a: f32, %b: f32, %x: f32):
64 %0 = arith.mulf %a, %b : f32
65 %1 = arith.mulf %s, %0 : f32
66 %2 = arith.addf %x, %1 : f32
69 return %0 : tensor<?x?xf32>
72 func private @getTensorFilename(index) -> (!Filename)
75 // Main driver that reads matrix from file and calls the sparse kernel.
78 %d0 = arith.constant 0.0 : f32
79 %c0 = arith.constant 0 : index
80 %c1 = arith.constant 1 : index
81 %c5 = arith.constant 5 : index
82 %c10 = arith.constant 10 : index
84 // Setup memory for the dense matrices and initialize.
85 %adata = memref.alloc(%c5, %c10) : memref<?x?xf32>
86 %bdata = memref.alloc(%c10, %c5) : memref<?x?xf32>
87 %xdata = memref.alloc(%c5, %c5) : memref<?x?xf32>
88 scf.for %i = %c0 to %c5 step %c1 {
89 scf.for %j = %c0 to %c5 step %c1 {
90 memref.store %d0, %xdata[%i, %j] : memref<?x?xf32>
92 %p = arith.addi %i, %c1 : index
93 %q = arith.index_cast %p : index to i32
94 %d = arith.sitofp %q : i32 to f32
95 scf.for %j = %c0 to %c10 step %c1 {
96 memref.store %d, %adata[%i, %j] : memref<?x?xf32>
97 memref.store %d, %bdata[%j, %i] : memref<?x?xf32>
100 %a = bufferization.to_tensor %adata : memref<?x?xf32>
101 %b = bufferization.to_tensor %bdata : memref<?x?xf32>
102 %x = bufferization.to_tensor %xdata : memref<?x?xf32>
104 // Read the sparse matrix from file, construct sparse storage.
105 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
106 %s = sparse_tensor.new %fileName : !Filename to tensor<?x?xf32, #SparseMatrix>
109 %0 = call @sampled_dense_dense(%s, %a, %b, %x)
110 : (tensor<?x?xf32, #SparseMatrix>,
111 tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32>
113 // Print the result for verification.
115 // CHECK: ( 10, 0, 0, 56, 0 )
116 // CHECK: ( 0, 80, 0, 0, 250 )
117 // CHECK: ( 0, 0, 270, 0, 0 )
118 // CHECK: ( 164, 0, 0, 640, 0 )
119 // CHECK: ( 0, 520, 0, 0, 1250 )
121 %r = bufferization.to_memref %0 : memref<?x?xf32>
122 scf.for %i = %c0 to %c5 step %c1 {
123 %v = vector.transfer_read %r[%i, %c0], %d0: memref<?x?xf32>, vector<5xf32>
124 vector.print %v : vector<5xf32>
127 // Release the resources.
128 memref.dealloc %adata : memref<?x?xf32>
129 memref.dealloc %bdata : memref<?x?xf32>
130 memref.dealloc %xdata : memref<?x?xf32>
131 sparse_tensor.release %s : tensor<?x?xf32, #SparseMatrix>