@@ -57,6 +57,10 @@ using namespace llvm;
57
57
58
58
#define DEBUG_TYPE " codegenaction"
59
59
60
+ namespace llvm {
61
+ extern cl::opt<bool > ClRelinkBuiltinBitcodePostop;
62
+ }
63
+
60
64
namespace clang {
61
65
class BackendConsumer ;
62
66
class ClangDiagnosticHandler final : public DiagnosticHandler {
@@ -251,32 +255,37 @@ bool BackendConsumer::LinkInModules(llvm::Module *M, bool ShouldLinkFiles) {
251
255
}
252
256
253
257
CurLinkModule = LM.Module .get ();
254
-
255
- // TODO: If CloneModule() is updated to support cloning of unmaterialized
256
- // modules, we can remove this
257
258
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
+ };
260
272
261
273
// Create a Clone to move to the linker, which preserves the original
262
274
// 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 ;
277
280
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
+ }
280
289
}
281
290
282
291
return false ; // success
0 commit comments