-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[RFC][flang] Replace special symbols in uniqued global names. #104859
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
//=== CompilerGeneratedNames.cpp - convert special symbols in global names ===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "flang/Optimizer/Dialect/FIRDialect.h" | ||
#include "flang/Optimizer/Dialect/FIROps.h" | ||
#include "flang/Optimizer/Dialect/FIROpsSupport.h" | ||
#include "flang/Optimizer/Support/InternalNames.h" | ||
#include "flang/Optimizer/Transforms/Passes.h" | ||
#include "mlir/IR/Attributes.h" | ||
#include "mlir/IR/SymbolTable.h" | ||
#include "mlir/Pass/Pass.h" | ||
|
||
namespace fir { | ||
#define GEN_PASS_DEF_COMPILERGENERATEDNAMESCONVERSION | ||
#include "flang/Optimizer/Transforms/Passes.h.inc" | ||
} // namespace fir | ||
|
||
using namespace mlir; | ||
|
||
namespace { | ||
|
||
class CompilerGeneratedNamesConversionPass | ||
: public fir::impl::CompilerGeneratedNamesConversionBase< | ||
CompilerGeneratedNamesConversionPass> { | ||
public: | ||
using CompilerGeneratedNamesConversionBase< | ||
CompilerGeneratedNamesConversionPass>:: | ||
CompilerGeneratedNamesConversionBase; | ||
|
||
mlir::ModuleOp getModule() { return getOperation(); } | ||
void runOnOperation() override; | ||
}; | ||
} // namespace | ||
|
||
void CompilerGeneratedNamesConversionPass::runOnOperation() { | ||
auto op = getOperation(); | ||
auto *context = &getContext(); | ||
|
||
llvm::DenseMap<mlir::StringAttr, mlir::FlatSymbolRefAttr> remappings; | ||
for (auto &funcOrGlobal : op->getRegion(0).front()) { | ||
if (llvm::isa<mlir::func::FuncOp>(funcOrGlobal) || | ||
llvm::isa<fir::GlobalOp>(funcOrGlobal)) { | ||
auto symName = funcOrGlobal.getAttrOfType<mlir::StringAttr>( | ||
mlir::SymbolTable::getSymbolAttrName()); | ||
auto deconstructedName = fir::NameUniquer::deconstruct(symName); | ||
if (deconstructedName.first != fir::NameUniquer::NameKind::NOT_UNIQUED && | ||
!fir::NameUniquer::isExternalFacingUniquedName(deconstructedName)) { | ||
std::string newName = | ||
fir::NameUniquer::replaceSpecialSymbols(symName.getValue().str()); | ||
if (newName != symName) { | ||
auto newAttr = mlir::StringAttr::get(context, newName); | ||
mlir::SymbolTable::setSymbolName(&funcOrGlobal, newAttr); | ||
auto newSymRef = mlir::FlatSymbolRefAttr::get(newAttr); | ||
remappings.try_emplace(symName, newSymRef); | ||
} | ||
} | ||
} | ||
} | ||
|
||
if (remappings.empty()) | ||
return; | ||
|
||
// Update all uses of the functions and globals that have been renamed. | ||
op.walk([&remappings](mlir::Operation *nestedOp) { | ||
llvm::SmallVector<std::pair<mlir::StringAttr, mlir::SymbolRefAttr>> updates; | ||
for (const mlir::NamedAttribute &attr : nestedOp->getAttrDictionary()) | ||
if (auto symRef = llvm::dyn_cast<mlir::SymbolRefAttr>(attr.getValue())) | ||
if (auto remap = remappings.find(symRef.getRootReference()); | ||
remap != remappings.end()) | ||
updates.emplace_back(std::pair<mlir::StringAttr, mlir::SymbolRefAttr>{ | ||
attr.getName(), mlir::SymbolRefAttr(remap->second)}); | ||
for (auto update : updates) | ||
nestedOp->setAttr(update.first, update.second); | ||
}); | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IO lowering is producing some global symbols with dots for the purpose of type bound IO that may not be considered unique here I think.
See:
llvm-project/flang/test/Lower/io-derived-type.f90
Line 134 in 34e15ad
llvm-project/flang/lib/Lower/IO.cpp
Line 302 in 34e15ad
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In addition to the
.nonTbpDefinedIoTable
suffix, IO.cpp also has two instances of a.list
suffix.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the pointers, Jean and Val! I would like to address these cases separately.
These names do not look quite from the reserved name space to me, so I guess they may conflict with the user space if I try to replace
.
withX
in them (e.g. when linking with an object file created from a C module). I think I can try to put them into the "Flang's name space", e.g.@_QEdefault.nonTbpDefinedIoTable
or@_QECdefault.nonTbpDefinedIoTable
, then the new pass should work for them and we will be safe from the name conflicts (given that_Q
is reserved).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Solution you suggest makes sense to me, I am OK doing it in separate patch.