xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_insert_3d.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// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
22// RUN: %{compile} | %{run} | FileCheck %s
23//
24// Do the same run, but now with vectorization.
25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=2 reassociate-fp-reductions=true enable-index-optimizations=true
26// RUN: %{compile} | %{run} | FileCheck %s
27//
28// Do the same run, but now with  VLA vectorization.
29// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
30
31#TensorCSR = #sparse_tensor.encoding<{
32  map = (d0, d1, d2) -> (d0 : compressed, d1 : dense, d2 : compressed)
33}>
34
35#TensorRow = #sparse_tensor.encoding<{
36  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : dense)
37}>
38
39#CCoo = #sparse_tensor.encoding<{
40  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed(nonunique), d2 : singleton)
41}>
42
43#DCoo = #sparse_tensor.encoding<{
44  map = (d0, d1, d2) -> (d0 : dense, d1 : compressed(nonunique), d2 : singleton)
45}>
46
47
48module {
49  //
50  // Main driver.
51  //
52  func.func @main() {
53    %c0 = arith.constant 0 : index
54    %c1 = arith.constant 1 : index
55    %c2 = arith.constant 2 : index
56    %c3 = arith.constant 3 : index
57    %c4 = arith.constant 4 : index
58    %f1 = arith.constant 1.1 : f64
59    %f2 = arith.constant 2.2 : f64
60    %f3 = arith.constant 3.3 : f64
61    %f4 = arith.constant 4.4 : f64
62    %f5 = arith.constant 5.5 : f64
63
64    // CHECK: ---- Sparse Tensor ----
65    // CHECK-NEXT: nse = 5
66    // CHECK-NEXT: dim = ( 5, 4, 3 )
67    // CHECK-NEXT: lvl = ( 5, 4, 3 )
68    // CHECK-NEXT: pos[0] : ( 0, 2 )
69    // CHECK-NEXT: crd[0] : ( 3, 4 )
70    // CHECK-NEXT: pos[2] : ( 0, 2, 2, 2, 3, 3, 3, 4, 5 )
71    // CHECK-NEXT: crd[2] : ( 1, 2, 1, 2, 2 )
72    // CHECK-NEXT: values : ( 1.1, 2.2, 3.3, 4.4, 5.5 )
73    // CHECK-NEXT: ----
74    %tensora = tensor.empty() : tensor<5x4x3xf64, #TensorCSR>
75    %tensor1 = tensor.insert %f1 into %tensora[%c3, %c0, %c1] : tensor<5x4x3xf64, #TensorCSR>
76    %tensor2 = tensor.insert %f2 into %tensor1[%c3, %c0, %c2] : tensor<5x4x3xf64, #TensorCSR>
77    %tensor3 = tensor.insert %f3 into %tensor2[%c3, %c3, %c1] : tensor<5x4x3xf64, #TensorCSR>
78    %tensor4 = tensor.insert %f4 into %tensor3[%c4, %c2, %c2] : tensor<5x4x3xf64, #TensorCSR>
79    %tensor5 = tensor.insert %f5 into %tensor4[%c4, %c3, %c2] : tensor<5x4x3xf64, #TensorCSR>
80    %tensorm = sparse_tensor.load %tensor5 hasInserts : tensor<5x4x3xf64, #TensorCSR>
81    sparse_tensor.print %tensorm : tensor<5x4x3xf64, #TensorCSR>
82
83    // CHECK-NEXT: ---- Sparse Tensor ----
84    // CHECK-NEXT: nse = 12
85    // CHECK-NEXT: dim = ( 5, 4, 3 )
86    // CHECK-NEXT: lvl = ( 5, 4, 3 )
87    // CHECK-NEXT: pos[0] : ( 0, 2 )
88    // CHECK-NEXT: crd[0] : ( 3, 4 )
89    // CHECK-NEXT: pos[1] : ( 0, 2, 4 )
90    // CHECK-NEXT: crd[1] : ( 0, 3, 2, 3 )
91    // CHECK-NEXT: values : ( 0, 1.1, 2.2, 0, 3.3, 0, 0, 0, 4.4, 0, 0, 5.5 )
92    // CHECK-NEXT: ----
93    %rowa = tensor.empty() : tensor<5x4x3xf64, #TensorRow>
94    %row1 = tensor.insert %f1 into %rowa[%c3, %c0, %c1] : tensor<5x4x3xf64, #TensorRow>
95    %row2 = tensor.insert %f2 into %row1[%c3, %c0, %c2] : tensor<5x4x3xf64, #TensorRow>
96    %row3 = tensor.insert %f3 into %row2[%c3, %c3, %c1] : tensor<5x4x3xf64, #TensorRow>
97    %row4 = tensor.insert %f4 into %row3[%c4, %c2, %c2] : tensor<5x4x3xf64, #TensorRow>
98    %row5 = tensor.insert %f5 into %row4[%c4, %c3, %c2] : tensor<5x4x3xf64, #TensorRow>
99    %rowm = sparse_tensor.load %row5 hasInserts : tensor<5x4x3xf64, #TensorRow>
100    sparse_tensor.print %rowm : tensor<5x4x3xf64, #TensorRow>
101
102    // CHECK-NEXT: ---- Sparse Tensor ----
103    // CHECK-NEXT: nse = 5
104    // CHECK-NEXT: dim = ( 5, 4, 3 )
105    // CHECK-NEXT: lvl = ( 5, 4, 3 )
106    // CHECK-NEXT: pos[0] : ( 0, 2 )
107    // CHECK-NEXT: crd[0] : ( 3, 4 )
108    // CHECK-NEXT: pos[1] : ( 0, 3, 5 )
109    // CHECK-NEXT: crd[1] : ( 0, 1, 0, 2, 3, 1, 2, 2, 3, 2 )
110    // CHECK-NEXT: values : ( 1.1, 2.2, 3.3, 4.4, 5.5 )
111    // CHECK-NEXT: ----
112    %ccoo = tensor.empty() : tensor<5x4x3xf64, #CCoo>
113    %ccoo1 = tensor.insert %f1 into %ccoo[%c3, %c0, %c1] : tensor<5x4x3xf64, #CCoo>
114    %ccoo2 = tensor.insert %f2 into %ccoo1[%c3, %c0, %c2] : tensor<5x4x3xf64, #CCoo>
115    %ccoo3 = tensor.insert %f3 into %ccoo2[%c3, %c3, %c1] : tensor<5x4x3xf64, #CCoo>
116    %ccoo4 = tensor.insert %f4 into %ccoo3[%c4, %c2, %c2] : tensor<5x4x3xf64, #CCoo>
117    %ccoo5 = tensor.insert %f5 into %ccoo4[%c4, %c3, %c2] : tensor<5x4x3xf64, #CCoo>
118    %ccoom = sparse_tensor.load %ccoo5 hasInserts : tensor<5x4x3xf64, #CCoo>
119    sparse_tensor.print %ccoom : tensor<5x4x3xf64, #CCoo>
120
121    // CHECK-NEXT: ---- Sparse Tensor ----
122    // CHECK-NEXT: nse = 5
123    // CHECK-NEXT: dim = ( 5, 4, 3 )
124    // CHECK-NEXT: lvl = ( 5, 4, 3 )
125    // CHECK-NEXT: pos[1] : ( 0, 0, 0, 0, 3, 5 )
126    // CHECK-NEXT: crd[1] : ( 0, 1, 0, 2, 3, 1, 2, 2, 3, 2 )
127    // CHECK-NEXT: values : ( 1.1, 2.2, 3.3, 4.4, 5.5 )
128    // CHECK-NEXT: ----
129    %dcoo = tensor.empty() : tensor<5x4x3xf64, #DCoo>
130    %dcoo1 = tensor.insert %f1 into %dcoo[%c3, %c0, %c1] : tensor<5x4x3xf64, #DCoo>
131    %dcoo2 = tensor.insert %f2 into %dcoo1[%c3, %c0, %c2] : tensor<5x4x3xf64, #DCoo>
132    %dcoo3 = tensor.insert %f3 into %dcoo2[%c3, %c3, %c1] : tensor<5x4x3xf64, #DCoo>
133    %dcoo4 = tensor.insert %f4 into %dcoo3[%c4, %c2, %c2] : tensor<5x4x3xf64, #DCoo>
134    %dcoo5 = tensor.insert %f5 into %dcoo4[%c4, %c3, %c2] : tensor<5x4x3xf64, #DCoo>
135    %dcoom = sparse_tensor.load %dcoo5 hasInserts : tensor<5x4x3xf64, #DCoo>
136    sparse_tensor.print %dcoom : tensor<5x4x3xf64, #DCoo>
137
138    // Release resources.
139    bufferization.dealloc_tensor %tensorm : tensor<5x4x3xf64, #TensorCSR>
140    bufferization.dealloc_tensor %rowm : tensor<5x4x3xf64, #TensorRow>
141    bufferization.dealloc_tensor %ccoom : tensor<5x4x3xf64, #CCoo>
142    bufferization.dealloc_tensor %dcoom : tensor<5x4x3xf64, #DCoo>
143
144    return
145  }
146}
147