xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_permute.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: %{env} = TENSOR0=%mlir_src_dir/test/Integration/data/mttkrp_b.tns
22// RUN: %{compile} | env %{env} %{run} | FileCheck %s
23//
24// Do the same run, but now with direct IR generation.
25// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false
26// RUN: %{compile} | env %{env} %{run} | FileCheck %s
27
28!Filename = !llvm.ptr
29
30#S1 = #sparse_tensor.encoding<{
31  map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed)
32}>
33
34#S2 = #sparse_tensor.encoding<{
35  map = (d0, d1, d2) -> (d0 : compressed, d2 : compressed, d1 : compressed)
36}>
37
38#S3 = #sparse_tensor.encoding<{
39  map = (d0, d1, d2) -> (d1 : compressed, d0 : compressed, d2 : compressed)
40}>
41
42#S4 = #sparse_tensor.encoding<{
43  map = (d0, d1, d2) -> (d1 : compressed, d2 : compressed, d0 : compressed)
44}>
45
46#S5 = #sparse_tensor.encoding<{
47  map = (d0, d1, d2) -> (d2 : compressed, d0 : compressed, d1 : compressed)
48}>
49
50#S6 = #sparse_tensor.encoding<{
51  map = (d0, d1, d2) -> (d2 : compressed, d1 : compressed, d0 : compressed)
52}>
53
54#trait_3d = {
55  indexing_maps = [
56    affine_map<(i,j,k) -> (i,j,k)>, // B
57    affine_map<(i,j,k) -> (i,j,k)>  // A (out)
58  ],
59  iterator_types = ["parallel", "parallel", "parallel"],
60  doc = "A(i,j,k) = B(i,j,k)"
61}
62
63//
64// Integration test that lowers a kernel annotated as sparse to
65// actual sparse code, initializes a matching sparse storage scheme
66// from file, and runs the resulting code with the JIT compiler.
67//
68module {
69  func.func private @getTensorFilename(index) -> (!Filename)
70
71  func.func @dump(%a: tensor<2x3x4xf64>) {
72    %c0 = arith.constant 0 : index
73    %f0 = arith.constant 0.0 : f64
74    %v = vector.transfer_read %a[%c0, %c0, %c0], %f0 : tensor<2x3x4xf64>, vector<2x3x4xf64>
75    vector.print %v : vector<2x3x4xf64>
76    return
77  }
78
79  //// S1
80
81  func.func @linalg1(%b: tensor<2x3x4xf64, #S1>)-> tensor<2x3x4xf64> {
82    %0 = arith.constant dense<0.000000e+00> : tensor<2x3x4xf64>
83    %a = linalg.generic #trait_3d
84      ins(%b: tensor<2x3x4xf64, #S1>)
85      outs(%0: tensor<2x3x4xf64>) {
86       ^bb(%x: f64, %y: f64):
87        linalg.yield %x : f64
88    } -> tensor<2x3x4xf64>
89    return %a : tensor<2x3x4xf64>
90  }
91
92  func.func @convert1(%b: tensor<2x3x4xf64, #S1>) -> tensor<2x3x4xf64> {
93    %a = sparse_tensor.convert %b : tensor<2x3x4xf64, #S1> to tensor<2x3x4xf64>
94    return %a : tensor<2x3x4xf64>
95  }
96
97  func.func @foo1(%fileName : !Filename) {
98    %b = sparse_tensor.new %fileName : !Filename to tensor<2x3x4xf64, #S1>
99    %0 = call @linalg1(%b) : (tensor<2x3x4xf64, #S1>) -> tensor<2x3x4xf64>
100    call @dump(%0) : (tensor<2x3x4xf64>) -> ()
101    %1 = call @convert1(%b) : (tensor<2x3x4xf64, #S1>) -> tensor<2x3x4xf64>
102    call @dump(%1) : (tensor<2x3x4xf64>) -> ()
103    bufferization.dealloc_tensor %0 : tensor<2x3x4xf64>
104    bufferization.dealloc_tensor %b : tensor<2x3x4xf64, #S1>
105    bufferization.dealloc_tensor %1 : tensor<2x3x4xf64>
106    return
107  }
108
109  //// S2
110
111  func.func @linalg2(%b: tensor<2x3x4xf64, #S2>)-> tensor<2x3x4xf64> {
112    %0 = arith.constant dense<0.000000e+00> : tensor<2x3x4xf64>
113    %a = linalg.generic #trait_3d
114      ins(%b: tensor<2x3x4xf64, #S2>)
115      outs(%0: tensor<2x3x4xf64>) {
116       ^bb(%x: f64, %y: f64):
117        linalg.yield %x : f64
118    } -> tensor<2x3x4xf64>
119    return %a : tensor<2x3x4xf64>
120  }
121
122  func.func @convert2(%b: tensor<2x3x4xf64, #S2>) -> tensor<2x3x4xf64> {
123    %a = sparse_tensor.convert %b : tensor<2x3x4xf64, #S2> to tensor<2x3x4xf64>
124    return %a : tensor<2x3x4xf64>
125  }
126
127  func.func @foo2(%fileName : !Filename) {
128    %b = sparse_tensor.new %fileName : !Filename to tensor<2x3x4xf64, #S2>
129    %0 = call @linalg2(%b) : (tensor<2x3x4xf64, #S2>) -> tensor<2x3x4xf64>
130    call @dump(%0) : (tensor<2x3x4xf64>) -> ()
131    %2 = call @convert2(%b) : (tensor<2x3x4xf64, #S2>) -> tensor<2x3x4xf64>
132    call @dump(%2) : (tensor<2x3x4xf64>) -> ()
133    bufferization.dealloc_tensor %0 : tensor<2x3x4xf64>
134    bufferization.dealloc_tensor %b : tensor<2x3x4xf64, #S2>
135    bufferization.dealloc_tensor %2 : tensor<2x3x4xf64>
136    return
137  }
138
139  //// S3
140
141  func.func @linalg3(%b: tensor<2x3x4xf64, #S3>)-> tensor<2x3x4xf64> {
142    %0 = arith.constant dense<0.000000e+00> : tensor<2x3x4xf64>
143    %a = linalg.generic #trait_3d
144      ins(%b: tensor<2x3x4xf64, #S3>)
145      outs(%0: tensor<2x3x4xf64>) {
146       ^bb(%x: f64, %y: f64):
147        linalg.yield %x : f64
148    } -> tensor<2x3x4xf64>
149    return %a : tensor<2x3x4xf64>
150  }
151
152  func.func @convert3(%b: tensor<2x3x4xf64, #S3>) -> tensor<2x3x4xf64> {
153    %a = sparse_tensor.convert %b : tensor<2x3x4xf64, #S3> to tensor<2x3x4xf64>
154    return %a : tensor<2x3x4xf64>
155  }
156
157  func.func @foo3(%fileName : !Filename) {
158    %b = sparse_tensor.new %fileName : !Filename to tensor<2x3x4xf64, #S3>
159    %0 = call @linalg3(%b) : (tensor<2x3x4xf64, #S3>) -> tensor<2x3x4xf64>
160    call @dump(%0) : (tensor<2x3x4xf64>) -> ()
161    %3 = call @convert3(%b) : (tensor<2x3x4xf64, #S3>) -> tensor<2x3x4xf64>
162    call @dump(%3) : (tensor<2x3x4xf64>) -> ()
163    bufferization.dealloc_tensor %0 : tensor<2x3x4xf64>
164    bufferization.dealloc_tensor %b : tensor<2x3x4xf64, #S3>
165    bufferization.dealloc_tensor %3 : tensor<2x3x4xf64>
166    return
167  }
168
169  //// S4
170
171  func.func @linalg4(%b: tensor<2x3x4xf64, #S4>)-> tensor<2x3x4xf64> {
172    %0 = arith.constant dense<0.000000e+00> : tensor<2x3x4xf64>
173    %a = linalg.generic #trait_3d
174      ins(%b: tensor<2x3x4xf64, #S4>)
175      outs(%0: tensor<2x3x4xf64>) {
176       ^bb(%x: f64, %y: f64):
177        linalg.yield %x : f64
178    } -> tensor<2x3x4xf64>
179    return %a : tensor<2x3x4xf64>
180  }
181
182  func.func @convert4(%b: tensor<2x3x4xf64, #S4>) -> tensor<2x3x4xf64> {
183    %a = sparse_tensor.convert %b : tensor<2x3x4xf64, #S4> to tensor<2x3x4xf64>
184    return %a : tensor<2x3x4xf64>
185  }
186
187  func.func @foo4(%fileName : !Filename) {
188    %b = sparse_tensor.new %fileName : !Filename to tensor<2x3x4xf64, #S4>
189    %0 = call @linalg4(%b) : (tensor<2x3x4xf64, #S4>) -> tensor<2x3x4xf64>
190    call @dump(%0) : (tensor<2x3x4xf64>) -> ()
191    %4 = call @convert4(%b) : (tensor<2x3x4xf64, #S4>) -> tensor<2x3x4xf64>
192    call @dump(%4) : (tensor<2x3x4xf64>) -> ()
193    bufferization.dealloc_tensor %0 : tensor<2x3x4xf64>
194    bufferization.dealloc_tensor %b : tensor<2x3x4xf64, #S4>
195    bufferization.dealloc_tensor %4 : tensor<2x3x4xf64>
196    return
197  }
198
199  //// S5
200
201  func.func @linalg5(%b: tensor<2x3x4xf64, #S5>)-> tensor<2x3x4xf64> {
202    %0 = arith.constant dense<0.000000e+00> : tensor<2x3x4xf64>
203    %a = linalg.generic #trait_3d
204      ins(%b: tensor<2x3x4xf64, #S5>)
205      outs(%0: tensor<2x3x4xf64>) {
206       ^bb(%x: f64, %y: f64):
207        linalg.yield %x : f64
208    } -> tensor<2x3x4xf64>
209    return %a : tensor<2x3x4xf64>
210  }
211
212  func.func @convert5(%b: tensor<2x3x4xf64, #S5>) -> tensor<2x3x4xf64> {
213    %a = sparse_tensor.convert %b : tensor<2x3x4xf64, #S5> to tensor<2x3x4xf64>
214    return %a : tensor<2x3x4xf64>
215  }
216
217  func.func @foo5(%fileName : !Filename) {
218    %b = sparse_tensor.new %fileName : !Filename to tensor<2x3x4xf64, #S5>
219    %0 = call @linalg5(%b) : (tensor<2x3x4xf64, #S5>) -> tensor<2x3x4xf64>
220    call @dump(%0) : (tensor<2x3x4xf64>) -> ()
221    %5 = call @convert5(%b) : (tensor<2x3x4xf64, #S5>) -> tensor<2x3x4xf64>
222    call @dump(%5) : (tensor<2x3x4xf64>) -> ()
223    bufferization.dealloc_tensor %0 : tensor<2x3x4xf64>
224    bufferization.dealloc_tensor %b : tensor<2x3x4xf64, #S5>
225    bufferization.dealloc_tensor %5 : tensor<2x3x4xf64>
226    return
227  }
228
229  //// S6
230
231  func.func @linalg6(%b: tensor<2x3x4xf64, #S6>)-> tensor<2x3x4xf64> {
232    %0 = arith.constant dense<0.000000e+00> : tensor<2x3x4xf64>
233    %a = linalg.generic #trait_3d
234      ins(%b: tensor<2x3x4xf64, #S6>)
235      outs(%0: tensor<2x3x4xf64>) {
236       ^bb(%x: f64, %y: f64):
237        linalg.yield %x : f64
238    } -> tensor<2x3x4xf64>
239    return %a : tensor<2x3x4xf64>
240  }
241
242  func.func @convert6(%b: tensor<2x3x4xf64, #S6>) -> tensor<2x3x4xf64> {
243    %a = sparse_tensor.convert %b : tensor<2x3x4xf64, #S6> to tensor<2x3x4xf64>
244    return %a : tensor<2x3x4xf64>
245  }
246
247  func.func @foo6(%fileName : !Filename) {
248    %b = sparse_tensor.new %fileName : !Filename to tensor<2x3x4xf64, #S6>
249    %0 = call @linalg6(%b) : (tensor<2x3x4xf64, #S6>) -> tensor<2x3x4xf64>
250    call @dump(%0) : (tensor<2x3x4xf64>) -> ()
251    %6 = call @convert6(%b) : (tensor<2x3x4xf64, #S6>) -> tensor<2x3x4xf64>
252    call @dump(%6) : (tensor<2x3x4xf64>) -> ()
253    bufferization.dealloc_tensor %0 : tensor<2x3x4xf64>
254    bufferization.dealloc_tensor %b : tensor<2x3x4xf64, #S6>
255    bufferization.dealloc_tensor %6 : tensor<2x3x4xf64>
256    return
257  }
258
259  //
260  // Main driver.
261  //
262  // CHECK-COUNT-12: ( ( ( 0, 0, 3, 63 ), ( 0, 11, 100, 0 ), ( 66, 61, 13, 43 ) ), ( ( 77, 0, 10, 46 ), ( 61, 53, 3, 75 ), ( 0, 22, 18, 0 ) ) )
263  //
264  func.func @main() {
265    %c0 = arith.constant 0 : index
266    %fileName = call @getTensorFilename(%c0) : (index) -> (!Filename)
267    call @foo1(%fileName) : (!Filename) -> ()
268    call @foo2(%fileName) : (!Filename) -> ()
269    call @foo3(%fileName) : (!Filename) -> ()
270    call @foo4(%fileName) : (!Filename) -> ()
271    call @foo5(%fileName) : (!Filename) -> ()
272    call @foo6(%fileName) : (!Filename) -> ()
273    return
274  }
275}
276