xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_scf_nested.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 vectorization.
28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false vl=4
29// RUN: %{compile} | %{run} | FileCheck %s
30//
31// Do the same run, but now with  VLA vectorization.
32// RUN: %if mlir_arm_sve_tests %{ %{compile_sve} | %{run_sve} | FileCheck %s %}
33
34#map = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
35#SparseMatrix = #sparse_tensor.encoding<{ map = (d0, d1, d2) -> (d0 : compressed, d1 : compressed, d2 : compressed) }>
36
37module @func_sparse.2 {
38  // Do elementwise x+1 when true, x-1 when false
39  func.func public @condition(%cond: i1, %arg0: tensor<2x3x4xf64, #SparseMatrix>) -> tensor<2x3x4xf64, #SparseMatrix> {
40    %1 = scf.if %cond -> (tensor<2x3x4xf64, #SparseMatrix>) {
41      %cst_2 = arith.constant dense<1.000000e+00> : tensor<f64>
42      %cst_3 = arith.constant dense<1.000000e+00> : tensor<2x3x4xf64>
43      %2 = tensor.empty() : tensor<2x3x4xf64, #SparseMatrix>
44      %3 = linalg.generic {
45        indexing_maps = [#map, #map, #map],
46        iterator_types = ["parallel", "parallel", "parallel"]}
47        ins(%arg0, %cst_3 : tensor<2x3x4xf64, #SparseMatrix>, tensor<2x3x4xf64>)
48        outs(%2 : tensor<2x3x4xf64, #SparseMatrix>) {
49          ^bb0(%arg1: f64, %arg2: f64, %arg3: f64):
50            %4 = arith.subf %arg1, %arg2 : f64
51            linalg.yield %4 : f64
52          } -> tensor<2x3x4xf64, #SparseMatrix>
53        scf.yield %3 : tensor<2x3x4xf64, #SparseMatrix>
54    } else {
55      %cst_2 = arith.constant dense<1.000000e+00> : tensor<f64>
56      %cst_3 = arith.constant dense<1.000000e+00> : tensor<2x3x4xf64>
57      %2 = tensor.empty() : tensor<2x3x4xf64, #SparseMatrix>
58      %3 = linalg.generic {
59        indexing_maps = [#map, #map, #map],
60        iterator_types = ["parallel", "parallel", "parallel"]}
61        ins(%arg0, %cst_3 : tensor<2x3x4xf64, #SparseMatrix>, tensor<2x3x4xf64>)
62        outs(%2 : tensor<2x3x4xf64, #SparseMatrix>) {
63          ^bb0(%arg1: f64, %arg2: f64, %arg3: f64):
64            %4 = arith.addf %arg1, %arg2 : f64
65            linalg.yield %4 : f64
66          } -> tensor<2x3x4xf64, #SparseMatrix>
67        scf.yield %3 : tensor<2x3x4xf64, #SparseMatrix>
68    }
69    return %1 : tensor<2x3x4xf64, #SparseMatrix>
70  }
71
72  func.func public @main() {
73    %src = arith.constant dense<[
74     [  [  1.0,  2.0,  3.0,  4.0 ],
75        [  5.0,  6.0,  7.0,  8.0 ],
76        [  9.0, 10.0, 11.0, 12.0 ] ],
77     [  [ 13.0, 14.0, 15.0, 16.0 ],
78        [ 17.0, 18.0, 19.0, 20.0 ],
79        [ 21.0, 22.0, 23.0, 24.0 ] ]
80    ]> : tensor<2x3x4xf64>
81
82    %t = arith.constant 1 : i1
83    %f = arith.constant 0 : i1
84
85    %sm = sparse_tensor.convert %src : tensor<2x3x4xf64> to tensor<2x3x4xf64, #SparseMatrix>
86
87    %sm_t = call @condition(%t, %sm) : (i1, tensor<2x3x4xf64, #SparseMatrix>) -> tensor<2x3x4xf64, #SparseMatrix>
88    %sm_f = call @condition(%f, %sm) : (i1, tensor<2x3x4xf64, #SparseMatrix>) -> tensor<2x3x4xf64, #SparseMatrix>
89
90    //
91    // CHECK:      ---- Sparse Tensor ----
92    // CHECK-NEXT: nse = 24
93    // CHECK-NEXT: dim = ( 2, 3, 4 )
94    // CHECK-NEXT: lvl = ( 2, 3, 4 )
95    // CHECK-NEXT: pos[0] : ( 0, 2 )
96    // CHECK-NEXT: crd[0] : ( 0, 1 )
97    // CHECK-NEXT: pos[1] : ( 0, 3, 6 )
98    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 0, 1, 2 )
99    // CHECK-NEXT: pos[2] : ( 0, 4, 8, 12, 16, 20, 24 )
100    // CHECK-NEXT: crd[2] : ( 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 )
101    // CHECK-NEXT: values : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23 )
102    // CHECK-NEXT: ----
103    // CHECK:      ---- Sparse Tensor ----
104    // CHECK-NEXT: nse = 24
105    // CHECK-NEXT: dim = ( 2, 3, 4 )
106    // CHECK-NEXT: lvl = ( 2, 3, 4 )
107    // CHECK-NEXT: pos[0] : ( 0, 2 )
108    // CHECK-NEXT: crd[0] : ( 0, 1 )
109    // CHECK-NEXT: pos[1] : ( 0, 3, 6 )
110    // CHECK-NEXT: crd[1] : ( 0, 1, 2, 0, 1, 2 )
111    // CHECK-NEXT: pos[2] : ( 0, 4, 8, 12, 16, 20, 24 )
112    // CHECK-NEXT: crd[2] : ( 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3, 0, 1, 2, 3 )
113    // CHECK-NEXT: values : ( 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25 )
114    // CHECK-NEXT: ----
115    //
116    sparse_tensor.print %sm_t : tensor<2x3x4xf64, #SparseMatrix>
117    sparse_tensor.print %sm_f : tensor<2x3x4xf64, #SparseMatrix>
118
119    bufferization.dealloc_tensor %sm : tensor<2x3x4xf64, #SparseMatrix>
120    bufferization.dealloc_tensor %sm_t : tensor<2x3x4xf64, #SparseMatrix>
121    bufferization.dealloc_tensor %sm_f : tensor<2x3x4xf64, #SparseMatrix>
122    return
123  }
124}
125