15
15
#include " mlir/IR/Value.h"
16
16
#include " mlir/Interfaces/DestinationStyleOpInterface.h"
17
17
#include " llvm/ADT/SetVector.h"
18
+ #include " llvm/ADT/SmallVector.h"
18
19
#include " llvm/Support/ExtensibleRTTI.h"
19
20
20
21
#include < queue>
@@ -111,6 +112,39 @@ class ValueBoundsConstraintSet
111
112
public:
112
113
static char ID;
113
114
115
+ // / A variable that can be added to the constraint set as a "column". The
116
+ // / value bounds infrastructure can compute bounds for variables and compare
117
+ // / two variables.
118
+ // /
119
+ // / Internally, a variable is represented as an affine map and operands.
120
+ class Variable {
121
+ public:
122
+ // / Construct a variable for an index-typed attribute or SSA value.
123
+ Variable (OpFoldResult ofr);
124
+
125
+ // / Construct a variable for an index-typed SSA value.
126
+ Variable (Value indexValue);
127
+
128
+ // / Construct a variable for a dimension of a shaped value.
129
+ Variable (Value shapedValue, int64_t dim);
130
+
131
+ // / Construct a variable for an index-typed attribute/SSA value or for a
132
+ // / dimension of a shaped value. A non-null dimension must be provided if
133
+ // / and only if `ofr` is a shaped value.
134
+ Variable (OpFoldResult ofr, std::optional<int64_t > dim);
135
+
136
+ // / Construct a variable for a map and its operands.
137
+ Variable (AffineMap map, ArrayRef<Variable> mapOperands);
138
+ Variable (AffineMap map, ArrayRef<Value> mapOperands);
139
+
140
+ MLIRContext *getContext () const { return map.getContext (); }
141
+
142
+ private:
143
+ friend class ValueBoundsConstraintSet ;
144
+ AffineMap map;
145
+ ValueDimList mapOperands;
146
+ };
147
+
114
148
// / The stop condition when traversing the backward slice of a shaped value/
115
149
// / index-type value. The traversal continues until the stop condition
116
150
// / evaluates to "true" for a value.
@@ -121,35 +155,31 @@ class ValueBoundsConstraintSet
121
155
using StopConditionFn = std::function<bool (
122
156
Value, std::optional<int64_t > /* dim*/ , ValueBoundsConstraintSet &cstr)>;
123
157
124
- // / Compute a bound for the given index-typed value or shape dimension size.
125
- // / The computed bound is stored in `resultMap`. The operands of the bound are
126
- // / stored in `mapOperands`. An operand is either an index-type SSA value
127
- // / or a shaped value and a dimension.
158
+ // / Compute a bound for the given variable. The computed bound is stored in
159
+ // / `resultMap`. The operands of the bound are stored in `mapOperands`. An
160
+ // / operand is either an index-type SSA value or a shaped value and a
161
+ // / dimension.
128
162
// /
129
- // / `dim` must be `nullopt` if and only if `value` is index-typed. The bound
130
- // / is computed in terms of values/dimensions for which `stopCondition`
131
- // / evaluates to "true". To that end, the backward slice (reverse use-def
132
- // / chain) of the given value is visited in a worklist-driven manner and the
133
- // / constraint set is populated according to `ValueBoundsOpInterface` for each
134
- // / visited value.
163
+ // / The bound is computed in terms of values/dimensions for which
164
+ // / `stopCondition` evaluates to "true". To that end, the backward slice
165
+ // / (reverse use-def chain) of the given value is visited in a worklist-driven
166
+ // / manner and the constraint set is populated according to
167
+ // / `ValueBoundsOpInterface` for each visited value.
135
168
// /
136
169
// / By default, lower/equal bounds are closed and upper bounds are open. If
137
170
// / `closedUB` is set to "true", upper bounds are also closed.
138
- static LogicalResult computeBound (AffineMap &resultMap,
139
- ValueDimList &mapOperands,
140
- presburger::BoundType type, Value value,
141
- std::optional<int64_t > dim,
142
- StopConditionFn stopCondition,
143
- bool closedUB = false );
171
+ static LogicalResult
172
+ computeBound (AffineMap &resultMap, ValueDimList &mapOperands,
173
+ presburger::BoundType type, const Variable &var,
174
+ StopConditionFn stopCondition, bool closedUB = false );
144
175
145
176
// / Compute a bound in terms of the values/dimensions in `dependencies`. The
146
177
// / computed bound consists of only constant terms and dependent values (or
147
178
// / dimension sizes thereof).
148
179
static LogicalResult
149
180
computeDependentBound (AffineMap &resultMap, ValueDimList &mapOperands,
150
- presburger::BoundType type, Value value,
151
- std::optional<int64_t > dim, ValueDimList dependencies,
152
- bool closedUB = false );
181
+ presburger::BoundType type, const Variable &var,
182
+ ValueDimList dependencies, bool closedUB = false );
153
183
154
184
// / Compute a bound in that is independent of all values in `independencies`.
155
185
// /
@@ -161,13 +191,10 @@ class ValueBoundsConstraintSet
161
191
// / appear in the computed bound.
162
192
static LogicalResult
163
193
computeIndependentBound (AffineMap &resultMap, ValueDimList &mapOperands,
164
- presburger::BoundType type, Value value,
165
- std::optional<int64_t > dim, ValueRange independencies,
166
- bool closedUB = false );
194
+ presburger::BoundType type, const Variable &var,
195
+ ValueRange independencies, bool closedUB = false );
167
196
168
- // / Compute a constant bound for the given affine map, where dims and symbols
169
- // / are bound to the given operands. The affine map must have exactly one
170
- // / result.
197
+ // / Compute a constant bound for the given variable.
171
198
// /
172
199
// / This function traverses the backward slice of the given operands in a
173
200
// / worklist-driven manner until `stopCondition` evaluates to "true". The
@@ -182,16 +209,9 @@ class ValueBoundsConstraintSet
182
209
// / By default, lower/equal bounds are closed and upper bounds are open. If
183
210
// / `closedUB` is set to "true", upper bounds are also closed.
184
211
static FailureOr<int64_t >
185
- computeConstantBound (presburger::BoundType type, Value value,
186
- std::optional<int64_t > dim = std::nullopt,
212
+ computeConstantBound (presburger::BoundType type, const Variable &var,
187
213
StopConditionFn stopCondition = nullptr ,
188
214
bool closedUB = false );
189
- static FailureOr<int64_t > computeConstantBound (
190
- presburger::BoundType type, AffineMap map, ValueDimList mapOperands,
191
- StopConditionFn stopCondition = nullptr , bool closedUB = false );
192
- static FailureOr<int64_t > computeConstantBound (
193
- presburger::BoundType type, AffineMap map, ArrayRef<Value> mapOperands,
194
- StopConditionFn stopCondition = nullptr , bool closedUB = false );
195
215
196
216
// / Compute a constant delta between the given two values. Return "failure"
197
217
// / if a constant delta could not be determined.
@@ -221,9 +241,7 @@ class ValueBoundsConstraintSet
221
241
// / proven. This could be because the specified relation does in fact not hold
222
242
// / or because there is not enough information in the constraint set. In other
223
243
// / words, if we do not know for sure, this function returns "false".
224
- bool populateAndCompare (OpFoldResult lhs, std::optional<int64_t > lhsDim,
225
- ComparisonOperator cmp, OpFoldResult rhs,
226
- std::optional<int64_t > rhsDim);
244
+ bool populateAndCompare (Variable lhs, ComparisonOperator cmp, Variable rhs);
227
245
228
246
// / Return "true" if "lhs cmp rhs" was proven to hold. Return "false" if the
229
247
// / specified relation could not be proven. This could be because the
@@ -233,24 +251,11 @@ class ValueBoundsConstraintSet
233
251
// /
234
252
// / This function keeps traversing the backward slice of lhs/rhs until could
235
253
// / prove the relation or until it ran out of IR.
236
- static bool compare (OpFoldResult lhs, std::optional<int64_t > lhsDim,
237
- ComparisonOperator cmp, OpFoldResult rhs,
238
- std::optional<int64_t > rhsDim);
239
- static bool compare (AffineMap lhs, ValueDimList lhsOperands,
240
- ComparisonOperator cmp, AffineMap rhs,
241
- ValueDimList rhsOperands);
242
- static bool compare (AffineMap lhs, ArrayRef<Value> lhsOperands,
243
- ComparisonOperator cmp, AffineMap rhs,
244
- ArrayRef<Value> rhsOperands);
245
-
246
- // / Compute whether the given values/dimensions are equal. Return "failure" if
254
+ static bool compare (Variable lhs, ComparisonOperator cmp, Variable rhs);
255
+
256
+ // / Compute whether the given variables are equal. Return "failure" if
247
257
// / equality could not be determined.
248
- // /
249
- // / `dim1`/`dim2` must be `nullopt` if and only if `value1`/`value2` are
250
- // / index-typed.
251
- static FailureOr<bool > areEqual (OpFoldResult value1, OpFoldResult value2,
252
- std::optional<int64_t > dim1 = std::nullopt,
253
- std::optional<int64_t > dim2 = std::nullopt);
258
+ static FailureOr<bool > areEqual (Variable var1, Variable var2);
254
259
255
260
// / Return "true" if the given slices are guaranteed to be overlapping.
256
261
// / Return "false" if the given slices are guaranteed to be non-overlapping.
@@ -317,9 +322,6 @@ class ValueBoundsConstraintSet
317
322
// /
318
323
// / This function does not analyze any IR and does not populate any additional
319
324
// / constraints.
320
- bool compareValueDims (OpFoldResult lhs, std::optional<int64_t > lhsDim,
321
- ComparisonOperator cmp, OpFoldResult rhs,
322
- std::optional<int64_t > rhsDim);
323
325
bool comparePos (int64_t lhsPos, ComparisonOperator cmp, int64_t rhsPos);
324
326
325
327
// / Given an affine map with a single result (and map operands), add a new
@@ -374,13 +376,16 @@ class ValueBoundsConstraintSet
374
376
// / constraint system. Return the position of the new column. Any operands
375
377
// / that were not analyzed yet are put on the worklist.
376
378
int64_t insert (AffineMap map, ValueDimList operands, bool isSymbol = true );
379
+ int64_t insert (const Variable &var, bool isSymbol = true );
377
380
378
381
// / Project out the given column in the constraint set.
379
382
void projectOut (int64_t pos);
380
383
381
384
// / Project out all columns for which the condition holds.
382
385
void projectOut (function_ref<bool (ValueDim)> condition);
383
386
387
+ void projectOutAnonymous (std::optional<int64_t > except = std::nullopt);
388
+
384
389
// / Mapping of columns to values/shape dimensions.
385
390
SmallVector<std::optional<ValueDim>> positionToValueDim;
386
391
// / Reverse mapping of values/shape dimensions to columns.
0 commit comments