@@ -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,50 @@ 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 ;
260
259
261
260
// Create a Clone to move to the linker, which preserves the original
262
261
// 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 ;
266
267
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 (
269
272
*M, std::move (Clone), LM.LinkFlags ,
270
273
[](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
+ });
274
278
});
275
- } else
276
- Err = Linker::linkModules (*M, std::move (Clone), LM.LinkFlags );
279
+ } else
280
+ Err = Linker::linkModules (*M, std::move (Clone), LM.LinkFlags );
277
281
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
+ }
280
302
}
281
303
282
304
return false ; // success
0 commit comments