@@ -67,8 +67,11 @@ 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) {
73
+ assert (stopCondition && " expected non-null stop condition" );
74
+ }
72
75
73
76
char ValueBoundsConstraintSet::ID = 0 ;
74
77
@@ -230,7 +233,8 @@ static Operation *getOwnerOfValue(Value value) {
230
233
return value.getDefiningOp ();
231
234
}
232
235
233
- void ValueBoundsConstraintSet::processWorklist (StopConditionFn stopCondition) {
236
+ void ValueBoundsConstraintSet::processWorklist () {
237
+ LLVM_DEBUG (llvm::dbgs () << " Processing value bounds worklist...\n " );
234
238
while (!worklist.empty ()) {
235
239
int64_t pos = worklist.front ();
236
240
worklist.pop ();
@@ -251,13 +255,19 @@ void ValueBoundsConstraintSet::processWorklist(StopConditionFn stopCondition) {
251
255
252
256
// Do not process any further if the stop condition is met.
253
257
auto maybeDim = dim == kIndexValue ? std::nullopt : std::make_optional (dim);
254
- if (stopCondition (value, maybeDim))
258
+ if (stopCondition (value, maybeDim, *this )) {
259
+ LLVM_DEBUG (llvm::dbgs () << " Stop condition met for: " << value
260
+ << " (dim: " << maybeDim << " )\n " );
255
261
continue ;
262
+ }
256
263
257
264
// Query `ValueBoundsOpInterface` for constraints. New items may be added to
258
265
// the worklist.
259
266
auto valueBoundsOp =
260
267
dyn_cast<ValueBoundsOpInterface>(getOwnerOfValue (value));
268
+ LLVM_DEBUG (llvm::dbgs ()
269
+ << " Query value bounds for: " << value
270
+ << " (owner: " << getOwnerOfValue (value)->getName () << " )\n " );
261
271
if (valueBoundsOp) {
262
272
if (dim == kIndexValue ) {
263
273
valueBoundsOp.populateBoundsForIndexValue (value, *this );
@@ -266,6 +276,7 @@ void ValueBoundsConstraintSet::processWorklist(StopConditionFn stopCondition) {
266
276
}
267
277
continue ;
268
278
}
279
+ LLVM_DEBUG (llvm::dbgs () << " --> ValueBoundsOpInterface not implemented\n " );
269
280
270
281
// If the op does not implement `ValueBoundsOpInterface`, check if it
271
282
// implements the `DestinationStyleOpInterface`. OpResults of such ops are
@@ -315,8 +326,6 @@ LogicalResult ValueBoundsConstraintSet::computeBound(
315
326
bool closedUB) {
316
327
#ifndef NDEBUG
317
328
assertValidValueDim (value, dim);
318
- assert (!stopCondition (value, dim) &&
319
- " stop condition should not be satisfied for starting point" );
320
329
#endif // NDEBUG
321
330
322
331
int64_t ubAdjustment = closedUB ? 0 : 1 ;
@@ -326,9 +335,11 @@ LogicalResult ValueBoundsConstraintSet::computeBound(
326
335
// Process the backward slice of `value` (i.e., reverse use-def chain) until
327
336
// `stopCondition` is met.
328
337
ValueDim valueDim = std::make_pair (value, dim.value_or (kIndexValue ));
329
- ValueBoundsConstraintSet cstr (value.getContext ());
338
+ ValueBoundsConstraintSet cstr (value.getContext (), stopCondition);
339
+ assert (!stopCondition (value, dim, cstr) &&
340
+ " stop condition should not be satisfied for starting point" );
330
341
int64_t pos = cstr.insert (value, dim, /* isSymbol=*/ false );
331
- cstr.processWorklist (stopCondition );
342
+ cstr.processWorklist ();
332
343
333
344
// Project out all variables (apart from `valueDim`) that do not match the
334
345
// stop condition.
@@ -338,7 +349,7 @@ LogicalResult ValueBoundsConstraintSet::computeBound(
338
349
return false ;
339
350
auto maybeDim =
340
351
p.second == kIndexValue ? std::nullopt : std::make_optional (p.second );
341
- return !stopCondition (p.first , maybeDim);
352
+ return !stopCondition (p.first , maybeDim, cstr );
342
353
});
343
354
344
355
// Compute lower and upper bounds for `valueDim`.
@@ -444,7 +455,7 @@ LogicalResult ValueBoundsConstraintSet::computeDependentBound(
444
455
bool closedUB) {
445
456
return computeBound (
446
457
resultMap, mapOperands, type, value, dim,
447
- [&](Value v, std::optional<int64_t > d) {
458
+ [&](Value v, std::optional<int64_t > d, ValueBoundsConstraintSet &cstr ) {
448
459
return llvm::is_contained (dependencies, std::make_pair (v, d));
449
460
},
450
461
closedUB);
@@ -480,7 +491,9 @@ LogicalResult ValueBoundsConstraintSet::computeIndependentBound(
480
491
// Reify bounds in terms of any independent values.
481
492
return computeBound (
482
493
resultMap, mapOperands, type, value, dim,
483
- [&](Value v, std::optional<int64_t > d) { return isIndependent (v); },
494
+ [&](Value v, std::optional<int64_t > d, ValueBoundsConstraintSet &cstr) {
495
+ return isIndependent (v);
496
+ },
484
497
closedUB);
485
498
}
486
499
@@ -513,43 +526,42 @@ FailureOr<int64_t> ValueBoundsConstraintSet::computeConstantBound(
513
526
presburger::BoundType type, AffineMap map, ValueDimList operands,
514
527
StopConditionFn stopCondition, bool closedUB) {
515
528
assert (map.getNumResults () == 1 && " expected affine map with one result" );
516
- ValueBoundsConstraintSet cstr (map.getContext ());
517
529
518
- int64_t pos = 0 ;
519
- if (stopCondition) {
520
- cstr.populateConstraintsSet (map, operands, stopCondition, &pos);
521
- } else {
522
- // No stop condition specified: Keep adding constraints until a bound could
523
- // be computed.
524
- cstr.populateConstraintsSet (
525
- map, operands,
526
- [&](Value v, std::optional<int64_t > dim) {
527
- return cstr.cstr .getConstantBound64 (type, pos).has_value ();
528
- },
529
- &pos);
530
- }
530
+ // Default stop condition if none was specified: Keep adding constraints until
531
+ // a bound could be computed.
532
+ int64_t pos;
533
+ auto defaultStopCondition = [&](Value v, std::optional<int64_t > dim,
534
+ ValueBoundsConstraintSet &cstr) {
535
+ return cstr.cstr .getConstantBound64 (type, pos).has_value ();
536
+ };
537
+
538
+ ValueBoundsConstraintSet cstr (
539
+ map.getContext (), stopCondition ? stopCondition : defaultStopCondition);
540
+ cstr.populateConstraintsSet (map, operands, &pos);
541
+
531
542
// Compute constant bound for `valueDim`.
532
543
int64_t ubAdjustment = closedUB ? 0 : 1 ;
533
544
if (auto bound = cstr.cstr .getConstantBound64 (type, pos))
534
545
return type == BoundType::UB ? *bound + ubAdjustment : *bound;
535
546
return failure ();
536
547
}
537
548
538
- int64_t ValueBoundsConstraintSet::populateConstraintsSet (
539
- Value value, std::optional<int64_t > dim, StopConditionFn stopCondition) {
549
+ int64_t
550
+ ValueBoundsConstraintSet::populateConstraintsSet (Value value,
551
+ std::optional<int64_t > dim) {
540
552
#ifndef NDEBUG
541
553
assertValidValueDim (value, dim);
542
554
#endif // NDEBUG
543
555
544
556
AffineMap map =
545
557
AffineMap::get (/* dimCount=*/ 1 , /* symbolCount=*/ 0 ,
546
558
Builder (value.getContext ()).getAffineDimExpr (0 ));
547
- return populateConstraintsSet (map, {{value, dim}}, stopCondition );
559
+ return populateConstraintsSet (map, {{value, dim}});
548
560
}
549
561
550
- int64_t ValueBoundsConstraintSet::populateConstraintsSet (
551
- AffineMap map, ValueDimList operands, StopConditionFn stopCondition ,
552
- int64_t *posOut) {
562
+ int64_t ValueBoundsConstraintSet::populateConstraintsSet (AffineMap map,
563
+ ValueDimList operands ,
564
+ int64_t *posOut) {
553
565
assert (map.getNumResults () == 1 && " expected affine map with one result" );
554
566
int64_t pos = insert (/* isSymbol=*/ false );
555
567
if (posOut)
@@ -570,13 +582,7 @@ int64_t ValueBoundsConstraintSet::populateConstraintsSet(
570
582
571
583
// Process the backward slice of `operands` (i.e., reverse use-def chain)
572
584
// until `stopCondition` is met.
573
- if (stopCondition) {
574
- processWorklist (stopCondition);
575
- } else {
576
- // No stop condition specified: Keep adding constraints until the worklist
577
- // is empty.
578
- processWorklist ([](Value v, std::optional<int64_t > dim) { return false ; });
579
- }
585
+ processWorklist ();
580
586
581
587
return pos;
582
588
}
0 commit comments