Skip to content

Commit 2d14ea6

Browse files
authored
[flang][NFC] speed-up external name conversion pass (#86814)
The ExternalNameConversion pass can be surprisingly slow on big programs. On an example with a 50kloc Fortran file with about 10000 calls to external procedures, the pass alone took 25s on my machine. This patch reduces this to 0.16s. The root cause is that using `replaceAllSymbolUses` on each modified FuncOp is very expensive: it is walking all operations and attribute every time. An alternative would be to use mlir::SymbolUserMap to avoid walking the module again and again, but this is still much more expensive than what is needed because it is essentially caching all symbol uses of the module, and there is no need to such caching here. Instead: - Do a shallow walk of the module (only top level operation) to detect FuncOp/GlobalOp that needs to be updated. Update them and place the name remapping in a DenseMap. - If any remapping were done, do a single deep walk of the module operation, and update any SymbolRefAttr that matches a name that was remapped.
1 parent fa8dc36 commit 2d14ea6

File tree

1 file changed

+34
-121
lines changed

1 file changed

+34
-121
lines changed

flang/lib/Optimizer/Transforms/ExternalNameConversion.cpp

Lines changed: 34 additions & 121 deletions
Original file line numberDiff line numberDiff line change
@@ -12,13 +12,9 @@
1212
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
1313
#include "flang/Optimizer/Support/InternalNames.h"
1414
#include "flang/Optimizer/Transforms/Passes.h"
15-
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
16-
#include "mlir/Dialect/OpenACC/OpenACC.h"
17-
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
1815
#include "mlir/IR/Attributes.h"
1916
#include "mlir/IR/SymbolTable.h"
2017
#include "mlir/Pass/Pass.h"
21-
#include "mlir/Transforms/DialectConversion.h"
2218

2319
namespace fir {
2420
#define GEN_PASS_DEF_EXTERNALNAMECONVERSION
@@ -44,102 +40,8 @@ mangleExternalName(const std::pair<fir::NameUniquer::NameKind,
4440
appendUnderscore);
4541
}
4642

47-
//===----------------------------------------------------------------------===//
48-
// Rewrite patterns
49-
//===----------------------------------------------------------------------===//
50-
5143
namespace {
5244

53-
struct MangleNameOnFuncOp : public mlir::OpRewritePattern<mlir::func::FuncOp> {
54-
public:
55-
using OpRewritePattern::OpRewritePattern;
56-
57-
MangleNameOnFuncOp(mlir::MLIRContext *ctx, bool appendUnderscore)
58-
: mlir::OpRewritePattern<mlir::func::FuncOp>(ctx),
59-
appendUnderscore(appendUnderscore) {}
60-
61-
mlir::LogicalResult
62-
matchAndRewrite(mlir::func::FuncOp op,
63-
mlir::PatternRewriter &rewriter) const override {
64-
mlir::LogicalResult ret = success();
65-
rewriter.startOpModification(op);
66-
llvm::StringRef oldName = op.getSymName();
67-
auto result = fir::NameUniquer::deconstruct(oldName);
68-
if (fir::NameUniquer::isExternalFacingUniquedName(result)) {
69-
auto newSymbol =
70-
rewriter.getStringAttr(mangleExternalName(result, appendUnderscore));
71-
72-
// Try to update all SymbolRef's in the module that match the current op
73-
if (mlir::ModuleOp mod = op->getParentOfType<mlir::ModuleOp>())
74-
ret = op.replaceAllSymbolUses(newSymbol, mod);
75-
76-
op.setSymNameAttr(newSymbol);
77-
mlir::SymbolTable::setSymbolName(op, newSymbol);
78-
79-
op->setAttr(fir::getInternalFuncNameAttrName(),
80-
mlir::StringAttr::get(op->getContext(), oldName));
81-
}
82-
rewriter.finalizeOpModification(op);
83-
return ret;
84-
}
85-
86-
private:
87-
bool appendUnderscore;
88-
};
89-
90-
struct MangleNameForCommonBlock : public mlir::OpRewritePattern<fir::GlobalOp> {
91-
public:
92-
using OpRewritePattern::OpRewritePattern;
93-
94-
MangleNameForCommonBlock(mlir::MLIRContext *ctx, bool appendUnderscore)
95-
: mlir::OpRewritePattern<fir::GlobalOp>(ctx),
96-
appendUnderscore(appendUnderscore) {}
97-
98-
mlir::LogicalResult
99-
matchAndRewrite(fir::GlobalOp op,
100-
mlir::PatternRewriter &rewriter) const override {
101-
rewriter.startOpModification(op);
102-
auto result = fir::NameUniquer::deconstruct(
103-
op.getSymref().getRootReference().getValue());
104-
if (fir::NameUniquer::isExternalFacingUniquedName(result)) {
105-
auto newName = mangleExternalName(result, appendUnderscore);
106-
op.setSymrefAttr(mlir::SymbolRefAttr::get(op.getContext(), newName));
107-
SymbolTable::setSymbolName(op, newName);
108-
}
109-
rewriter.finalizeOpModification(op);
110-
return success();
111-
}
112-
113-
private:
114-
bool appendUnderscore;
115-
};
116-
117-
struct MangleNameOnAddrOfOp : public mlir::OpRewritePattern<fir::AddrOfOp> {
118-
public:
119-
using OpRewritePattern::OpRewritePattern;
120-
121-
MangleNameOnAddrOfOp(mlir::MLIRContext *ctx, bool appendUnderscore)
122-
: mlir::OpRewritePattern<fir::AddrOfOp>(ctx),
123-
appendUnderscore(appendUnderscore) {}
124-
125-
mlir::LogicalResult
126-
matchAndRewrite(fir::AddrOfOp op,
127-
mlir::PatternRewriter &rewriter) const override {
128-
auto result = fir::NameUniquer::deconstruct(
129-
op.getSymbol().getRootReference().getValue());
130-
if (fir::NameUniquer::isExternalFacingUniquedName(result)) {
131-
auto newName = SymbolRefAttr::get(
132-
op.getContext(), mangleExternalName(result, appendUnderscore));
133-
rewriter.replaceOpWithNewOp<fir::AddrOfOp>(op, op.getResTy().getType(),
134-
newName);
135-
}
136-
return success();
137-
}
138-
139-
private:
140-
bool appendUnderscore;
141-
};
142-
14345
class ExternalNameConversionPass
14446
: public fir::impl::ExternalNameConversionBase<ExternalNameConversionPass> {
14547
public:
@@ -162,31 +64,42 @@ void ExternalNameConversionPass::runOnOperation() {
16264
auto *context = &getContext();
16365

16466
appendUnderscores = (usePassOpt) ? appendUnderscoreOpt : appendUnderscores;
67+
llvm::DenseMap<mlir::StringAttr, mlir::FlatSymbolRefAttr> remappings;
68+
// Update names of external Fortran functions and names of Common Block
69+
// globals.
70+
for (auto &funcOrGlobal : op->getRegion(0).front()) {
71+
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal) ||
72+
llvm::isa<fir::GlobalOp>(funcOrGlobal)) {
73+
auto symName = funcOrGlobal.getAttrOfType<mlir::StringAttr>(
74+
mlir::SymbolTable::getSymbolAttrName());
75+
auto deconstructedName = fir::NameUniquer::deconstruct(symName);
76+
if (fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) {
77+
auto newName = mangleExternalName(deconstructedName, appendUnderscores);
78+
auto newAttr = mlir::StringAttr::get(context, newName);
79+
mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr);
80+
auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr);
81+
remappings.try_emplace(symName, newSymRef);
82+
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal))
83+
funcOrGlobal.setAttr(fir::getInternalFuncNameAttrName(), symName);
84+
}
85+
}
86+
}
16587

166-
mlir::RewritePatternSet patterns(context);
167-
patterns.insert<MangleNameOnFuncOp, MangleNameForCommonBlock,
168-
MangleNameOnAddrOfOp>(context, appendUnderscores);
169-
170-
ConversionTarget target(*context);
171-
target.addLegalDialect<fir::FIROpsDialect, LLVM::LLVMDialect,
172-
acc::OpenACCDialect, omp::OpenMPDialect>();
173-
174-
target.addDynamicallyLegalOp<mlir::func::FuncOp>([](mlir::func::FuncOp op) {
175-
return !fir::NameUniquer::needExternalNameMangling(op.getSymName());
176-
});
177-
178-
target.addDynamicallyLegalOp<fir::GlobalOp>([](fir::GlobalOp op) {
179-
return !fir::NameUniquer::needExternalNameMangling(
180-
op.getSymref().getRootReference().getValue());
181-
});
182-
183-
target.addDynamicallyLegalOp<fir::AddrOfOp>([](fir::AddrOfOp op) {
184-
return !fir::NameUniquer::needExternalNameMangling(
185-
op.getSymbol().getRootReference().getValue());
88+
if (remappings.empty())
89+
return;
90+
91+
// Update all uses of the functions and globals that have been renamed.
92+
op.walk([&remappings](mlir::Operation *nestedOp) {
93+
llvm::SmallVector<std::pair<mlir::StringAttr, mlir::SymbolRefAttr>> updates;
94+
for (const mlir::NamedAttribute &attr : nestedOp->getAttrDictionary())
95+
if (auto symRef = llvm::dyn_cast<mlir::SymbolRefAttr>(attr.getValue()))
96+
if (auto remap = remappings.find(symRef.getRootReference());
97+
remap != remappings.end())
98+
updates.emplace_back(std::pair<mlir::StringAttr, mlir::SymbolRefAttr>{
99+
attr.getName(), mlir::SymbolRefAttr(remap->second)});
100+
for (auto update : updates)
101+
nestedOp->setAttr(update.first, update.second);
186102
});
187-
188-
if (failed(applyPartialConversion(op, target, std::move(patterns))))
189-
signalPassFailure();
190103
}
191104

192105
std::unique_ptr<mlir::Pass> fir::createExternalNameConversionPass() {

0 commit comments

Comments
 (0)