@@ -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+
6064namespace clang {
6165class BackendConsumer ;
6266class 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