102 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// RUN: %{compile} | %{run} | FileCheck %s21//22// Do the same run, but now with direct IR generation.23// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false24// RUN: %{compile} | %{run} | FileCheck %s25//26// Do the same run, but now with vectorization.27// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true28// RUN: %{compile} | %{run} | FileCheck %s29//30// Do the same run, but now with VLA vectorization.31// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}32 33!Filename = !llvm.ptr34 35#SparseMatrix = #sparse_tensor.encoding<{36 map = (d0, d1) -> (d0 : compressed, d1 : compressed)37}>38 39#trait_sum_reduce = {40 indexing_maps = [41 affine_map<(i,j) -> (i,j)>, // A42 affine_map<(i,j) -> ()> // x (out)43 ],44 iterator_types = ["reduction", "reduction"],45 doc = "x += A(i,j)"46}47 48module {49 //50 // A kernel that sum-reduces a matrix to a single scalar.51 //52 func.func @kernel_sum_reduce(%arga: tensor<?x?xbf16, #SparseMatrix>,53 %argx: tensor<bf16>) -> tensor<bf16> {54 %0 = linalg.generic #trait_sum_reduce55 ins(%arga: tensor<?x?xbf16, #SparseMatrix>)56 outs(%argx: tensor<bf16>) {57 ^bb(%a: bf16, %x: bf16):58 %0 = arith.addf %x, %a : bf1659 linalg.yield %0 : bf1660 } -> tensor<bf16>61 return %0 : tensor<bf16>62 }63 64 func.func private @getTensorFilename(index) -> (!Filename)65 66 //67 // Main driver that reads matrix from file and calls the sparse kernel.68 //69 func.func @main() {70 // Setup input sparse matrix from compressed constant.71 %d = arith.constant dense <[72 [ 1.1, 1.2, 0.0, 1.4 ],73 [ 0.0, 0.0, 0.0, 0.0 ],74 [ 3.1, 0.0, 3.3, 3.4 ]75 ]> : tensor<3x4xbf16>76 %a = sparse_tensor.convert %d : tensor<3x4xbf16> to tensor<?x?xbf16, #SparseMatrix>77 78 %d0 = arith.constant 0.0 : bf1679 // Setup memory for a single reduction scalar,80 // initialized to zero.81 %x = tensor.from_elements %d0 : tensor<bf16>82 83 // Call the kernel.84 %0 = call @kernel_sum_reduce(%a, %x)85 : (tensor<?x?xbf16, #SparseMatrix>, tensor<bf16>) -> tensor<bf16>86 87 // Print the result for verification.88 //89 // CHECK: 13.590 //91 %v = tensor.extract %0[] : tensor<bf16>92 %vf = arith.extf %v: bf16 to f3293 vector.print %vf : f3294 95 // Release the resources.96 bufferization.dealloc_tensor %a : tensor<?x?xbf16, #SparseMatrix>97 bufferization.dealloc_tensor %0 : tensor<bf16>98 99 return100 }101}102