diff options
author | Jacob Lambert <jacob.lambert@amd.com> | 2023-11-29 16:44:40 -0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2023-11-29 16:44:40 -0800 |
commit | 8a4b90321f4aee78f70ef164031eb00da6d6b5c9 (patch) | |
tree | 45aa986d4a52f1e0163ebeb35b23e91535747e2e /clang/lib/CodeGen/CodeGenAction.cpp | |
parent | 2323b541e1ae16843b193c26ca3067440d09c413 (diff) | |
download | llvm-8a4b90321f4aee78f70ef164031eb00da6d6b5c9.zip llvm-8a4b90321f4aee78f70ef164031eb00da6d6b5c9.tar.gz llvm-8a4b90321f4aee78f70ef164031eb00da6d6b5c9.tar.bz2 |
[CodeGen] Add conditional to module cloning in bitcode linking (#72478)
Now that we have a commandline option dictating a second link step,
(-relink-builtin-bitcode-postop), we can condition the module creation
when linking in bitcode modules. This aims to improve performance by
avoiding unnecessary linking
Diffstat (limited to 'clang/lib/CodeGen/CodeGenAction.cpp')
-rw-r--r-- | clang/lib/CodeGen/CodeGenAction.cpp | 51 |
1 files changed, 30 insertions, 21 deletions
diff --git a/clang/lib/CodeGen/CodeGenAction.cpp b/clang/lib/CodeGen/CodeGenAction.cpp index a31a271..bb6b1a3 100644 --- a/clang/lib/CodeGen/CodeGenAction.cpp +++ b/clang/lib/CodeGen/CodeGenAction.cpp @@ -57,6 +57,10 @@ using namespace llvm; #define DEBUG_TYPE "codegenaction" +namespace llvm { +extern cl::opt<bool> ClRelinkBuiltinBitcodePostop; +} + namespace clang { class BackendConsumer; class ClangDiagnosticHandler final : public DiagnosticHandler { @@ -251,32 +255,37 @@ bool BackendConsumer::LinkInModules(llvm::Module *M, bool ShouldLinkFiles) { } CurLinkModule = LM.Module.get(); - - // TODO: If CloneModule() is updated to support cloning of unmaterialized - // modules, we can remove this bool Err; - if (Error E = CurLinkModule->materializeAll()) - return false; + + auto DoLink = [&](auto &Mod) { + if (LM.Internalize) { + Err = Linker::linkModules( + *M, std::move(Mod), LM.LinkFlags, + [](llvm::Module &M, const llvm::StringSet<> &GVS) { + internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) { + return !GV.hasName() || (GVS.count(GV.getName()) == 0); + }); + }); + } else + Err = Linker::linkModules(*M, std::move(Mod), LM.LinkFlags); + }; // Create a Clone to move to the linker, which preserves the original // linking modules, allowing them to be linked again in the future - // TODO: Add a ShouldCleanup option to make Cloning optional. When - // set, we can pass the original modules to the linker for cleanup - std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module); - - if (LM.Internalize) { - Err = Linker::linkModules( - *M, std::move(Clone), LM.LinkFlags, - [](llvm::Module &M, const llvm::StringSet<> &GVS) { - internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) { - return !GV.hasName() || (GVS.count(GV.getName()) == 0); - }); - }); - } else - Err = Linker::linkModules(*M, std::move(Clone), LM.LinkFlags); + if (ClRelinkBuiltinBitcodePostop) { + // TODO: If CloneModule() is updated to support cloning of unmaterialized + // modules, we can remove this + if (Error E = CurLinkModule->materializeAll()) + return false; - if (Err) - return true; + std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module); + + DoLink(Clone); + } + // Otherwise we can link (and clean up) the original modules + else { + DoLink(LM.Module); + } } return false; // success |