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