[llvm] 6b539f5 - Reland "[gold] Add preliminary FatLTO support to the Gold plugin""
Paul Kirth via llvm-commits
llvm-commits at lists.llvm.org
Fri Aug 18 15:56:57 PDT 2023
Author: Paul Kirth
Date: 2023-08-18T22:56:48Z
New Revision: 6b539f5eb8ef1d3a3c87873caa2dbd5147e1adbd
URL: https://github.com/llvm/llvm-project/commit/6b539f5eb8ef1d3a3c87873caa2dbd5147e1adbd
DIFF: https://github.com/llvm/llvm-project/commit/6b539f5eb8ef1d3a3c87873caa2dbd5147e1adbd.diff
LOG: Reland "[gold] Add preliminary FatLTO support to the Gold plugin""
This changes the definition if isSectionBitcode to only be valid for the
.llvm.lto section, since this API is only called from LTO, and the
.llvmbc section was not intended to be used for LTO. This allows the
gold plugin to keep its existing behavior without introducing any
significant changes.
Reviewed By: MaskRay, nikic
Differential Revision: https://reviews.llvm.org/D152973
Added:
llvm/test/LTO/X86/Inputs/llvm.lto.section.s
llvm/test/LTO/X86/llvm.lto.section.ll
llvm/test/tools/gold/X86/fatlto/fatlto.invalid.s
llvm/test/tools/gold/X86/fatlto/fatlto.test
Modified:
llvm/docs/BitCodeFormat.rst
llvm/lib/Object/ObjectFile.cpp
llvm/test/LTO/X86/Inputs/bcsection.macho.s
llvm/test/LTO/X86/bcsection.ll
llvm/tools/gold/gold-plugin.cpp
Removed:
################################################################################
diff --git a/llvm/docs/BitCodeFormat.rst b/llvm/docs/BitCodeFormat.rst
index 7bbdc5493f1750..70be73abef19d6 100644
--- a/llvm/docs/BitCodeFormat.rst
+++ b/llvm/docs/BitCodeFormat.rst
@@ -470,13 +470,18 @@ Native Object File Wrapper Format
Bitcode files for LLVM IR may also be wrapped in a native object file
(i.e. ELF, COFF, Mach-O). The bitcode must be stored in a section of the object
-file named ``__LLVM,__bitcode`` for MachO and ``.llvmbc`` for the other object
-formats. This wrapper format is useful for accommodating LTO in compilation
-pipelines where intermediate objects must be native object files which contain
-metadata in other sections.
+file named ``__LLVM,__bitcode`` for MachO or ``.llvmbc`` for the other object
+formats. ELF objects additionally support a ``.llvm.lto`` section for
+:doc:`FatLTO`, which contains bitcode suitable for LTO compilation (i.e. bitcode
+that has gone through a pre-link LTO pipeline). The ``.llvmbc`` section
+predates FatLTO support in LLVM, and may not always contain bitcode that is
+suitable for LTO (i.e. from ``-fembed-bitcode``). The wrapper format is useful
+for accommodating LTO in compilation pipelines where intermediate objects must
+be native object files which contain metadata in other sections.
Not all tools support this format. For example, lld and the gold plugin will
-ignore these sections when linking object files.
+ignore the ``.llvmbc`` section when linking object files, but can use
+``.llvm.lto`` sections when passed the correct command line options.
.. _encoding of LLVM IR:
diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp
index 0820187f32e14c..2a672467f422dc 100644
--- a/llvm/lib/Object/ObjectFile.cpp
+++ b/llvm/lib/Object/ObjectFile.cpp
@@ -79,7 +79,7 @@ uint32_t ObjectFile::getSymbolAlignment(DataRefImpl DRI) const { return 0; }
bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
Expected<StringRef> NameOrErr = getSectionName(Sec);
if (NameOrErr)
- return *NameOrErr == ".llvmbc" || *NameOrErr == ".llvm.lto";
+ return *NameOrErr == ".llvm.lto";
consumeError(NameOrErr.takeError());
return false;
}
diff --git a/llvm/test/LTO/X86/Inputs/bcsection.macho.s b/llvm/test/LTO/X86/Inputs/bcsection.macho.s
index 24884b5a913659..70e3b477102abe 100644
--- a/llvm/test/LTO/X86/Inputs/bcsection.macho.s
+++ b/llvm/test/LTO/X86/Inputs/bcsection.macho.s
@@ -2,4 +2,4 @@
.asciz "Wrong Section"
.section __LLVM,__bitcode
-.incbin "bcsection.bc"
+.incbin "llvm.lto.section.bc"
diff --git a/llvm/test/LTO/X86/Inputs/llvm.lto.section.s b/llvm/test/LTO/X86/Inputs/llvm.lto.section.s
new file mode 100644
index 00000000000000..5457d0ffd6f963
--- /dev/null
+++ b/llvm/test/LTO/X86/Inputs/llvm.lto.section.s
@@ -0,0 +1,2 @@
+.section .llvm.lto
+.incbin "llvm.lto.section.bc"
diff --git a/llvm/test/LTO/X86/bcsection.ll b/llvm/test/LTO/X86/bcsection.ll
index 1c7f3095480355..673853f0a30217 100644
--- a/llvm/test/LTO/X86/bcsection.ll
+++ b/llvm/test/LTO/X86/bcsection.ll
@@ -2,23 +2,17 @@
; RUN: llvm-as -o %t/bcsection.bc %s
; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-pc-win32 -o %t/bcsection.coff.bco %p/Inputs/bcsection.s
-; RUN: llvm-nm %t/bcsection.coff.bco | FileCheck %s
-; RUN: llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/bcsection.coff.o %t/bcsection.coff.bco
-; RUN: llvm-nm %t/bcsection.coff.o | FileCheck %s
+; RUN: llvm-nm %t/bcsection.coff.bco | FileCheck %s --allow-empty
+; RUN: not llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/bcsection.coff.o %t/bcsection.coff.bco
; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-unknown-linux-gnu -o %t/bcsection.elf.bco %p/Inputs/bcsection.s
-; RUN: llvm-nm %t/bcsection.elf.bco | FileCheck %s
-; RUN: llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/bcsection.elf.o %t/bcsection.elf.bco
-; RUN: llvm-nm %t/bcsection.elf.o | FileCheck %s
-
-; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-apple-darwin11 -o %t/bcsection.macho.bco %p/Inputs/bcsection.macho.s
-; RUN: llvm-nm %t/bcsection.macho.bco | FileCheck %s
-; RUN: llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/bcsection.macho.o %t/bcsection.macho.bco
-; RUN: llvm-nm %t/bcsection.macho.o | FileCheck %s
+; RUN: llvm-nm %t/bcsection.elf.bco | FileCheck %s --allow-empty
+; RUN: not llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/bcsection.elf.o %t/bcsection.elf.bco
target triple = "x86_64-unknown-linux-gnu"
-; CHECK: main
+;; The .llvmbc section is not intended for use with LTO, so there should be nothing here
+; CHECK-NOT: main
define i32 @main() {
ret i32 0
}
diff --git a/llvm/test/LTO/X86/llvm.lto.section.ll b/llvm/test/LTO/X86/llvm.lto.section.ll
new file mode 100644
index 00000000000000..33d0bdb6e74527
--- /dev/null
+++ b/llvm/test/LTO/X86/llvm.lto.section.ll
@@ -0,0 +1,25 @@
+; RUN: rm -rf %t && mkdir -p %t
+; RUN: llvm-as -o %t/llvm.lto.section.bc %s
+
+; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-pc-win32 -o %t/llvm.lto.section.coff.bco %p/Inputs/llvm.lto.section.s
+; RUN: llvm-nm %t/llvm.lto.section.coff.bco | FileCheck %s
+; RUN: llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/llvm.lto.section.coff.o %t/llvm.lto.section.coff.bco
+; RUN: llvm-nm %t/llvm.lto.section.coff.o | FileCheck %s
+
+; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-unknown-linux-gnu -o %t/llvm.lto.section.elf.bco %p/Inputs/llvm.lto.section.s
+; RUN: llvm-nm %t/llvm.lto.section.elf.bco | FileCheck %s
+; RUN: llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/llvm.lto.section.elf.o %t/llvm.lto.section.elf.bco
+; RUN: llvm-nm %t/llvm.lto.section.elf.o | FileCheck %s
+
+
+; RUN: llvm-mc -I=%t -filetype=obj -triple=x86_64-apple-darwin11 -o %t/bcsection.macho.bco %p/Inputs/bcsection.macho.s
+; RUN: llvm-nm %t/bcsection.macho.bco | FileCheck %s
+; RUN: llvm-lto -exported-symbol=main -exported-symbol=_main -o %t/bcsection.macho.o %t/bcsection.macho.bco
+; RUN: llvm-nm %t/bcsection.macho.o | FileCheck %s
+
+target triple = "x86_64-unknown-linux-gnu"
+
+; CHECK: main
+define i32 @main() {
+ ret i32 0
+}
diff --git a/llvm/test/tools/gold/X86/fatlto/fatlto.invalid.s b/llvm/test/tools/gold/X86/fatlto/fatlto.invalid.s
new file mode 100644
index 00000000000000..28714ebf0c96aa
--- /dev/null
+++ b/llvm/test/tools/gold/X86/fatlto/fatlto.invalid.s
@@ -0,0 +1,11 @@
+# REQUIRES: x86_64-linux
+
+# RUN: llvm-mc -filetype=obj -triple=x86_64-unknown-linux %s -o %t
+# RUN: not %gold -plugin %llvmshlibdir/LLVMgold%shlibext %t -o /dev/null 2>&1 | FileCheck %s
+
+# CHECK: error:{{.*}} Invalid bitcode signature
+
+.section .llvm.lto,"e", at progbits
+.Lllvm.embedded.object:
+ .asciz "BC\300\3365\000"
+ .size .Lllvm.embedded.object, 12
diff --git a/llvm/test/tools/gold/X86/fatlto/fatlto.test b/llvm/test/tools/gold/X86/fatlto/fatlto.test
new file mode 100644
index 00000000000000..339f2de87b2c04
--- /dev/null
+++ b/llvm/test/tools/gold/X86/fatlto/fatlto.test
@@ -0,0 +1,109 @@
+;; Basic FatLTO tests.
+; REQUIRES: x86_64-linux
+
+; RUN: rm -rf %t && split-file %s %t
+
+;; Ensure that input files contain .llvm.lto section
+; RUN: llc %t/a-LTO.ll --filetype=obj -o %t/a-fatLTO.o
+; RUN: opt --module-summary %t/a-LTO.ll -o %t/a-fatLTO.bc
+; RUN: llvm-objcopy --add-section=.llvm.lto=%t/a-fatLTO.bc %t/a-fatLTO.o
+; RUN: llvm-objcopy --set-section-flags=.llvm.lto=readonly,exclude %t/a-fatLTO.o
+; RUN: llvm-readobj -S %t/a-fatLTO.o | FileCheck --check-prefix=CHECK-A %s
+
+; CHECK-A: Name: .llvm.lto
+
+; RUN: llc %t/main-LTO.ll --filetype=obj -o %t/main-fatLTO.o
+; RUN: opt --module-summary %t/main-LTO.ll -o %t/main-fatLTO.bc
+; RUN: llvm-objcopy --add-section=.llvm.lto=%t/main-fatLTO.bc %t/main-fatLTO.o
+; RUN: llvm-objcopy --set-section-flags=.llvm.lto=readonly,exclude %t/main-fatLTO.o
+; RUN: llvm-readobj -S %t/main-fatLTO.o | FileCheck --check-prefix=CHECK-MAIN %s
+
+; CHECK-MAIN: Name: .llvm.lto
+
+;; Final executable should not have .llvm.lto section no matter what the target is
+; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext -o %t/foo-fatLTO %t/a-fatLTO.o %t/main-fatLTO.o
+; RUN: llvm-readobj -S %t/foo-fatLTO | FileCheck --check-prefix=CHECK-LTO-TARGET %s
+
+;; Check that fat objects work w/ s=--start-lib
+; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext -o %t/foo-fatLTO.start_lib --start-lib %t/a-fatLTO.o %t/main-fatLTO.o --end-lib
+; RUN: llvm-readobj -S %t/foo-fatLTO.start_lib | FileCheck --check-prefix=CHECK-LTO-TARGET %s
+
+;; Check if .llvm.lto section gets aggregated in LTO target
+; CHECK-LTO-TARGET-NOT: Name: .llvm.lto
+
+;; Final executable should not have .llvm.lto section no matter what the target is
+; RUN: %gold -o %t/foo-fatNoLTO %t/a-fatLTO.o %/t/main-fatLTO.o
+; RUN: llvm-readobj -S %t/foo-fatNoLTO | FileCheck --check-prefix=CHECK-NON-LTO-TARGET %s
+
+;; Check if .llvm.lto section gets aggregated in non-LTO target
+; CHECK-NON-LTO-TARGET-NOT: Name: .llvm.lto
+
+;; Check if the LTO target executable produced from FatLTO object file is
+;; identical to the one produced from LTO modules
+; RUN: opt --module-summary %t/a-LTO.ll -o %t/a-LTO.bc
+; RUN: opt --module-summary %t/main-LTO.ll -o %t/main-LTO.bc
+; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext -o %t/foo-LTO %t/a-LTO.bc %t/main-LTO.bc
+; RUN: cmp %t/foo-fatLTO %t/foo-LTO
+
+;; Check if the no-LTO target executable produced from FatLTO object file is
+;; identical to the one produced from regular object files
+
+; RUN: llc %t/a-LTO.ll --filetype=obj -o %t/a.o
+; RUN: llc %t/main-LTO.ll --filetype=obj -o %t/main.o
+
+; RUN: %gold -o %t/foo-noLTO %t/a.o %t/main.o
+; RUN: cmp %t/foo-fatNoLTO %t/foo-noLTO
+
+;; Check archive support
+; RUN: llvm-ar rcs %t/a.a %t/a-fatLTO.o
+; RUN: %gold -plugin %llvmshlibdir/LLVMgold%shlibext -o %t/foo-fatLTO.archive %t/main-LTO.bc %t/a.a
+; RUN: cmp %t/foo-fatLTO.archive %t/foo-LTO
+
+;--- a-LTO.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @_start() #0 {
+entry:
+ ret i32 0
+}
+
+attributes #0 = { noinline nounwind uwtable }
+
+!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{i32 7, !"frame-pointer", i32 2}
+!5 = !{i32 1, !"ThinLTO", i32 0}
+!6 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
+
+;--- main-LTO.ll
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; Function Attrs: noinline nounwind uwtable
+define dso_local i32 @main() #0 {
+entry:
+ %retval = alloca i32, align 4
+ store i32 0, ptr %retval, align 4
+ %call = call i32 (...) @_start()
+ ret i32 %call
+}
+
+declare i32 @_start(...)
+
+attributes #0 = { noinline nounwind uwtable }
+
+!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{i32 7, !"PIC Level", i32 2}
+!2 = !{i32 7, !"PIE Level", i32 2}
+!3 = !{i32 7, !"uwtable", i32 2}
+!4 = !{i32 7, !"frame-pointer", i32 2}
+!5 = !{i32 1, !"ThinLTO", i32 0}
+!6 = !{i32 1, !"EnableSplitLTOUnit", i32 1}
diff --git a/llvm/tools/gold/gold-plugin.cpp b/llvm/tools/gold/gold-plugin.cpp
index a47dba35aa472e..59bd3f6e668600 100644
--- a/llvm/tools/gold/gold-plugin.cpp
+++ b/llvm/tools/gold/gold-plugin.cpp
@@ -538,14 +538,6 @@ static ld_plugin_status claim_file_hook(const ld_plugin_input_file *file,
BufferRef = Buffer->getMemBufferRef();
}
- // Only use bitcode files for LTO. InputFile::create() will load bitcode
- // from the .llvmbc section within a binary object, this bitcode is typically
- // generated by -fembed-bitcode and is not to be used by LLVMgold.so for LTO.
- if (identify_magic(BufferRef.getBuffer()) != file_magic::bitcode) {
- *claimed = 0;
- return LDPS_OK;
- }
-
*claimed = 1;
Expected<std::unique_ptr<InputFile>> ObjOrErr = InputFile::create(BufferRef);
More information about the llvm-commits
mailing list