Skip to content

Commit 7ad63c0

Browse files
authored
[mlir][MathToFuncs] MathToFuncs only support integer type (#113693)
This PR fixes a bug in `MathToFuncs` where it incorrectly converts index type for `math.ctlz` and `math.ipowi`, leading to a crash. Fixes #108150.
1 parent 40fffba commit 7ad63c0

File tree

3 files changed

+38
-4
lines changed

3 files changed

+38
-4
lines changed

mlir/lib/Conversion/MathToFuncs/MathToFuncs.cpp

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -781,6 +781,9 @@ struct ConvertMathToFuncsPass
781781
// or equal to minWidthOfFPowIExponent option value.
782782
bool isFPowIConvertible(math::FPowIOp op);
783783

784+
// Reture true, if operation is integer type.
785+
bool isConvertible(Operation *op);
786+
784787
// Generate outlined implementations for power operations
785788
// and store them in funcImpls map.
786789
void generateOpImplementations();
@@ -798,13 +801,17 @@ bool ConvertMathToFuncsPass::isFPowIConvertible(math::FPowIOp op) {
798801
return (expTy && expTy.getWidth() >= minWidthOfFPowIExponent);
799802
}
800803

804+
bool ConvertMathToFuncsPass::isConvertible(Operation *op) {
805+
return isa<IntegerType>(getElementTypeOrSelf(op->getResult(0).getType()));
806+
}
807+
801808
void ConvertMathToFuncsPass::generateOpImplementations() {
802809
ModuleOp module = getOperation();
803810

804811
module.walk([&](Operation *op) {
805812
TypeSwitch<Operation *>(op)
806813
.Case<math::CountLeadingZerosOp>([&](math::CountLeadingZerosOp op) {
807-
if (!convertCtlz)
814+
if (!convertCtlz || !isConvertible(op))
808815
return;
809816
Type resultType = getElementTypeOrSelf(op.getResult().getType());
810817

@@ -816,6 +823,9 @@ void ConvertMathToFuncsPass::generateOpImplementations() {
816823
entry.first->second = createCtlzFunc(&module, resultType);
817824
})
818825
.Case<math::IPowIOp>([&](math::IPowIOp op) {
826+
if (!isConvertible(op))
827+
return;
828+
819829
Type resultType = getElementTypeOrSelf(op.getResult().getType());
820830

821831
// Generate the software implementation of this operation,
@@ -873,9 +883,12 @@ void ConvertMathToFuncsPass::runOnOperation() {
873883
func::FuncDialect, scf::SCFDialect,
874884
vector::VectorDialect>();
875885

876-
target.addIllegalOp<math::IPowIOp>();
877-
if (convertCtlz)
878-
target.addIllegalOp<math::CountLeadingZerosOp>();
886+
target.addDynamicallyLegalOp<math::IPowIOp>(
887+
[this](math::IPowIOp op) { return !isConvertible(op); });
888+
if (convertCtlz) {
889+
target.addDynamicallyLegalOp<math::CountLeadingZerosOp>(
890+
[this](math::CountLeadingZerosOp op) { return !isConvertible(op); });
891+
}
879892
target.addDynamicallyLegalOp<math::FPowIOp>(
880893
[this](math::FPowIOp op) { return !isFPowIConvertible(op); });
881894
if (failed(applyPartialConversion(module, target, std::move(patterns))))

mlir/test/Conversion/MathToFuncs/ctlz.mlir

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,3 +91,13 @@ func.func @main(%arg0: i8) {
9191
func.return
9292
}
9393

94+
// -----
95+
96+
// Check that index is not converted
97+
98+
// CHECK-LABEL: func.func @ctlz_index
99+
// CHECK: math.ctlz
100+
func.func @ctlz_index(%arg0: index) {
101+
%0 = math.ctlz %arg0 : index
102+
func.return
103+
}

mlir/test/Conversion/MathToFuncs/ipowi.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,3 +170,14 @@ func.func @ipowi_vec(%arg0: vector<2x3xi64>, %arg1: vector<2x3xi64>) {
170170
%0 = math.ipowi %arg0, %arg1 : vector<2x3xi64>
171171
func.return
172172
}
173+
174+
// -----
175+
176+
// Check that index is not converted
177+
178+
// CHECK-LABEL: func.func @ipowi_index
179+
// CHECK: math.ipowi
180+
func.func @ipowi_index(%arg0: index, %arg1: index) {
181+
%0 = math.ipowi %arg0, %arg1 : index
182+
func.return
183+
}

0 commit comments

Comments
 (0)