Skip to content

Commit 26ef79a

Browse files
[Util] Fix AssumeIntOp::inferResultRanges bug (#19195)
I noticed that the util-optimize-int-arithmetic pass, sometimes was incorrectly optimizing away operations. I tracked the issue down to the fact that `AssumeIntOp::inferResultRanges` doesn't always call `setResultRange`. Looking at the docs for `InferIntRangeInterface` in LLVM, it suggests that `setResultRange` must be called for each result, and looking further it's clear that some of the `arith` op folders rely on the fact that the range is always set (eg. `arith.select`). This PR updates `AssumeIntOp::inferResultRanges` to call `setResultRange` even when umin or umax are not set. I added a test case that used to be optimized to an incorrect constant, and now is not optimized out. Signed-off-by: James Bartlett <[email protected]>
1 parent 47432c6 commit 26ef79a

File tree

2 files changed

+19
-5
lines changed

2 files changed

+19
-5
lines changed

compiler/src/iree/compiler/Dialect/Util/IR/UtilOps.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1217,11 +1217,14 @@ void AssumeIntOp::inferResultRanges(ArrayRef<ConstantIntRanges> argRanges,
12171217
else
12181218
continue;
12191219
auto [umin, umax] = getUnionedUnsignedRange(index);
1220-
if (umin && umax) {
1221-
APInt uminAp(bitWidth, *umin);
1222-
APInt umaxAp(bitWidth, *umax);
1223-
setResultRange(result, ConstantIntRanges::fromUnsigned(uminAp, umaxAp));
1224-
}
1220+
auto uminAp = APInt::getMinValue(bitWidth);
1221+
auto umaxAp = APInt::getMaxValue(bitWidth);
1222+
if (umin)
1223+
uminAp = APInt(bitWidth, *umin);
1224+
if (umax)
1225+
umaxAp = APInt(bitWidth, *umax);
1226+
1227+
setResultRange(result, ConstantIntRanges::fromUnsigned(uminAp, umaxAp));
12251228
}
12261229
}
12271230

compiler/src/iree/compiler/Dialect/Util/Transforms/test/optimize_int_arithmetic.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,3 +523,14 @@ util.func @hal_buffer_view_rank_min_max(%bv : !hal.buffer_view) -> (i1, i1, i1)
523523
// CHECK: util.return %[[FALSE]], %[[TRUE]], %[[FALSE]]
524524
util.return %1, %2, %3 : i1, i1, i1
525525
}
526+
527+
// -----
528+
529+
util.func @assume_int_with_single_bound(%bool: i1, %ind: index) -> index {
530+
%c1 = arith.constant 1 : index
531+
%0 = util.assume.int %ind<umin = 1> : index
532+
%1 = arith.select %bool, %0, %c1 : index
533+
// select should not be optimized away.
534+
// CHECK: arith.select
535+
util.return %1 : index
536+
}

0 commit comments

Comments
 (0)