@@ -67,8 +67,9 @@ static std::optional<int64_t> getConstantIntValue(OpFoldResult ofr) {
67
67
return std::nullopt;
68
68
}
69
69
70
- ValueBoundsConstraintSet::ValueBoundsConstraintSet (MLIRContext *ctx)
71
- : builder(ctx) {}
70
+ ValueBoundsConstraintSet::ValueBoundsConstraintSet (
71
+ MLIRContext *ctx, StopConditionFn stopCondition)
72
+ : builder(ctx), stopCondition(stopCondition) {}
72
73
73
74
#ifndef NDEBUG
74
75
static void assertValidValueDim (Value value, std::optional<int64_t > dim) {
@@ -228,7 +229,8 @@ static Operation *getOwnerOfValue(Value value) {
228
229
return value.getDefiningOp ();
229
230
}
230
231
231
- void ValueBoundsConstraintSet::processWorklist (StopConditionFn stopCondition) {
232
+ void ValueBoundsConstraintSet::processWorklist () {
233
+ LLVM_DEBUG (llvm::dbgs () << " Processing value bounds worklist...\n " );
232
234
while (!worklist.empty ()) {
233
235
int64_t pos = worklist.front ();
234
236
worklist.pop ();
@@ -249,13 +251,19 @@ void ValueBoundsConstraintSet::processWorklist(StopConditionFn stopCondition) {
249
251
250
252
// Do not process any further if the stop condition is met.
251
253
auto maybeDim = dim == kIndexValue ? std::nullopt : std::make_optional (dim);
252
- if (stopCondition (value, maybeDim))
254
+ if (stopCondition (value, maybeDim, *this )) {
255
+ LLVM_DEBUG (llvm::dbgs () << " Stop condition met for: " << value
256
+ << " (dim: " << maybeDim << " )\n " );
253
257
continue ;
258
+ }
254
259
255
260
// Query `ValueBoundsOpInterface` for constraints. New items may be added to
256
261
// the worklist.
257
262
auto valueBoundsOp =
258
263
dyn_cast<ValueBoundsOpInterface>(getOwnerOfValue (value));
264
+ LLVM_DEBUG (llvm::dbgs ()
265
+ << " Query value bounds for: " << value
266
+ << " (owner: " << getOwnerOfValue (value)->getName () << " )\n " );
259
267
if (valueBoundsOp) {
260
268
if (dim == kIndexValue ) {
261
269
valueBoundsOp.populateBoundsForIndexValue (value, *this );
@@ -264,6 +272,7 @@ void ValueBoundsConstraintSet::processWorklist(StopConditionFn stopCondition) {
264
272
}
265
273
continue ;
266
274
}
275
+ LLVM_DEBUG (llvm::dbgs () << " --> ValueBoundsOpInterface not implemented\n " );
267
276
268
277
// If the op does not implement `ValueBoundsOpInterface`, check if it
269
278
// implements the `DestinationStyleOpInterface`. OpResults of such ops are
@@ -313,8 +322,6 @@ LogicalResult ValueBoundsConstraintSet::computeBound(
313
322
bool closedUB) {
314
323
#ifndef NDEBUG
315
324
assertValidValueDim (value, dim);
316
- assert (!stopCondition (value, dim) &&
317
- " stop condition should not be satisfied for starting point" );
318
325
#endif // NDEBUG
319
326
320
327
int64_t ubAdjustment = closedUB ? 0 : 1 ;
@@ -324,9 +331,11 @@ LogicalResult ValueBoundsConstraintSet::computeBound(
324
331
// Process the backward slice of `value` (i.e., reverse use-def chain) until
325
332
// `stopCondition` is met.
326
333
ValueDim valueDim = std::make_pair (value, dim.value_or (kIndexValue ));
327
- ValueBoundsConstraintSet cstr (value.getContext ());
334
+ ValueBoundsConstraintSet cstr (value.getContext (), stopCondition);
335
+ assert (!stopCondition (value, dim, cstr) &&
336
+ " stop condition should not be satisfied for starting point" );
328
337
int64_t pos = cstr.insert (value, dim, /* isSymbol=*/ false );
329
- cstr.processWorklist (stopCondition );
338
+ cstr.processWorklist ();
330
339
331
340
// Project out all variables (apart from `valueDim`) that do not match the
332
341
// stop condition.
@@ -336,7 +345,7 @@ LogicalResult ValueBoundsConstraintSet::computeBound(
336
345
return false ;
337
346
auto maybeDim =
338
347
p.second == kIndexValue ? std::nullopt : std::make_optional (p.second );
339
- return !stopCondition (p.first , maybeDim);
348
+ return !stopCondition (p.first , maybeDim, cstr );
340
349
});
341
350
342
351
// Compute lower and upper bounds for `valueDim`.
@@ -442,7 +451,7 @@ LogicalResult ValueBoundsConstraintSet::computeDependentBound(
442
451
bool closedUB) {
443
452
return computeBound (
444
453
resultMap, mapOperands, type, value, dim,
445
- [&](Value v, std::optional<int64_t > d) {
454
+ [&](Value v, std::optional<int64_t > d, ValueBoundsConstraintSet &cstr ) {
446
455
return llvm::is_contained (dependencies, std::make_pair (v, d));
447
456
},
448
457
closedUB);
@@ -478,7 +487,9 @@ LogicalResult ValueBoundsConstraintSet::computeIndependentBound(
478
487
// Reify bounds in terms of any independent values.
479
488
return computeBound (
480
489
resultMap, mapOperands, type, value, dim,
481
- [&](Value v, std::optional<int64_t > d) { return isIndependent (v); },
490
+ [&](Value v, std::optional<int64_t > d, ValueBoundsConstraintSet &cstr) {
491
+ return isIndependent (v);
492
+ },
482
493
closedUB);
483
494
}
484
495
@@ -500,8 +511,18 @@ FailureOr<int64_t> ValueBoundsConstraintSet::computeConstantBound(
500
511
presburger::BoundType type, AffineMap map, ValueDimList operands,
501
512
StopConditionFn stopCondition, bool closedUB) {
502
513
assert (map.getNumResults () == 1 && " expected affine map with one result" );
503
- ValueBoundsConstraintSet cstr (map.getContext ());
504
- int64_t pos = cstr.insert (/* isSymbol=*/ false );
514
+
515
+ // Default stop condition if none was specified: Keep adding constraints until
516
+ // a bound could be computed.
517
+ int64_t pos;
518
+ auto defaultStopCondition = [&](Value v, std::optional<int64_t > dim,
519
+ ValueBoundsConstraintSet &cstr) {
520
+ return cstr.cstr .getConstantBound64 (type, pos).has_value ();
521
+ };
522
+
523
+ ValueBoundsConstraintSet cstr (
524
+ map.getContext (), stopCondition ? stopCondition : defaultStopCondition);
525
+ pos = cstr.insert (/* isSymbol=*/ false );
505
526
506
527
// Add map and operands to the constraint set. Dimensions are converted to
507
528
// symbols. All operands are added to the worklist.
@@ -517,17 +538,8 @@ FailureOr<int64_t> ValueBoundsConstraintSet::computeConstantBound(
517
538
map.getResult (0 ).replaceDimsAndSymbols (dimReplacements, symReplacements));
518
539
519
540
// Process the backward slice of `operands` (i.e., reverse use-def chain)
520
- // until `stopCondition` is met.
521
- if (stopCondition) {
522
- cstr.processWorklist (stopCondition);
523
- } else {
524
- // No stop condition specified: Keep adding constraints until a bound could
525
- // be computed.
526
- cstr.processWorklist (
527
- /* stopCondition=*/ [&](Value v, std::optional<int64_t > dim) {
528
- return cstr.cstr .getConstantBound64 (type, pos).has_value ();
529
- });
530
- }
541
+ // until the stop condition is met.
542
+ cstr.processWorklist ();
531
543
532
544
// Compute constant bound for `valueDim`.
533
545
int64_t ubAdjustment = closedUB ? 0 : 1 ;
0 commit comments