1// RUN: mlir-opt %s --sparse-compiler | \ 2// RUN: TENSOR0="%mlir_integration_test_dir/data/test.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 \ 11// RUN: --sparse-compiler="vectorization-strategy=2 vl=4 enable-simd-index32" | \ 12// RUN: TENSOR0="%mlir_integration_test_dir/data/test.mtx" \ 13// RUN: mlir-cpu-runner \ 14// RUN: -e entry -entry-point-result=void \ 15// RUN: -shared-libs=%mlir_integration_test_dir/libmlir_c_runner_utils%shlibext | \ 16// RUN: FileCheck %s 17// 18 19!Filename = !llvm.ptr<i8> 20 21#SparseMatrix = #sparse_tensor.encoding<{ 22 dimLevelType = [ "compressed", "compressed" ], 23 pointerBitWidth = 32, 24 indexBitWidth = 32 25}> 26 27#trait_sampled_dense_dense = { 28 indexing_maps = [ 29 affine_map<(i,j,k) -> (i,j)>, // S 30 affine_map<(i,j,k) -> (i,k)>, // A 31 affine_map<(i,j,k) -> (k,j)>, // B 32 affine_map<(i,j,k) -> (i,j)> // X (out) 33 ], 34 iterator_types = ["parallel", "parallel", "reduction"], 35 doc = "X(i,j) += S(i,j) SUM_k A(i,k) B(k,j)" 36} 37 38// 39// Integration test that lowers a kernel annotated as sparse to 40// actual sparse code, initializes a matching sparse storage scheme 41// from file, and runs the resulting code with the JIT compiler. 42// 43module { 44 // 45 // A kernel that computes a sampled matrix matrix multiplication. 46 // 47 func.func @sampled_dense_dense(%args: tensor<?x?xf32, #SparseMatrix>, 48 %arga: tensor<?x?xf32>, 49 %argb: tensor<?x?xf32>, 50 %argx: tensor<?x?xf32>) -> tensor<?x?xf32> { 51 %0 = linalg.generic #trait_sampled_dense_dense 52 ins(%args, %arga, %argb: tensor<?x?xf32, #SparseMatrix>, tensor<?x?xf32>, tensor<?x?xf32>) 53 outs(%argx: tensor<?x?xf32>) { 54 ^bb(%s: f32, %a: f32, %b: f32, %x: f32): 55 %0 = arith.mulf %a, %b : f32 56 %1 = arith.mulf %s, %0 : f32 57 %2 = arith.addf %x, %1 : f32 58 linalg.yield %2 : f32 59 } -> tensor<?x?xf32> 60 return %0 : tensor<?x?xf32> 61 } 62 63 func.func private @getTensorFilename(index) -> (!Filename) 64 65 // 66 // Main driver that reads matrix from file and calls the sparse kernel. 67 // 68 func.func @entry() { 69 %d0 = arith.constant 0.0 : f32 70 %c0 = arith.constant 0 : index 71 %c1 = arith.constant 1 : index 72 %c5 = arith.constant 5 : index 73 %c10 = arith.constant 10 : index 74 75 // Setup memory for the dense matrices and initialize. 76 %a0 = bufferization.alloc_tensor(%c5, %c10) : tensor<?x?xf32> 77 %b0 = bufferization.alloc_tensor(%c10, %c5) : tensor<?x?xf32> 78 %x0 = bufferization.alloc_tensor(%c5, %c5) : tensor<?x?xf32> 79 %a, %b, %x = scf.for %i = %c0 to %c5 step %c1 iter_args(%a1 = %a0, %b1 = %b0, %x1 = %x0) 80 -> (tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) { 81 %x2 = scf.for %j = %c0 to %c5 step %c1 iter_args(%x3 = %x1) -> (tensor<?x?xf32>) { 82 %x4 = tensor.insert %d0 into %x3[%i, %j] : tensor<?x?xf32> 83 scf.yield %x4 : tensor<?x?xf32> 84 } 85 %p = arith.addi %i, %c1 : index 86 %q = arith.index_cast %p : index to i32 87 %d = arith.sitofp %q : i32 to f32 88 %a2, %b2 = scf.for %j = %c0 to %c10 step %c1 iter_args(%a3 = %a1, %b3 = %b1) 89 -> (tensor<?x?xf32>, tensor<?x?xf32>) { 90 %a4 = tensor.insert %d into %a3[%i, %j] : tensor<?x?xf32> 91 %b4 = tensor.insert %d into %b3[%j, %i] : tensor<?x?xf32> 92 scf.yield %a4, %b4 : tensor<?x?xf32>, tensor<?x?xf32> 93 } 94 scf.yield %a2, %b2, %x2 : tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32> 95 } 96 97 // Read the sparse matrix from file, construct sparse storage. 98 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename) 99 %s = sparse_tensor.new %fileName : !Filename to tensor<?x?xf32, #SparseMatrix> 100 101 // Call the kernel. 102 %0 = call @sampled_dense_dense(%s, %a, %b, %x) 103 : (tensor<?x?xf32, #SparseMatrix>, 104 tensor<?x?xf32>, tensor<?x?xf32>, tensor<?x?xf32>) -> tensor<?x?xf32> 105 106 // Print the result for verification. 107 // 108 // CHECK: ( 10, 0, 0, 56, 0 ) 109 // CHECK: ( 0, 80, 0, 0, 250 ) 110 // CHECK: ( 0, 0, 270, 0, 0 ) 111 // CHECK: ( 164, 0, 0, 640, 0 ) 112 // CHECK: ( 0, 520, 0, 0, 1250 ) 113 // 114 scf.for %i = %c0 to %c5 step %c1 { 115 %v = vector.transfer_read %0[%i, %c0], %d0: tensor<?x?xf32>, vector<5xf32> 116 vector.print %v : vector<5xf32> 117 } 118 119 // Release the resources. 120 bufferization.dealloc_tensor %s : tensor<?x?xf32, #SparseMatrix> 121 122 return 123 } 124} 125