xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_complex32.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 enable-buffer-initialization=true
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 enable-buffer-initialization=true 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_op = {
37  indexing_maps = [
38    affine_map<(i) -> (i)>,  // a (in)
39    affine_map<(i) -> (i)>,  // b (in)
40    affine_map<(i) -> (i)>   // x (out)
41  ],
42  iterator_types = ["parallel"],
43  doc = "x(i) = a(i) OP b(i)"
44}
45
46module {
47  func.func @cadd(%arga: tensor<?xcomplex<f32>, #SparseVector>,
48                  %argb: tensor<?xcomplex<f32>, #SparseVector>)
49                      -> tensor<?xcomplex<f32>, #SparseVector> {
50    %c = arith.constant 0 : index
51    %d = tensor.dim %arga, %c : tensor<?xcomplex<f32>, #SparseVector>
52    %xv = tensor.empty(%d) : tensor<?xcomplex<f32>, #SparseVector>
53    %0 = linalg.generic #trait_op
54       ins(%arga, %argb: tensor<?xcomplex<f32>, #SparseVector>,
55                         tensor<?xcomplex<f32>, #SparseVector>)
56        outs(%xv: tensor<?xcomplex<f32>, #SparseVector>) {
57        ^bb(%a: complex<f32>, %b: complex<f32>, %x: complex<f32>):
58          %1 = complex.add %a, %b : complex<f32>
59          linalg.yield %1 : complex<f32>
60    } -> tensor<?xcomplex<f32>, #SparseVector>
61    return %0 : tensor<?xcomplex<f32>, #SparseVector>
62  }
63
64  func.func @cmul(%arga: tensor<?xcomplex<f32>, #SparseVector>,
65                  %argb: tensor<?xcomplex<f32>, #SparseVector>)
66                      -> tensor<?xcomplex<f32>, #SparseVector> {
67    %c = arith.constant 0 : index
68    %d = tensor.dim %arga, %c : tensor<?xcomplex<f32>, #SparseVector>
69    %xv = tensor.empty(%d) : tensor<?xcomplex<f32>, #SparseVector>
70    %0 = linalg.generic #trait_op
71       ins(%arga, %argb: tensor<?xcomplex<f32>, #SparseVector>,
72                         tensor<?xcomplex<f32>, #SparseVector>)
73        outs(%xv: tensor<?xcomplex<f32>, #SparseVector>) {
74        ^bb(%a: complex<f32>, %b: complex<f32>, %x: complex<f32>):
75          %1 = complex.mul %a, %b : complex<f32>
76          linalg.yield %1 : complex<f32>
77    } -> tensor<?xcomplex<f32>, #SparseVector>
78    return %0 : tensor<?xcomplex<f32>, #SparseVector>
79  }
80
81  // Driver method to call and verify complex kernels.
82  func.func @main() {
83    // Setup sparse vectors.
84    %v1 = arith.constant sparse<
85       [ [0], [28], [31] ],
86         [ (511.13, 2.0), (3.0, 4.0), (5.0, 6.0) ] > : tensor<32xcomplex<f32>>
87    %v2 = arith.constant sparse<
88       [ [1], [28], [31] ],
89         [ (1.0, 0.0), (2.0, 0.0), (3.0, 0.0) ] > : tensor<32xcomplex<f32>>
90    %sv1 = sparse_tensor.convert %v1 : tensor<32xcomplex<f32>> to tensor<?xcomplex<f32>, #SparseVector>
91    %sv2 = sparse_tensor.convert %v2 : tensor<32xcomplex<f32>> to tensor<?xcomplex<f32>, #SparseVector>
92
93    // Call sparse vector kernels.
94    %0 = call @cadd(%sv1, %sv2)
95       : (tensor<?xcomplex<f32>, #SparseVector>,
96          tensor<?xcomplex<f32>, #SparseVector>) -> tensor<?xcomplex<f32>, #SparseVector>
97    %1 = call @cmul(%sv1, %sv2)
98       : (tensor<?xcomplex<f32>, #SparseVector>,
99          tensor<?xcomplex<f32>, #SparseVector>) -> tensor<?xcomplex<f32>, #SparseVector>
100
101    //
102    // Verify the results.
103    //
104    // CHECK:   ---- Sparse Tensor ----
105    // CHECK-NEXT: nse = 4
106    // CHECK-NEXT: dim = ( 32 )
107    // CHECK-NEXT: lvl = ( 32 )
108    // CHECK-NEXT: pos[0] : ( 0, 4 )
109    // CHECK-NEXT: crd[0] : ( 0, 1, 28, 31 )
110    // CHECK-NEXT: values : ( ( 511.13, 2 ), ( 1, 0 ), ( 5, 4 ), ( 8, 6 ) )
111    // CHECK-NEXT: ----
112    //
113    // CHECK-NEXT: ---- Sparse Tensor ----
114    // CHECK-NEXT: nse = 2
115    // CHECK-NEXT: dim = ( 32 )
116    // CHECK-NEXT: lvl = ( 32 )
117    // CHECK-NEXT: pos[0] : ( 0, 2 )
118    // CHECK-NEXT: crd[0] : ( 28, 31 )
119    // CHECK-NEXT: values : ( ( 6, 8 ), ( 15, 18 ) )
120    // CHECK-NEXT: ----
121    //
122    sparse_tensor.print %0 : tensor<?xcomplex<f32>, #SparseVector>
123    sparse_tensor.print %1 : tensor<?xcomplex<f32>, #SparseVector>
124
125    // Release the resources.
126    bufferization.dealloc_tensor %sv1 : tensor<?xcomplex<f32>, #SparseVector>
127    bufferization.dealloc_tensor %sv2 : tensor<?xcomplex<f32>, #SparseVector>
128    bufferization.dealloc_tensor %0 : tensor<?xcomplex<f32>, #SparseVector>
129    bufferization.dealloc_tensor %1 : tensor<?xcomplex<f32>, #SparseVector>
130    return
131  }
132}
133