1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: TENSOR0="%mlir_integration_test_dir/data/test_symmetric.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 --sparse-compiler="vectorization-strategy=2 vl=2" | \ 11// RUN: TENSOR0="%mlir_integration_test_dir/data/test_symmetric.mtx" \ 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#SparseMatrix = #sparse_tensor.encoding<{ 20 dimLevelType = [ "compressed", "compressed" ] 21}> 22 23#trait_sum_reduce = { 24 indexing_maps = [ 25 affine_map<(i,j) -> (i,j)>, // A 26 affine_map<(i,j) -> ()> // x (out) 27 ], 28 iterator_types = ["reduction", "reduction"], 29 doc = "x += A(i,j)" 30} 31 32// 33// Integration test that lowers a kernel annotated as sparse to 34// actual sparse code, initializes a matching sparse storage scheme 35// from file, and runs the resulting code with the JIT compiler. 36// 37module { 38 // 39 // A kernel that sum-reduces a matrix to a single scalar. 40 // 41 func @kernel_sum_reduce(%arga: tensor<?x?xf64, #SparseMatrix>, 42 %argx: tensor<f64> {linalg.inplaceable = true}) -> tensor<f64> { 43 %0 = linalg.generic #trait_sum_reduce 44 ins(%arga: tensor<?x?xf64, #SparseMatrix>) 45 outs(%argx: tensor<f64>) { 46 ^bb(%a: f64, %x: f64): 47 %0 = arith.addf %x, %a : f64 48 linalg.yield %0 : f64 49 } -> tensor<f64> 50 return %0 : tensor<f64> 51 } 52 53 func private @getTensorFilename(index) -> (!Filename) 54 55 // 56 // Main driver that reads matrix from file and calls the sparse kernel. 57 // 58 func @entry() { 59 %d0 = arith.constant 0.0 : f64 60 %c0 = arith.constant 0 : index 61 62 // Setup memory for a single reduction scalar, 63 // initialized to zero. 64 %xdata = memref.alloc() : memref<f64> 65 memref.store %d0, %xdata[] : memref<f64> 66 %x = bufferization.to_tensor %xdata : memref<f64> 67 68 // Read the sparse matrix from file, construct sparse storage. 69 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 70 %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #SparseMatrix> 71 72 // Call the kernel. 73 %0 = call @kernel_sum_reduce(%a, %x) 74 : (tensor<?x?xf64, #SparseMatrix>, tensor<f64>) -> tensor<f64> 75 76 // Print the result for verification. 77 // 78 // CHECK: 30.2 79 // 80 %m = bufferization.to_memref %0 : memref<f64> 81 %v = memref.load %m[] : memref<f64> 82 vector.print %v : f64 83 84 // Release the resources. 85 memref.dealloc %xdata : memref<f64> 86 sparse_tensor.release %a : tensor<?x?xf64, #SparseMatrix> 87 88 return 89 } 90} 91