Skip to content

Commit 7d86fde

Browse files
committed
[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 Change-Id: Icc3c897b2452c48f9250ee203b36e2afd5d69932
1 parent 6031fae commit 7d86fde

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
@@ -101,7 +101,7 @@ static cl::opt<bool> ClSanitizeOnOptimizerEarlyEP(
101101
cl::desc("Insert sanitizers on OptimizerEarlyEP."), cl::init(false));
102102

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

clang/lib/CodeGen/CodeGenAction.cpp

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

5959
#define DEBUG_TYPE "codegenaction"
6060

61+
namespace llvm {
62+
extern cl::opt<bool> ClRelinkBuiltinBitcodePostop;
63+
}
64+
6165
namespace clang {
6266
class BackendConsumer;
6367
class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -252,32 +256,37 @@ bool BackendConsumer::LinkInModules(llvm::Module *M, bool ShouldLinkFiles) {
252256
}
253257

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

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

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

283292
return false; // success

0 commit comments

Comments
 (0)