|
| 1 | + |
| 2 | +//===- MathToEmitC.cpp - Math to EmitC Pass Implementation ----------===// |
| 3 | +// |
| 4 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 5 | +// See https://llvm.org/LICENSE.txt for license information. |
| 6 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 7 | +// |
| 8 | +//===----------------------------------------------------------------------===// |
| 9 | + |
| 10 | +#include "mlir/Conversion/MathToEmitC/MathToEmitC.h" |
| 11 | +#include "mlir/Dialect/EmitC/IR/EmitC.h" |
| 12 | +#include "mlir/Dialect/Math/IR/Math.h" |
| 13 | +#include "mlir/Pass/Pass.h" |
| 14 | +#include "mlir/Transforms/DialectConversion.h" |
| 15 | + |
| 16 | +namespace mlir { |
| 17 | +#define GEN_PASS_DEF_CONVERTMATHTOEMITC |
| 18 | +#include "mlir/Conversion/Passes.h.inc" |
| 19 | +} // namespace mlir |
| 20 | + |
| 21 | +using namespace mlir; |
| 22 | +namespace { |
| 23 | + |
| 24 | +// Replaces Math operations with `emitc.call_opaque` operations. |
| 25 | +struct ConvertMathToEmitCPass |
| 26 | + : public impl::ConvertMathToEmitCBase<ConvertMathToEmitCPass> { |
| 27 | +public: |
| 28 | + void runOnOperation() final; |
| 29 | +}; |
| 30 | + |
| 31 | +} // end anonymous namespace |
| 32 | + |
| 33 | +template <typename OpType> |
| 34 | +class LowerToEmitCCallOpaque : public mlir::OpRewritePattern<OpType> { |
| 35 | + std::string calleeStr; |
| 36 | + |
| 37 | +public: |
| 38 | + LowerToEmitCCallOpaque(MLIRContext *context, std::string calleeStr) |
| 39 | + : OpRewritePattern<OpType>(context), calleeStr(calleeStr) {} |
| 40 | + |
| 41 | + LogicalResult matchAndRewrite(OpType op, |
| 42 | + PatternRewriter &rewriter) const override; |
| 43 | +}; |
| 44 | + |
| 45 | +// Populates patterns to replace `math` operations with `emitc.call_opaque`, |
| 46 | +// using function names consistent with those in <math.h>. |
| 47 | +static void populateConvertMathToEmitCPatterns(RewritePatternSet &patterns) { |
| 48 | + auto *context = patterns.getContext(); |
| 49 | + patterns.insert<LowerToEmitCCallOpaque<math::FloorOp>>(context, "floor"); |
| 50 | + patterns.insert<LowerToEmitCCallOpaque<math::RoundEvenOp>>(context, "rint"); |
| 51 | + patterns.insert<LowerToEmitCCallOpaque<math::ExpOp>>(context, "exp"); |
| 52 | + patterns.insert<LowerToEmitCCallOpaque<math::CosOp>>(context, "cos"); |
| 53 | + patterns.insert<LowerToEmitCCallOpaque<math::SinOp>>(context, "sin"); |
| 54 | + patterns.insert<LowerToEmitCCallOpaque<math::AcosOp>>(context, "acos"); |
| 55 | + patterns.insert<LowerToEmitCCallOpaque<math::AsinOp>>(context, "asin"); |
| 56 | + patterns.insert<LowerToEmitCCallOpaque<math::Atan2Op>>(context, "atan2"); |
| 57 | + patterns.insert<LowerToEmitCCallOpaque<math::CeilOp>>(context, "ceil"); |
| 58 | + patterns.insert<LowerToEmitCCallOpaque<math::AbsFOp>>(context, "fabs"); |
| 59 | + patterns.insert<LowerToEmitCCallOpaque<math::FPowIOp>>(context, "powf"); |
| 60 | + patterns.insert<LowerToEmitCCallOpaque<math::IPowIOp>>(context, "pow"); |
| 61 | +} |
| 62 | + |
| 63 | +template <typename OpType> |
| 64 | +LogicalResult LowerToEmitCCallOpaque<OpType>::matchAndRewrite( |
| 65 | + OpType op, PatternRewriter &rewriter) const { |
| 66 | + mlir::StringAttr callee = rewriter.getStringAttr(calleeStr); |
| 67 | + auto actualOp = mlir::cast<OpType>(op); |
| 68 | + rewriter.replaceOpWithNewOp<mlir::emitc::CallOpaqueOp>( |
| 69 | + actualOp, actualOp.getType(), callee, actualOp->getOperands()); |
| 70 | + return mlir::success(); |
| 71 | +} |
| 72 | + |
| 73 | +void ConvertMathToEmitCPass::runOnOperation() { |
| 74 | + auto moduleOp = getOperation(); |
| 75 | + // Insert #include <math.h> at the beginning of the module |
| 76 | + OpBuilder builder(moduleOp.getBodyRegion()); |
| 77 | + builder.setInsertionPointToStart(&moduleOp.getBodyRegion().front()); |
| 78 | + builder.create<emitc::IncludeOp>(moduleOp.getLoc(), |
| 79 | + builder.getStringAttr("math.h")); |
| 80 | + |
| 81 | + ConversionTarget target(getContext()); |
| 82 | + target.addLegalOp<emitc::CallOpaqueOp>(); |
| 83 | + |
| 84 | + target.addIllegalOp<math::FloorOp, math::ExpOp, math::RoundEvenOp, |
| 85 | + math::CosOp, math::SinOp, math::Atan2Op, math::CeilOp, |
| 86 | + math::AcosOp, math::AsinOp, math::AbsFOp, math::PowFOp, |
| 87 | + math::FPowIOp, math::IPowIOp>(); |
| 88 | + |
| 89 | + RewritePatternSet patterns(&getContext()); |
| 90 | + populateConvertMathToEmitCPatterns(patterns); |
| 91 | + |
| 92 | + if (failed(applyPartialConversion(moduleOp, target, std::move(patterns)))) |
| 93 | + signalPassFailure(); |
| 94 | +} |
| 95 | + |
| 96 | +std::unique_ptr<OperationPass<mlir::ModuleOp>> |
| 97 | +mlir::createConvertMathToEmitCPass() { |
| 98 | + return std::make_unique<ConvertMathToEmitCPass>(); |
| 99 | +} |
0 commit comments