17#define GEN_PASS_DEF_CONVERTCOMPLEXTOLIBM
18#include "mlir/Conversion/Passes.h.inc"
26struct ComplexTypeResolver {
27 std::optional<bool> operator()(Type type)
const {
28 auto complexType = cast<ComplexType>(type);
29 auto elementType = complexType.getElementType();
30 if (!isa<Float32Type, Float64Type>(elementType))
33 return elementType.getIntOrFloatBitWidth() == 64;
39struct FloatTypeResolver {
40 std::optional<bool> operator()(Type type)
const {
41 auto elementType = cast<FloatType>(type);
42 if (!isa<Float32Type, Float64Type>(elementType))
45 return elementType.getIntOrFloatBitWidth() == 64;
53template <
typename Op,
typename TypeResolver = ComplexTypeResolver>
56 using OpRewritePattern<
Op>::OpRewritePattern;
57 ScalarOpToLibmCall(MLIRContext *context, StringRef floatFunc,
58 StringRef doubleFunc, PatternBenefit benefit)
59 : OpRewritePattern<
Op>(context, benefit), floatFunc(floatFunc),
60 doubleFunc(doubleFunc){};
62 LogicalResult matchAndRewrite(Op op, PatternRewriter &rewriter)
const final;
65 std::string floatFunc, doubleFunc;
69template <
typename Op,
typename TypeResolver>
70LogicalResult ScalarOpToLibmCall<Op, TypeResolver>::matchAndRewrite(
72 auto module = SymbolTable::getNearestSymbolTable(op);
73 auto isDouble = TypeResolver()(op.getType());
74 if (!isDouble.has_value())
77 auto name = *isDouble ? doubleFunc : floatFunc;
79 auto opFunc = dyn_cast_or_null<SymbolOpInterface>(
85 auto opFunctionTy = FunctionType::get(
86 rewriter.
getContext(), op->getOperandTypes(), op->getResultTypes());
87 opFunc = func::FuncOp::create(rewriter, rewriter.
getUnknownLoc(), name,
102 "cpowf",
"cpow", benefit);
104 "csqrtf",
"csqrt", benefit);
106 "ctanhf",
"ctanh", benefit);
108 "ccosf",
"ccos", benefit);
110 "csinf",
"csin", benefit);
112 "conjf",
"conj", benefit);
114 "clogf",
"clog", benefit);
115 patterns.add<ScalarOpToLibmCall<complex::AbsOp, FloatTypeResolver>>(
116 patterns.getContext(),
"cabsf",
"cabs", benefit);
117 patterns.add<ScalarOpToLibmCall<complex::AngleOp, FloatTypeResolver>>(
118 patterns.getContext(),
"cargf",
"carg", benefit);
120 "ctanf",
"ctan", benefit);
124struct ConvertComplexToLibmPass
126 void runOnOperation()
override;
130void ConvertComplexToLibmPass::runOnOperation() {
131 auto module = getOperation();
137 target.addLegalDialect<func::FuncDialect>();
138 target.addIllegalOp<complex::PowOp, complex::SqrtOp, complex::TanhOp,
139 complex::CosOp, complex::SinOp, complex::ConjOp,
140 complex::LogOp, complex::AbsOp, complex::AngleOp,
MLIRContext * getContext() const
RAII guard to reset the insertion point of the builder when destroyed.
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
This provides public APIs that all operations should have.
This class represents the benefit of a pattern match in a unitless scheme that ranges from 0 (very li...
A special type of RewriterBase that coordinates the application of a rewrite pattern on the current I...
OpTy replaceOpWithNewOp(Operation *op, Args &&...args)
Replace the results of the given (original) op with a new op that is created without verification (re...
static Operation * lookupSymbolIn(Operation *op, StringAttr symbol)
Returns the operation registered with the given symbol name with the regions of 'symbolTableOp'.
NestedPattern Op(FilterFunctionType filter=defaultFilterFunction)
Include the generated interface declarations.
const FrozenRewritePatternSet & patterns
void populateComplexToLibmConversionPatterns(RewritePatternSet &patterns, PatternBenefit benefit)
Populate the given list with patterns that convert from Complex to Libm calls.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...