Skip to content

Commit c36c3b7

Browse files
committed
[mlir][scf]: Add value bound for the computed upper bound of for loop
Add additional bound for the induction variable of the `scf.for` such that: `%iv <= %lower_bound + (%trip_count - 1) * step`
1 parent 70906f0 commit c36c3b7

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

mlir/lib/Dialect/SCF/IR/ValueBoundsOpInterfaceImpl.cpp

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,16 @@ namespace {
2020
struct ForOpInterface
2121
: public ValueBoundsOpInterface::ExternalModel<ForOpInterface, ForOp> {
2222

23+
static AffineExpr getTripCountExpr(scf::ForOp forOp,
24+
ValueBoundsConstraintSet &cstr) {
25+
AffineExpr lbExpr = cstr.getExpr(forOp.getLowerBound());
26+
AffineExpr ubExpr = cstr.getExpr(forOp.getUpperBound());
27+
AffineExpr stepExpr = cstr.getExpr(forOp.getStep());
28+
AffineExpr tripCountExpr =
29+
AffineExpr(ubExpr - lbExpr).ceilDiv(stepExpr); // (ub - lb) / step
30+
return tripCountExpr;
31+
}
32+
2333
/// Populate bounds of values/dimensions for iter_args/OpResults. If the
2434
/// value/dimension size does not change in an iteration, we can deduce that
2535
/// it the same as the initial value/dimension.
@@ -77,11 +87,7 @@ struct ForOpInterface
7787
// `value` is result of `forOp`, we can prove that:
7888
// %result == %init_arg + trip_count * (%yielded_value - %iter_arg).
7989
// Where trip_count is (ub - lb) / step.
80-
AffineExpr lbExpr = cstr.getExpr(forOp.getLowerBound());
81-
AffineExpr ubExpr = cstr.getExpr(forOp.getUpperBound());
82-
AffineExpr stepExpr = cstr.getExpr(forOp.getStep());
83-
AffineExpr tripCountExpr =
84-
AffineExpr(ubExpr - lbExpr).ceilDiv(stepExpr); // (ub - lb) / step
90+
AffineExpr tripCountExpr = getTripCountExpr(forOp, cstr);
8591
AffineExpr oneIterAdvanceExpr =
8692
cstr.getExpr(yieldedValue) - cstr.getExpr(iterArg);
8793
cstr.bound(value) ==
@@ -93,9 +99,14 @@ struct ForOpInterface
9399
auto forOp = cast<ForOp>(op);
94100

95101
if (value == forOp.getInductionVar()) {
96-
// TODO: Take into account step size.
97102
cstr.bound(value) >= forOp.getLowerBound();
98103
cstr.bound(value) < forOp.getUpperBound();
104+
AffineExpr tripCountMinusOne =
105+
getTripCountExpr(forOp, cstr) - cstr.getExpr(1);
106+
AffineExpr computedUpperBound =
107+
cstr.getExpr(forOp.getLowerBound()) +
108+
AffineExpr(tripCountMinusOne * cstr.getExpr(forOp.getStep()));
109+
cstr.bound(value) <= computedUpperBound;
99110
return;
100111
}
101112

mlir/test/Dialect/SCF/value-bounds-op-interface-impl.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,27 @@ func.func @compare_scf_for(%a: index, %b: index, %c: index) {
270270

271271
// -----
272272

273+
func.func @scf_for_induction_var_upper_bound(%i : index) {
274+
%c0 = arith.constant 0 : index
275+
%c2 = arith.constant 2 : index
276+
%c3 = arith.constant 3 : index
277+
%c4 = arith.constant 4 : index
278+
%c5 = arith.constant 5 : index
279+
%c8 = arith.constant 8 : index
280+
%c10 = arith.constant 10 : index
281+
scf.for %iv = %c0 to %c10 step %c4 {
282+
// expected-remark @below{{true}}
283+
"test.compare"(%iv, %c8) {cmp = "LE"} : (index, index) -> ()
284+
}
285+
scf.for %iv = %c2 to %c8 step %c3 {
286+
// expected-remark @below{{true}}
287+
"test.compare"(%iv, %c5) {cmp = "LE"} : (index, index) -> ()
288+
}
289+
return
290+
}
291+
292+
// -----
293+
273294
func.func @scf_for_result_infer() {
274295
%c0 = arith.constant 0 : index
275296
%c1 = arith.constant 1 : index

0 commit comments

Comments
 (0)