Skip to content

Commit b78883f

Browse files
authored
[mlir][intrange] Fix inference of zero-trip loop bound (#96429)
When lower bound and exclusive upper bound of a loop are the same, and the zero-trip loop is not canonicalized away before the analysis, this leads to a meaningless range for the induction variable being inferred. This patch adds a check to make sure that the inferred range for the IV is meaningful before updating the analysis state. Fix #94423
1 parent a9ac319 commit b78883f

File tree

2 files changed

+26
-3
lines changed

2 files changed

+26
-3
lines changed

mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,14 @@ void IntegerRangeAnalysis::visitNonControlFlowArguments(
195195
max -= 1;
196196
}
197197

198-
IntegerValueRangeLattice *ivEntry = getLatticeElement(*iv);
199-
auto ivRange = ConstantIntRanges::fromSigned(min, max);
200-
propagateIfChanged(ivEntry, ivEntry->join(IntegerValueRange{ivRange}));
198+
// If we infer the lower bound to be larger than the upper bound, the
199+
// resulting range is meaningless and should not be used in further
200+
// inferences.
201+
if (max.sge(min)) {
202+
IntegerValueRangeLattice *ivEntry = getLatticeElement(*iv);
203+
auto ivRange = ConstantIntRanges::fromSigned(min, max);
204+
propagateIfChanged(ivEntry, ivEntry->join(IntegerValueRange{ivRange}));
205+
}
201206
return;
202207
}
203208

mlir/test/Dialect/Arith/int-range-interface.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -918,3 +918,21 @@ func.func @test_cmpf_propagates(%a: f32, %b: f32) -> index {
918918
func.return %2 : index
919919
}
920920

921+
// CHECK-LABEL: func @zero_trip_loop
922+
func.func @zero_trip_loop() {
923+
%idx1 = arith.constant 1 : index
924+
scf.for %arg0 = %idx1 to %idx1 step %idx1 {
925+
%138 = index.floordivs %arg0, %arg0
926+
}
927+
return
928+
}
929+
930+
// CHECK-LABEL: func @zero_trip_loop2
931+
func.func @zero_trip_loop2() {
932+
%idx1 = arith.constant 1 : index
933+
%idxm1 = arith.constant -1 : index
934+
scf.for %arg0 = %idx1 to %idx1 step %idxm1 {
935+
%138 = index.floordivs %arg0, %arg0
936+
}
937+
return
938+
}

0 commit comments

Comments
 (0)