135 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 parallelization strategy.29// REDEFINE: %{sparsifier_opts} = enable-runtime-library=true parallelization-strategy=any-storage-any-loop30// RUN: %{compile} | env %{env} %{run} | FileCheck %s31//32// Do the same run, but now with direct IR generation and parallelization strategy.33// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false parallelization-strategy=any-storage-any-loop34// RUN: %{compile} | env %{env} %{run} | FileCheck %s35//36// Do the same run, but now with direct IR generation and vectorization.37// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true38// RUN: %{compile} | env %{env} %{run} | FileCheck %s39//40// Do the same run, but now with direct IR generation and, if available, VLA41// vectorization.42// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | env %{env} %{run_sve} | FileCheck %s %}43 44!Filename = !llvm.ptr45 46#SparseMatrix = #sparse_tensor.encoding<{47 map = (d0, d1) -> (d0 : dense, d1 : compressed),48 posWidth = 8,49 crdWidth = 850}>51 52#matvec = {53 indexing_maps = [54 affine_map<(i,j) -> (i,j)>, // A55 affine_map<(i,j) -> (j)>, // b56 affine_map<(i,j) -> (i)> // x (out)57 ],58 iterator_types = ["parallel", "reduction"],59 doc = "X(i) += A(i,j) * B(j)"60}61 62//63// Integration test that lowers a kernel annotated as sparse to64// actual sparse code, initializes a matching sparse storage scheme65// from file, and runs the resulting code with the JIT compiler.66//67module {68 //69 // A kernel that multiplies a sparse matrix A with a dense vector b70 // into a dense vector x.71 //72 func.func @kernel_matvec(%arga: tensor<?x?xi32, #SparseMatrix>,73 %argb: tensor<?xi32>,74 %argx: tensor<?xi32>)75 -> tensor<?xi32> {76 %0 = linalg.generic #matvec77 ins(%arga, %argb: tensor<?x?xi32, #SparseMatrix>, tensor<?xi32>)78 outs(%argx: tensor<?xi32>) {79 ^bb(%a: i32, %b: i32, %x: i32):80 %0 = arith.muli %a, %b : i3281 %1 = arith.addi %x, %0 : i3282 linalg.yield %1 : i3283 } -> tensor<?xi32>84 return %0 : tensor<?xi32>85 }86 87 func.func private @getTensorFilename(index) -> (!Filename)88 89 //90 // Main driver that reads matrix from file and calls the sparse kernel.91 //92 func.func @main() {93 %i0 = arith.constant 0 : i3294 %c0 = arith.constant 0 : index95 %c1 = arith.constant 1 : index96 %c4 = arith.constant 4 : index97 %c256 = arith.constant 256 : index98 99 // Read the sparse matrix from file, construct sparse storage.100 %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)101 %a = sparse_tensor.new %fileName : !Filename to tensor<?x?xi32, #SparseMatrix>102 103 // Initialize dense vectors.104 %b = tensor.generate %c256 {105 ^bb0(%i : index):106 %k = arith.addi %i, %c1 : index107 %j = arith.index_cast %k : index to i32108 tensor.yield %j : i32109 } : tensor<?xi32>110 111 %x = tensor.generate %c4 {112 ^bb0(%i : index):113 tensor.yield %i0 : i32114 } : tensor<?xi32>115 116 // Call kernel.117 %0 = call @kernel_matvec(%a, %b, %x)118 : (tensor<?x?xi32, #SparseMatrix>, tensor<?xi32>, tensor<?xi32>) -> tensor<?xi32>119 120 // Print the result for verification.121 //122 // CHECK: ( 889, 1514, -21, -3431 )123 //124 %v = vector.transfer_read %0[%c0], %i0: tensor<?xi32>, vector<4xi32>125 vector.print %v : vector<4xi32>126 127 // Release the resources.128 bufferization.dealloc_tensor %a : tensor<?x?xi32, #SparseMatrix>129 bufferization.dealloc_tensor %b : tensor<?xi32>130 bufferization.dealloc_tensor %0 : tensor<?xi32>131 132 return133 }134}135