xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_insert_2d.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 VLA vectorization.
29// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
30
31#Dense = #sparse_tensor.encoding<{
32  map = (d0, d1) -> (d0 : dense, d1 : dense)
33}>
34
35#SortedCOO = #sparse_tensor.encoding<{
36  map = (d0, d1) -> (d0 : compressed(nonunique), d1 : singleton(soa))
37}>
38
39#CSR = #sparse_tensor.encoding<{
40  map = (d0, d1) -> (d0 : dense, d1 : compressed)
41}>
42
43#DCSR = #sparse_tensor.encoding<{
44  map = (d0, d1) -> (d0 : compressed, d1 : compressed)
45}>
46
47#Row = #sparse_tensor.encoding<{
48  map = (d0, d1) -> (d0 : compressed, d1 : dense)
49}>
50
51module {
52  //
53  // Main driver. We test the contents of various sparse tensor
54  // schemes when they are still empty and after a few insertions.
55  //
56  func.func @main() {
57    %c0 = arith.constant 0 : index
58    %c2 = arith.constant 2 : index
59    %c3 = arith.constant 3 : index
60    %f1 = arith.constant 1.0 : f64
61    %f2 = arith.constant 2.0 : f64
62    %f3 = arith.constant 3.0 : f64
63    %f4 = arith.constant 4.0 : f64
64
65    //
66    // Dense case.
67    //
68    // CHECK:      ---- Sparse Tensor ----
69    // CHECK-NEXT: nse = 12
70    // CHECK-NEXT: dim = ( 4, 3 )
71    // CHECK-NEXT: lvl = ( 4, 3 )
72    // CHECK-NEXT: values : ( 1, 0, 0, 0, 0, 0, 0, 0, 2, 3, 0, 4 )
73    // CHECK-NEXT: ----
74    //
75    %densea = tensor.empty() : tensor<4x3xf64, #Dense>
76    %dense1 = tensor.insert %f1 into %densea[%c0, %c0] : tensor<4x3xf64, #Dense>
77    %dense2 = tensor.insert %f2 into %dense1[%c2, %c2] : tensor<4x3xf64, #Dense>
78    %dense3 = tensor.insert %f3 into %dense2[%c3, %c0] : tensor<4x3xf64, #Dense>
79    %dense4 = tensor.insert %f4 into %dense3[%c3, %c2] : tensor<4x3xf64, #Dense>
80    %densem = sparse_tensor.load %dense4 hasInserts : tensor<4x3xf64, #Dense>
81    sparse_tensor.print %densem : tensor<4x3xf64, #Dense>
82
83    //
84    // COO case.
85    //
86    // CHECK-NEXT: ---- Sparse Tensor ----
87    // CHECK-NEXT: nse = 4
88    // CHECK-NEXT: dim = ( 4, 3 )
89    // CHECK-NEXT: lvl = ( 4, 3 )
90    // CHECK-NEXT: pos[0] : ( 0, 4 )
91    // CHECK-NEXT: crd[0] : ( 0, 2, 3, 3 )
92    // CHECK-NEXT: crd[1] : ( 0, 2, 0, 2 )
93    // CHECK-NEXT: values : ( 1, 2, 3, 4 )
94    // CHECK-NEXT: ----
95    //
96    %cooa = tensor.empty() : tensor<4x3xf64, #SortedCOO>
97    %coo1 = tensor.insert %f1 into %cooa[%c0, %c0] : tensor<4x3xf64, #SortedCOO>
98    %coo2 = tensor.insert %f2 into %coo1[%c2, %c2] : tensor<4x3xf64, #SortedCOO>
99    %coo3 = tensor.insert %f3 into %coo2[%c3, %c0] : tensor<4x3xf64, #SortedCOO>
100    %coo4 = tensor.insert %f4 into %coo3[%c3, %c2] : tensor<4x3xf64, #SortedCOO>
101    %coom = sparse_tensor.load %coo4 hasInserts : tensor<4x3xf64, #SortedCOO>
102    sparse_tensor.print %coom : tensor<4x3xf64, #SortedCOO>
103
104    //
105    // CSR case.
106    //
107    // CHECK-NEXT: ---- Sparse Tensor ----
108    // CHECK-NEXT: nse = 4
109    // CHECK-NEXT: dim = ( 4, 3 )
110    // CHECK-NEXT: lvl = ( 4, 3 )
111    // CHECK-NEXT: pos[1] : ( 0, 1, 1, 2, 4 )
112    // CHECK-NEXT: crd[1] : ( 0, 2, 0, 2 )
113    // CHECK-NEXT: values : ( 1, 2, 3, 4 )
114    // CHECK-NEXT: ----
115    //
116    %csra = tensor.empty() : tensor<4x3xf64, #CSR>
117    %csr1 = tensor.insert %f1 into %csra[%c0, %c0] : tensor<4x3xf64, #CSR>
118    %csr2 = tensor.insert %f2 into %csr1[%c2, %c2] : tensor<4x3xf64, #CSR>
119    %csr3 = tensor.insert %f3 into %csr2[%c3, %c0] : tensor<4x3xf64, #CSR>
120    %csr4 = tensor.insert %f4 into %csr3[%c3, %c2] : tensor<4x3xf64, #CSR>
121    %csrm = sparse_tensor.load %csr4 hasInserts : tensor<4x3xf64, #CSR>
122    sparse_tensor.print %csrm : tensor<4x3xf64, #CSR>
123
124    //
125    // DCSR case.
126    //
127    // CHECK-NEXT: ---- Sparse Tensor ----
128    // CHECK-NEXT: nse = 4
129    // CHECK-NEXT: dim = ( 4, 3 )
130    // CHECK-NEXT: lvl = ( 4, 3 )
131    // CHECK-NEXT: pos[0] : ( 0, 3 )
132    // CHECK-NEXT: crd[0] : ( 0, 2, 3 )
133    // CHECK-NEXT: pos[1] : ( 0, 1, 2, 4 )
134    // CHECK-NEXT: crd[1] : ( 0, 2, 0, 2 )
135    // CHECK-NEXT: values : ( 1, 2, 3, 4 )
136    // CHECK-NEXT: ----
137    //
138    %dcsra = tensor.empty() : tensor<4x3xf64, #DCSR>
139    %dcsr1 = tensor.insert %f1 into %dcsra[%c0, %c0] : tensor<4x3xf64, #DCSR>
140    %dcsr2 = tensor.insert %f2 into %dcsr1[%c2, %c2] : tensor<4x3xf64, #DCSR>
141    %dcsr3 = tensor.insert %f3 into %dcsr2[%c3, %c0] : tensor<4x3xf64, #DCSR>
142    %dcsr4 = tensor.insert %f4 into %dcsr3[%c3, %c2] : tensor<4x3xf64, #DCSR>
143    %dcsrm = sparse_tensor.load %dcsr4 hasInserts : tensor<4x3xf64, #DCSR>
144    sparse_tensor.print %dcsrm : tensor<4x3xf64, #DCSR>
145
146    //
147    // Row case.
148    //
149    // CHECK-NEXT: ---- Sparse Tensor ----
150    // CHECK-NEXT: nse = 9
151    // CHECK-NEXT: dim = ( 4, 3 )
152    // CHECK-NEXT: lvl = ( 4, 3 )
153    // CHECK-NEXT: pos[0] : ( 0, 3 )
154    // CHECK-NEXT: crd[0] : ( 0, 2, 3 )
155    // CHECK-NEXT: values : ( 1, 0, 0, 0, 0, 2, 3, 0, 4 )
156    // CHECK-NEXT: ----
157    //
158    %rowa = tensor.empty() : tensor<4x3xf64, #Row>
159    %row1 = tensor.insert %f1 into %rowa[%c0, %c0] : tensor<4x3xf64, #Row>
160    %row2 = tensor.insert %f2 into %row1[%c2, %c2] : tensor<4x3xf64, #Row>
161    %row3 = tensor.insert %f3 into %row2[%c3, %c0] : tensor<4x3xf64, #Row>
162    %row4 = tensor.insert %f4 into %row3[%c3, %c2] : tensor<4x3xf64, #Row>
163    %rowm = sparse_tensor.load %row4 hasInserts : tensor<4x3xf64, #Row>
164    sparse_tensor.print %rowm : tensor<4x3xf64, #Row>
165
166    // Release resources.
167    bufferization.dealloc_tensor %densem : tensor<4x3xf64, #Dense>
168    bufferization.dealloc_tensor %coom : tensor<4x3xf64, #SortedCOO>
169    bufferization.dealloc_tensor %csrm : tensor<4x3xf64, #CSR>
170    bufferization.dealloc_tensor %dcsrm : tensor<4x3xf64, #DCSR>
171    bufferization.dealloc_tensor %rowm : tensor<4x3xf64, #Row>
172
173    return
174  }
175}
176