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