21struct MathToEmitCDialectInterface :
public ConvertToEmitCPatternInterface {
22 MathToEmitCDialectInterface(Dialect *dialect)
23 : ConvertToEmitCPatternInterface(dialect) {}
27 void populateConvertToEmitCConversionPatterns(
28 ConversionTarget &
target, TypeConverter &typeConverter,
29 RewritePatternSet &patterns, std::optional<bool> lowerToCpp)
const final {
31 patterns, lowerToCpp.value_or(
true) ? emitc::LanguageTarget::cpp11
32 : emitc::LanguageTarget::c99);
36template <
typename OpType>
38 std::string calleeStr;
43 LowerToEmitCCallOpaque(MLIRContext *context, std::string calleeStr,
45 bool typeSuffix =
true)
46 : OpRewritePattern<OpType>(context), calleeStr(std::move(calleeStr)),
47 languageTarget(languageTarget), typeSuffix(typeSuffix) {}
49 LogicalResult matchAndRewrite(OpType op,
50 PatternRewriter &rewriter)
const override;
53template <
typename OpType>
54LogicalResult LowerToEmitCCallOpaque<OpType>::matchAndRewrite(
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>(type) || type.isInteger(1);
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())
68 "static rounding mode not supported");
69 std::string modifiedCalleeStr = calleeStr;
71 modifiedCalleeStr =
"std::" + calleeStr;
73 auto operandType = op->getOperandTypes()[0];
74 if (operandType.isF32())
75 modifiedCalleeStr = calleeStr +
"f";
78 op, op.getType(), modifiedCalleeStr, op->getOperands());
86 dialect->addInterfaces<MathToEmitCDialectInterface>();
95 patterns.
insert<LowerToEmitCCallOpaque<math::FloorOp>>(context,
"floor",
97 patterns.
insert<LowerToEmitCCallOpaque<math::RoundOp>>(context,
"round",
99 patterns.
insert<LowerToEmitCCallOpaque<math::RoundEvenOp>>(
101 patterns.
insert<LowerToEmitCCallOpaque<math::ExpOp>>(context,
"exp",
103 patterns.
insert<LowerToEmitCCallOpaque<math::CosOp>>(context,
"cos",
105 patterns.
insert<LowerToEmitCCallOpaque<math::SinOp>>(context,
"sin",
107 patterns.
insert<LowerToEmitCCallOpaque<math::AcosOp>>(context,
"acos",
109 patterns.
insert<LowerToEmitCCallOpaque<math::AsinOp>>(context,
"asin",
111 patterns.
insert<LowerToEmitCCallOpaque<math::Atan2Op>>(context,
"atan2",
113 patterns.
insert<LowerToEmitCCallOpaque<math::CeilOp>>(context,
"ceil",
115 patterns.
insert<LowerToEmitCCallOpaque<math::AbsFOp>>(context,
"fabs",
117 patterns.
insert<LowerToEmitCCallOpaque<math::PowFOp>>(context,
"pow",
119 patterns.
insert<LowerToEmitCCallOpaque<math::SqrtOp>>(context,
"sqrt",
121 patterns.
insert<LowerToEmitCCallOpaque<math::TanOp>>(context,
"tan",
123 patterns.
insert<LowerToEmitCCallOpaque<math::AtanOp>>(context,
"atan",
125 patterns.
insert<LowerToEmitCCallOpaque<math::SinhOp>>(context,
"sinh",
127 patterns.
insert<LowerToEmitCCallOpaque<math::CoshOp>>(context,
"cosh",
129 patterns.
insert<LowerToEmitCCallOpaque<math::TanhOp>>(context,
"tanh",
131 patterns.
insert<LowerToEmitCCallOpaque<math::AsinhOp>>(context,
"asinh",
133 patterns.
insert<LowerToEmitCCallOpaque<math::AcoshOp>>(context,
"acosh",
135 patterns.
insert<LowerToEmitCCallOpaque<math::AtanhOp>>(context,
"atanh",
137 patterns.
insert<LowerToEmitCCallOpaque<math::Exp2Op>>(context,
"exp2",
139 patterns.
insert<LowerToEmitCCallOpaque<math::ExpM1Op>>(context,
"expm1",
141 patterns.
insert<LowerToEmitCCallOpaque<math::LogOp>>(context,
"log",
143 patterns.
insert<LowerToEmitCCallOpaque<math::Log2Op>>(context,
"log2",
145 patterns.
insert<LowerToEmitCCallOpaque<math::Log10Op>>(context,
"log10",
147 patterns.
insert<LowerToEmitCCallOpaque<math::Log1pOp>>(context,
"log1p",
149 patterns.
insert<LowerToEmitCCallOpaque<math::CbrtOp>>(context,
"cbrt",
151 patterns.
insert<LowerToEmitCCallOpaque<math::ErfOp>>(context,
"erf",
153 patterns.
insert<LowerToEmitCCallOpaque<math::ErfcOp>>(context,
"erfc",
155 patterns.
insert<LowerToEmitCCallOpaque<math::TruncOp>>(context,
"trunc",
157 patterns.
insert<LowerToEmitCCallOpaque<math::CopySignOp>>(context,
"copysign",
159 patterns.
insert<LowerToEmitCCallOpaque<math::FmaOp>>(context,
"fma",
161 patterns.
insert<LowerToEmitCCallOpaque<math::IsNaNOp>>(
162 context,
"isnan", languageTarget,
false);
163 patterns.
insert<LowerToEmitCCallOpaque<math::IsInfOp>>(
164 context,
"isinf", languageTarget,
false);
165 patterns.
insert<LowerToEmitCCallOpaque<math::IsFiniteOp>>(
166 context,
"isfinite", languageTarget,
false);
167 patterns.
insert<LowerToEmitCCallOpaque<math::IsNormalOp>>(
168 context,
"isnormal", languageTarget,
false);
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
MLIRContext is the top-level object for a collection of MLIR operations.
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
RewritePatternSet & insert(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
MLIRContext * getContext() const
std::enable_if_t<!std::is_convertible< CallbackT, Twine >::value, LogicalResult > notifyMatchFailure(Location loc, CallbackT &&reasonCallback)
Used to notify the listener that the IR failed to be rewritten because of a match failure,...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
LanguageTarget
Enum to specify the language target for EmitC code generation.
Include the generated interface declarations.
void registerConvertMathToEmitCInterface(DialectRegistry ®istry)
void populateConvertMathToEmitCPatterns(RewritePatternSet &patterns, emitc::LanguageTarget languageTarget)
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...