Skip to content

[mlir][IntegerRangeAnalysis] expose maybeReplaceWithConstant #133151

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

makslevental
Copy link
Contributor

@makslevental makslevental commented Mar 26, 2025

This PR exposes maybeReplaceWithConstant in headers for downstream use.

@makslevental makslevental marked this pull request as ready for review March 26, 2025 20:20
@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-mlir

Author: Maksim Levental (makslevental)

Changes

This PR exposes maybeReplaceWithConstant in headers for downstream use.


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

2 Files Affected:

  • (modified) mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h (+4)
  • (modified) mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp (+4-3)
diff --git a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
index b9e5573d6ad40..b643ca5284187 100644
--- a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
+++ b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
@@ -19,6 +19,7 @@
 #define MLIR_ANALYSIS_DATAFLOW_INTEGERANGEANALYSIS_H
 
 #include "mlir/Analysis/DataFlow/SparseAnalysis.h"
+#include "mlir/IR/PatternMatch.h"
 #include "mlir/Interfaces/InferIntRangeInterface.h"
 
 namespace mlir {
@@ -83,6 +84,9 @@ LogicalResult staticallyNonNegative(DataFlowSolver &solver, Operation *op);
 /// to use a non-64-bit index.
 LogicalResult staticallyNonNegative(DataFlowSolver &solver, Value v);
 
+LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
+                                       PatternRewriter &rewriter, Value value);
+
 } // end namespace dataflow
 } // end namespace mlir
 
diff --git a/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp b/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
index f866c91ef6e39..22e14fabba9a7 100644
--- a/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
+++ b/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
@@ -57,10 +57,10 @@ static void copyIntegerRange(DataFlowSolver &solver, Value oldVal,
       *oldState);
 }
 
+namespace mlir::dataflow {
 /// Patterned after SCCP
-static LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
-                                              PatternRewriter &rewriter,
-                                              Value value) {
+LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
+                                       PatternRewriter &rewriter, Value value) {
   if (value.use_empty())
     return failure();
   std::optional<APInt> maybeConstValue = getMaybeConstantValue(solver, value);
@@ -95,6 +95,7 @@ static LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
   rewriter.replaceAllUsesWith(value, constOp->getResult(0));
   return success();
 }
+} // namespace mlir::dataflow
 
 namespace {
 class DataFlowListener : public RewriterBase::Listener {

@llvmbot
Copy link
Member

llvmbot commented Mar 26, 2025

@llvm/pr-subscribers-mlir-arith

Author: Maksim Levental (makslevental)

Changes

This PR exposes maybeReplaceWithConstant in headers for downstream use.


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

2 Files Affected:

  • (modified) mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h (+4)
  • (modified) mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp (+4-3)
diff --git a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
index b9e5573d6ad40..b643ca5284187 100644
--- a/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
+++ b/mlir/include/mlir/Analysis/DataFlow/IntegerRangeAnalysis.h
@@ -19,6 +19,7 @@
 #define MLIR_ANALYSIS_DATAFLOW_INTEGERANGEANALYSIS_H
 
 #include "mlir/Analysis/DataFlow/SparseAnalysis.h"
+#include "mlir/IR/PatternMatch.h"
 #include "mlir/Interfaces/InferIntRangeInterface.h"
 
 namespace mlir {
@@ -83,6 +84,9 @@ LogicalResult staticallyNonNegative(DataFlowSolver &solver, Operation *op);
 /// to use a non-64-bit index.
 LogicalResult staticallyNonNegative(DataFlowSolver &solver, Value v);
 
+LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
+                                       PatternRewriter &rewriter, Value value);
+
 } // end namespace dataflow
 } // end namespace mlir
 
diff --git a/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp b/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
index f866c91ef6e39..22e14fabba9a7 100644
--- a/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
+++ b/mlir/lib/Dialect/Arith/Transforms/IntRangeOptimizations.cpp
@@ -57,10 +57,10 @@ static void copyIntegerRange(DataFlowSolver &solver, Value oldVal,
       *oldState);
 }
 
+namespace mlir::dataflow {
 /// Patterned after SCCP
-static LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
-                                              PatternRewriter &rewriter,
-                                              Value value) {
+LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
+                                       PatternRewriter &rewriter, Value value) {
   if (value.use_empty())
     return failure();
   std::optional<APInt> maybeConstValue = getMaybeConstantValue(solver, value);
@@ -95,6 +95,7 @@ static LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
   rewriter.replaceAllUsesWith(value, constOp->getResult(0));
   return success();
 }
+} // namespace mlir::dataflow
 
 namespace {
 class DataFlowListener : public RewriterBase::Listener {

Copy link
Contributor

@krzysz00 krzysz00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor API notes, approved after those

@@ -19,6 +19,7 @@
#define MLIR_ANALYSIS_DATAFLOW_INTEGERANGEANALYSIS_H

#include "mlir/Analysis/DataFlow/SparseAnalysis.h"
#include "mlir/IR/PatternMatch.h"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can forward-declare this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@@ -83,6 +84,9 @@ LogicalResult staticallyNonNegative(DataFlowSolver &solver, Operation *op);
/// to use a non-64-bit index.
LogicalResult staticallyNonNegative(DataFlowSolver &solver, Value v);

LogicalResult maybeReplaceWithConstant(DataFlowSolver &solver,
PatternRewriter &rewriter, Value value);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This could be an RewriterBase & since this isn't necessarily being called in a pattern context?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@makslevental makslevental force-pushed the makslevental/expose-maybe-replace-with-constants-arith branch from 6c7d94f to 936c023 Compare March 26, 2025 20:25
@makslevental makslevental merged commit bfe8523 into llvm:main Mar 26, 2025
10 of 11 checks passed
@makslevental makslevental deleted the makslevental/expose-maybe-replace-with-constants-arith branch March 26, 2025 22:10
antiagainst pushed a commit to triton-lang/triton that referenced this pull request Mar 28, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants