|
| 1 | +//===- ConvertComplexPow.cpp - Convert complex.pow to library 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 "flang/Common/static-multimap-view.h" |
| 10 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 11 | +#include "flang/Optimizer/Dialect/FIRDialect.h" |
| 12 | +#include "flang/Optimizer/Transforms/Passes.h" |
| 13 | +#include "flang/Runtime/entry-names.h" |
| 14 | +#include "mlir/Dialect/Arith/IR/Arith.h" |
| 15 | +#include "mlir/Dialect/Complex/IR/Complex.h" |
| 16 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 17 | +#include "mlir/Pass/Pass.h" |
| 18 | + |
| 19 | +namespace fir { |
| 20 | +#define GEN_PASS_DEF_CONVERTCOMPLEXPOW |
| 21 | +#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 22 | +} // namespace fir |
| 23 | + |
| 24 | +using namespace mlir; |
| 25 | + |
| 26 | +namespace { |
| 27 | +class ConvertComplexPowPass |
| 28 | + : public fir::impl::ConvertComplexPowBase<ConvertComplexPowPass> { |
| 29 | +public: |
| 30 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 31 | + registry.insert<fir::FIROpsDialect, complex::ComplexDialect, |
| 32 | + arith::ArithDialect, func::FuncDialect>(); |
| 33 | + } |
| 34 | + void runOnOperation() override; |
| 35 | +}; |
| 36 | +} // namespace |
| 37 | + |
| 38 | +// Helper to declare or get a math library function. |
| 39 | +static func::FuncOp getOrDeclare(fir::FirOpBuilder &builder, Location loc, |
| 40 | + StringRef name, FunctionType type) { |
| 41 | + if (auto func = builder.getNamedFunction(name)) |
| 42 | + return func; |
| 43 | + auto func = builder.createFunction(loc, name, type); |
| 44 | + func->setAttr(fir::getSymbolAttrName(), builder.getStringAttr(name)); |
| 45 | + func->setAttr(fir::FIROpsDialect::getFirRuntimeAttrName(), |
| 46 | + builder.getUnitAttr()); |
| 47 | + return func; |
| 48 | +} |
| 49 | + |
| 50 | +static bool isZero(Value v) { |
| 51 | + if (auto cst = v.getDefiningOp<arith::ConstantOp>()) |
| 52 | + if (auto attr = dyn_cast<FloatAttr>(cst.getValue())) |
| 53 | + return attr.getValue().isZero(); |
| 54 | + return false; |
| 55 | +} |
| 56 | + |
| 57 | +void ConvertComplexPowPass::runOnOperation() { |
| 58 | + auto func = getOperation(); |
| 59 | + auto mod = func->getParentOfType<ModuleOp>(); |
| 60 | + if (fir::getTargetTriple(mod).isAMDGCN()) |
| 61 | + return; |
| 62 | + |
| 63 | + fir::FirOpBuilder builder(func, fir::getKindMapping(mod)); |
| 64 | + |
| 65 | + func.walk([&](complex::PowOp op) { |
| 66 | + builder.setInsertionPoint(op); |
| 67 | + Location loc = op.getLoc(); |
| 68 | + auto complexTy = cast<ComplexType>(op.getType()); |
| 69 | + auto elemTy = complexTy.getElementType(); |
| 70 | + |
| 71 | + Value base = op.getLhs(); |
| 72 | + Value rhs = op.getRhs(); |
| 73 | + |
| 74 | + Value intExp; |
| 75 | + if (auto create = rhs.getDefiningOp<complex::CreateOp>()) { |
| 76 | + if (isZero(create.getImaginary())) { |
| 77 | + if (auto conv = create.getReal().getDefiningOp<fir::ConvertOp>()) { |
| 78 | + if (auto intTy = dyn_cast<IntegerType>(conv.getValue().getType())) |
| 79 | + intExp = conv.getValue(); |
| 80 | + } |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + func::FuncOp callee; |
| 85 | + SmallVector<Value> args; |
| 86 | + if (intExp) { |
| 87 | + unsigned realBits = cast<FloatType>(elemTy).getWidth(); |
| 88 | + unsigned intBits = cast<IntegerType>(intExp.getType()).getWidth(); |
| 89 | + auto funcTy = builder.getFunctionType( |
| 90 | + {complexTy, builder.getIntegerType(intBits)}, {complexTy}); |
| 91 | + if (realBits == 32 && intBits == 32) |
| 92 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(cpowi), funcTy); |
| 93 | + else if (realBits == 32 && intBits == 64) |
| 94 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(cpowk), funcTy); |
| 95 | + else if (realBits == 64 && intBits == 32) |
| 96 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(zpowi), funcTy); |
| 97 | + else if (realBits == 64 && intBits == 64) |
| 98 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(zpowk), funcTy); |
| 99 | + else if (realBits == 128 && intBits == 32) |
| 100 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(cqpowi), funcTy); |
| 101 | + else if (realBits == 128 && intBits == 64) |
| 102 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(cqpowk), funcTy); |
| 103 | + else |
| 104 | + return; |
| 105 | + args = {base, intExp}; |
| 106 | + } else { |
| 107 | + unsigned realBits = cast<FloatType>(elemTy).getWidth(); |
| 108 | + auto funcTy = |
| 109 | + builder.getFunctionType({complexTy, complexTy}, {complexTy}); |
| 110 | + if (realBits == 32) |
| 111 | + callee = getOrDeclare(builder, loc, "cpowf", funcTy); |
| 112 | + else if (realBits == 64) |
| 113 | + callee = getOrDeclare(builder, loc, "cpow", funcTy); |
| 114 | + else if (realBits == 128) |
| 115 | + callee = getOrDeclare(builder, loc, RTNAME_STRING(CPowF128), funcTy); |
| 116 | + else |
| 117 | + return; |
| 118 | + args = {base, rhs}; |
| 119 | + } |
| 120 | + |
| 121 | + auto call = fir::CallOp::create(builder, loc, callee, args); |
| 122 | + op.replaceAllUsesWith(call.getResult(0)); |
| 123 | + op.erase(); |
| 124 | + }); |
| 125 | +} |
0 commit comments