|
| 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, this |
| 5 | +// config could be moved to lit.local.cfg. However, there are downstream users that |
| 6 | +// do not use these LIT config files. Hence why this is kept inline. |
| 7 | +// |
| 8 | +// DEFINE: %{sparsifier_opts} = enable-runtime-library=true |
| 9 | +// 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_utils |
| 13 | +// DEFINE: %{run_opts} = -e main -entry-point-result=void |
| 14 | +// DEFINE: %{run} = mlir-cpu-runner %{run_opts} %{run_libs} |
| 15 | +// DEFINE: %{run_sve} = %mcr_aarch64_cmd --march=aarch64 --mattr="+sve" %{run_opts} %{run_libs} |
| 16 | +// |
| 17 | +// DEFINE: %{env} = |
| 18 | +//-------------------------------------------------------------------------------------------------- |
| 19 | + |
| 20 | +// RUN: %{compile} | %{run} | FileCheck %s |
| 21 | +// |
| 22 | +// Do the same run, but now with direct IR generation. |
| 23 | +// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true |
| 24 | +// RUN: %{compile} | %{run} | FileCheck %s |
| 25 | +// |
| 26 | +// Do the same run, but now with direct IR generation and vectorization. |
| 27 | +// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false enable-buffer-initialization=true vl=2 reassociate-fp-reductions=true enable-index-optimizations=true |
| 28 | +// RUN: %{compile} | %{run} | FileCheck %s |
| 29 | +// |
| 30 | +// Do the same run, but now with direct IR generation and VLA vectorization. |
| 31 | +// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %} |
| 32 | + |
| 33 | + |
| 34 | +#map = affine_map<(d0) -> (d0)> |
| 35 | + |
| 36 | +#SV = #sparse_tensor.encoding<{ |
| 37 | + map = (d0) -> (d0 : compressed) |
| 38 | +}> |
| 39 | + |
| 40 | +module { |
| 41 | + |
| 42 | + // This directly yields an empty sparse vector. |
| 43 | + func.func @empty() -> tensor<10xf32, #SV> { |
| 44 | + %0 = tensor.empty() : tensor<10xf32, #SV> |
| 45 | + return %0 : tensor<10xf32, #SV> |
| 46 | + } |
| 47 | + |
| 48 | + // This also directly yields an empty sparse vector. |
| 49 | + func.func @empty_alloc() -> tensor<10xf32, #SV> { |
| 50 | + %0 = bufferization.alloc_tensor() : tensor<10xf32, #SV> |
| 51 | + return %0 : tensor<10xf32, #SV> |
| 52 | + } |
| 53 | + |
| 54 | + // This yields a hidden empty sparse vector (all zeros). |
| 55 | + func.func @zeros() -> tensor<10xf32, #SV> { |
| 56 | + %cst = arith.constant 0.0 : f32 |
| 57 | + %0 = bufferization.alloc_tensor() : tensor<10xf32, #SV> |
| 58 | + %1 = linalg.generic { |
| 59 | + indexing_maps = [#map], |
| 60 | + iterator_types = ["parallel"]} |
| 61 | + outs(%0 : tensor<10xf32, #SV>) { |
| 62 | + ^bb0(%out: f32): |
| 63 | + linalg.yield %cst : f32 |
| 64 | + } -> tensor<10xf32, #SV> |
| 65 | + return %1 : tensor<10xf32, #SV> |
| 66 | + } |
| 67 | + |
| 68 | + // This yields a filled sparse vector (all ones). |
| 69 | + func.func @ones() -> tensor<10xf32, #SV> { |
| 70 | + %cst = arith.constant 1.0 : f32 |
| 71 | + %0 = bufferization.alloc_tensor() : tensor<10xf32, #SV> |
| 72 | + %1 = linalg.generic { |
| 73 | + indexing_maps = [#map], |
| 74 | + iterator_types = ["parallel"]} |
| 75 | + outs(%0 : tensor<10xf32, #SV>) { |
| 76 | + ^bb0(%out: f32): |
| 77 | + linalg.yield %cst : f32 |
| 78 | + } -> tensor<10xf32, #SV> |
| 79 | + return %1 : tensor<10xf32, #SV> |
| 80 | + } |
| 81 | + |
| 82 | + // |
| 83 | + // Main driver. |
| 84 | + // |
| 85 | + func.func @main() { |
| 86 | + |
| 87 | + %0 = call @empty() : () -> tensor<10xf32, #SV> |
| 88 | + %1 = call @empty_alloc() : () -> tensor<10xf32, #SV> |
| 89 | + %2 = call @zeros() : () -> tensor<10xf32, #SV> |
| 90 | + %3 = call @ones() : () -> tensor<10xf32, #SV> |
| 91 | + |
| 92 | + // |
| 93 | + // Verify the output. In particular, make sure that |
| 94 | + // all empty sparse vector data structures are properly |
| 95 | + // finalized with a pair (0,0) for positions. |
| 96 | + // |
| 97 | + // CHECK: ---- Sparse Tensor ---- |
| 98 | + // CHECK-NEXT: nse = 0 |
| 99 | + // CHECK-NEXT: dim = ( 10 ) |
| 100 | + // CHECK-NEXT: lvl = ( 10 ) |
| 101 | + // CHECK-NEXT: pos[0] : ( 0, 0, |
| 102 | + // CHECK-NEXT: crd[0] : ( |
| 103 | + // CHECK-NEXT: values : ( |
| 104 | + // CHECK-NEXT: ---- |
| 105 | + // |
| 106 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 107 | + // CHECK-NEXT: nse = 0 |
| 108 | + // CHECK-NEXT: dim = ( 10 ) |
| 109 | + // CHECK-NEXT: lvl = ( 10 ) |
| 110 | + // CHECK-NEXT: pos[0] : ( 0, 0, |
| 111 | + // CHECK-NEXT: crd[0] : ( |
| 112 | + // CHECK-NEXT: values : ( |
| 113 | + // CHECK-NEXT: ---- |
| 114 | + // |
| 115 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 116 | + // CHECK-NEXT: nse = 0 |
| 117 | + // CHECK-NEXT: dim = ( 10 ) |
| 118 | + // CHECK-NEXT: lvl = ( 10 ) |
| 119 | + // CHECK-NEXT: pos[0] : ( 0, 0, |
| 120 | + // CHECK-NEXT: crd[0] : ( |
| 121 | + // CHECK-NEXT: values : ( |
| 122 | + // CHECK-NEXT: ---- |
| 123 | + // |
| 124 | + // CHECK-NEXT: ---- Sparse Tensor ---- |
| 125 | + // CHECK-NEXT: nse = 10 |
| 126 | + // CHECK-NEXT: dim = ( 10 ) |
| 127 | + // CHECK-NEXT: lvl = ( 10 ) |
| 128 | + // CHECK-NEXT: pos[0] : ( 0, 10, |
| 129 | + // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 130 | + // CHECK-NEXT: values : ( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, |
| 131 | + // CHECK-NEXT: ---- |
| 132 | + // |
| 133 | + sparse_tensor.print %0 : tensor<10xf32, #SV> |
| 134 | + sparse_tensor.print %1 : tensor<10xf32, #SV> |
| 135 | + sparse_tensor.print %2 : tensor<10xf32, #SV> |
| 136 | + sparse_tensor.print %3 : tensor<10xf32, #SV> |
| 137 | + |
| 138 | + bufferization.dealloc_tensor %0 : tensor<10xf32, #SV> |
| 139 | + bufferization.dealloc_tensor %1 : tensor<10xf32, #SV> |
| 140 | + bufferization.dealloc_tensor %2 : tensor<10xf32, #SV> |
| 141 | + bufferization.dealloc_tensor %3 : tensor<10xf32, #SV> |
| 142 | + return |
| 143 | + } |
| 144 | +} |
0 commit comments