brintos

brintos / llvm-project-archived public Read only

0
0
Text · 4.7 KiB · 94fa5fb Raw
112 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=false25// RUN: %{compile} | %{run} | FileCheck %s26//27// Do the same run, but now with direct IR generation and vectorization.28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true29// RUN: %{compile} | %{run} | FileCheck %s30//31// Do the same run, but now with direct IR generation and 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#DenseVector = #sparse_tensor.encoding<{map = (d0) -> (d0 : dense)}>36 37#trait_vec_op = {38  indexing_maps = [39    affine_map<(i) -> (i)>,  // a (in)40    affine_map<(i) -> (i)>,  // b (in)41    affine_map<(i) -> (i)>   // x (out)42  ],43  iterator_types = ["parallel"]44}45 46module {47  // Creates a dense vector using the minimum values from two input sparse vectors.48  // When there is no overlap, include the present value in the output.49  func.func @vector_min(%arga: tensor<?xf16, #SparseVector>,50                        %argb: tensor<?xf16, #SparseVector>) -> tensor<?xf16, #DenseVector> {51    %c = arith.constant 0 : index52    %d = tensor.dim %arga, %c : tensor<?xf16, #SparseVector>53    %xv = tensor.empty (%d) : tensor<?xf16, #DenseVector>54    %0 = linalg.generic #trait_vec_op55       ins(%arga, %argb: tensor<?xf16, #SparseVector>, tensor<?xf16, #SparseVector>)56        outs(%xv: tensor<?xf16, #DenseVector>) {57        ^bb(%a: f16, %b: f16, %x: f16):58          %1 = sparse_tensor.binary %a, %b : f16, f16 to f1659            overlap={60              ^bb0(%a0: f16, %b0: f16):61                %cmp = arith.cmpf "olt", %a0, %b0 : f1662                %2 = arith.select %cmp, %a0, %b0: f1663                sparse_tensor.yield %2 : f1664            }65            left=identity66            right=identity67          linalg.yield %1 : f1668    } -> tensor<?xf16, #DenseVector>69    return %0 : tensor<?xf16, #DenseVector>70  }71 72  // Driver method to call and verify the kernel.73  func.func @main() {74    %c0 = arith.constant 0 : index75 76    // Setup sparse vectors.77    %v1 = arith.constant sparse<78       [ [0], [3], [11], [17], [20], [21], [28], [29], [31] ],79         [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]80    > : tensor<32xf16>81    %v2 = arith.constant sparse<82       [ [1], [3], [4], [10], [16], [18], [21], [28], [29], [31] ],83         [11.0, 12.0, 13.0, 14.0, 15.0, 16.0, 17.0, 18.0, 19.0, 20.0 ]84    > : tensor<32xf16>85    %sv1 = sparse_tensor.convert %v1 : tensor<32xf16> to tensor<?xf16, #SparseVector>86    %sv2 = sparse_tensor.convert %v2 : tensor<32xf16> to tensor<?xf16, #SparseVector>87 88    // Call the sparse vector kernel.89    %0 = call @vector_min(%sv1, %sv2)90       : (tensor<?xf16, #SparseVector>,91          tensor<?xf16, #SparseVector>) -> tensor<?xf16, #DenseVector>92 93    //94    // Verify the result.95    //96    // CHECK:      ---- Sparse Tensor ----97    // CHECK-NEXT: nse = 3298    // CHECK-NEXT: dim = ( 32 )99    // CHECK-NEXT: lvl = ( 32 )100    // CHECK-NEXT: values : ( 1, 11, 0, 2, 13, 0, 0, 0, 0, 0, 14, 3, 0, 0, 0, 0, 15, 4, 16, 0, 5, 6, 0, 0, 0, 0, 0, 0, 7, 8, 0, 9 )101    // CHECK-NEXT: ----102    //103    sparse_tensor.print %0 : tensor<?xf16, #DenseVector>104 105    // Release the resources.106    bufferization.dealloc_tensor %sv1 : tensor<?xf16, #SparseVector>107    bufferization.dealloc_tensor %sv2 : tensor<?xf16, #SparseVector>108    bufferization.dealloc_tensor %0 : tensor<?xf16, #DenseVector>109    return110  }111}112