xref: /llvm-project/mlir/test/Dialect/SparseTensor/sparse_nd.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// Example with cyclic iteration graph with sparse and dense constraints,
5// but an acyclic iteration graph using sparse constraints only.
6
7#SparseTensor = #sparse_tensor.encoding<{
8  map = (d0, d1, d2, d3,
9         d4, d5, d6, d7) -> (d0 : dense, d1 : dense, d2 : dense,
10                             d3 : compressed, d4 : compressed, d5 : dense,
11                             d6 : dense, d7 : dense)
12}>
13
14#trait_mul = {
15  indexing_maps = [
16    affine_map<(i,j,k,l,m,n,o,p) -> (i,j,k,l,m,n,o,p)>,  // A
17    affine_map<(i,j,k,l,m,n,o,p) -> (p,o,n,m,l,k,j,i)>,  // B
18    affine_map<(i,j,k,l,m,n,o,p) -> (i,j,k,l,m,n,o,p)>   // X
19  ],
20  iterator_types = ["parallel", "parallel", "parallel", "parallel",
21                    "parallel", "parallel", "parallel", "parallel"],
22  doc = "X(i,j,k,l,m,n,o,p) = A(i,j,k,l,m,n,o,p) * B(p,o,n,m,l,k,j,i)"
23}
24
25// CHECK-LABEL:   func @mul(
26// CHECK-SAME:              %[[VAL_0:.*]]: tensor<10x20x30x40x50x60x70x80xf32>,
27// CHECK-SAME:              %[[VAL_1:.*]]: tensor<80x70x60x50x40x30x20x10xf32, #sparse{{[0-9]*}}>,
28// CHECK-SAME:              %[[VAL_2:.*]]: tensor<10x20x30x40x50x60x70x80xf32>) -> tensor<10x20x30x40x50x60x70x80xf32> {
29// CHECK-DAG:       %[[ZERO:.*]] = arith.constant 0.000000e+00 : f32
30// CHECK-DAG:       %[[VAL_5:.*]] = arith.constant 10 : index
31// CHECK-DAG:       %[[VAL_6:.*]] = arith.constant 20 : index
32// CHECK-DAG:       %[[VAL_7:.*]] = arith.constant 30 : index
33// CHECK-DAG:       %[[VAL_8:.*]] = arith.constant 60 : index
34// CHECK-DAG:       %[[VAL_9:.*]] = arith.constant 70 : index
35// CHECK-DAG:       %[[VAL_10:.*]] = arith.constant 80 : index
36// CHECK-DAG:       %[[VAL_11:.*]] = arith.constant 0 : index
37// CHECK-DAG:       %[[VAL_12:.*]] = arith.constant 1 : index
38// CHECK-DAG:       %[[VAL_13:.*]] = bufferization.to_memref %[[VAL_0]] : tensor<10x20x30x40x50x60x70x80xf32> to memref<10x20x30x40x50x60x70x80xf32>
39// CHECK-DAG:       %[[VAL_14:.*]] = sparse_tensor.positions %[[VAL_1]] {level = 3 : index} : tensor<80x70x60x50x40x30x20x10xf32, #sparse{{[0-9]*}}> to memref<?xindex>
40// CHECK-DAG:       %[[VAL_15:.*]] = sparse_tensor.coordinates %[[VAL_1]] {level = 3 : index} : tensor<80x70x60x50x40x30x20x10xf32, #sparse{{[0-9]*}}> to memref<?xindex>
41// CHECK-DAG:       %[[VAL_16:.*]] = sparse_tensor.positions %[[VAL_1]] {level = 4 : index} : tensor<80x70x60x50x40x30x20x10xf32, #sparse{{[0-9]*}}> to memref<?xindex>
42// CHECK-DAG:       %[[VAL_17:.*]] = sparse_tensor.coordinates %[[VAL_1]] {level = 4 : index} : tensor<80x70x60x50x40x30x20x10xf32, #sparse{{[0-9]*}}> to memref<?xindex>
43// CHECK-DAG:       %[[VAL_18:.*]] = sparse_tensor.values %[[VAL_1]] : tensor<80x70x60x50x40x30x20x10xf32, #sparse{{[0-9]*}}> to memref<?xf32>
44// CHECK-DAG:       %[[VAL_20:.*]] = bufferization.to_memref %[[VAL_2]] : tensor<10x20x30x40x50x60x70x80xf32> to memref<10x20x30x40x50x60x70x80xf32>
45// CHECK-DAG:       linalg.fill ins(%[[ZERO]] : f32) outs(%[[VAL_20]] : memref<10x20x30x40x50x60x70x80xf32>
46// CHECK:           scf.for %[[VAL_21:.*]] = %[[VAL_11]] to %[[VAL_10]] step %[[VAL_12]] {
47// CHECK:             %[[VAL_23:.*]] = arith.muli %[[VAL_21]], %[[VAL_9]] : index
48// CHECK:             scf.for %[[VAL_22:.*]] = %[[VAL_11]] to %[[VAL_9]] step %[[VAL_12]] {
49// CHECK:               %[[VAL_24:.*]] = arith.addi %[[VAL_22]], %[[VAL_23]] : index
50// CHECK:               %[[VAL_26:.*]] = arith.muli %[[VAL_24]], %[[VAL_8]] : index
51// CHECK:               scf.for %[[VAL_25:.*]] = %[[VAL_11]] to %[[VAL_8]] step %[[VAL_12]] {
52// CHECK:                 %[[VAL_27:.*]] = arith.addi %[[VAL_25]], %[[VAL_26]] : index
53// CHECK:                 %[[VAL_28:.*]] = memref.load %[[VAL_14]]{{\[}}%[[VAL_27]]] : memref<?xindex>
54// CHECK:                 %[[VAL_29:.*]] = arith.addi %[[VAL_27]], %[[VAL_12]] : index
55// CHECK:                 %[[VAL_30:.*]] = memref.load %[[VAL_14]]{{\[}}%[[VAL_29]]] : memref<?xindex>
56// CHECK:                 scf.for %[[VAL_31:.*]] = %[[VAL_28]] to %[[VAL_30]] step %[[VAL_12]] {
57// CHECK:                   %[[VAL_32:.*]] = memref.load %[[VAL_15]]{{\[}}%[[VAL_31]]] : memref<?xindex>
58// CHECK:                   %[[VAL_33:.*]] = memref.load %[[VAL_16]]{{\[}}%[[VAL_31]]] : memref<?xindex>
59// CHECK:                   %[[VAL_34:.*]] = arith.addi %[[VAL_31]], %[[VAL_12]] : index
60// CHECK:                   %[[VAL_35:.*]] = memref.load %[[VAL_16]]{{\[}}%[[VAL_34]]] : memref<?xindex>
61// CHECK:                   scf.for %[[VAL_36:.*]] = %[[VAL_33]] to %[[VAL_35]] step %[[VAL_12]] {
62// CHECK:                     %[[VAL_37:.*]] = memref.load %[[VAL_17]]{{\[}}%[[VAL_36]]] : memref<?xindex>
63// CHECK:                     %[[VAL_39:.*]] = arith.muli %[[VAL_36]], %[[VAL_7]] : index
64// CHECK:                     scf.for %[[VAL_38:.*]] = %[[VAL_11]] to %[[VAL_7]] step %[[VAL_12]] {
65// CHECK:                       %[[VAL_40:.*]] = arith.addi %[[VAL_38]], %[[VAL_39]] : index
66// CHECK:                       %[[VAL_42:.*]] = arith.muli %[[VAL_40]], %[[VAL_6]] : index
67// CHECK:                       scf.for %[[VAL_41:.*]] = %[[VAL_11]] to %[[VAL_6]] step %[[VAL_12]] {
68// CHECK:                         %[[VAL_43:.*]] = arith.addi %[[VAL_41]], %[[VAL_42]] : index
69// CHECK:                         %[[VAL_45:.*]] = arith.muli %[[VAL_43]], %[[VAL_5]] : index
70// CHECK:                         scf.for %[[VAL_44:.*]] = %[[VAL_11]] to %[[VAL_5]] step %[[VAL_12]] {
71// CHECK:                           %[[VAL_46:.*]] = arith.addi %[[VAL_44]], %[[VAL_45]] : index
72// CHECK:                           %[[VAL_47:.*]] = memref.load %[[VAL_13]]{{\[}}%[[VAL_44]], %[[VAL_41]], %[[VAL_38]], %[[VAL_37]], %[[VAL_32]], %[[VAL_25]], %[[VAL_22]], %[[VAL_21]]] : memref<10x20x30x40x50x60x70x80xf32>
73// CHECK:                           %[[VAL_48:.*]] = memref.load %[[VAL_18]]{{\[}}%[[VAL_46]]] : memref<?xf32>
74// CHECK:                           %[[VAL_49:.*]] = arith.mulf %[[VAL_47]], %[[VAL_48]] : f32
75// CHECK:                           memref.store %[[VAL_49]], %[[VAL_20]]{{\[}}%[[VAL_44]], %[[VAL_41]], %[[VAL_38]], %[[VAL_37]], %[[VAL_32]], %[[VAL_25]], %[[VAL_22]], %[[VAL_21]]] : memref<10x20x30x40x50x60x70x80xf32>
76// CHECK:                         }
77// CHECK:                       }
78// CHECK:                     }
79// CHECK:                   }
80// CHECK:                 }
81// CHECK:               }
82// CHECK:             }
83// CHECK:           }
84// CHECK:           %[[VAL_50:.*]] = bufferization.to_tensor %[[VAL_20]] : memref<10x20x30x40x50x60x70x80xf32>
85// CHECK:           return %[[VAL_50]] : tensor<10x20x30x40x50x60x70x80xf32>
86// CHECK:         }
87func.func @mul(%arga: tensor<10x20x30x40x50x60x70x80xf32>,
88          %argb: tensor<80x70x60x50x40x30x20x10xf32, #SparseTensor>,
89          %argx: tensor<10x20x30x40x50x60x70x80xf32>)
90	      -> tensor<10x20x30x40x50x60x70x80xf32> {
91  %0 = linalg.generic #trait_mul
92    ins(%arga, %argb: tensor<10x20x30x40x50x60x70x80xf32>,
93                      tensor<80x70x60x50x40x30x20x10xf32, #SparseTensor>)
94    outs(%argx: tensor<10x20x30x40x50x60x70x80xf32>) {
95      ^bb(%a: f32, %b: f32, %x: f32):
96        %0 = arith.mulf %a, %b : f32
97        linalg.yield %0 : f32
98    }      -> tensor<10x20x30x40x50x60x70x80xf32>
99  return %0 : tensor<10x20x30x40x50x60x70x80xf32>
100}
101