xref: /llvm-project/mlir/test/Dialect/SparseTensor/sparse_perm.mlir (revision ced2fc7819d5ddea616ec330f18e08ff284c1868)
1// NOTE: Assertions have been autogenerated by utils/generate-test-checks.py
2// RUN: mlir-opt %s --sparse-reinterpret-map -sparsification | FileCheck %s
3
4#X = #sparse_tensor.encoding<{
5  map = (d0, d1, d2) -> (d2 : dense, d0 : dense, d1 : dense)
6}>
7
8#trait = {
9  indexing_maps = [
10    affine_map<(i,j,k) -> (k,i,j)>,  // A (in)
11    affine_map<(i,j,k) -> (i,j,k)>   // X (out)
12  ],
13  iterator_types = ["parallel", "parallel", "parallel"]
14}
15
16// CHECK-LABEL:   func @sparse_static_dims(
17// CHECK-SAME:                          %[[VAL_0:.*]]: tensor<10x20x30xf32, #sparse{{[0-9]*}}>,
18// CHECK-SAME:                          %[[VAL_1:.*]]: tensor<20x30x10xf32>) -> tensor<20x30x10xf32> {
19// CHECK-DAG:       %[[ZERO:.*]] = arith.constant 0.000000e+00 : f32
20// CHECK-DAG:       %[[VAL_2:.*]] = arith.constant 20 : index
21// CHECK-DAG:       %[[VAL_3:.*]] = arith.constant 30 : index
22// CHECK-DAG:       %[[VAL_4:.*]] = arith.constant 10 : index
23// CHECK-DAG:       %[[VAL_5:.*]] = arith.constant 0 : index
24// CHECK-DAG:       %[[VAL_6:.*]] = arith.constant 1 : index
25// CHECK:           %[[DEMAP:.*]] = sparse_tensor.reinterpret_map %[[VAL_0]]
26// CHECK-DAG:       %[[VAL_7:.*]] = sparse_tensor.values %[[DEMAP]] : tensor<30x10x20xf32, #sparse{{[0-9]*}}>
27// CHECK-DAG:       %[[VAL_9:.*]] = bufferization.to_memref %[[VAL_1]] : tensor<20x30x10xf32> to memref<20x30x10xf32>
28// CHECK:           linalg.fill ins(%[[ZERO]] : f32) outs(%[[VAL_9]] : memref<20x30x10xf32>)
29// CHECK:           scf.for %[[VAL_10:.*]] = %[[VAL_5]] to %[[VAL_3]] step %[[VAL_6]] {
30// CHECK:             %[[VAL_12:.*]] = arith.muli %[[VAL_10]], %[[VAL_4]] : index
31// CHECK:             scf.for %[[VAL_11:.*]] = %[[VAL_5]] to %[[VAL_4]] step %[[VAL_6]] {
32// CHECK:               %[[VAL_13:.*]] = arith.addi %[[VAL_11]], %[[VAL_12]] : index
33// CHECK:               %[[VAL_15:.*]] = arith.muli %[[VAL_13]], %[[VAL_2]] : index
34// CHECK:               scf.for %[[VAL_14:.*]] = %[[VAL_5]] to %[[VAL_2]] step %[[VAL_6]] {
35// CHECK:                 %[[VAL_16:.*]] = arith.addi %[[VAL_14]], %[[VAL_15]] : index
36// CHECK:                 %[[VAL_17:.*]] = memref.load %[[VAL_7]]{{\[}}%[[VAL_16]]] : memref<?xf32>
37// CHECK:                 memref.store %[[VAL_17]], %[[VAL_9]]{{\[}}%[[VAL_14]], %[[VAL_10]], %[[VAL_11]]] : memref<20x30x10xf32>
38// CHECK:               }
39// CHECK:             }
40// CHECK:           }
41// CHECK:           %[[VAL_18:.*]] = bufferization.to_tensor %[[VAL_9]] : memref<20x30x10xf32>
42// CHECK:           return %[[VAL_18]] : tensor<20x30x10xf32>
43// CHECK:         }
44func.func @sparse_static_dims(%arga: tensor<10x20x30xf32, #X>,
45                              %argx: tensor<20x30x10xf32>) -> tensor<20x30x10xf32> {
46  %0 = linalg.generic #trait
47    ins(%arga: tensor<10x20x30xf32, #X>)
48    outs(%argx: tensor<20x30x10xf32>) {
49      ^bb(%a : f32, %x: f32):
50        linalg.yield %a : f32
51  } -> tensor<20x30x10xf32>
52  return %0 : tensor<20x30x10xf32>
53}
54
55// CHECK-LABEL:   func @sparse_dynamic_dims(
56// CHECK-SAME:                          %[[VAL_0:.*]]: tensor<?x?x?xf32, #sparse{{[0-9]*}}>,
57// CHECK-SAME:                          %[[VAL_1:.*]]: tensor<?x?x?xf32>) -> tensor<?x?x?xf32> {
58// CHECK-DAG:       %[[ZERO:.*]] = arith.constant 0.000000e+00 : f32
59// CHECK-DAG:       %[[VAL_2:.*]] = arith.constant 2 : index
60// CHECK-DAG:       %[[VAL_3:.*]] = arith.constant 0 : index
61// CHECK-DAG:       %[[VAL_4:.*]] = arith.constant 1 : index
62// CHECK:           %[[DEMAP:.*]] = sparse_tensor.reinterpret_map %[[VAL_0]]
63// CHECK-DAG:       %[[VAL_5:.*]] = sparse_tensor.values %[[DEMAP]] : tensor<?x?x?xf32, #sparse{{[0-9]*}}>
64// CHECK-DAG:       %[[VAL_6:.*]] = sparse_tensor.lvl %[[DEMAP]], %[[VAL_2]] : tensor<?x?x?xf32, #sparse{{[0-9]*}}>
65// CHECK-DAG:       %[[VAL_7:.*]] = sparse_tensor.lvl %[[DEMAP]], %[[VAL_3]] : tensor<?x?x?xf32, #sparse{{[0-9]*}}>
66// CHECK-DAG:       %[[VAL_8:.*]] = sparse_tensor.lvl %[[DEMAP]], %[[VAL_4]] : tensor<?x?x?xf32, #sparse{{[0-9]*}}>
67// CHECK-DAG:       %[[VAL_10:.*]] = bufferization.to_memref %[[VAL_1]] : tensor<?x?x?xf32> to memref<?x?x?xf32>
68// CHECK-DAG:       linalg.fill ins(%[[ZERO]] : f32) outs(%[[VAL_10]] : memref<?x?x?xf32>)
69// CHECK:           scf.for %[[VAL_11:.*]] = %[[VAL_3]] to %[[VAL_7]] step %[[VAL_4]] {
70// CHECK:             %[[VAL_13:.*]] = arith.muli %[[VAL_11]], %[[VAL_8]] : index
71// CHECK:             scf.for %[[VAL_12:.*]] = %[[VAL_3]] to %[[VAL_8]] step %[[VAL_4]] {
72// CHECK:               %[[VAL_14:.*]] = arith.addi %[[VAL_12]], %[[VAL_13]] : index
73// CHECK:               %[[VAL_16:.*]] = arith.muli %[[VAL_14]], %[[VAL_6]] : index
74// CHECK:               scf.for %[[VAL_15:.*]] = %[[VAL_3]] to %[[VAL_6]] step %[[VAL_4]] {
75// CHECK:                 %[[VAL_17:.*]] = arith.addi %[[VAL_15]], %[[VAL_16]] : index
76// CHECK:                 %[[VAL_18:.*]] = memref.load %[[VAL_5]]{{\[}}%[[VAL_17]]] : memref<?xf32>
77// CHECK:                 memref.store %[[VAL_18]], %[[VAL_10]]{{\[}}%[[VAL_15]], %[[VAL_11]], %[[VAL_12]]] : memref<?x?x?xf32>
78// CHECK:               }
79// CHECK:             }
80// CHECK:           }
81// CHECK:           %[[VAL_19:.*]] = bufferization.to_tensor %[[VAL_10]] : memref<?x?x?xf32>
82// CHECK:           return %[[VAL_19]] : tensor<?x?x?xf32>
83// CHECK:         }
84func.func @sparse_dynamic_dims(%arga: tensor<?x?x?xf32, #X>,
85                               %argx: tensor<?x?x?xf32>) -> tensor<?x?x?xf32> {
86  %0 = linalg.generic #trait
87    ins(%arga: tensor<?x?x?xf32, #X>)
88    outs(%argx: tensor<?x?x?xf32>) {
89      ^bb(%a : f32, %x: f32):
90        linalg.yield %a : f32
91  } -> tensor<?x?x?xf32>
92  return %0 : tensor<?x?x?xf32>
93}
94