Debian Codegraph @

flang/lib/Optimizer/Transforms/ConstantArgumentGlobalisation.cpp

raw
1 //===- ConstantArgumentGlobalisation.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/FIRBuilder.h"
10 #include "flang/Optimizer/Dialect/FIRDialect.h"
11 #include "flang/Optimizer/Dialect/FIROps.h"
12 #include "flang/Optimizer/Dialect/FIRType.h"
13 #include "flang/Optimizer/Transforms/Passes.h"
14 #include "mlir/Dialect/Func/IR/FuncOps.h"
15 #include "mlir/IR/Diagnostics.h"
16 #include "mlir/IR/Dominance.h"
17 #include "mlir/Pass/Pass.h"
18 #include "mlir/Transforms/GreedyPatternRewriteDriver.h"
19
20 namespace fir {
21 #define GEN_PASS_DEF_CONSTANTARGUMENTGLOBALISATIONOPT
22 #include "flang/Optimizer/Transforms/Passes.h.inc"
23 } // namespace fir
24
25 #define DEBUG_TYPE "flang-constant-argument-globalisation-opt"
26
27 namespace {
28 unsigned uniqueLitId = 1;
29
30 class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
31 protected:
32 const mlir::DominanceInfo &di;
33
34 public:
35 using OpRewritePattern::OpRewritePattern;
36
37 CallOpRewriter(mlir::MLIRContext *ctx, const mlir::DominanceInfo &_di)
38 : OpRewritePattern(ctx), di(_di) {}
39
40 llvm::LogicalResult
41 matchAndRewrite(fir::CallOp callOp,
42 mlir::PatternRewriter &rewriter) const override {
43 LLVM_DEBUG(llvm::dbgs() << "Processing call op: " << callOp << "\n");
44 auto module = callOp->getParentOfType<mlir::ModuleOp>();
45 bool needUpdate = false;
46 fir::FirOpBuilder builder(rewriter, module);
47 llvm::SmallVector<mlir::Value> newOperands;
48 llvm::SmallVector<std::pair<mlir::Operation *, mlir::Operation *>> allocas;
49 for (const mlir::Value &a : callOp.getArgs()) {
50 auto alloca = mlir::dyn_cast_or_null<fir::AllocaOp>(a.getDefiningOp());
51 // We can convert arguments that are alloca, and that has
52 // the value by reference attribute. All else is just added
53 // to the argument list.
54 if (!alloca || !alloca->hasAttr(fir::getAdaptToByRefAttrName())) {
55 newOperands.push_back(a);
56 continue;
57 }
58
59 mlir::Type varTy = alloca.getInType();
60 assert(!fir::hasDynamicSize(varTy) &&
61 "only expect statically sized scalars to be by value");
62
63 // Find immediate store with const argument
64 mlir::Operation *store = nullptr;
65 for (mlir::Operation *s : alloca->getUsers()) {
66 if (mlir::isa<fir::StoreOp>(s) && di.dominates(s, callOp)) {
67 // We can only deal with ONE store - if already found one,
68 // set to nullptr and exit the loop.
69 if (store) {
70 store = nullptr;
71 break;
72 }
73 store = s;
74 }
75 }
76
77 // If we didn't find any store, or multiple stores, add argument as is
78 // and move on.
79 if (!store) {
80 newOperands.push_back(a);
81 continue;
82 }
83
84 LLVM_DEBUG(llvm::dbgs() << " found store " << *store << "\n");
85
86 mlir::Operation *definingOp = store->getOperand(0).getDefiningOp();
87 // If not a constant, add to operands and move on.
88 if (!mlir::isa<mlir::arith::ConstantOp>(definingOp)) {
89 // Unable to remove alloca arg
90 newOperands.push_back(a);
91 continue;
92 }
93
94 LLVM_DEBUG(llvm::dbgs() << " found define " << *definingOp << "\n");
95
96 std::string globalName =
97 "_global_const_." + std::to_string(uniqueLitId++);
98 assert(!builder.getNamedGlobal(globalName) &&
99 "We should have a unique name here");
100
101 if (std::find_if(allocas.begin(), allocas.end(), [alloca](auto x) {
102 return x.first == alloca;
103 }) == allocas.end()) {
104 allocas.push_back(std::make_pair(alloca, store));
105 }
106
107 auto loc = callOp.getLoc();
108 fir::GlobalOp global = builder.createGlobalConstant(
109 loc, varTy, globalName,
110 [&](fir::FirOpBuilder &builder) {
111 mlir::Operation *cln = definingOp->clone();
112 builder.insert(cln);
113 mlir::Value val =
114 builder.createConvert(loc, varTy, cln->getResult(0));
115 builder.create<fir::HasValueOp>(loc, val);
116 },
117 builder.createInternalLinkage());
118 mlir::Value addr = builder.create<fir::AddrOfOp>(loc, global.resultType(),
119 global.getSymbol());
120 newOperands.push_back(addr);
121 needUpdate = true;
122 }
123
124 if (needUpdate) {
125 auto loc = callOp.getLoc();
126 llvm::SmallVector<mlir::Type> newResultTypes;
127 newResultTypes.append(callOp.getResultTypes().begin(),
128 callOp.getResultTypes().end());
129 fir::CallOp newOp = builder.create<fir::CallOp>(
130 loc, newResultTypes,
131 callOp.getCallee().has_value() ? callOp.getCallee().value()
132 : mlir::SymbolRefAttr{},
133 newOperands);
134 // Copy all the attributes from the old to new op.
135 newOp->setAttrs(callOp->getAttrs());
136 rewriter.replaceOp(callOp, newOp);
137
138 for (auto a : allocas) {
139 if (a.first->hasOneUse()) {
140 // If the alloca is only used for a store and the call operand, the
141 // store is no longer required.
142 rewriter.eraseOp(a.second);
143 rewriter.eraseOp(a.first);
144 }
145 }
146 LLVM_DEBUG(llvm::dbgs() << "global constant for " << callOp << " as "
147 << newOp << '\n');
148 return mlir::success();
149 }
150
151 // Failure here just means "we couldn't do the conversion", which is
152 // perfectly acceptable to the upper layers of this function.
153 return mlir::failure();
154 }
155 };
156
157 // this pass attempts to convert immediate scalar literals in function calls
158 // to global constants to allow transformations such as Dead Argument
159 // Elimination
160 class ConstantArgumentGlobalisationOpt
161 : public fir::impl::ConstantArgumentGlobalisationOptBase<
162 ConstantArgumentGlobalisationOpt> {
163 public:
164 ConstantArgumentGlobalisationOpt() = default;
165
166 void runOnOperation() override {
167 mlir::ModuleOp mod = getOperation();
168 mlir::DominanceInfo *di = &getAnalysis<mlir::DominanceInfo>();
169 auto *context = &getContext();
170 mlir::RewritePatternSet patterns(context);
171 mlir::GreedyRewriteConfig config;
172 config.enableRegionSimplification =
173 mlir::GreedySimplifyRegionLevel::Disabled;
174 config.strictMode = mlir::GreedyRewriteStrictness::ExistingOps;
175
176 patterns.insert<CallOpRewriter>(context, *di);
177 if (mlir::failed(mlir::applyPatternsAndFoldGreedily(
178 mod, std::move(patterns), config))) {
179 mlir::emitError(mod.getLoc(),
180 "error in constant globalisation optimization\n");
181 signalPassFailure();
182 }
183 }
184 };
185 } // namespace