[mlir][IntRangeAnalysis] Handle unstructured loop arguments correctly by krzysz00 · Pull Request #119459 · llvm/llvm-project (original) (raw)

@llvm/pr-subscribers-mlir-arith

@llvm/pr-subscribers-mlir

Author: Krzysztof Drewniak (krzysz00)

Changes

The integer range analysis currently has a bug where, because of how it interacts with dead code analysis, it will sometimes declare code dead that isn't dead, becaues it hasn't seen the edge that loops an incremented value back to itself yet.

This commit fixes the issue by overriding the join method on lattice values in order to detect these back-edges on non-entry blocks and then snapping the passed-around value to its maximum possible range, just like we do for loop-varying values in region control flow.

Fixes #119045


Full diff: https://github.com/llvm/llvm-project/pull/119459.diff

3 Files Affected:

diff --git a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h index f99eae379596b6..464a47355b4207 100644 --- a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h +++ b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h @@ -31,6 +31,16 @@ class IntegerValueRangeLattice : public Lattice { public: using Lattice::Lattice;

+/// Return true if block is a non-entry block with a predecessor that's +/// defined after the block. This allows us to detect loop-varying values +/// in unstructured control flow. +static bool isLoopLikeBlock(Block *block) {

@@ -206,6 +250,8 @@ void IntegerRangeAnalysis::visitNonControlFlowArguments( if (max.sge(min)) { IntegerValueRangeLattice *ivEntry = getLatticeElement(*iv); auto ivRange = ConstantIntRanges::fromSigned(min, max);