xref: /llvm-project/mlir/test/Integration/Dialect/SparseTensor/CPU/sparse_empty.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
35#map = affine_map<(d0) -> (d0)>
36
37#SV  = #sparse_tensor.encoding<{
38  map = (d0) -> (d0 : compressed)
39}>
40
41module {
42
43  // This directly yields an empty sparse vector.
44  func.func @empty() -> tensor<10xf32, #SV> {
45    %0 = tensor.empty() : tensor<10xf32, #SV>
46    return %0 : tensor<10xf32, #SV>
47  }
48
49  // This also directly yields an empty sparse vector.
50  func.func @empty_alloc() -> tensor<10xf32, #SV> {
51    %0 = bufferization.alloc_tensor() : tensor<10xf32, #SV>
52    return %0 : tensor<10xf32, #SV>
53  }
54
55  // This yields a hidden empty sparse vector (all zeros).
56  func.func @zeros() -> tensor<10xf32, #SV> {
57    %cst = arith.constant 0.0 : f32
58    %0 = bufferization.alloc_tensor() : tensor<10xf32, #SV>
59    %1 = linalg.generic {
60        indexing_maps = [#map],
61	iterator_types = ["parallel"]}
62      outs(%0 : tensor<10xf32, #SV>) {
63         ^bb0(%out: f32):
64            linalg.yield %cst : f32
65    } -> tensor<10xf32, #SV>
66    return %1 : tensor<10xf32, #SV>
67  }
68
69  // This yields a filled sparse vector (all ones).
70  func.func @ones() -> tensor<10xf32, #SV> {
71    %cst = arith.constant 1.0 : f32
72    %0 = bufferization.alloc_tensor() : tensor<10xf32, #SV>
73    %1 = linalg.generic {
74        indexing_maps = [#map],
75	iterator_types = ["parallel"]}
76      outs(%0 : tensor<10xf32, #SV>) {
77         ^bb0(%out: f32):
78            linalg.yield %cst : f32
79    } -> tensor<10xf32, #SV>
80    return %1 : tensor<10xf32, #SV>
81  }
82
83  //
84  // Main driver.
85  //
86  func.func @main() {
87
88    %0 = call @empty()       : () -> tensor<10xf32, #SV>
89    %1 = call @empty_alloc() : () -> tensor<10xf32, #SV>
90    %2 = call @zeros()       : () -> tensor<10xf32, #SV>
91    %3 = call @ones()        : () -> tensor<10xf32, #SV>
92
93    //
94    // Verify the output. In particular, make sure that
95    // all empty sparse vector data structures are properly
96    // finalized with a pair (0,0) for positions.
97    //
98    // CHECK:      ---- Sparse Tensor ----
99    // CHECK-NEXT: nse = 0
100    // CHECK-NEXT: dim = ( 10 )
101    // CHECK-NEXT: lvl = ( 10 )
102    // CHECK-NEXT: pos[0] : ( 0, 0 )
103    // CHECK-NEXT: crd[0] : ( )
104    // CHECK-NEXT: values : ( )
105    // CHECK-NEXT: ----
106    //
107    // CHECK-NEXT: ---- Sparse Tensor ----
108    // CHECK-NEXT: nse = 0
109    // CHECK-NEXT: dim = ( 10 )
110    // CHECK-NEXT: lvl = ( 10 )
111    // CHECK-NEXT: pos[0] : ( 0, 0 )
112    // CHECK-NEXT: crd[0] : ( )
113    // CHECK-NEXT: values : ( )
114    // CHECK-NEXT: ----
115    //
116    // CHECK-NEXT: ---- Sparse Tensor ----
117    // CHECK-NEXT: nse = 0
118    // CHECK-NEXT: dim = ( 10 )
119    // CHECK-NEXT: lvl = ( 10 )
120    // CHECK-NEXT: pos[0] : ( 0, 0 )
121    // CHECK-NEXT: crd[0] : ( )
122    // CHECK-NEXT: values : ( )
123    // CHECK-NEXT: ----
124    //
125    // CHECK-NEXT: ---- Sparse Tensor ----
126    // CHECK-NEXT: nse = 10
127    // CHECK-NEXT: dim = ( 10 )
128    // CHECK-NEXT: lvl = ( 10 )
129    // CHECK-NEXT: pos[0] : ( 0, 10 )
130    // CHECK-NEXT: crd[0] : ( 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 )
131    // CHECK-NEXT: values : ( 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 )
132    // CHECK-NEXT: ----
133    //
134    sparse_tensor.print %0 : tensor<10xf32, #SV>
135    sparse_tensor.print %1 : tensor<10xf32, #SV>
136    sparse_tensor.print %2 : tensor<10xf32, #SV>
137    sparse_tensor.print %3 : tensor<10xf32, #SV>
138
139    bufferization.dealloc_tensor %0 : tensor<10xf32, #SV>
140    bufferization.dealloc_tensor %1 : tensor<10xf32, #SV>
141    bufferization.dealloc_tensor %2 : tensor<10xf32, #SV>
142    bufferization.dealloc_tensor %3 : tensor<10xf32, #SV>
143    return
144  }
145}
146