125 lines · plain
1//--------------------------------------------------------------------------------------------------2// WHEN CREATING A NEW TEST, PLEASE JUST COPY & PASTE WITHOUT EDITS.3//4// Set-up that's shared across all tests in this directory. In principle, this5// config could be moved to lit.local.cfg. However, there are downstream users that6// do not use these LIT config files. Hence why this is kept inline.7//8// DEFINE: %{sparsifier_opts} = enable-runtime-library=true9// DEFINE: %{sparsifier_opts_sve} = enable-arm-sve=true %{sparsifier_opts}10// DEFINE: %{compile} = mlir-opt %s --sparsifier="%{sparsifier_opts}"11// DEFINE: %{compile_sve} = mlir-opt %s --sparsifier="%{sparsifier_opts_sve}"12// DEFINE: %{run_libs} = -shared-libs=%mlir_c_runner_utils,%mlir_runner_utils13// DEFINE: %{run_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils14// DEFINE: %{run_opts} = -e main -entry-point-result=void15// DEFINE: %{run} = mlir-runner %{run_opts} %{run_libs}16// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs_sve}17//18// DEFINE: %{env} =19//--------------------------------------------------------------------------------------------------20 21// REDEFINE: %{env} = TENSOR0="%mlir_src_dir/test/Integration/data/wide.mtx"22// RUN: %{compile} | env %{env} %{run} | FileCheck %s23//24// Do the same run, but now with direct IR generation.25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false26// RUN: %{compile} | env %{env} %{run} | FileCheck %s27//28// Do the same run, but now with vectorization.29// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true30// RUN: %{compile} | env %{env} %{run} | FileCheck %s31//32// Do the same run, but now with VLA vectorization.33// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | env %{env} %{run_sve} | FileCheck %s %}34 35!Filename = !llvm.ptr36 37#SparseMatrix = #sparse_tensor.encoding<{38 map = (d0, d1) -> (d0 : dense, d1 : compressed)39}>40 41#spmm = {42 indexing_maps = [43 affine_map<(i,j,k) -> (i,k)>, // A44 affine_map<(i,j,k) -> (k,j)>, // B45 affine_map<(i,j,k) -> (i,j)> // X (out)46 ],47 iterator_types = ["parallel", "parallel", "reduction"],48 doc = "X(i,j) += A(i,k) * B(k,j)"49}50 51//52// Integration test that lowers a kernel annotated as sparse to53// actual sparse code, initializes a matching sparse storage scheme54// from file, and runs the resulting code with the JIT compiler.55//56module {57 //58 // A kernel that multiplies a sparse matrix A with a dense matrix B59 // into a dense matrix X.60 //61 func.func @kernel_spmm(%arga: tensor<?x?xf64, #SparseMatrix>,62 %argb: tensor<?x?xf64>,63 %argx: tensor<?x?xf64>) -> tensor<?x?xf64> {64 %0 = linalg.generic #spmm65 ins(%arga, %argb: tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>)66 outs(%argx: tensor<?x?xf64>) {67 ^bb(%a: f64, %b: f64, %x: f64):68 %0 = arith.mulf %a, %b : f6469 %1 = arith.addf %x, %0 : f6470 linalg.yield %1 : f6471 } -> tensor<?x?xf64>72 return %0 : tensor<?x?xf64>73 }74 75 func.func private @getTensorFilename(index) -> (!Filename)76 77 //78 // Main driver that reads matrix from file and calls the sparse kernel.79 //80 func.func @main() {81 %i0 = arith.constant 0.0 : f6482 %c0 = arith.constant 0 : index83 %c1 = arith.constant 1 : index84 %c4 = arith.constant 4 : index85 %c256 = arith.constant 256 : index86 87 // Read the sparse matrix from file, construct sparse storage.88 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)89 %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xf64, #SparseMatrix>90 91 // Initialize dense tensors.92 %b = tensor.generate %c256, %c4 {93 ^bb0(%i : index, %j : index):94 %k0 = arith.muli %i, %c4 : index95 %k1 = arith.addi %j, %k0 : index96 %k2 = arith.index_cast %k1 : index to i3297 %k = arith.sitofp %k2 : i32 to f6498 tensor.yield %k : f6499 } : tensor<?x?xf64>100 101 %x = tensor.generate %c4, %c4 {102 ^bb0(%i : index, %j : index):103 tensor.yield %i0 : f64104 } : tensor<?x?xf64>105 106 // Call kernel.107 %0 = call @kernel_spmm(%a, %b, %x)108 : (tensor<?x?xf64, #SparseMatrix>, tensor<?x?xf64>, tensor<?x?xf64>) -> tensor<?x?xf64>109 110 // Print the result for verification.111 //112 // CHECK: ( ( 3548, 3550, 3552, 3554 ), ( 6052, 6053, 6054, 6055 ), ( -56, -63, -70, -77 ), ( -13704, -13709, -13714, -13719 ) )113 //114 %v = vector.transfer_read %0[%c0, %c0], %i0: tensor<?x?xf64>, vector<4x4xf64>115 vector.print %v : vector<4x4xf64>116 117 // Release the resources.118 bufferization.dealloc_tensor %a : tensor<?x?xf64, #SparseMatrix>119 bufferization.dealloc_tensor %b : tensor<?x?xf64>120 bufferization.dealloc_tensor %0 : tensor<?x?xf64>121 122 return123 }124}125