[Mlir-commits] [mlir] [IntegerRangeAnalysis] remove constraint on integer-typed results (PR #72007)

Jeremy Kun llvmlistbot at llvm.org
Fri Nov 10 16:50:23 PST 2023


https://github.com/j2kun created https://github.com/llvm/llvm-project/pull/72007

I have an out-of-tree situation in which I'd like to run this analysis, but with custom types and integer ranges attached to types that are not classical integers. No upstream tests depend on this condition, and so relaxing it seems like a natural way to make it more useful out of tree.

I'm open to making any additional changes if the reviewer foresees some unexpected problems as a result of this change. E.g., if having this analysis available in an IR with mixed arith/non-arith ops would cause a pass like `IntRangeOptimizationsPass` to misbehave, but I imagine it would not because the patterns match against specific arith ops.

I can add a regression test if there's a strong desire, but I feel the chance of this regressing is relatively low compared to the effort to write the test.

>From ee2a4c8caa279cdcee625080de3bca8bb1c1c7f9 Mon Sep 17 00:00:00 2001
From: Jeremy Kun <jkun at google.com>
Date: Fri, 10 Nov 2023 16:36:08 -0800
Subject: [PATCH] remove constraint on integer-typed results

---
 .../Analysis/DataFlow/IntegerRangeAnalysis.h     |  8 +++++++-
 .../Analysis/DataFlow/IntegerRangeAnalysis.cpp   | 16 ----------------
 2 files changed, 7 insertions(+), 17 deletions(-)

diff --git a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
index 99f620ed26a1b4b..8bd7cf880c6afb5 100644
--- a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
+++ b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
@@ -8,7 +8,10 @@
 //
 // This file declares the dataflow analysis class for integer range inference
 // so that it can be used in transformations over the `arith` dialect such as
-// branch elimination or signed->unsigned rewriting
+// branch elimination or signed->unsigned rewriting.
+//
+// One can also implement InferIntRangeInterface on ops in custom dialects,
+// and then use this analysis to propagate ranges with custom semantics.
 //
 //===----------------------------------------------------------------------===//
 
@@ -81,6 +84,9 @@ class IntegerValueRangeLattice : public Lattice<IntegerValueRange> {
 /// Integer range analysis determines the integer value range of SSA values
 /// using operations that define `InferIntRangeInterface` and also sets the
 /// range of iteration indices of loops with known bounds.
+///
+/// This analysis depends on DeadCodeAnalysis, and will be a silent no-op
+/// if DeadCodeAnalysis is not loaded in the same solver context.
 class IntegerRangeAnalysis
     : public SparseForwardDataFlowAnalysis<IntegerValueRangeLattice> {
 public:
diff --git a/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp b/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp
index a43263bc11113b8..39b3a0996396ffb 100644
--- a/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp
+++ b/mlir/lib/Analysis/DataFlow/IntegerRangeAnalysis.cpp
@@ -79,22 +79,6 @@ void IntegerRangeAnalysis::visitOperation(
     return;
   }
 
-  // Ignore non-integer outputs - return early if the op has no scalar
-  // integer results
-  bool hasIntegerResult = false;
-  for (auto it : llvm::zip(results, op->getResults())) {
-    Value value = std::get<1>(it);
-    if (value.getType().isIntOrIndex()) {
-      hasIntegerResult = true;
-    } else {
-      IntegerValueRangeLattice *lattice = std::get<0>(it);
-      propagateIfChanged(lattice,
-                         lattice->join(IntegerValueRange::getMaxRange(value)));
-    }
-  }
-  if (!hasIntegerResult)
-    return;
-
   auto inferrable = dyn_cast<InferIntRangeInterface>(op);
   if (!inferrable)
     return setAllToEntryStates(results);



More information about the Mlir-commits mailing list