| 1 | //===- MathToEmitC.cpp - Math to EmitC Patterns -----------------*- C++ -*-===// |
| 2 | // |
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | // See https://llvm.org/LICENSE.txt for license information. |
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #include "mlir/Conversion/MathToEmitC/MathToEmitC.h" |
| 10 | |
| 11 | #include "mlir/Conversion/ConvertToEmitC/ToEmitCInterface.h" |
| 12 | #include "mlir/Dialect/Arith/IR/ArithOpInterfaces.h" |
| 13 | #include "mlir/Dialect/EmitC/IR/EmitC.h" |
| 14 | #include "mlir/Dialect/Math/IR/Math.h" |
| 15 | #include "mlir/Transforms/DialectConversion.h" |
| 16 | |
| 17 | using namespace mlir; |
| 18 | |
| 19 | namespace { |
| 20 | /// Implement the interface to convert Math to EmitC. |
| 21 | struct MathToEmitCDialectInterface : public ConvertToEmitCPatternInterface { |
| 22 | MathToEmitCDialectInterface(Dialect *dialect) |
| 23 | : ConvertToEmitCPatternInterface(dialect) {} |
| 24 | |
| 25 | /// Hook for derived dialect interface to provide conversion patterns |
| 26 | /// and mark dialect legal for the conversion target. |
| 27 | void populateConvertToEmitCConversionPatterns( |
| 28 | ConversionTarget &target, TypeConverter &typeConverter, |
| 29 | RewritePatternSet &patterns, std::optional<bool> lowerToCpp) const final { |
| 30 | populateConvertMathToEmitCPatterns( |
| 31 | patterns, languageTarget: lowerToCpp.value_or(u: true) ? emitc::LanguageTarget::cpp11 |
| 32 | : emitc::LanguageTarget::c99); |
| 33 | } |
| 34 | }; |
| 35 | |
| 36 | template <typename OpType> |
| 37 | class LowerToEmitCCallOpaque : public OpRewritePattern<OpType> { |
| 38 | std::string calleeStr; |
| 39 | emitc::LanguageTarget languageTarget; |
| 40 | bool typeSuffix; |
| 41 | |
| 42 | public: |
| 43 | LowerToEmitCCallOpaque(MLIRContext *context, std::string calleeStr, |
| 44 | emitc::LanguageTarget languageTarget, |
| 45 | bool typeSuffix = true) |
| 46 | : OpRewritePattern<OpType>(context), calleeStr(std::move(calleeStr)), |
| 47 | languageTarget(languageTarget), typeSuffix(typeSuffix) {} |
| 48 | |
| 49 | LogicalResult matchAndRewrite(OpType op, |
| 50 | PatternRewriter &rewriter) const override; |
| 51 | }; |
| 52 | |
| 53 | template <typename OpType> |
| 54 | LogicalResult LowerToEmitCCallOpaque<OpType>::matchAndRewrite( |
| 55 | OpType op, PatternRewriter &rewriter) const { |
| 56 | if (!llvm::all_of(op->getOperandTypes(), |
| 57 | llvm::IsaPred<Float32Type, Float64Type>) || |
| 58 | !llvm::all_of(op->getResultTypes(), [](Type type) { |
| 59 | return isa<Float32Type, Float64Type>(Val: type) || type.isInteger(width: 1); |
| 60 | })) |
| 61 | return rewriter.notifyMatchFailure( |
| 62 | op.getLoc(), "expected all operands to be of type f32 or f64, and " |
| 63 | "all results to be of type f32, f64 or i1" ); |
| 64 | if (auto roundingModeOp = |
| 65 | dyn_cast<arith::ArithRoundingModeInterface>(op.getOperation()); |
| 66 | roundingModeOp && roundingModeOp.getRoundingModeAttr()) |
| 67 | return rewriter.notifyMatchFailure(op.getLoc(), |
| 68 | "static rounding mode not supported" ); |
| 69 | std::string modifiedCalleeStr = calleeStr; |
| 70 | if (languageTarget == emitc::LanguageTarget::cpp11) { |
| 71 | modifiedCalleeStr = "std::" + calleeStr; |
| 72 | } else if (languageTarget == emitc::LanguageTarget::c99 && typeSuffix) { |
| 73 | auto operandType = op->getOperandTypes()[0]; |
| 74 | if (operandType.isF32()) |
| 75 | modifiedCalleeStr = calleeStr + "f" ; |
| 76 | } |
| 77 | rewriter.replaceOpWithNewOp<emitc::CallOpaqueOp>( |
| 78 | op, op.getType(), modifiedCalleeStr, op->getOperands()); |
| 79 | return success(); |
| 80 | } |
| 81 | |
| 82 | } // namespace |
| 83 | |
| 84 | void mlir::registerConvertMathToEmitCInterface(DialectRegistry ®istry) { |
| 85 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, math::MathDialect *dialect) { |
| 86 | dialect->addInterfaces<MathToEmitCDialectInterface>(); |
| 87 | }); |
| 88 | } |
| 89 | |
| 90 | // Populates patterns to replace `math` operations with `emitc.call_opaque`, |
| 91 | // using function names consistent with those in <math.h> or a builtin. |
| 92 | void mlir::populateConvertMathToEmitCPatterns( |
| 93 | RewritePatternSet &patterns, emitc::LanguageTarget languageTarget) { |
| 94 | auto *context = patterns.getContext(); |
| 95 | patterns.insert<LowerToEmitCCallOpaque<math::FloorOp>>(arg&: context, args: "floor" , |
| 96 | args&: languageTarget); |
| 97 | patterns.insert<LowerToEmitCCallOpaque<math::RoundOp>>(arg&: context, args: "round" , |
| 98 | args&: languageTarget); |
| 99 | patterns.insert<LowerToEmitCCallOpaque<math::RoundEvenOp>>( |
| 100 | arg&: context, args: "__builtin_roundeven" , args: emitc::LanguageTarget::c99); |
| 101 | patterns.insert<LowerToEmitCCallOpaque<math::ExpOp>>(arg&: context, args: "exp" , |
| 102 | args&: languageTarget); |
| 103 | patterns.insert<LowerToEmitCCallOpaque<math::CosOp>>(arg&: context, args: "cos" , |
| 104 | args&: languageTarget); |
| 105 | patterns.insert<LowerToEmitCCallOpaque<math::SinOp>>(arg&: context, args: "sin" , |
| 106 | args&: languageTarget); |
| 107 | patterns.insert<LowerToEmitCCallOpaque<math::AcosOp>>(arg&: context, args: "acos" , |
| 108 | args&: languageTarget); |
| 109 | patterns.insert<LowerToEmitCCallOpaque<math::AsinOp>>(arg&: context, args: "asin" , |
| 110 | args&: languageTarget); |
| 111 | patterns.insert<LowerToEmitCCallOpaque<math::Atan2Op>>(arg&: context, args: "atan2" , |
| 112 | args&: languageTarget); |
| 113 | patterns.insert<LowerToEmitCCallOpaque<math::CeilOp>>(arg&: context, args: "ceil" , |
| 114 | args&: languageTarget); |
| 115 | patterns.insert<LowerToEmitCCallOpaque<math::AbsFOp>>(arg&: context, args: "fabs" , |
| 116 | args&: languageTarget); |
| 117 | patterns.insert<LowerToEmitCCallOpaque<math::PowFOp>>(arg&: context, args: "pow" , |
| 118 | args&: languageTarget); |
| 119 | patterns.insert<LowerToEmitCCallOpaque<math::SqrtOp>>(arg&: context, args: "sqrt" , |
| 120 | args&: languageTarget); |
| 121 | patterns.insert<LowerToEmitCCallOpaque<math::TanOp>>(arg&: context, args: "tan" , |
| 122 | args&: languageTarget); |
| 123 | patterns.insert<LowerToEmitCCallOpaque<math::AtanOp>>(arg&: context, args: "atan" , |
| 124 | args&: languageTarget); |
| 125 | patterns.insert<LowerToEmitCCallOpaque<math::SinhOp>>(arg&: context, args: "sinh" , |
| 126 | args&: languageTarget); |
| 127 | patterns.insert<LowerToEmitCCallOpaque<math::CoshOp>>(arg&: context, args: "cosh" , |
| 128 | args&: languageTarget); |
| 129 | patterns.insert<LowerToEmitCCallOpaque<math::TanhOp>>(arg&: context, args: "tanh" , |
| 130 | args&: languageTarget); |
| 131 | patterns.insert<LowerToEmitCCallOpaque<math::AsinhOp>>(arg&: context, args: "asinh" , |
| 132 | args&: languageTarget); |
| 133 | patterns.insert<LowerToEmitCCallOpaque<math::AcoshOp>>(arg&: context, args: "acosh" , |
| 134 | args&: languageTarget); |
| 135 | patterns.insert<LowerToEmitCCallOpaque<math::AtanhOp>>(arg&: context, args: "atanh" , |
| 136 | args&: languageTarget); |
| 137 | patterns.insert<LowerToEmitCCallOpaque<math::Exp2Op>>(arg&: context, args: "exp2" , |
| 138 | args&: languageTarget); |
| 139 | patterns.insert<LowerToEmitCCallOpaque<math::ExpM1Op>>(arg&: context, args: "expm1" , |
| 140 | args&: languageTarget); |
| 141 | patterns.insert<LowerToEmitCCallOpaque<math::LogOp>>(arg&: context, args: "log" , |
| 142 | args&: languageTarget); |
| 143 | patterns.insert<LowerToEmitCCallOpaque<math::Log2Op>>(arg&: context, args: "log2" , |
| 144 | args&: languageTarget); |
| 145 | patterns.insert<LowerToEmitCCallOpaque<math::Log10Op>>(arg&: context, args: "log10" , |
| 146 | args&: languageTarget); |
| 147 | patterns.insert<LowerToEmitCCallOpaque<math::Log1pOp>>(arg&: context, args: "log1p" , |
| 148 | args&: languageTarget); |
| 149 | patterns.insert<LowerToEmitCCallOpaque<math::CbrtOp>>(arg&: context, args: "cbrt" , |
| 150 | args&: languageTarget); |
| 151 | patterns.insert<LowerToEmitCCallOpaque<math::ErfOp>>(arg&: context, args: "erf" , |
| 152 | args&: languageTarget); |
| 153 | patterns.insert<LowerToEmitCCallOpaque<math::ErfcOp>>(arg&: context, args: "erfc" , |
| 154 | args&: languageTarget); |
| 155 | patterns.insert<LowerToEmitCCallOpaque<math::TruncOp>>(arg&: context, args: "trunc" , |
| 156 | args&: languageTarget); |
| 157 | patterns.insert<LowerToEmitCCallOpaque<math::CopySignOp>>(arg&: context, args: "copysign" , |
| 158 | args&: languageTarget); |
| 159 | patterns.insert<LowerToEmitCCallOpaque<math::FmaOp>>(arg&: context, args: "fma" , |
| 160 | args&: languageTarget); |
| 161 | patterns.insert<LowerToEmitCCallOpaque<math::IsNaNOp>>( |
| 162 | arg&: context, args: "isnan" , args&: languageTarget, /*typeSuffix=*/args: false); |
| 163 | patterns.insert<LowerToEmitCCallOpaque<math::IsInfOp>>( |
| 164 | arg&: context, args: "isinf" , args&: languageTarget, /*typeSuffix=*/args: false); |
| 165 | patterns.insert<LowerToEmitCCallOpaque<math::IsFiniteOp>>( |
| 166 | arg&: context, args: "isfinite" , args&: languageTarget, /*typeSuffix=*/args: false); |
| 167 | patterns.insert<LowerToEmitCCallOpaque<math::IsNormalOp>>( |
| 168 | arg&: context, args: "isnormal" , args&: languageTarget, /*typeSuffix=*/args: false); |
| 169 | } |
| 170 | |