|
| 1 | +//=== ComplexToROCDLLibraryCalls.cpp - convert from Complex to ROCDL calls ===// |
| 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/ComplexToROCDLLibraryCalls/ComplexToROCDLLibraryCalls.h" |
| 10 | +#include "mlir/Dialect/Complex/IR/Complex.h" |
| 11 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 12 | +#include "mlir/IR/PatternMatch.h" |
| 13 | +#include "mlir/Transforms/DialectConversion.h" |
| 14 | + |
| 15 | +namespace mlir { |
| 16 | +#define GEN_PASS_DEF_CONVERTCOMPLEXTOROCDLLIBRARYCALLS |
| 17 | +#include "mlir/Conversion/Passes.h.inc" |
| 18 | +} // namespace mlir |
| 19 | + |
| 20 | +using namespace mlir; |
| 21 | + |
| 22 | +namespace { |
| 23 | + |
| 24 | +template <typename Op, typename FloatTy> |
| 25 | +// Pattern to convert Complex ops to ROCDL function calls. |
| 26 | +struct ComplexOpToROCDLLibraryCalls : public OpRewritePattern<Op> { |
| 27 | + using OpRewritePattern<Op>::OpRewritePattern; |
| 28 | + ComplexOpToROCDLLibraryCalls(MLIRContext *context, StringRef funcName, |
| 29 | + PatternBenefit benefit = 1) |
| 30 | + : OpRewritePattern<Op>(context, benefit), funcName(funcName) {} |
| 31 | + |
| 32 | + LogicalResult matchAndRewrite(Op op, PatternRewriter &rewriter) const final { |
| 33 | + Operation *symTable = SymbolTable::getNearestSymbolTable(op); |
| 34 | + Type resType = op.getType(); |
| 35 | + if (auto complexType = dyn_cast<ComplexType>(resType)) |
| 36 | + resType = complexType.getElementType(); |
| 37 | + if (!isa<FloatTy>(resType)) |
| 38 | + return failure(); |
| 39 | + |
| 40 | + auto opFunc = dyn_cast_or_null<SymbolOpInterface>( |
| 41 | + SymbolTable::lookupSymbolIn(symTable, funcName)); |
| 42 | + if (!opFunc) { |
| 43 | + OpBuilder::InsertionGuard guard(rewriter); |
| 44 | + rewriter.setInsertionPointToStart(&symTable->getRegion(0).front()); |
| 45 | + auto funcTy = FunctionType::get( |
| 46 | + rewriter.getContext(), op->getOperandTypes(), op->getResultTypes()); |
| 47 | + opFunc = rewriter.create<func::FuncOp>(rewriter.getUnknownLoc(), funcName, |
| 48 | + funcTy); |
| 49 | + opFunc.setPrivate(); |
| 50 | + } |
| 51 | + rewriter.replaceOpWithNewOp<func::CallOp>(op, funcName, op.getType(), |
| 52 | + op->getOperands()); |
| 53 | + return success(); |
| 54 | + } |
| 55 | + |
| 56 | +private: |
| 57 | + std::string funcName; |
| 58 | +}; |
| 59 | +} // namespace |
| 60 | + |
| 61 | +void mlir::populateComplexToROCDLLibraryCallsConversionPatterns( |
| 62 | + RewritePatternSet &patterns) { |
| 63 | + patterns.add<ComplexOpToROCDLLibraryCalls<complex::AbsOp, Float32Type>>( |
| 64 | + patterns.getContext(), "__ocml_cabs_f32"); |
| 65 | + patterns.add<ComplexOpToROCDLLibraryCalls<complex::AbsOp, Float64Type>>( |
| 66 | + patterns.getContext(), "__ocml_cabs_f64"); |
| 67 | + patterns.add<ComplexOpToROCDLLibraryCalls<complex::ExpOp, Float32Type>>( |
| 68 | + patterns.getContext(), "__ocml_cexp_f32"); |
| 69 | + patterns.add<ComplexOpToROCDLLibraryCalls<complex::ExpOp, Float64Type>>( |
| 70 | + patterns.getContext(), "__ocml_cexp_f64"); |
| 71 | +} |
| 72 | + |
| 73 | +namespace { |
| 74 | +struct ConvertComplexToROCDLLibraryCallsPass |
| 75 | + : public impl::ConvertComplexToROCDLLibraryCallsBase< |
| 76 | + ConvertComplexToROCDLLibraryCallsPass> { |
| 77 | + void runOnOperation() override; |
| 78 | +}; |
| 79 | +} // namespace |
| 80 | + |
| 81 | +void ConvertComplexToROCDLLibraryCallsPass::runOnOperation() { |
| 82 | + Operation *op = getOperation(); |
| 83 | + |
| 84 | + RewritePatternSet patterns(&getContext()); |
| 85 | + populateComplexToROCDLLibraryCallsConversionPatterns(patterns); |
| 86 | + |
| 87 | + ConversionTarget target(getContext()); |
| 88 | + target.addLegalDialect<func::FuncDialect>(); |
| 89 | + target.addIllegalOp<complex::AbsOp, complex::ExpOp>(); |
| 90 | + if (failed(applyPartialConversion(op, target, std::move(patterns)))) |
| 91 | + signalPassFailure(); |
| 92 | +} |
0 commit comments