Skip to content

Commit 476ca33

Browse files
[LTO] Don't apply LTOPostLink module flag during writeMergedModule
For `ld64` which uses legacy LTOCodeGenerator, it relies on writeMergedModule to perform `ld -r` (generates a linked object file). If all the inputs to `ld -r` is fullLTO bitcode, `ld64` will linked the bitcode module, internalize all the symbols and write out another fullLTO bitcode object file. This bitcode file doesn't have all the bitcode inputs and it should not have LTOPostLink module flag. It will also cause error when this bitcode object file is linked with other LTO object file. Fix the issue by not applying LTOPostLink flag during writeMergedModule function. The flag should only be added when all the bitcode are linked and ready to be optimized. rdar://problem/58462798 Reviewed By: tejohnson Differential Revision: https://reviews.llvm.org/D84789
1 parent 6538fff commit 476ca33

File tree

3 files changed

+21
-3
lines changed

3 files changed

+21
-3
lines changed

llvm/lib/LTO/LTOCodeGenerator.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -466,8 +466,6 @@ void LTOCodeGenerator::applyScopeRestrictions() {
466466

467467
internalizeModule(*MergedModule, mustPreserveGV);
468468

469-
MergedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
470-
471469
ScopeRestrictionsDone = true;
472470
}
473471

@@ -559,6 +557,9 @@ bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
559557
// Mark which symbols can not be internalized
560558
this->applyScopeRestrictions();
561559

560+
// Write LTOPostLink flag for passes that require all the modules.
561+
MergedModule->addModuleFlag(Module::Error, "LTOPostLink", 1);
562+
562563
// Instantiate the pass manager to organize the passes.
563564
legacy::PassManager passes;
564565

llvm/test/LTO/ARM/lto-linking-metadata.ll

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
; RUN: opt %s -o %t1.bc
22

3-
; RUN: llvm-lto %t1.bc -o %t1.save.opt -save-merged-module -O1 --exported-symbol=foo
3+
; RUN: llvm-lto %t1.bc -o %t1.save.opt -save-linked-module -save-merged-module -O1 --exported-symbol=foo
44
; RUN: llvm-dis < %t1.save.opt.merged.bc | FileCheck %s
5+
; RUN: llvm-dis < %t1.save.opt.linked.bc | FileCheck %s --check-prefix=CHECK-LINKED
56

67
; RUN: llvm-lto2 run %t1.bc -o %t.out.o -save-temps \
78
; RUN: -r=%t1.bc,foo,pxl
@@ -17,3 +18,6 @@ entry:
1718

1819
; CHECK: !llvm.module.flags = !{[[MD_NUM:![0-9]+]]}
1920
; CHECK: [[MD_NUM]] = !{i32 1, !"LTOPostLink", i32 1}
21+
22+
; CHECK-LINKED: @foo
23+
; CHECK-LINKED-NOT: LTOPostLink

llvm/tools/llvm-lto/llvm-lto.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,10 @@ static cl::opt<std::string> ThinLTOGeneratedObjectsDir(
181181
cl::desc("Save ThinLTO generated object files using filenames created in "
182182
"the given directory."));
183183

184+
static cl::opt<bool> SaveLinkedModuleFile(
185+
"save-linked-module", cl::init(false),
186+
cl::desc("Write linked LTO module to file before optimize"));
187+
184188
static cl::opt<bool>
185189
SaveModuleFile("save-merged-module", cl::init(false),
186190
cl::desc("Write merged LTO module to file before CodeGen"));
@@ -1029,6 +1033,15 @@ int main(int argc, char **argv) {
10291033
CodeGen.setFileType(FT.getValue());
10301034

10311035
if (!OutputFilename.empty()) {
1036+
if (SaveLinkedModuleFile) {
1037+
std::string ModuleFilename = OutputFilename;
1038+
ModuleFilename += ".linked.bc";
1039+
std::string ErrMsg;
1040+
1041+
if (!CodeGen.writeMergedModules(ModuleFilename))
1042+
error("writing linked module failed.");
1043+
}
1044+
10321045
if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
10331046
DisableLTOVectorization)) {
10341047
// Diagnostic messages should have been printed by the handler.

0 commit comments

Comments
 (0)