@@ -22,7 +22,7 @@ namespace fir {
22
22
#include " flang/Optimizer/Transforms/Passes.h.inc"
23
23
} // namespace fir
24
24
25
- #define DEBUG_TYPE " flang-constang -argument-globalisation-opt"
25
+ #define DEBUG_TYPE " flang-constant -argument-globalisation-opt"
26
26
27
27
namespace {
28
28
unsigned uniqueLitId = 1 ;
@@ -45,7 +45,7 @@ class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
45
45
bool needUpdate = false ;
46
46
fir::FirOpBuilder builder (rewriter, module );
47
47
llvm::SmallVector<mlir::Value> newOperands;
48
- llvm::SmallVector<mlir::Operation *> toErase ;
48
+ llvm::SmallVector<std::pair< mlir::Operation *, mlir::Operation *>> allocas ;
49
49
for (const mlir::Value &a : callOp.getArgs ()) {
50
50
auto alloca = mlir::dyn_cast_or_null<fir::AllocaOp>(a.getDefiningOp ());
51
51
// We can convert arguments that are alloca, and that has
@@ -74,53 +74,45 @@ class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
74
74
}
75
75
}
76
76
77
- // If we didn't find one signle store, add argument as is, and move on.
77
+ // If we didn't find any store, or multiple stores, add argument as is
78
+ // and move on.
78
79
if (!store) {
79
80
newOperands.push_back (a);
80
81
continue ;
81
82
}
82
83
83
84
LLVM_DEBUG (llvm::dbgs () << " found store " << *store << " \n " );
84
85
85
- mlir::Operation *constant_def = store->getOperand (0 ).getDefiningOp ();
86
- // Expect constant definition operation or force legalisation of the
87
- // callOp and continue with its next argument
88
- if (!mlir::isa<mlir::arith::ConstantOp>(constant_def)) {
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
89
// Unable to remove alloca arg
90
90
newOperands.push_back (a);
91
91
continue ;
92
92
}
93
93
94
- LLVM_DEBUG (llvm::dbgs () << " found define " << *constant_def << " \n " );
94
+ LLVM_DEBUG (llvm::dbgs () << " found define " << *definingOp << " \n " );
95
95
96
96
std::string globalName =
97
97
" _global_const_." + std::to_string (uniqueLitId++);
98
98
assert (!builder.getNamedGlobal (globalName) &&
99
99
" We should have a unique name here" );
100
100
101
- unsigned count = 0 ;
102
- for (mlir::Operation *s : alloca->getUsers ())
103
- if (di.dominates (store, s))
104
- ++count;
105
-
106
- // Delete if dominates itself and one more operation (which should
107
- // be callOp)
108
- if (count == 2 )
109
- toErase.push_back (store);
101
+ allocas.push_back (std::make_pair (alloca, store));
110
102
111
103
auto loc = callOp.getLoc ();
112
104
fir::GlobalOp global = builder.createGlobalConstant (
113
105
loc, varTy, globalName,
114
106
[&](fir::FirOpBuilder &builder) {
115
- mlir::Operation *cln = constant_def ->clone ();
107
+ mlir::Operation *cln = definingOp ->clone ();
116
108
builder.insert (cln);
117
109
mlir::Value val =
118
110
builder.createConvert (loc, varTy, cln->getResult (0 ));
119
111
builder.create <fir::HasValueOp>(loc, val);
120
112
},
121
113
builder.createInternalLinkage ());
122
- mlir::Value addr = { builder.create <fir::AddrOfOp>(
123
- loc, global.resultType (), global.getSymbol ())} ;
114
+ mlir::Value addr = builder.create <fir::AddrOfOp>(
115
+ loc, global.resultType (), global.getSymbol ());
124
116
newOperands.push_back (addr);
125
117
needUpdate = true ;
126
118
}
@@ -139,8 +131,19 @@ class CallOpRewriter : public mlir::OpRewritePattern<fir::CallOp> {
139
131
newOp->setAttrs (callOp->getAttrs ());
140
132
rewriter.replaceOp (callOp, newOp);
141
133
142
- for (auto e : toErase)
143
- rewriter.eraseOp (e);
134
+ for (auto a : allocas) {
135
+ unsigned count = 0 ;
136
+
137
+ for (auto i : a.first ->getUsers ())
138
+ ++count;
139
+
140
+ // If the alloca is only used for a store and the call operand, the
141
+ // store is no longer required.
142
+ if (count == 1 ) {
143
+ rewriter.eraseOp (a.second );
144
+ rewriter.eraseOp (a.first );
145
+ }
146
+ }
144
147
LLVM_DEBUG (llvm::dbgs () << " global constant for " << callOp << " as "
145
148
<< newOp << ' \n ' );
146
149
return mlir::success ();
0 commit comments