Skip to content

Commit 9f0f6df

Browse files
authored
[mlir] Add arith-int-range-narrowing pass (#112404)
This pass intended to narrow integer calculations to the specific bitwidth, using `IntegerRangeAnalysis`. We already have the `arith-int-narrowing` pass, but it mostly only doing local analysis, while `IntegerRangeAnalysis` analyses entire program. They ideally should be unified, but it's a task for the future.
1 parent 2de3d00 commit 9f0f6df

File tree

9 files changed

+591
-1970
lines changed

9 files changed

+591
-1970
lines changed

mlir/include/mlir/Dialect/Arith/Transforms/Passes.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,9 +77,10 @@ void populateUnsignedWhenEquivalentPatterns(RewritePatternSet &patterns,
7777
/// Create a pass which do optimizations based on integer range analysis.
7878
std::unique_ptr<Pass> createIntRangeOptimizationsPass();
7979

80-
/// Add patterns for integer bitwidth narrowing.
81-
void populateArithIntNarrowingPatterns(RewritePatternSet &patterns,
82-
const ArithIntNarrowingOptions &options);
80+
/// Add patterns for int range based narrowing.
81+
void populateIntRangeNarrowingPatterns(RewritePatternSet &patterns,
82+
DataFlowSolver &solver,
83+
ArrayRef<unsigned> bitwidthsSupported);
8384

8485
//===----------------------------------------------------------------------===//
8586
// Registration

mlir/include/mlir/Dialect/Arith/Transforms/Passes.td

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,28 @@ def ArithIntRangeOpts : Pass<"int-range-optimizations"> {
5050
];
5151
}
5252

53+
def ArithIntRangeNarrowing : Pass<"arith-int-range-narrowing"> {
54+
let summary = "Reduce integer operations bitwidth based on integer range analysis";
55+
let description = [{
56+
This pass runs integer range analysis and tries to narrow arith ops to the
57+
specified bitwidth based on its results.
58+
59+
`bitwidthsSupported` assumed to be not wider than `index` type.
60+
TODO: get index width from DLTI.
61+
}];
62+
63+
let options = [
64+
ListOption<"bitwidthsSupported", "int-bitwidths-supported", "unsigned",
65+
"Integer bitwidths supported">,
66+
];
67+
68+
// Explicitly depend on "arith" because this pass could create operations in
69+
// `arith` out of thin air in some cases.
70+
let dependentDialects = [
71+
"::mlir::arith::ArithDialect"
72+
];
73+
}
74+
5375
def ArithEmulateUnsupportedFloats : Pass<"arith-emulate-unsupported-floats"> {
5476
let summary = "Emulate operations on unsupported floats with extf/truncf";
5577
let description = [{
@@ -92,18 +114,4 @@ def ArithEmulateWideInt : Pass<"arith-emulate-wide-int"> {
92114
let dependentDialects = ["vector::VectorDialect"];
93115
}
94116

95-
def ArithIntNarrowing : Pass<"arith-int-narrowing"> {
96-
let summary = "Reduce integer operation bitwidth";
97-
let description = [{
98-
Reduce bitwidths of integer types used in arith operations. This pass
99-
prefers the narrowest available integer bitwidths that are guaranteed to
100-
produce the same results.
101-
}];
102-
let dependentDialects = ["vector::VectorDialect"];
103-
let options = [
104-
ListOption<"bitwidthsSupported", "int-bitwidths-supported", "unsigned",
105-
"Integer bitwidths supported">,
106-
];
107-
}
108-
109117
#endif // MLIR_DIALECT_ARITH_TRANSFORMS_PASSES

mlir/lib/Dialect/Arith/Transforms/CMakeLists.txt

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ add_mlir_dialect_library(MLIRArithTransforms
66
EmulateWideInt.cpp
77
EmulateNarrowType.cpp
88
ExpandOps.cpp
9-
IntNarrowing.cpp
109
IntRangeOptimizations.cpp
1110
ReifyValueBounds.cpp
1211
UnsignedWhenEquivalent.cpp

0 commit comments

Comments
 (0)