brintos

brintos / llvm-project-archived public Read only

0
0
Text · 9.6 KiB · 58592c9 Raw
256 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#DCSR = #sparse_tensor.encoding<{map = (d0, d1) -> (d0 : compressed, d1 : compressed)}>35 36//37// Traits for 2-d tensor (aka matrix) operations.38//39#trait_scale = {40  indexing_maps = [41    affine_map<(i,j) -> (i,j)>,  // A (in)42    affine_map<(i,j) -> (i,j)>   // X (out)43  ],44  iterator_types = ["parallel", "parallel"],45  doc = "X(i,j) = A(i,j) * 2.0"46}47#trait_scale_inpl = {48  indexing_maps = [49    affine_map<(i,j) -> (i,j)>   // X (out)50  ],51  iterator_types = ["parallel", "parallel"],52  doc = "X(i,j) *= 2.0"53}54#trait_op = {55  indexing_maps = [56    affine_map<(i,j) -> (i,j)>,  // A (in)57    affine_map<(i,j) -> (i,j)>,  // B (in)58    affine_map<(i,j) -> (i,j)>   // X (out)59  ],60  iterator_types = ["parallel", "parallel"],61  doc = "X(i,j) = A(i,j) OP B(i,j)"62}63 64module {65  // Scales a sparse matrix into a new sparse matrix.66  func.func @matrix_scale(%arga: tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> {67    %s = arith.constant 2.0 : f6468    %c0 = arith.constant 0 : index69    %c1 = arith.constant 1 : index70    %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #DCSR>71    %d1 = tensor.dim %arga, %c1 : tensor<?x?xf64, #DCSR>72    %xm = tensor.empty(%d0, %d1) : tensor<?x?xf64, #DCSR>73    %0 = linalg.generic #trait_scale74       ins(%arga: tensor<?x?xf64, #DCSR>)75        outs(%xm: tensor<?x?xf64, #DCSR>) {76        ^bb(%a: f64, %x: f64):77          %1 = arith.mulf %a, %s : f6478          linalg.yield %1 : f6479    } -> tensor<?x?xf64, #DCSR>80    return %0 : tensor<?x?xf64, #DCSR>81  }82 83  // Scales a sparse matrix in place.84  func.func @matrix_scale_inplace(%argx: tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> {85    %s = arith.constant 2.0 : f6486    %0 = linalg.generic #trait_scale_inpl87      outs(%argx: tensor<?x?xf64, #DCSR>) {88        ^bb(%x: f64):89          %1 = arith.mulf %x, %s : f6490          linalg.yield %1 : f6491    } -> tensor<?x?xf64, #DCSR>92    return %0 : tensor<?x?xf64, #DCSR>93  }94 95  // Adds two sparse matrices element-wise into a new sparse matrix.96  func.func @matrix_add(%arga: tensor<?x?xf64, #DCSR>,97                        %argb: tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> {98    %c0 = arith.constant 0 : index99    %c1 = arith.constant 1 : index100    %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #DCSR>101    %d1 = tensor.dim %arga, %c1 : tensor<?x?xf64, #DCSR>102    %xv = tensor.empty(%d0, %d1) : tensor<?x?xf64, #DCSR>103    %0 = linalg.generic #trait_op104       ins(%arga, %argb: tensor<?x?xf64, #DCSR>, tensor<?x?xf64, #DCSR>)105        outs(%xv: tensor<?x?xf64, #DCSR>) {106        ^bb(%a: f64, %b: f64, %x: f64):107          %1 = arith.addf %a, %b : f64108          linalg.yield %1 : f64109    } -> tensor<?x?xf64, #DCSR>110    return %0 : tensor<?x?xf64, #DCSR>111  }112 113  // Multiplies two sparse matrices element-wise into a new sparse matrix.114  func.func @matrix_mul(%arga: tensor<?x?xf64, #DCSR>,115                        %argb: tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR> {116    %c0 = arith.constant 0 : index117    %c1 = arith.constant 1 : index118    %d0 = tensor.dim %arga, %c0 : tensor<?x?xf64, #DCSR>119    %d1 = tensor.dim %arga, %c1 : tensor<?x?xf64, #DCSR>120    %xv = tensor.empty(%d0, %d1) : tensor<?x?xf64, #DCSR>121    %0 = linalg.generic #trait_op122       ins(%arga, %argb: tensor<?x?xf64, #DCSR>, tensor<?x?xf64, #DCSR>)123        outs(%xv: tensor<?x?xf64, #DCSR>) {124        ^bb(%a: f64, %b: f64, %x: f64):125          %1 = arith.mulf %a, %b : f64126          linalg.yield %1 : f64127    } -> tensor<?x?xf64, #DCSR>128    return %0 : tensor<?x?xf64, #DCSR>129  }130 131  // Driver method to call and verify matrix kernels.132  func.func @main() {133    %c0 = arith.constant 0 : index134    %d1 = arith.constant 1.1 : f64135 136    // Setup sparse matrices.137    %m1 = arith.constant sparse<138       [ [0,0], [0,1], [1,7], [2,2], [2,4], [2,7], [3,0], [3,2], [3,3] ],139         [ 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0 ]140    > : tensor<4x8xf64>141    %m2 = arith.constant sparse<142       [ [0,0], [0,7], [1,0], [1,6], [2,1], [2,7] ],143         [6.0, 5.0, 4.0, 3.0, 2.0, 1.0 ]144    > : tensor<4x8xf64>145    %sm1 = sparse_tensor.convert %m1 : tensor<4x8xf64> to tensor<?x?xf64, #DCSR>146    // TODO: Use %sm1 when we support sparse tensor copies.147    %sm1_dup = sparse_tensor.convert %m1 : tensor<4x8xf64> to tensor<?x?xf64, #DCSR>148    %sm2 = sparse_tensor.convert %m2 : tensor<4x8xf64> to tensor<?x?xf64, #DCSR>149 150    // Call sparse matrix kernels.151    %0 = call @matrix_scale(%sm1)152      : (tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR>153    %1 = call @matrix_scale_inplace(%sm1_dup)154      : (tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR>155    %2 = call @matrix_add(%1, %sm2)156      : (tensor<?x?xf64, #DCSR>, tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR>157    %3 = call @matrix_mul(%1, %sm2)158      : (tensor<?x?xf64, #DCSR>, tensor<?x?xf64, #DCSR>) -> tensor<?x?xf64, #DCSR>159 160    //161    // Verify the results.162    //163    // CHECK:      ---- Sparse Tensor ----164    // CHECK-NEXT: nse = 9165    // CHECK-NEXT: dim = ( 4, 8 )166    // CHECK-NEXT: lvl = ( 4, 8 )167    // CHECK-NEXT: pos[0] : ( 0, 4 )168    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )169    // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 )170    // CHECK-NEXT: crd[1] : ( 0, 1, 7, 2, 4, 7, 0, 2, 3 )171    // CHECK-NEXT: values : ( 1, 2, 3, 4, 5, 6, 7, 8, 9 )172    // CHECK-NEXT: ----173    //174    sparse_tensor.print %sm1 : tensor<?x?xf64, #DCSR>175 176    //177    // CHECK:      ---- Sparse Tensor ----178    // CHECK-NEXT: nse = 6179    // CHECK-NEXT: dim = ( 4, 8 )180    // CHECK-NEXT: lvl = ( 4, 8 )181    // CHECK-NEXT: pos[0] : ( 0, 3 )182    // CHECK-NEXT: crd[0] : ( 0, 1, 2 )183    // CHECK-NEXT: pos[1] : ( 0, 2, 4, 6 )184    // CHECK-NEXT: crd[1] : ( 0, 7, 0, 6, 1, 7 )185    // CHECK-NEXT: values : ( 6, 5, 4, 3, 2, 1 )186    // CHECK-NEXT: ----187    //188    sparse_tensor.print %sm2 : tensor<?x?xf64, #DCSR>189 190    //191    // CHECK:      ---- Sparse Tensor ----192    // CHECK-NEXT: nse = 9193    // CHECK-NEXT: dim = ( 4, 8 )194    // CHECK-NEXT: lvl = ( 4, 8 )195    // CHECK-NEXT: pos[0] : ( 0, 4 )196    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )197    // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 )198    // CHECK-NEXT: crd[1] : ( 0, 1, 7, 2, 4, 7, 0, 2, 3 )199    // CHECK-NEXT: values : ( 2, 4, 6, 8, 10, 12, 14, 16, 18 )200    // CHECK-NEXT: ----201    //202    sparse_tensor.print %0 : tensor<?x?xf64, #DCSR>203 204    //205    // CHECK:      ---- Sparse Tensor ----206    // CHECK-NEXT: nse = 9207    // CHECK-NEXT: dim = ( 4, 8 )208    // CHECK-NEXT: lvl = ( 4, 8 )209    // CHECK-NEXT: pos[0] : ( 0, 4 )210    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )211    // CHECK-NEXT: pos[1] : ( 0, 2, 3, 6, 9 )212    // CHECK-NEXT: crd[1] : ( 0, 1, 7, 2, 4, 7, 0, 2, 3 )213    // CHECK-NEXT: values : ( 2, 4, 6, 8, 10, 12, 14, 16, 18 )214    // CHECK-NEXT: ----215    //216    sparse_tensor.print %1 : tensor<?x?xf64, #DCSR>217 218    //219    // CHECK:      ---- Sparse Tensor ----220    // CHECK-NEXT: nse = 13221    // CHECK-NEXT: dim = ( 4, 8 )222    // CHECK-NEXT: lvl = ( 4, 8 )223    // CHECK-NEXT: pos[0] : ( 0, 4 )224    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )225    // CHECK-NEXT: pos[1] : ( 0, 3, 6, 10, 13 )226    // CHECK-NEXT: crd[1] : ( 0, 1, 7, 0, 6, 7, 1, 2, 4, 7, 0, 2, 3 )227    // CHECK-NEXT: values : ( 8, 4, 5, 4, 3, 6, 2, 8, 10, 13, 14, 16, 18 )228    // CHECK-NEXT: ----229    //230    sparse_tensor.print %2 : tensor<?x?xf64, #DCSR>231 232    //233    // CHECK:      ---- Sparse Tensor ----234    // CHECK-NEXT: nse = 2235    // CHECK-NEXT: dim = ( 4, 8 )236    // CHECK-NEXT: lvl = ( 4, 8 )237    // CHECK-NEXT: pos[0] : ( 0, 2 )238    // CHECK-NEXT: crd[0] : ( 0, 2 )239    // CHECK-NEXT: pos[1] : ( 0, 1, 2 )240    // CHECK-NEXT: crd[1] : ( 0, 7 )241    // CHECK-NEXT: values : ( 12, 12 )242    // CHECK-NEXT: ----243    //244    sparse_tensor.print %3 : tensor<?x?xf64, #DCSR>245 246    // Release the resources.247    bufferization.dealloc_tensor %sm1 : tensor<?x?xf64, #DCSR>248    bufferization.dealloc_tensor %sm1_dup : tensor<?x?xf64, #DCSR>249    bufferization.dealloc_tensor %sm2 : tensor<?x?xf64, #DCSR>250    bufferization.dealloc_tensor %0 : tensor<?x?xf64, #DCSR>251    bufferization.dealloc_tensor %2 : tensor<?x?xf64, #DCSR>252    bufferization.dealloc_tensor %3 : tensor<?x?xf64, #DCSR>253    return254  }255}256