1// NOTE: Assertions have been autogenerated by utils/generate-test-checks.py
2// RUN: mlir-opt %s -sparsification | FileCheck %s
3
4#SpVec = #sparse_tensor.encoding<{ dimLevelType = [ "compressed" ] }>
5#CSR   = #sparse_tensor.encoding<{ dimLevelType = [ "dense", "compressed" ] }>
6
7#trait1 = {
8  indexing_maps = [
9    affine_map<(i) -> (i)>,  // a
10    affine_map<(i) -> (3)>,  // b
11    affine_map<(i) -> (i)>   // x (out)
12  ],
13  iterator_types = ["parallel"],
14  doc = "x(i) += a(i) * b(3)"
15}
16
17// CHECK-LABEL:   func @mul_inv_dense1d(
18// CHECK-SAME:                          %[[VAL_0:.*]]: tensor<32xf32, #sparse_tensor.encoding<{{{.*}}}>>,
19// CHECK-SAME:                          %[[VAL_1:.*]]: tensor<4xf32>,
20// CHECK-SAME:                          %[[VAL_2:.*]]: tensor<32xf32>) -> tensor<32xf32> {
21// CHECK-DAG:       %[[VAL_3:.*]] = arith.constant 0 : index
22// CHECK-DAG:       %[[VAL_4:.*]] = arith.constant 3 : index
23// CHECK-DAG:       %[[VAL_5:.*]] = arith.constant 1 : index
24// CHECK-DAG:       %[[VAL_6:.*]] = sparse_tensor.pointers %[[VAL_0]], %[[VAL_3]] : tensor<32xf32, #sparse_tensor.encoding<{{{.*}}}>>
25// CHECK-DAG:       %[[VAL_7:.*]] = sparse_tensor.indices %[[VAL_0]], %[[VAL_3]] : tensor<32xf32, #sparse_tensor.encoding<{{{.*}}}>>
26// CHECK-DAG:       %[[VAL_8:.*]] = sparse_tensor.values %[[VAL_0]] : tensor<32xf32, #sparse_tensor.encoding<{{{.*}}}>>
27// CHECK-DAG:       %[[VAL_9:.*]] = bufferization.to_memref %[[VAL_1]] : memref<4xf32>
28// CHECK-DAG:       %[[VAL_10:.*]] = bufferization.to_memref %[[VAL_2]] : memref<32xf32>
29// CHECK-DAG:       %[[VAL_11:.*]] = memref.alloc() : memref<32xf32>
30// CHECK:           memref.copy %[[VAL_10]], %[[VAL_11]] : memref<32xf32> to memref<32xf32>
31// CHECK:           %[[VAL_12:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_4]]] : memref<4xf32>
32// CHECK:           %[[VAL_13:.*]] = memref.load %[[VAL_6]]{{\[}}%[[VAL_3]]] : memref<?xindex>
33// CHECK:           %[[VAL_14:.*]] = memref.load %[[VAL_6]]{{\[}}%[[VAL_5]]] : memref<?xindex>
34// CHECK:           scf.for %[[VAL_15:.*]] = %[[VAL_13]] to %[[VAL_14]] step %[[VAL_5]] {
35// CHECK:             %[[VAL_16:.*]] = memref.load %[[VAL_7]]{{\[}}%[[VAL_15]]] : memref<?xindex>
36// CHECK:             %[[VAL_17:.*]] = memref.load %[[VAL_11]]{{\[}}%[[VAL_16]]] : memref<32xf32>
37// CHECK:             %[[VAL_18:.*]] = memref.load %[[VAL_8]]{{\[}}%[[VAL_15]]] : memref<?xf32>
38// CHECK:             %[[VAL_19:.*]] = arith.mulf %[[VAL_18]], %[[VAL_12]] : f32
39// CHECK:             %[[VAL_20:.*]] = arith.addf %[[VAL_17]], %[[VAL_19]] : f32
40// CHECK:             memref.store %[[VAL_20]], %[[VAL_11]]{{\[}}%[[VAL_16]]] : memref<32xf32>
41// CHECK:           }
42// CHECK:           %[[VAL_21:.*]] = bufferization.to_tensor %[[VAL_11]] : memref<32xf32>
43// CHECK:           return %[[VAL_21]] : tensor<32xf32>
44// CHECK:         }
45func.func @mul_inv_dense1d(%arga: tensor<32xf32, #SpVec>,
46                      %argb: tensor<4xf32>,
47                      %argx: tensor<32xf32>) -> tensor<32xf32> {
48  %0 = linalg.generic #trait1
49     ins(%arga, %argb: tensor<32xf32, #SpVec>, tensor<4xf32>)
50    outs(%argx: tensor<32xf32>) {
51      ^bb(%a: f32, %b: f32, %x: f32):
52        %0 = arith.mulf %a, %b : f32
53        %1 = arith.addf %x, %0 : f32
54        linalg.yield %1 : f32
55  } -> tensor<32xf32>
56  return %0 : tensor<32xf32>
57}
58
59#trait2 = {
60  indexing_maps = [
61    affine_map<(i) -> (i)>,  // a
62    affine_map<(i) -> (i+2)>,  // b
63    affine_map<(i) -> (i)>   // x (out)
64  ],
65  iterator_types = ["parallel"],
66  doc = "x(i) = a(i) & b(i+2)"
67}
68
69// CHECK-LABEL:   func @and_affine_dense1d(
70// CHECK-SAME:                             %[[VAL_0:.*]]: tensor<32xi32, #sparse_tensor.encoding<{{{.*}}}>>,
71// CHECK-SAME:                             %[[VAL_1:.*]]: tensor<34xi32>,
72// CHECK-SAME:                             %[[VAL_2:.*]]: tensor<32xi32>) -> tensor<32xi32> {
73// CHECK-DAG:       %[[VAL_3:.*]] = arith.constant 0 : index
74// CHECK-DAG:       %[[VAL_4:.*]] = arith.constant 1 : index
75// CHECK-DAG:       %[[VAL_5:.*]] = arith.constant 2 : index
76// CHECK-DAG:       %[[VAL_6:.*]] = sparse_tensor.pointers %[[VAL_0]], %[[VAL_3]] : tensor<32xi32, #sparse_tensor.encoding<{{{.*}}}>>
77// CHECK-DAG:       %[[VAL_7:.*]] = sparse_tensor.indices %[[VAL_0]], %[[VAL_3]] : tensor<32xi32, #sparse_tensor.encoding<{{{.*}}}>>
78// CHECK-DAG:       %[[VAL_8:.*]] = sparse_tensor.values %[[VAL_0]] : tensor<32xi32, #sparse_tensor.encoding<{{{.*}}}>>
79// CHECK-DAG:       %[[VAL_9:.*]] = bufferization.to_memref %[[VAL_1]] : memref<34xi32>
80// CHECK-DAG:       %[[VAL_10:.*]] = bufferization.to_memref %[[VAL_2]] : memref<32xi32>
81// CHECK-DAG:       %[[VAL_11:.*]] = memref.alloc() : memref<32xi32>
82// CHECK:           memref.copy %[[VAL_10]], %[[VAL_11]] : memref<32xi32> to memref<32xi32>
83// CHECK:           %[[VAL_12:.*]] = memref.load %[[VAL_6]]{{\[}}%[[VAL_3]]] : memref<?xindex>
84// CHECK:           %[[VAL_13:.*]] = memref.load %[[VAL_6]]{{\[}}%[[VAL_4]]] : memref<?xindex>
85// CHECK:           scf.for %[[VAL_14:.*]] = %[[VAL_12]] to %[[VAL_13]] step %[[VAL_4]] {
86// CHECK:             %[[VAL_15:.*]] = memref.load %[[VAL_7]]{{\[}}%[[VAL_14]]] : memref<?xindex>
87// CHECK:             %[[VAL_16:.*]] = memref.load %[[VAL_8]]{{\[}}%[[VAL_14]]] : memref<?xi32>
88// CHECK:             %[[VAL_17:.*]] = arith.addi %[[VAL_15]], %[[VAL_5]] : index
89// CHECK:             %[[VAL_18:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_17]]] : memref<34xi32>
90// CHECK:             %[[VAL_19:.*]] = arith.andi %[[VAL_16]], %[[VAL_18]] : i32
91// CHECK:             memref.store %[[VAL_19]], %[[VAL_11]]{{\[}}%[[VAL_15]]] : memref<32xi32>
92// CHECK:           }
93// CHECK:           %[[VAL_20:.*]] = bufferization.to_tensor %[[VAL_11]] : memref<32xi32>
94// CHECK:           return %[[VAL_20]] : tensor<32xi32>
95// CHECK:         }
96func.func @and_affine_dense1d(%arga: tensor<32xi32, #SpVec>,
97                         %argb: tensor<34xi32>,
98                         %argx: tensor<32xi32>) -> tensor<32xi32> {
99  %0 = linalg.generic #trait2
100     ins(%arga, %argb: tensor<32xi32, #SpVec>, tensor<34xi32>)
101    outs(%argx: tensor<32xi32>) {
102      ^bb(%a: i32, %b: i32, %x: i32):
103        %0 = arith.andi %a, %b : i32
104        linalg.yield %0 : i32
105  } -> tensor<32xi32>
106  return %0 : tensor<32xi32>
107}
108
109#trait3 = {
110  indexing_maps = [
111    affine_map<(i,j) -> (i,j)>,      // a
112    affine_map<(i,j) -> (i+2,j+3)>,  // b
113    affine_map<(i,j) -> (i,j)>       // x (out)
114  ],
115  iterator_types = ["parallel","parallel"],
116  doc = "x(i,j) += a(i,j) * b(i+2,j+3)"
117}
118
119// CHECK-LABEL:   func @mul_affine_dense2d(
120// CHECK-SAME:                             %[[VAL_0:.*]]: tensor<32x16xf64, #sparse_tensor.encoding<{{{.*}}}>>,
121// CHECK-SAME:                             %[[VAL_1:.*]]: tensor<34x19xf64>,
122// CHECK-SAME:                             %[[VAL_2:.*]]: tensor<32x16xf64>) -> tensor<32x16xf64> {
123// CHECK-DAG:       %[[VAL_3:.*]] = arith.constant 1 : index
124// CHECK-DAG:       %[[VAL_4:.*]] = arith.constant 32 : index
125// CHECK-DAG:       %[[VAL_5:.*]] = arith.constant 0 : index
126// CHECK-DAG:       %[[VAL_6:.*]] = arith.constant 2 : index
127// CHECK-DAG:       %[[VAL_7:.*]] = arith.constant 3 : index
128// CHECK-DAG:       %[[VAL_8:.*]] = sparse_tensor.pointers %[[VAL_0]], %[[VAL_3]] : tensor<32x16xf64, #sparse_tensor.encoding<{{{.*}}}>>
129// CHECK-DAG:       %[[VAL_9:.*]] = sparse_tensor.indices %[[VAL_0]], %[[VAL_3]] : tensor<32x16xf64, #sparse_tensor.encoding<{{{.*}}}>>
130// CHECK-DAG:       %[[VAL_10:.*]] = sparse_tensor.values %[[VAL_0]] : tensor<32x16xf64, #sparse_tensor.encoding<{{{.*}}}>>
131// CHECK-DAG:       %[[VAL_11:.*]] = bufferization.to_memref %[[VAL_1]] : memref<34x19xf64>
132// CHECK-DAG:       %[[VAL_12:.*]] = bufferization.to_memref %[[VAL_2]] : memref<32x16xf64>
133// CHECK-DAG:       %[[VAL_13:.*]] = memref.alloc() : memref<32x16xf64>
134// CHECK:           memref.copy %[[VAL_12]], %[[VAL_13]] : memref<32x16xf64> to memref<32x16xf64>
135// CHECK:           scf.for %[[VAL_14:.*]] = %[[VAL_5]] to %[[VAL_4]] step %[[VAL_3]] {
136// CHECK:             %[[VAL_15:.*]] = memref.load %[[VAL_8]]{{\[}}%[[VAL_14]]] : memref<?xindex>
137// CHECK:             %[[VAL_16:.*]] = arith.addi %[[VAL_14]], %[[VAL_3]] : index
138// CHECK:             %[[VAL_17:.*]] = memref.load %[[VAL_8]]{{\[}}%[[VAL_16]]] : memref<?xindex>
139// CHECK:             scf.for %[[VAL_18:.*]] = %[[VAL_15]] to %[[VAL_17]] step %[[VAL_3]] {
140// CHECK:               %[[VAL_19:.*]] = memref.load %[[VAL_9]]{{\[}}%[[VAL_18]]] : memref<?xindex>
141// CHECK:               %[[VAL_20:.*]] = memref.load %[[VAL_13]]{{\[}}%[[VAL_14]], %[[VAL_19]]] : memref<32x16xf64>
142// CHECK:               %[[VAL_21:.*]] = memref.load %[[VAL_10]]{{\[}}%[[VAL_18]]] : memref<?xf64>
143// CHECK:               %[[VAL_22:.*]] = arith.addi %[[VAL_14]], %[[VAL_6]] : index
144// CHECK:               %[[VAL_23:.*]] = arith.addi %[[VAL_19]], %[[VAL_7]] : index
145// CHECK:               %[[VAL_24:.*]] = memref.load %[[VAL_11]]{{\[}}%[[VAL_22]], %[[VAL_23]]] : memref<34x19xf64>
146// CHECK:               %[[VAL_25:.*]] = arith.mulf %[[VAL_21]], %[[VAL_24]] : f64
147// CHECK:               %[[VAL_26:.*]] = arith.addf %[[VAL_20]], %[[VAL_25]] : f64
148// CHECK:               memref.store %[[VAL_26]], %[[VAL_13]]{{\[}}%[[VAL_14]], %[[VAL_19]]] : memref<32x16xf64>
149// CHECK:             }
150// CHECK:           }
151// CHECK:           %[[VAL_27:.*]] = bufferization.to_tensor %[[VAL_13]] : memref<32x16xf64>
152// CHECK:           return %[[VAL_27]] : tensor<32x16xf64>
153// CHECK:         }
154func.func @mul_affine_dense2d(%arga: tensor<32x16xf64, #CSR>,
155                         %argb: tensor<34x19xf64>,
156                         %argx: tensor<32x16xf64>) -> tensor<32x16xf64> {
157  %0 = linalg.generic #trait3
158     ins(%arga, %argb: tensor<32x16xf64, #CSR>, tensor<34x19xf64>)
159    outs(%argx: tensor<32x16xf64>) {
160      ^bb(%a: f64, %b: f64, %x: f64):
161        %0 = arith.mulf %a, %b : f64
162        %1 = arith.addf %x, %0 : f64
163        linalg.yield %1 : f64
164  } -> tensor<32x16xf64>
165  return %0 : tensor<32x16xf64>
166}
167