Skip to content

Commit 8a4b903

Browse files
authored
[CodeGen] Add conditional to module cloning in bitcode linking (llvm#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
1 parent 2323b54 commit 8a4b903

File tree

2 files changed

+31
-22
lines changed

2 files changed

+31
-22
lines changed

clang/lib/CodeGen/BackendUtil.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
103103
cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
104104

105105
// Re-link builtin bitcodes after optimization
106-
static cl::opt<bool> ClRelinkBuiltinBitcodePostop(
106+
cl::opt<bool> ClRelinkBuiltinBitcodePostop(
107107
"relink-builtin-bitcode-postop", cl::Optional,
108108
cl::desc("Re-link builtin bitcodes after optimization."), cl::init(false));
109109
}

clang/lib/CodeGen/CodeGenAction.cpp

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ using namespace llvm;
5757

5858
#define DEBUG_TYPE "codegenaction"
5959

60+
namespace llvm {
61+
extern cl::opt<bool> ClRelinkBuiltinBitcodePostop;
62+
}
63+
6064
namespace clang {
6165
class BackendConsumer;
6266
class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -251,32 +255,37 @@ bool BackendConsumer::LinkInModules(llvm::Module *M, bool ShouldLinkFiles) {
251255
}
252256

253257
CurLinkModule = LM.Module.get();
254-
255-
// TODO: If CloneModule() is updated to support cloning of unmaterialized
256-
// modules, we can remove this
257258
bool Err;
258-
if (Error E = CurLinkModule->materializeAll())
259-
return false;
259+
260+
auto DoLink = [&](auto &Mod) {
261+
if (LM.Internalize) {
262+
Err = Linker::linkModules(
263+
*M, std::move(Mod), LM.LinkFlags,
264+
[](llvm::Module &M, const llvm::StringSet<> &GVS) {
265+
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
266+
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
267+
});
268+
});
269+
} else
270+
Err = Linker::linkModules(*M, std::move(Mod), LM.LinkFlags);
271+
};
260272

261273
// Create a Clone to move to the linker, which preserves the original
262274
// linking modules, allowing them to be linked again in the future
263-
// TODO: Add a ShouldCleanup option to make Cloning optional. When
264-
// set, we can pass the original modules to the linker for cleanup
265-
std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module);
266-
267-
if (LM.Internalize) {
268-
Err = Linker::linkModules(
269-
*M, std::move(Clone), LM.LinkFlags,
270-
[](llvm::Module &M, const llvm::StringSet<> &GVS) {
271-
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
272-
return !GV.hasName() || (GVS.count(GV.getName()) == 0);
273-
});
274-
});
275-
} else
276-
Err = Linker::linkModules(*M, std::move(Clone), LM.LinkFlags);
275+
if (ClRelinkBuiltinBitcodePostop) {
276+
// TODO: If CloneModule() is updated to support cloning of unmaterialized
277+
// modules, we can remove this
278+
if (Error E = CurLinkModule->materializeAll())
279+
return false;
277280

278-
if (Err)
279-
return true;
281+
std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module);
282+
283+
DoLink(Clone);
284+
}
285+
// Otherwise we can link (and clean up) the original modules
286+
else {
287+
DoLink(LM.Module);
288+
}
280289
}
281290

282291
return false; // success

0 commit comments

Comments
 (0)