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 --lower-affine \
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_symmetric.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=2" --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_symmetric.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 | \
27 !Filename = type !llvm.ptr<i8>
29 #SparseMatrix = #sparse_tensor.encoding<{
30 dimLevelType = [ "compressed", "compressed" ]
35 affine_map<(i,j) -> (i,j)>, // A
36 affine_map<(i,j) -> ()> // x (out)
38 iterator_types = ["reduction", "reduction"],
43 // Integration test that lowers a kernel annotated as sparse to
44 // actual sparse code, initializes a matching sparse storage scheme
45 // from file, and runs the resulting code with the JIT compiler.
49 // A kernel that sum-reduces a matrix to a single scalar.
51 func @kernel_sum_reduce(%arga: tensor<?x?xf64, #SparseMatrix>,
52 %argx: tensor<f64> {linalg.inplaceable = true}) -> tensor<f64> {
53 %0 = linalg.generic #trait_sum_reduce
54 ins(%arga: tensor<?x?xf64, #SparseMatrix>)
55 outs(%argx: tensor<f64>) {
56 ^bb(%a: f64, %x: f64):
57 %0 = arith.addf %x, %a : f64
60 return %0 : tensor<f64>
63 func private @getTensorFilename(index) -> (!Filename)
66 // Main driver that reads matrix from file and calls the sparse kernel.
69 %d0 = arith.constant 0.0 : f64
70 %c0 = arith.constant 0 : index
72 // Setup memory for a single reduction scalar,
73 // initialized to zero.
74 %xdata = memref.alloc() : memref<f64>
75 memref.store %d0, %xdata[] : memref<f64>
76 %x = bufferization.to_tensor %xdata : memref<f64>
78 // Read the sparse matrix from file, construct sparse storage.
79 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
80 %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #SparseMatrix>
83 %0 = call @kernel_sum_reduce(%a, %x)
84 : (tensor<?x?xf64, #SparseMatrix>, tensor<f64>) -> tensor<f64>
86 // Print the result for verification.
90 %m = bufferization.to_memref %0 : memref<f64>
91 %v = memref.load %m[] : memref<f64>
94 // Release the resources.
95 memref.dealloc %xdata : memref<f64>
96 sparse_tensor.release %a : tensor<?x?xf64, #SparseMatrix>