18#define GEN_PASS_DEF_CONVERTCOMPLEXTOROCDLLIBRARYCALLS
19#include "mlir/Conversion/Passes.h.inc"
26template <
typename Op,
typename FloatTy>
29 using OpRewritePattern<
Op>::OpRewritePattern;
30 ComplexOpToROCDLLibraryCalls(MLIRContext *context, StringRef funcName,
31 PatternBenefit benefit = 1)
32 : OpRewritePattern<Op>(context, benefit), funcName(funcName) {}
34 LogicalResult matchAndRewrite(
Op op, PatternRewriter &rewriter)
const final {
36 Type resType = op.getType();
37 if (
auto complexType = dyn_cast<ComplexType>(resType))
38 resType = complexType.getElementType();
39 if (!isa<FloatTy>(resType))
42 auto opFunc = dyn_cast_or_null<SymbolOpInterface>(
45 OpBuilder::InsertionGuard guard(rewriter);
47 auto funcTy = FunctionType::get(
48 rewriter.
getContext(), op->getOperandTypes(), op->getResultTypes());
49 opFunc = func::FuncOp::create(rewriter, rewriter.
getUnknownLoc(),
64 using OpRewritePattern<complex::PowOp>::OpRewritePattern;
66 LogicalResult matchAndRewrite(complex::PowOp op,
67 PatternRewriter &rewriter)
const final {
68 Location loc = op.getLoc();
69 auto fastmath = op.getFastmathAttr();
71 complex::LogOp::create(rewriter, loc, op.getLhs(), fastmath);
73 complex::MulOp::create(rewriter, loc, op.getRhs(), logBase, fastmath);
74 Value exp = complex::ExpOp::create(rewriter, loc,
mul, fastmath);
75 rewriter.replaceOp(op, exp);
82 using OpRewritePattern<complex::PowiOp>::OpRewritePattern;
84 LogicalResult matchAndRewrite(complex::PowiOp op,
85 PatternRewriter &rewriter)
const final {
87 Type elementType = complexType.getElementType();
89 Type exponentType = op.getRhs().getType();
90 Type exponentFloatType = elementType;
91 if (
auto shapedType = dyn_cast<ShapedType>(exponentType))
92 exponentFloatType = shapedType.cloneWith(std::nullopt, elementType);
94 Location loc = op.getLoc();
96 arith::SIToFPOp::create(rewriter, loc, exponentFloatType, op.getRhs());
97 Value zeroImag = arith::ConstantOp::create(
98 rewriter, loc, rewriter.getZeroAttr(exponentFloatType));
99 Value exponent = complex::CreateOp::create(
100 rewriter, loc, op.getLhs().getType(), exponentReal, zeroImag);
102 rewriter.replaceOpWithNewOp<complex::PowOp>(op, op.getType(), op.getLhs(),
103 exponent, op.getFastmathAttr());
111 patterns.
add<PowiOpToROCDLLibraryCalls>(patterns.
getContext());
112 patterns.
add<PowOpToROCDLLibraryCalls>(patterns.
getContext());
113 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::AbsOp, Float32Type>>(
115 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::AbsOp, Float64Type>>(
117 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::CosOp, Float32Type>>(
119 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::CosOp, Float64Type>>(
121 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::ExpOp, Float32Type>>(
123 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::ExpOp, Float64Type>>(
125 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::LogOp, Float32Type>>(
127 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::LogOp, Float64Type>>(
129 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::SinOp, Float32Type>>(
131 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::SinOp, Float64Type>>(
133 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::SqrtOp, Float32Type>>(
135 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::SqrtOp, Float64Type>>(
137 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::TanOp, Float32Type>>(
139 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::TanOp, Float64Type>>(
141 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::TanhOp, Float32Type>>(
143 patterns.
add<ComplexOpToROCDLLibraryCalls<complex::TanhOp, Float64Type>>(
148struct ConvertComplexToROCDLLibraryCallsPass
149 :
public impl::ConvertComplexToROCDLLibraryCallsBase<
150 ConvertComplexToROCDLLibraryCallsPass> {
151 void runOnOperation()
override;
155void ConvertComplexToROCDLLibraryCallsPass::runOnOperation() {
156 Operation *op = getOperation();
162 target.addLegalDialect<arith::ArithDialect, func::FuncDialect>();
163 target.addLegalOp<complex::CreateOp, complex::MulOp>();
164 target.addIllegalOp<complex::AbsOp, complex::CosOp, complex::ExpOp,
165 complex::LogOp, complex::PowOp, complex::PowiOp,
166 complex::SinOp, complex::SqrtOp, complex::TanOp,
168 if (
failed(applyPartialConversion(op,
target, std::move(patterns))))
MLIRContext * getContext() const
void setInsertionPointToStart(Block *block)
Sets the insertion point to the start of the specified block.
Region & getRegion(unsigned index)
Returns the region held by this operation at position 'index'.
MLIRContext * getContext() const
RewritePatternSet & add(ConstructorArg &&arg, ConstructorArgs &&...args)
Add an instance of each of the pattern types 'Ts' to the pattern list with the given arguments.
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'.
static Operation * getNearestSymbolTable(Operation *from)
Returns the nearest symbol table from a given operation from.
NestedPattern Op(FilterFunctionType filter=defaultFilterFunction)
Include the generated interface declarations.
void populateComplexToROCDLLibraryCallsConversionPatterns(RewritePatternSet &patterns)
Populate the given list with patterns that convert from Complex to ROCDL calls.
Type getElementTypeOrSelf(Type type)
Return the element type or return the type itself.
OpRewritePattern is a wrapper around RewritePattern that allows for matching and rewriting against an...