Skip to content

Commit 13c648f

Browse files
[MLIR][IntegerRangeAnalysis] Avoid crash reached when loop bound is uninitialized (#74832)
If the loop bound is not initialized, the analysis crashed, as it only checked for nullity. Also checking for initialization fixes the issue. Signed-off-by: Victor Perez <[email protected]> Co-authored-by: Tsang, Whitney <[email protected]>
1 parent 75193b1 commit 13c648f

File tree

3 files changed

+29
-1
lines changed

3 files changed

+29
-1
lines changed

mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -180,7 +180,7 @@ void IntegerRangeAnalysis::visitNonControlFlowArguments(
180180
} else if (auto value = llvm::dyn_cast_if_present<Value>(*loopBound)) {
181181
const IntegerValueRangeLattice *lattice =
182182
getLatticeElementFor(op, value);
183-
if (lattice != nullptr)
183+
if (lattice != nullptr && !lattice->getValue().isUninitialized())
184184
return getUpper ? lattice->getValue().getValue().smax()
185185
: lattice->getValue().getValue().smin();
186186
}

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

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,3 +730,29 @@ func.func @extui_uses_unsigned(%arg0 : i32) -> i1 {
730730
%4 = arith.andi %2, %3 : i1
731731
func.return %4 : i1
732732
}
733+
734+
/// Catch a bug that caused a crash in getLoopBoundFromFold when
735+
/// SparseConstantPropagation is loaded in the solver.
736+
737+
// CHECK-LABEL: func.func @caller(
738+
// CHECK-SAME: %[[VAL_0:.*]]: memref<?xindex, 4>) {
739+
// CHECK: call @callee(%[[VAL_0]]) : (memref<?xindex, 4>) -> ()
740+
// CHECK: return
741+
// CHECK: }
742+
func.func @caller(%arg0: memref<?xindex, 4>) {
743+
call @callee(%arg0) : (memref<?xindex, 4>) -> ()
744+
return
745+
}
746+
747+
// CHECK-LABEL: func.func private @callee(
748+
// CHECK-SAME: %[[VAL_0:.*]]: memref<?xindex, 4>) {
749+
// CHECK: return
750+
// CHECK: }
751+
func.func private @callee(%arg0: memref<?xindex, 4>) {
752+
%c1 = arith.constant 1 : index
753+
%c0 = arith.constant 0 : index
754+
%0 = affine.load %arg0[0] : memref<?xindex, 4>
755+
scf.for %arg1 = %c0 to %0 step %c1 {
756+
}
757+
return
758+
}

mlir/test/lib/Transforms/TestIntRangeInference.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
// functionality has been integrated into SCCP.
1010
//===----------------------------------------------------------------------===//
1111

12+
#include "mlir/Analysis/DataFlow/ConstantPropagationAnalysis.h"
1213
#include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h"
1314
#include "mlir/Analysis/DataFlow/IntegerRangeAnalysis.h"
1415
#include "mlir/Interfaces/SideEffectInterfaces.h"
@@ -107,6 +108,7 @@ struct TestIntRangeInference
107108
Operation *op = getOperation();
108109
DataFlowSolver solver;
109110
solver.load<DeadCodeAnalysis>();
111+
solver.load<SparseConstantPropagation>();
110112
solver.load<IntegerRangeAnalysis>();
111113
if (failed(solver.initializeAndRun(op)))
112114
return signalPassFailure();

0 commit comments

Comments
 (0)