133 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// RUN: %{compile} | %{run} | FileCheck %s22//23// Do the same run, but now with direct IR generation.24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true25// RUN: %{compile} | %{run} | FileCheck %s26//27// Do the same run, but now with vectorization.28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=4 enable-buffer-initialization=true29// RUN: %{compile} | %{run} | FileCheck %s30//31// Do the same run, but now with VLA vectorization.32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}33 34#SparseVector = #sparse_tensor.encoding<{ map = (d0) -> (d0 : compressed) }>35 36#trait_op = {37 indexing_maps = [38 affine_map<(i) -> (i)>, // a39 affine_map<(i) -> (i)> // x (out)40 ],41 iterator_types = ["parallel"],42 doc = "x(i) = OP a(i)"43}44 45module {46 // Performs sign operation (using semi-ring unary op)47 // with semantics that48 // > 0 : +1.049 // < 0 : -1.050 // +Inf: +1.051 // -Inf: -1.052 // +NaN: +NaN53 // -NaN: -NaN54 // +0.0: +0.055 // -0.0: -0.056 func.func @sparse_sign(%arg0: tensor<?xf64, #SparseVector>)57 -> tensor<?xf64, #SparseVector> {58 %c0 = arith.constant 0 : index59 %d = tensor.dim %arg0, %c0 : tensor<?xf64, #SparseVector>60 %xin = tensor.empty(%d) : tensor<?xf64, #SparseVector>61 %0 = linalg.generic #trait_op62 ins(%arg0: tensor<?xf64, #SparseVector>)63 outs(%xin: tensor<?xf64, #SparseVector>) {64 ^bb0(%a: f64, %x: f64) :65 %result = sparse_tensor.unary %a : f64 to f6466 present={67 ^bb1(%s: f64):68 %z = arith.constant 0.0 : f6469 %1 = arith.cmpf one, %s, %z : f6470 %2 = arith.uitofp %1 : i1 to f6471 %3 = math.copysign %2, %s : f6472 %4 = arith.cmpf uno, %s, %s : f6473 %5 = arith.select %4, %s, %3 : f6474 sparse_tensor.yield %5 : f6475 }76 absent={}77 linalg.yield %result : f6478 } -> tensor<?xf64, #SparseVector>79 return %0 : tensor<?xf64, #SparseVector>80 }81 82 // Driver method to call and verify sign kernel.83 func.func @main() {84 %c0 = arith.constant 0 : index85 %du = arith.constant 0.0 : f6486 87 %pnan = arith.constant 0x7FF0000001000000 : f6488 %nnan = arith.constant 0xFFF0000001000000 : f6489 %pinf = arith.constant 0x7FF0000000000000 : f6490 %ninf = arith.constant 0xFFF0000000000000 : f6491 92 // Setup sparse vector.93 %v1 = arith.constant sparse<94 [ [0], [3], [5], [11], [13], [17], [18], [20], [21], [28], [29], [31] ],95 [ -1.5, 1.5, -10.2, 11.3, 1.0, -1.0,96 0x7FF0000001000000, // +NaN97 0xFFF0000001000000, // -NaN98 0x7FF0000000000000, // +Inf99 0xFFF0000000000000, // -Inf100 -0.0, // -Zero101 0.0 // +Zero102 ]103 > : tensor<32xf64>104 %sv1 = sparse_tensor.convert %v1105 : tensor<32xf64> to tensor<?xf64, #SparseVector>106 107 // Call sign kernel.108 %0 = call @sparse_sign(%sv1) : (tensor<?xf64, #SparseVector>)109 -> tensor<?xf64, #SparseVector>110 111 //112 // Verify the results.113 //114 // CHECK: ---- Sparse Tensor ----115 // CHECK-NEXT: nse = 12116 // CHECK-NEXT: dim = ( 32 )117 // CHECK-NEXT: lvl = ( 32 )118 // CHECK-NEXT: pos[0] : ( 0, 12 )119 // CHECK-NEXT: crd[0] : ( 0, 3, 5, 11, 13, 17, 18, 20, 21, 28, 29, 31 )120 // CHECK-NEXT: values : ( -1, 1, -1, 1, 1, -1, nan, -nan, 1, -1, -0, 0 )121 // CHECK-NEXT: ----122 //123 sparse_tensor.print %0 : tensor<?xf64, #SparseVector>124 125 // Release the resources.126 bufferization.dealloc_tensor %sv1 : tensor<?xf64, #SparseVector>127 bufferization.dealloc_tensor %0 : tensor<?xf64, #SparseVector>128 return129 }130}131 132 133