xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_insert_1d.mlir (revision eb206e9ea84eff0a0596fed2de8316d924f946d1)
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_libs_sve} = -shared-libs=%native_mlir_runner_utils,%native_mlir_c_runner_utils
14// DEFINE: %{run_opts} = -e main -entry-point-result=void
15// 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 %s
22//
23// Do the same run, but now with direct IR generation.
24// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
25// RUN: %{compile} | %{run} | FileCheck %s
26//
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=true
29// RUN: %{compile} | %{run} | FileCheck %s
30//
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
36#trait_mul_s = {
37  indexing_maps = [
38    affine_map<(i) -> (i)>   // x (out)
39  ],
40  iterator_types = ["parallel"],
41  doc = "x(i) = x(i) * 2.0"
42}
43
44module {
45  func.func @main() {
46    %f1    = arith.constant 1.0 : f32
47    %f2    = arith.constant 2.0 : f32
48    %f3    = arith.constant 3.0 : f32
49    %f4    = arith.constant 4.0 : f32
50    %c0    = arith.constant 0 : index
51    %c1    = arith.constant 1 : index
52    %c3    = arith.constant 3 : index
53    %c8    = arith.constant 8 : index
54    %c1023 = arith.constant 1023 : index
55
56    // Build the sparse vector from straightline code.
57    %0 = tensor.empty() : tensor<1024xf32, #SparseVector>
58    %1 = tensor.insert %f1 into %0[%c0] : tensor<1024xf32, #SparseVector>
59    %2 = tensor.insert %f2 into %1[%c1] : tensor<1024xf32, #SparseVector>
60    %3 = tensor.insert %f3 into %2[%c3] : tensor<1024xf32, #SparseVector>
61    %4 = tensor.insert %f4 into %3[%c1023] : tensor<1024xf32, #SparseVector>
62    %5 = sparse_tensor.load %4 hasInserts : tensor<1024xf32, #SparseVector>
63
64    //
65    // CHECK:   ---- Sparse Tensor ----
66    // CHECK-NEXT: nse = 4
67    // CHECK-NEXT: dim = ( 1024 )
68    // CHECK-NEXT: lvl = ( 1024 )
69    // CHECK-NEXT: pos[0] : ( 0, 4 )
70    // CHECK-NEXT: crd[0] : ( 0, 1, 3, 1023 )
71    // CHECK-NEXT: values : ( 1, 2, 3, 4 )
72    // CHECK-NEXT: ----
73    //
74    sparse_tensor.print %5 : tensor<1024xf32, #SparseVector>
75
76    // Build another sparse vector in a loop.
77    %6 = tensor.empty() : tensor<1024xf32, #SparseVector>
78    %7 = scf.for %i = %c0 to %c8 step %c1 iter_args(%vin = %6) -> tensor<1024xf32, #SparseVector> {
79      %ii = arith.muli %i, %c3 : index
80      %vout = tensor.insert %f1 into %vin[%ii] : tensor<1024xf32, #SparseVector>
81      scf.yield %vout : tensor<1024xf32, #SparseVector>
82    }
83    %8 = sparse_tensor.load %7 hasInserts : tensor<1024xf32, #SparseVector>
84
85    //
86    // CHECK-NEXT: ---- Sparse Tensor ----
87    // CHECK-NEXT: nse = 8
88    // CHECK-NEXT: dim = ( 1024 )
89    // CHECK-NEXT: lvl = ( 1024 )
90    // CHECK-NEXT: pos[0] : ( 0, 8 )
91    // CHECK-NEXT: crd[0] : ( 0, 3, 6, 9, 12, 15, 18, 21 )
92    // CHECK-NEXT: values : ( 1, 1, 1, 1, 1, 1, 1, 1 )
93    // CHECK-NEXT: ----
94    //
95    sparse_tensor.print %8 : tensor<1024xf32, #SparseVector>
96
97    // Free resources.
98    bufferization.dealloc_tensor %5 : tensor<1024xf32, #SparseVector>
99    bufferization.dealloc_tensor %8 : tensor<1024xf32, #SparseVector>
100    return
101  }
102}
103