Skip to content

Commit 5bfc060

Browse files
committed
[CodeGen] Add conditional to module cloning in bitcode linking
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 4c17452 commit 5bfc060

File tree

2 files changed

+40
-18
lines changed

2 files changed

+40
-18
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: 39 additions & 17 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,50 @@ 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;
260259

261260
// Create a Clone to move to the linker, which preserves the original
262261
// 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);
262+
if (ClRelinkBuiltinBitcodePostop) {
263+
// TODO: If CloneModule() is updated to support cloning of unmaterialized
264+
// modules, we can remove this
265+
if (Error E = CurLinkModule->materializeAll())
266+
return false;
266267

267-
if (LM.Internalize) {
268-
Err = Linker::linkModules(
268+
std::unique_ptr<llvm::Module> Clone = llvm::CloneModule(*LM.Module);
269+
270+
if (LM.Internalize) {
271+
Err = Linker::linkModules(
269272
*M, std::move(Clone), LM.LinkFlags,
270273
[](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+
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
275+
return !GV.hasName() ||
276+
(GVS.count(GV.getName()) == 0);
277+
});
274278
});
275-
} else
276-
Err = Linker::linkModules(*M, std::move(Clone), LM.LinkFlags);
279+
} else
280+
Err = Linker::linkModules(*M, std::move(Clone), LM.LinkFlags);
277281

278-
if (Err)
279-
return true;
282+
if (Err)
283+
return true;
284+
}
285+
// Otherwise we can link (and clean up) the original modules
286+
else {
287+
if (LM.Internalize) {
288+
Err = Linker::linkModules(
289+
*M, std::move(LM.Module), LM.LinkFlags,
290+
[](llvm::Module &M, const llvm::StringSet<> &GVS) {
291+
internalizeModule(M, [&GVS](const llvm::GlobalValue &GV) {
292+
return !GV.hasName() ||
293+
(GVS.count(GV.getName()) == 0);
294+
});
295+
});
296+
} else
297+
Err = Linker::linkModules(*M, std::move(LM.Module), LM.LinkFlags);
298+
299+
if (Err)
300+
return true;
301+
}
280302
}
281303

282304
return false; // success

0 commit comments

Comments
 (0)