brintos

brintos / llvm-project-archived public Read only

0
0
Text · 6.8 KiB · 505729c Raw
169 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 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<{35  map = (d0, d1) -> (d0 : compressed, d1 : compressed)36}>37 38#trait = {39  indexing_maps = [40    affine_map<(i,j) -> (i,j)>, // A41    affine_map<(i,j) -> (i,j)>, // B42    affine_map<(i,j) -> (i,j)>  // x (out)43  ],44  iterator_types = ["parallel", "parallel"],45  doc = "X(i, j) = cmp A(i,j) B(i, j)"46}47 48//49// Integration test that lowers a kernel annotated as sparse to50// actual sparse code, initializes a matching sparse storage scheme51// from file, and runs the resulting code with the JIT compiler.52//53module {54  func.func @cmp_all_dense(%arga: tensor<4x4xf64>,55                           %argb: tensor<4x4xf64>,56                           %argx: tensor<4x4xi8>) -> tensor<4x4xi8> {57    %0 = linalg.generic #trait58       ins(%arga, %argb: tensor<4x4xf64>, tensor<4x4xf64>)59      outs(%argx: tensor<4x4xi8>) {60        ^bb(%a: f64, %b: f64, %x: i8):61          %0 = arith.cmpf ult, %a, %b : f6462          %1 = arith.extui %0 : i1 to i863          linalg.yield %1 : i864    } -> tensor<4x4xi8>65    return %0 : tensor<4x4xi8>66  }67 68  func.func @cmp_lhs_sparse(%arga: tensor<4x4xf64, #DCSR>,69                            %argb: tensor<4x4xf64>) -> tensor<4x4xi8, #DCSR> {70    %argx = tensor.empty() : tensor<4x4xi8, #DCSR>71    %0 = linalg.generic #trait72       ins(%arga, %argb: tensor<4x4xf64, #DCSR>, tensor<4x4xf64>)73      outs(%argx: tensor<4x4xi8, #DCSR>) {74        ^bb(%a: f64, %b: f64, %x: i8):75          %0 = arith.cmpf ult, %a, %b : f6476          %1 = arith.extui %0 : i1 to i877          linalg.yield %1 : i878    } -> tensor<4x4xi8, #DCSR>79    return %0 : tensor<4x4xi8, #DCSR>80  }81 82  func.func @cmp_all_sparse(%arga: tensor<4x4xf64, #DCSR>,83                            %argb: tensor<4x4xf64, #DCSR>) -> tensor<4x4xi8, #DCSR> {84    %argx = tensor.empty() : tensor<4x4xi8, #DCSR>85    %0 = linalg.generic #trait86       ins(%arga, %argb: tensor<4x4xf64, #DCSR>, tensor<4x4xf64, #DCSR>)87      outs(%argx: tensor<4x4xi8, #DCSR>) {88        ^bb(%a: f64, %b: f64, %x: i8):89          %0 = arith.cmpf ult, %a, %b : f6490          %1 = arith.extui %0 : i1 to i891          linalg.yield %1 : i892    } -> tensor<4x4xi8, #DCSR>93    return %0 : tensor<4x4xi8, #DCSR>94  }95 96  //97  // Main driver that constructs matrix and calls the sparse kernel to perform98  // element-wise comparison.99  //100  func.func @main() {101    %d0 = arith.constant 0 : i8102    %c0 = arith.constant 0 : index103 104    %lhs_dn = arith.constant dense<105      [ [ 0.0, 0.0, 1.5, 1.0],106        [ 0.0, 3.5, 0.0, 0.0],107        [ 1.0, 5.0, 2.0, 0.0],108        [ 1.0, 0.5, 0.0, 0.0] ]> : tensor<4x4xf64>109 110    %rhs_dn = arith.constant dense<111      [ [ 0.0, 1.5, 1.0, 1.5],112        [ 3.5, 0.0, 0.0, 0.0],113        [ 5.0, 2.0, 0.0, 2.0],114        [ 0.5, 0.0, 0.0, 0.0] ]> : tensor<4x4xf64>115 116    %lhs_sp = sparse_tensor.convert %lhs_dn : tensor<4x4xf64> to tensor<4x4xf64, #DCSR>117    %rhs_sp = sparse_tensor.convert %rhs_dn : tensor<4x4xf64> to tensor<4x4xf64, #DCSR>118 119    %output = arith.constant dense<0> : tensor<4x4xi8>120    %all_dn_out = call @cmp_all_dense(%lhs_dn, %rhs_dn, %output)121            : (tensor<4x4xf64>, tensor<4x4xf64>, tensor<4x4xi8>) -> tensor<4x4xi8>122    %lhs_sp_out = call @cmp_lhs_sparse(%lhs_sp, %rhs_dn)123            : (tensor<4x4xf64, #DCSR>, tensor<4x4xf64>) -> tensor<4x4xi8, #DCSR>124    %all_sp_out = call @cmp_all_sparse(%lhs_sp, %rhs_sp)125            : (tensor<4x4xf64, #DCSR>, tensor<4x4xf64, #DCSR>) -> tensor<4x4xi8, #DCSR>126 127    //128    // All should have the same boolean values.129    //130    // CHECK: ( ( 0, 1, 0, 1 ), ( 1, 0, 0, 0 ), ( 1, 0, 0, 1 ), ( 0, 0, 0, 0 ) )131    //132    // CHECK:      ---- Sparse Tensor ----133    // CHECK-NEXT: nse = 16134    // CHECK-NEXT: dim = ( 4, 4 )135    // CHECK-NEXT: lvl = ( 4, 4 )136    // CHECK-NEXT: pos[0] : ( 0, 4 )137    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )138    // CHECK-NEXT: pos[1] : ( 0, 4, 8, 12, 16 )139    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 )140    // CHECK-NEXT: values : ( 0, 1, 0, 1, 1, 0, 0, 0, 1, 0, 0, 1, 0, 0, 0, 0 )141    // CHECK-NEXT: ----142    //143    // CHECK:      ---- Sparse Tensor ----144    // CHECK-NEXT: nse = 11145    // CHECK-NEXT: dim = ( 4, 4 )146    // CHECK-NEXT: lvl = ( 4, 4 )147    // CHECK-NEXT: pos[0] : ( 0, 4 )148    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3 )149    // CHECK-NEXT: pos[1] : ( 0, 3, 5, 9, 11 )150    // CHECK-NEXT: crd[1] : ( 1, 2, 3, 0, 1, 0, 1, 2, 3, 0, 1 )151    // CHECK-NEXT: values : ( 1, 0, 1, 1, 0, 1, 0, 0, 1, 0, 0 )152    // CHECK-NEXT: ----153    //154    %v = vector.transfer_read %all_dn_out[%c0, %c0], %d0155       : tensor<4x4xi8>, vector<4x4xi8>156    vector.print %v : vector<4x4xi8>157    sparse_tensor.print %lhs_sp_out : tensor<4x4xi8, #DCSR>158    sparse_tensor.print %all_sp_out : tensor<4x4xi8, #DCSR>159 160    bufferization.dealloc_tensor %lhs_sp : tensor<4x4xf64, #DCSR>161    bufferization.dealloc_tensor %rhs_sp : tensor<4x4xf64, #DCSR>162    bufferization.dealloc_tensor %all_dn_out : tensor<4x4xi8>163    bufferization.dealloc_tensor %lhs_sp_out : tensor<4x4xi8, #DCSR>164    bufferization.dealloc_tensor %all_sp_out : tensor<4x4xi8, #DCSR>165 166    return167  }168}169