1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
|
// RUN: mlir-opt %s -pass-pipeline='builtin.module(func.func(test-affine-reify-value-bounds))' -verify-diagnostics \
// RUN: -split-input-file | FileCheck %s
func.func @unknown_op() -> index {
%0 = "test.foo"() : () -> (tensor<?x?xf32>)
// expected-error @below{{could not reify bound}}
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?x?xf32>) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @cast(
// CHECK: %[[c10:.*]] = arith.constant 10 : index
// CHECK: return %[[c10]]
func.func @cast(%t: tensor<10xf32>) -> index {
%0 = tensor.cast %t : tensor<10xf32> to tensor<?xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?xf32>) -> (index)
return %1 : index
}
// -----
func.func @cast_unranked(%t: tensor<*xf32>) -> index {
%0 = tensor.cast %t : tensor<*xf32> to tensor<?xf32>
// expected-error @below{{could not reify bound}}
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?xf32>) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @dim(
// CHECK-SAME: %[[t:.*]]: tensor<?xf32>
// CHECK: %[[dim:.*]] = tensor.dim %[[t]]
// CHECK: %[[dim:.*]] = tensor.dim %[[t]]
// CHECK: return %[[dim]]
func.func @dim(%t: tensor<?xf32>) -> index {
%c0 = arith.constant 0 : index
%0 = tensor.dim %t, %c0 : tensor<?xf32>
%1 = "test.reify_bound"(%0) : (index) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @dim_all_positive(
func.func @dim_all_positive(%t: tensor<?xf32>, %x: index) {
%c0 = arith.constant 0 : index
%0 = tensor.dim %t, %x : tensor<?xf32>
// expected-remark @below{{true}}
"test.compare"(%0, %c0) {cmp = "GE" } : (index, index) -> ()
return
}
// -----
// CHECK-LABEL: func @empty(
// CHECK-SAME: %[[sz:.*]]: index
// CHECK: %[[c6:.*]] = arith.constant 6 : index
// CHECK: return %[[c6]], %[[sz]]
func.func @empty(%sz: index) -> (index, index) {
%0 = tensor.empty(%sz) : tensor<6x?xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<6x?xf32>) -> (index)
%2 = "test.reify_bound"(%0) {dim = 1} : (tensor<6x?xf32>) -> (index)
return %1, %2 : index, index
}
// -----
// CHECK-LABEL: func @extract_slice_dynamic(
// CHECK-SAME: %[[t:.*]]: tensor<?xf32>, %[[sz:.*]]: index
// CHECK: return %[[sz]]
func.func @extract_slice_dynamic(%t: tensor<?xf32>, %sz: index) -> index {
%0 = tensor.extract_slice %t[2][%sz][1] : tensor<?xf32> to tensor<?xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?xf32>) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @extract_slice_static(
// CHECK-SAME: %[[t:.*]]: tensor<?xf32>
// CHECK: %[[c5:.*]] = arith.constant 5 : index
// CHECK: return %[[c5]]
func.func @extract_slice_static(%t: tensor<?xf32>) -> index {
%0 = tensor.extract_slice %t[2][5][1] : tensor<?xf32> to tensor<5xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<5xf32>) -> (index)
return %1 : index
}
// -----
func.func @extract_slice_dynamic_constant(%t: tensor<?xf32>, %sz: index) -> index {
%0 = tensor.extract_slice %t[2][%sz][1] : tensor<?xf32> to tensor<?xf32>
// expected-error @below{{could not reify bound}}
%1 = "test.reify_bound"(%0) {dim = 0, constant} : (tensor<?xf32>) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @extract_slice_static_constant(
// CHECK-SAME: %[[t:.*]]: tensor<?xf32>
// CHECK: %[[c5:.*]] = arith.constant 5 : index
// CHECK: return %[[c5]]
func.func @extract_slice_static_constant(%t: tensor<?xf32>) -> index {
%0 = tensor.extract_slice %t[2][5][1] : tensor<?xf32> to tensor<5xf32>
%1 = "test.reify_bound"(%0) {dim = 0, constant} : (tensor<5xf32>) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @extract_slice_rank_reduce(
// CHECK-SAME: %[[t:.*]]: tensor<?x?xf32>, %[[sz:.*]]: index
// CHECK: return %[[sz]]
func.func @extract_slice_rank_reduce(%t: tensor<?x?xf32>, %sz: index) -> index {
%0 = tensor.extract_slice %t[0, 2][1, %sz][1, 1] : tensor<?x?xf32> to tensor<?xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?xf32>) -> (index)
return %1 : index
}
// -----
// CHECK-LABEL: func @insert(
// CHECK-SAME: %[[t:.*]]: tensor<?xf32>
// CHECK: %[[c0:.*]] = arith.constant 0 : index
// CHECK: %[[dim:.*]] = tensor.dim %[[t]], %[[c0]]
// CHECK: return %[[dim]]
func.func @insert(%t: tensor<?xf32>, %f: f32, %pos: index) -> index {
%0 = tensor.insert %f into %t[%pos] : tensor<?xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?xf32>) -> (index)
return %1 : index
}
// -----
// CHECK: #[[$map:.*]] = affine_map<()[s0, s1] -> (s0 + s1 * 2)>
// CHECK: #[[$map1:.*]] = affine_map<()[s0] -> (s0 + 12)>
// CHECK-LABEL: func @pad(
// CHECK-SAME: %[[t:.*]]: tensor<?x7xf32>, %[[a:.*]]: index, %[[b:.*]]: index
// CHECK: %[[c0:.*]] = arith.constant 0 : index
// CHECK: %[[dim0:.*]] = tensor.dim %[[t]], %[[c0]]
// CHECK: %[[bound0:.*]] = affine.apply #[[$map]]()[%[[dim0]], %[[a]]]
// CHECK: %[[bound1:.*]] = affine.apply #[[$map1]]()[%[[b]]]
// CHECK: return %[[bound0]], %[[bound1]]
func.func @pad(%t: tensor<?x7xf32>, %a: index, %b: index) -> (index, index) {
%pad = arith.constant 0.0 : f32
%0 = tensor.pad %t low[%a, 5] high[%a, %b] {
^bb0(%arg1: index, %arg2: index):
tensor.yield %pad : f32
} : tensor<?x7xf32> to tensor<?x?xf32>
%1 = "test.reify_bound"(%0) {dim = 0} : (tensor<?x?xf32>) -> (index)
%2 = "test.reify_bound"(%0) {dim = 1} : (tensor<?x?xf32>) -> (index)
return %1, %2 : index, index
}
// -----
// CHECK-LABEL: func @rank(
// CHECK-SAME: %[[t:.*]]: tensor<5xf32>
// CHECK: %[[c1:.*]] = arith.constant 1 : index
// CHECK: return %[[c1]]
func.func @rank(%t: tensor<5xf32>) -> index {
%0 = tensor.rank %t : tensor<5xf32>
%1 = "test.reify_bound"(%0) : (index) -> (index)
return %1 : index
}
// -----
func.func @dynamic_dims_are_equal(%t: tensor<?xf32>) {
%c0 = arith.constant 0 : index
%dim0 = tensor.dim %t, %c0 : tensor<?xf32>
%dim1 = tensor.dim %t, %c0 : tensor<?xf32>
// expected-remark @below {{true}}
"test.compare"(%dim0, %dim1) : (index, index) -> ()
return
}
// -----
func.func @dynamic_dims_are_different(%t: tensor<?xf32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%dim0 = tensor.dim %t, %c0 : tensor<?xf32>
%val = arith.addi %dim0, %c1 : index
// expected-remark @below {{false}}
"test.compare"(%dim0, %val) : (index, index) -> ()
return
}
// -----
func.func @dynamic_dims_are_maybe_equal_1(%t: tensor<?xf32>) {
%c0 = arith.constant 0 : index
%c5 = arith.constant 5 : index
%dim0 = tensor.dim %t, %c0 : tensor<?xf32>
// expected-error @below {{unknown}}
"test.compare"(%dim0, %c5) : (index, index) -> ()
return
}
// -----
func.func @dynamic_dims_are_maybe_equal_2(%t: tensor<?x?xf32>) {
%c0 = arith.constant 0 : index
%c1 = arith.constant 1 : index
%dim0 = tensor.dim %t, %c0 : tensor<?x?xf32>
%dim1 = tensor.dim %t, %c1 : tensor<?x?xf32>
// expected-error @below {{unknown}}
"test.compare"(%dim0, %dim1) : (index, index) -> ()
return
}
// -----
// CHECK-LABEL: func.func @pad_reification
func.func @pad_reification(%cst : f32, %idx : index, %t: tensor<64x?x64xf32>) {
%pad_amt = affine.apply affine_map<(d0) -> (-d0 + 256)>(%idx)
%es = tensor.extract_slice %t[0, 0, 0] [1, %idx, 64] [1, 1, 1] : tensor<64x?x64xf32> to tensor<1x?x64xf32>
%padded = tensor.pad %es low[0, 0, 0] high[0, %pad_amt, 0] {
^bb0(%a: index, %b: index, %c: index):
tensor.yield %cst : f32
} : tensor<1x?x64xf32> to tensor<1x?x64xf32>
// CHECK: arith.constant 256 : index
%1 = "test.reify_bound"(%padded) {dim = 1, constant} : (tensor<1x?x64xf32>) -> (index)
return
}
|