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