Skip to content

Commit

Permalink
[mlir][MathToFuncs] MathToFuncs only support integer type (llvm#113693
Browse files Browse the repository at this point in the history
)

This PR fixes a bug in `MathToFuncs` where it incorrectly converts index
type for `math.ctlz` and `math.ipowi`, leading to a crash. Fixes
llvm#108150.
  • Loading branch information
CoTinker authored and NoumanAmir657 committed Nov 4, 2024
1 parent d9463a3 commit 15e0c70
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 4 deletions.
21 changes: 17 additions & 4 deletions mlir/lib/Conversion/MathToFuncs/MathToFuncs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -781,6 +781,9 @@ struct ConvertMathToFuncsPass
// or equal to minWidthOfFPowIExponent option value.
bool isFPowIConvertible(math::FPowIOp op);

// Reture true, if operation is integer type.
bool isConvertible(Operation *op);

// Generate outlined implementations for power operations
// and store them in funcImpls map.
void generateOpImplementations();
Expand All @@ -798,13 +801,17 @@ bool ConvertMathToFuncsPass::isFPowIConvertible(math::FPowIOp op) {
return (expTy && expTy.getWidth() >= minWidthOfFPowIExponent);
}

bool ConvertMathToFuncsPass::isConvertible(Operation *op) {
return isa<IntegerType>(getElementTypeOrSelf(op->getResult(0).getType()));
}

void ConvertMathToFuncsPass::generateOpImplementations() {
ModuleOp module = getOperation();

module.walk([&](Operation *op) {
TypeSwitch<Operation *>(op)
.Case<math::CountLeadingZerosOp>([&](math::CountLeadingZerosOp op) {
if (!convertCtlz)
if (!convertCtlz || !isConvertible(op))
return;
Type resultType = getElementTypeOrSelf(op.getResult().getType());

Expand All @@ -816,6 +823,9 @@ void ConvertMathToFuncsPass::generateOpImplementations() {
entry.first->second = createCtlzFunc(&module, resultType);
})
.Case<math::IPowIOp>([&](math::IPowIOp op) {
if (!isConvertible(op))
return;

Type resultType = getElementTypeOrSelf(op.getResult().getType());

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

target.addIllegalOp<math::IPowIOp>();
if (convertCtlz)
target.addIllegalOp<math::CountLeadingZerosOp>();
target.addDynamicallyLegalOp<math::IPowIOp>(
[this](math::IPowIOp op) { return !isConvertible(op); });
if (convertCtlz) {
target.addDynamicallyLegalOp<math::CountLeadingZerosOp>(
[this](math::CountLeadingZerosOp op) { return !isConvertible(op); });
}
target.addDynamicallyLegalOp<math::FPowIOp>(
[this](math::FPowIOp op) { return !isFPowIConvertible(op); });
if (failed(applyPartialConversion(module, target, std::move(patterns))))
Expand Down
10 changes: 10 additions & 0 deletions mlir/test/Conversion/MathToFuncs/ctlz.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -91,3 +91,13 @@ func.func @main(%arg0: i8) {
func.return
}

// -----

// Check that index is not converted

// CHECK-LABEL: func.func @ctlz_index
// CHECK: math.ctlz
func.func @ctlz_index(%arg0: index) {
%0 = math.ctlz %arg0 : index
func.return
}
11 changes: 11 additions & 0 deletions mlir/test/Conversion/MathToFuncs/ipowi.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -170,3 +170,14 @@ func.func @ipowi_vec(%arg0: vector<2x3xi64>, %arg1: vector<2x3xi64>) {
%0 = math.ipowi %arg0, %arg1 : vector<2x3xi64>
func.return
}

// -----

// Check that index is not converted

// CHECK-LABEL: func.func @ipowi_index
// CHECK: math.ipowi
func.func @ipowi_index(%arg0: index, %arg1: index) {
%0 = math.ipowi %arg0, %arg1 : index
func.return
}

0 comments on commit 15e0c70

Please sign in to comment.