|
| 1 | +//===-- ComplexToROCDL.cpp - conversion 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/ComplexToROCDL/ComplexToROCDL.h" |
| 10 | + |
| 11 | +#include "mlir/Dialect/Complex/IR/Complex.h" |
| 12 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 13 | +#include "mlir/IR/PatternMatch.h" |
| 14 | +#include "mlir/Transforms/DialectConversion.h" |
| 15 | +#include <optional> |
| 16 | + |
| 17 | +namespace mlir { |
| 18 | +#define GEN_PASS_DEF_CONVERTCOMPLEXTOROCDL |
| 19 | +#include "mlir/Conversion/Passes.h.inc" |
| 20 | +} // namespace mlir |
| 21 | + |
| 22 | +using namespace mlir; |
| 23 | + |
| 24 | +namespace { |
| 25 | +struct FloatTypeResolver { |
| 26 | + std::optional<bool> operator()(Type type) const { |
| 27 | + auto elementType = cast<FloatType>(type); |
| 28 | + if (!isa<Float32Type, Float64Type>(elementType)) |
| 29 | + return {}; |
| 30 | + return elementType.getIntOrFloatBitWidth() == 64; |
| 31 | + } |
| 32 | +}; |
| 33 | + |
| 34 | +template <typename Op, typename TypeResolver = FloatTypeResolver> |
| 35 | +struct ScalarOpToROCDLCall : public OpRewritePattern<Op> { |
| 36 | + using OpRewritePattern<Op>::OpRewritePattern; |
| 37 | + ScalarOpToROCDLCall(MLIRContext *context, StringRef floatFunc, |
| 38 | + StringRef doubleFunc, PatternBenefit benefit) |
| 39 | + : OpRewritePattern<Op>(context, benefit), floatFunc(floatFunc), |
| 40 | + doubleFunc(doubleFunc) {} |
| 41 | + |
| 42 | + LogicalResult matchAndRewrite(Op op, PatternRewriter &rewriter) const final { |
| 43 | + auto module = SymbolTable::getNearestSymbolTable(op); |
| 44 | + auto isDouble = TypeResolver()(op.getType()); |
| 45 | + if (!isDouble.has_value()) |
| 46 | + return failure(); |
| 47 | + |
| 48 | + auto name = *isDouble ? doubleFunc : floatFunc; |
| 49 | + |
| 50 | + auto opFunc = dyn_cast_or_null<SymbolOpInterface>( |
| 51 | + SymbolTable::lookupSymbolIn(module, name)); |
| 52 | + if (!opFunc) { |
| 53 | + OpBuilder::InsertionGuard guard(rewriter); |
| 54 | + rewriter.setInsertionPointToStart(&module->getRegion(0).front()); |
| 55 | + auto funcTy = FunctionType::get( |
| 56 | + rewriter.getContext(), op->getOperandTypes(), op->getResultTypes()); |
| 57 | + opFunc = |
| 58 | + rewriter.create<func::FuncOp>(rewriter.getUnknownLoc(), name, funcTy); |
| 59 | + opFunc.setPrivate(); |
| 60 | + } |
| 61 | + rewriter.replaceOpWithNewOp<func::CallOp>(op, name, op.getType(), |
| 62 | + op->getOperands()); |
| 63 | + return success(); |
| 64 | + } |
| 65 | + |
| 66 | +private: |
| 67 | + std::string floatFunc, doubleFunc; |
| 68 | +}; |
| 69 | +} // namespace |
| 70 | + |
| 71 | +void mlir::populateComplexToROCDLConversionPatterns(RewritePatternSet &patterns, |
| 72 | + PatternBenefit benefit) { |
| 73 | + patterns.add<ScalarOpToROCDLCall<complex::AbsOp>>( |
| 74 | + patterns.getContext(), "__ocml_cabs_f32", "__ocml_cabs_f64", benefit); |
| 75 | +} |
| 76 | + |
| 77 | +namespace { |
| 78 | +struct ConvertComplexToROCDLPass |
| 79 | + : public impl::ConvertComplexToROCDLBase<ConvertComplexToROCDLPass> { |
| 80 | + void runOnOperation() override; |
| 81 | +}; |
| 82 | +} // namespace |
| 83 | + |
| 84 | +void ConvertComplexToROCDLPass::runOnOperation() { |
| 85 | + auto module = getOperation(); |
| 86 | + |
| 87 | + RewritePatternSet patterns(&getContext()); |
| 88 | + populateComplexToROCDLConversionPatterns(patterns, /*benefit=*/1); |
| 89 | + |
| 90 | + ConversionTarget target(getContext()); |
| 91 | + target.addLegalDialect<func::FuncDialect>(); |
| 92 | + target.addIllegalOp<complex::AbsOp>(); |
| 93 | + if (failed(applyPartialConversion(module, target, std::move(patterns)))) |
| 94 | + signalPassFailure(); |
| 95 | +} |
0 commit comments