|
| 1 | +//===- ConstExtruder.cpp -----------------------------------------------===// |
| 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/Optimizer/Builder/BoxValue.h" |
| 10 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 11 | +#include "flang/Optimizer/Dialect/FIRDialect.h" |
| 12 | +#include "flang/Optimizer/Dialect/FIROps.h" |
| 13 | +#include "flang/Optimizer/Dialect/FIRType.h" |
| 14 | +#include "flang/Optimizer/Transforms/Passes.h" |
| 15 | +#include "mlir/Dialect/Func/IR/FuncOps.h" |
| 16 | +#include "mlir/IR/Diagnostics.h" |
| 17 | +#include "mlir/IR/Dominance.h" |
| 18 | +#include "mlir/Pass/Pass.h" |
| 19 | +#include "mlir/Transforms/DialectConversion.h" |
| 20 | +#include "mlir/Transforms/Passes.h" |
| 21 | +#include "llvm/ADT/TypeSwitch.h" |
| 22 | +#include <atomic> |
| 23 | + |
| 24 | +namespace fir { |
| 25 | +#define GEN_PASS_DEF_CONSTEXTRUDEROPT |
| 26 | +#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 27 | +} // namespace fir |
| 28 | + |
| 29 | +#define DEBUG_TYPE "flang-const-extruder-opt" |
| 30 | + |
| 31 | +namespace { |
| 32 | +std::atomic<int> uniqueLitId = 1; |
| 33 | + |
| 34 | +static bool needsExtrusion(const mlir::Value *a) { |
| 35 | + if (!a || !a->getDefiningOp()) |
| 36 | + return false; |
| 37 | + |
| 38 | + // is alloca |
| 39 | + if (auto alloca = mlir::dyn_cast_or_null<fir::AllocaOp>(a->getDefiningOp())) { |
| 40 | + // alloca has annotation |
| 41 | + if (alloca->hasAttr(fir::getAdaptToByRefAttrName())) { |
| 42 | + for (mlir::Operation *s : alloca.getOperation()->getUsers()) { |
| 43 | + if (const auto store = mlir::dyn_cast_or_null<fir::StoreOp>(s)) { |
| 44 | + auto constant_def = store->getOperand(0).getDefiningOp(); |
| 45 | + // Expect constant definition operation |
| 46 | + if (mlir::isa<mlir::arith::ConstantOp>(constant_def)) { |
| 47 | + return true; |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } |
| 52 | + } |
| 53 | + return false; |
| 54 | +} |
| 55 | + |
| 56 | +class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> { |
| 57 | +protected: |
| 58 | + mlir::DominanceInfo &di; |
| 59 | + |
| 60 | +public: |
| 61 | + using OpRewritePattern::OpRewritePattern; |
| 62 | + |
| 63 | + CallOpRewriter(mlir::MLIRContext *ctx, mlir::DominanceInfo &_di) |
| 64 | + : OpRewritePattern(ctx), di(_di) {} |
| 65 | + |
| 66 | + mlir::LogicalResult |
| 67 | + matchAndRewrite(fir::CallOp callOp, |
| 68 | + mlir::PatternRewriter &rewriter) const override { |
| 69 | + LLVM_DEBUG(llvm::dbgs() << "Processing call op: " << callOp << "\n"); |
| 70 | + auto module = callOp->getParentOfType<mlir::ModuleOp>(); |
| 71 | + fir::FirOpBuilder builder(rewriter, module); |
| 72 | + llvm::SmallVector<mlir::Value> newOperands; |
| 73 | + llvm::SmallVector<mlir::Operation *> toErase; |
| 74 | + for (const auto &a : callOp.getArgs()) { |
| 75 | + if (auto alloca = |
| 76 | + mlir::dyn_cast_or_null<fir::AllocaOp>(a.getDefiningOp())) { |
| 77 | + if (needsExtrusion(&a)) { |
| 78 | + |
| 79 | + mlir::Type varTy = alloca.getInType(); |
| 80 | + assert(!fir::hasDynamicSize(varTy) && |
| 81 | + "only expect statically sized scalars to be by value"); |
| 82 | + |
| 83 | + // find immediate store with const argument |
| 84 | + llvm::SmallVector<mlir::Operation *> stores; |
| 85 | + for (mlir::Operation *s : alloca.getOperation()->getUsers()) |
| 86 | + if (mlir::isa<fir::StoreOp>(s) && di.dominates(s, callOp)) |
| 87 | + stores.push_back(s); |
| 88 | + assert(stores.size() == 1 && "expected exactly one store"); |
| 89 | + LLVM_DEBUG(llvm::dbgs() << " found store " << *stores[0] << "\n"); |
| 90 | + |
| 91 | + auto constant_def = stores[0]->getOperand(0).getDefiningOp(); |
| 92 | + // Expect constant definition operation or force legalisation of the |
| 93 | + // callOp and continue with its next argument |
| 94 | + if (!mlir::isa<mlir::arith::ConstantOp>(constant_def)) { |
| 95 | + // unable to remove alloca arg |
| 96 | + newOperands.push_back(a); |
| 97 | + continue; |
| 98 | + } |
| 99 | + |
| 100 | + LLVM_DEBUG(llvm::dbgs() << " found define " << *constant_def << "\n"); |
| 101 | + |
| 102 | + auto loc = callOp.getLoc(); |
| 103 | + llvm::StringRef globalPrefix = "_extruded_"; |
| 104 | + |
| 105 | + std::string globalName; |
| 106 | + while (!globalName.length() || builder.getNamedGlobal(globalName)) |
| 107 | + globalName = |
| 108 | + globalPrefix.str() + "." + std::to_string(uniqueLitId++); |
| 109 | + |
| 110 | + if (alloca->hasOneUse()) { |
| 111 | + toErase.push_back(alloca); |
| 112 | + toErase.push_back(stores[0]); |
| 113 | + } else { |
| 114 | + int count = -2; |
| 115 | + for (mlir::Operation *s : alloca.getOperation()->getUsers()) |
| 116 | + if (di.dominates(stores[0], s)) |
| 117 | + ++count; |
| 118 | + |
| 119 | + // delete if dominates itself and one more operation (which should |
| 120 | + // be callOp) |
| 121 | + if (!count) |
| 122 | + toErase.push_back(stores[0]); |
| 123 | + } |
| 124 | + auto global = builder.createGlobalConstant( |
| 125 | + loc, varTy, globalName, |
| 126 | + [&](fir::FirOpBuilder &builder) { |
| 127 | + mlir::Operation *cln = constant_def->clone(); |
| 128 | + builder.insert(cln); |
| 129 | + fir::ExtendedValue exv{cln->getResult(0)}; |
| 130 | + mlir::Value valBase = fir::getBase(exv); |
| 131 | + mlir::Value val = builder.createConvert(loc, varTy, valBase); |
| 132 | + builder.create<fir::HasValueOp>(loc, val); |
| 133 | + }, |
| 134 | + builder.createInternalLinkage()); |
| 135 | + mlir::Value ope = {builder.create<fir::AddrOfOp>( |
| 136 | + loc, global.resultType(), global.getSymbol())}; |
| 137 | + newOperands.push_back(ope); |
| 138 | + } else { |
| 139 | + // alloca but without attr, add it |
| 140 | + newOperands.push_back(a); |
| 141 | + } |
| 142 | + } else { |
| 143 | + // non-alloca operand, add it |
| 144 | + newOperands.push_back(a); |
| 145 | + } |
| 146 | + } |
| 147 | + |
| 148 | + auto loc = callOp.getLoc(); |
| 149 | + llvm::SmallVector<mlir::Type> newResultTypes; |
| 150 | + newResultTypes.append(callOp.getResultTypes().begin(), |
| 151 | + callOp.getResultTypes().end()); |
| 152 | + fir::CallOp newOp = builder.create<fir::CallOp>( |
| 153 | + loc, newResultTypes, |
| 154 | + callOp.getCallee().has_value() ? callOp.getCallee().value() |
| 155 | + : mlir::SymbolRefAttr{}, |
| 156 | + newOperands, callOp.getFastmathAttr()); |
| 157 | + rewriter.replaceOp(callOp, newOp); |
| 158 | + |
| 159 | + for (auto e : toErase) |
| 160 | + rewriter.eraseOp(e); |
| 161 | + |
| 162 | + LLVM_DEBUG(llvm::dbgs() << "extruded constant for " << callOp << " as " |
| 163 | + << newOp << '\n'); |
| 164 | + return mlir::success(); |
| 165 | + } |
| 166 | +}; |
| 167 | + |
| 168 | +// This pass attempts to convert immediate scalar literals in function calls |
| 169 | +// to global constants to allow transformations as Dead Argument Elimination |
| 170 | +class ConstExtruderOpt |
| 171 | + : public fir::impl::ConstExtruderOptBase<ConstExtruderOpt> { |
| 172 | +protected: |
| 173 | + mlir::DominanceInfo *di; |
| 174 | + |
| 175 | +public: |
| 176 | + ConstExtruderOpt() {} |
| 177 | + |
| 178 | + void runOnOperation() override { |
| 179 | + mlir::ModuleOp mod = getOperation(); |
| 180 | + di = &getAnalysis<mlir::DominanceInfo>(); |
| 181 | + mod.walk([this](mlir::func::FuncOp func) { runOnFunc(func); }); |
| 182 | + } |
| 183 | + |
| 184 | + void runOnFunc(mlir::func::FuncOp &func) { |
| 185 | + auto *context = &getContext(); |
| 186 | + mlir::RewritePatternSet patterns(context); |
| 187 | + mlir::ConversionTarget target(*context); |
| 188 | + |
| 189 | + // If func is a declaration, skip it. |
| 190 | + if (func.empty()) |
| 191 | + return; |
| 192 | + |
| 193 | + target.addLegalDialect<fir::FIROpsDialect, mlir::arith::ArithDialect, |
| 194 | + mlir::func::FuncDialect>(); |
| 195 | + target.addDynamicallyLegalOp<fir::CallOp>([&](fir::CallOp op) { |
| 196 | + for (auto a : op.getArgs()) { |
| 197 | + if (needsExtrusion(&a)) |
| 198 | + return false; |
| 199 | + } |
| 200 | + return true; |
| 201 | + }); |
| 202 | + |
| 203 | + patterns.insert<CallOpRewriter>(context, *di); |
| 204 | + if (mlir::failed( |
| 205 | + mlir::applyPartialConversion(func, target, std::move(patterns)))) { |
| 206 | + mlir::emitError(func.getLoc(), |
| 207 | + "error in constant extrusion optimization\n"); |
| 208 | + signalPassFailure(); |
| 209 | + } |
| 210 | + } |
| 211 | +}; |
| 212 | +} // namespace |
| 213 | + |
| 214 | +std::unique_ptr<mlir::Pass> fir::createConstExtruderPass() { |
| 215 | + return std::make_unique<ConstExtruderOpt>(); |
| 216 | +} |
0 commit comments