Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
}
Loading