1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: TENSOR0="%mlir_integration_test_dir/data/wide.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/wide.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 = [ "dense", "compressed" ] 21}> 22 23#spmm = { 24 indexing_maps = [ 25 affine_map<(i,j,k) -> (i,k)>, // A 26 affine_map<(i,j,k) -> (k,j)>, // B 27 affine_map<(i,j,k) -> (i,j)> // X (out) 28 ], 29 iterator_types = ["parallel", "parallel", "reduction"], 30 doc = "X(i,j) += A(i,k) * B(k,j)" 31} 32 33// 34// Integration test that lowers a kernel annotated as sparse to 35// actual sparse code, initializes a matching sparse storage scheme 36// from file, and runs the resulting code with the JIT compiler. 37// 38module { 39 // 40 // A kernel that multiplies a sparse matrix A with a dense matrix B 41 // into a dense matrix X. 42 // 43 func.func @kernel_spmm(%arga: tensor<?x?xf64, #SparseMatrix>, 44 %argb: tensor<?x?xf64>, 45 %argx: tensor<?x?xf64> {linalg.inplaceable = true}) -> tensor<?x?xf64> { 46 %0 = linalg.generic #spmm 47 ins(%arga, %argb: tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>) 48 outs(%argx: tensor<?x?xf64>) { 49 ^bb(%a: f64, %b: f64, %x: f64): 50 %0 = arith.mulf %a, %b : f64 51 %1 = arith.addf %x, %0 : f64 52 linalg.yield %1 : f64 53 } -> tensor<?x?xf64> 54 return %0 : tensor<?x?xf64> 55 } 56 57 func.func private @getTensorFilename(index) -> (!Filename) 58 59 // 60 // Main driver that reads matrix from file and calls the sparse kernel. 61 // 62 func.func @entry() { 63 %i0 = arith.constant 0.0 : f64 64 %c0 = arith.constant 0 : index 65 %c1 = arith.constant 1 : index 66 %c4 = arith.constant 4 : index 67 %c256 = arith.constant 256 : index 68 69 // Read the sparse matrix from file, construct sparse storage. 70 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 71 %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #SparseMatrix> 72 73 // Initialize dense vectors. 74 %bdata = memref.alloc(%c256, %c4) : memref<?x?xf64> 75 %xdata = memref.alloc(%c4, %c4) : memref<?x?xf64> 76 scf.for %i = %c0 to %c256 step %c1 { 77 scf.for %j = %c0 to %c4 step %c1 { 78 %k0 = arith.muli %i, %c4 : index 79 %k1 = arith.addi %j, %k0 : index 80 %k2 = arith.index_cast %k1 : index to i32 81 %k = arith.sitofp %k2 : i32 to f64 82 memref.store %k, %bdata[%i, %j] : memref<?x?xf64> 83 } 84 } 85 scf.for %i = %c0 to %c4 step %c1 { 86 scf.for %j = %c0 to %c4 step %c1 { 87 memref.store %i0, %xdata[%i, %j] : memref<?x?xf64> 88 } 89 } 90 %b = bufferization.to_tensor %bdata : memref<?x?xf64> 91 %x = bufferization.to_tensor %xdata : memref<?x?xf64> 92 93 // Call kernel. 94 %0 = call @kernel_spmm(%a, %b, %x) 95 : (tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>, tensor<?x?xf64>) -> tensor<?x?xf64> 96 97 // Print the result for verification. 98 // 99 // CHECK: ( ( 3548, 3550, 3552, 3554 ), ( 6052, 6053, 6054, 6055 ), ( -56, -63, -70, -77 ), ( -13704, -13709, -13714, -13719 ) ) 100 // 101 %m = bufferization.to_memref %0 : memref<?x?xf64> 102 %v = vector.transfer_read %m[%c0, %c0], %i0: memref<?x?xf64>, vector<4x4xf64> 103 vector.print %v : vector<4x4xf64> 104 105 // Release the resources. 106 memref.dealloc %bdata : memref<?x?xf64> 107 memref.dealloc %xdata : memref<?x?xf64> 108 sparse_tensor.release %a : tensor<?x?xf64, #SparseMatrix> 109 110 return 111 } 112} 113