xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_re_im.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 direct IR generation and vectorization.
28// REDEFINE: %{sparsifier_opts} = enable-runtime-library=false 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)>   // x (out)
40  ],
41  iterator_types = ["parallel"],
42  doc = "x(i) = OP a(i)"
43}
44
45module {
46  func.func @cre(%arga: tensor<?xcomplex<f32>, #SparseVector>)
47                -> tensor<?xf32, #SparseVector> {
48    %c = arith.constant 0 : index
49    %d = tensor.dim %arga, %c : tensor<?xcomplex<f32>, #SparseVector>
50    %xv = tensor.empty(%d) : tensor<?xf32, #SparseVector>
51    %0 = linalg.generic #trait_op
52       ins(%arga: tensor<?xcomplex<f32>, #SparseVector>)
53        outs(%xv: tensor<?xf32, #SparseVector>) {
54        ^bb(%a: complex<f32>, %x: f32):
55          %1 = complex.re %a : complex<f32>
56          linalg.yield %1 : f32
57    } -> tensor<?xf32, #SparseVector>
58    return %0 : tensor<?xf32, #SparseVector>
59  }
60
61  func.func @cim(%arga: tensor<?xcomplex<f32>, #SparseVector>)
62                -> tensor<?xf32, #SparseVector> {
63    %c = arith.constant 0 : index
64    %d = tensor.dim %arga, %c : tensor<?xcomplex<f32>, #SparseVector>
65    %xv = tensor.empty(%d) : tensor<?xf32, #SparseVector>
66    %0 = linalg.generic #trait_op
67       ins(%arga: tensor<?xcomplex<f32>, #SparseVector>)
68        outs(%xv: tensor<?xf32, #SparseVector>) {
69        ^bb(%a: complex<f32>, %x: f32):
70          %1 = complex.im %a : complex<f32>
71          linalg.yield %1 : f32
72    } -> tensor<?xf32, #SparseVector>
73    return %0 : tensor<?xf32, #SparseVector>
74  }
75
76  func.func @main() {
77    // Setup sparse vectors.
78    %v1 = arith.constant sparse<
79       [ [0], [20], [31] ],
80         [ (5.13, 2.0), (3.0, 4.0), (5.0, 6.0) ] > : tensor<32xcomplex<f32>>
81    %sv1 = sparse_tensor.convert %v1 : tensor<32xcomplex<f32>> to tensor<?xcomplex<f32>, #SparseVector>
82
83    // Call sparse vector kernels.
84    %0 = call @cre(%sv1)
85       : (tensor<?xcomplex<f32>, #SparseVector>) -> tensor<?xf32, #SparseVector>
86
87    %1 = call @cim(%sv1)
88       : (tensor<?xcomplex<f32>, #SparseVector>) -> tensor<?xf32, #SparseVector>
89
90    //
91    // Verify the results.
92    //
93    // CHECK:    ---- Sparse Tensor ----
94    // CHECK-NEXT: nse = 3
95    // CHECK-NEXT: dim = ( 32 )
96    // CHECK-NEXT: lvl = ( 32 )
97    // CHECK-NEXT: pos[0] : ( 0, 3 )
98    // CHECK-NEXT: crd[0] : ( 0, 20, 31 )
99    // CHECK-NEXT: values : ( 5.13, 3, 5 )
100    // CHECK-NEXT: ----
101    //
102    // CHECK-NEXT: ---- Sparse Tensor ----
103    // CHECK-NEXT: nse = 3
104    // CHECK-NEXT: dim = ( 32 )
105    // CHECK-NEXT: lvl = ( 32 )
106    // CHECK-NEXT: pos[0] : ( 0, 3 )
107    // CHECK-NEXT: crd[0] : ( 0, 20, 31 )
108    // CHECK-NEXT: values : ( 2, 4, 6 )
109    // CHECK-NEXT: ----
110    //
111    sparse_tensor.print %0 : tensor<?xf32, #SparseVector>
112    sparse_tensor.print %1 : tensor<?xf32, #SparseVector>
113
114    // Release the resources.
115    bufferization.dealloc_tensor %sv1 : tensor<?xcomplex<f32>, #SparseVector>
116    bufferization.dealloc_tensor %0   : tensor<?xf32, #SparseVector>
117    bufferization.dealloc_tensor %1   : tensor<?xf32, #SparseVector>
118    return
119  }
120}
121