diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -196,6 +196,7 @@ uint64_t> callGraphProfile; bool allowMultipleDefinition; + bool fatLTOObjects; bool androidPackDynRelocs = false; bool armHasBlx = false; bool armHasMovtMovw = false; diff --git a/lld/ELF/Driver.cpp b/lld/ELF/Driver.cpp --- a/lld/ELF/Driver.cpp +++ b/lld/ELF/Driver.cpp @@ -52,6 +52,7 @@ #include "llvm/Config/llvm-config.h" #include "llvm/LTO/LTO.h" #include "llvm/Object/Archive.h" +#include "llvm/Object/IRObjectFile.h" #include "llvm/Remarks/HotnessThresholdParser.h" #include "llvm/Support/CommandLine.h" #include "llvm/Support/Compression.h" @@ -308,6 +309,14 @@ files.push_back(make(mbref, "", 0, inLib)); break; case file_magic::elf_relocatable: + if (config->fatLTOObjects) { + Expected fatLTOData = + IRObjectFile::findBitcodeInMemBuffer(mbref); + if (!errorToBool(fatLTOData.takeError())) { + files.push_back(make(*fatLTOData, "", 0, inLib)); + break; + } + } files.push_back(createObjFile(mbref, "", inLib)); break; default: @@ -1091,6 +1100,8 @@ args.hasFlag(OPT_android_memtag_heap, OPT_no_android_memtag_heap, false); config->androidMemtagStack = args.hasFlag(OPT_android_memtag_stack, OPT_no_android_memtag_stack, false); + config->fatLTOObjects = + args.hasFlag(OPT_fatlto_objects, OPT_no_fatlto_objects, false); config->androidMemtagMode = getMemtagMode(args); config->auxiliaryList = args::getStrings(args, OPT_auxiliary); if (opt::Arg *arg = @@ -2758,6 +2769,11 @@ for (InputSectionBase *s : f->getSections()) { if (!s || s == &InputSection::discarded) continue; + + // Do not aggregate the .llvm.lto section + if (s->name == ".llvm.lto") + continue; + if (LLVM_UNLIKELY(isa(s))) ctx.ehInputSections.push_back(cast(s)); else diff --git a/lld/ELF/Options.td b/lld/ELF/Options.td --- a/lld/ELF/Options.td +++ b/lld/ELF/Options.td @@ -619,6 +619,10 @@ def thinlto_single_module_eq: JJ<"thinlto-single-module=">, HelpText<"Specify a single module to compile in ThinLTO mode, for debugging only">; +defm fatlto_objects: B<"fat-lto-objects", + "Use the embedded bitcode in the .llvm.lto section of the object file", + "Use the assembly in the object file (default)">; + def: J<"plugin-opt=O">, Alias, HelpText<"Alias for --lto-O">; def: F<"plugin-opt=debug-pass-manager">, Alias, HelpText<"Alias for --lto-debug-pass-manager">; diff --git a/lld/docs/ReleaseNotes.rst b/lld/docs/ReleaseNotes.rst --- a/lld/docs/ReleaseNotes.rst +++ b/lld/docs/ReleaseNotes.rst @@ -26,6 +26,9 @@ ELF Improvements ---------------- +- Support LTO with LVLM FatLTO objects by adding the `-fat-lto-objects` flag. + Without `-fat-lto-objects` LLD will link Fat LTO objects without LTO. + Breaking changes ---------------- diff --git a/lld/test/ELF/fatlto/Inputs/a-LTO.ll b/lld/test/ELF/fatlto/Inputs/a-LTO.ll new file mode 100644 --- /dev/null +++ b/lld/test/ELF/fatlto/Inputs/a-LTO.ll @@ -0,0 +1,29 @@ +; ModuleID = 'a-LTO.bc' +source_filename = "a.c" +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 @a() #0 { +entry: + ret i32 0 +} + +attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6} +!llvm.ident = !{!7} + +!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} +!7 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 2288ebbec148ce8000fc2c1f63a4033cd847669e)"} + +^0 = module: (path: "a-LTO.bc", hash: (0, 0, 0, 0, 0)) +^1 = gv: (name: "a", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 1, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 1, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0)))) ; guid = 12157170054180749580 +^2 = flags: 8 +^3 = blockcount: 1 diff --git a/lld/test/ELF/fatlto/Inputs/main-LTO.ll b/lld/test/ELF/fatlto/Inputs/main-LTO.ll new file mode 100644 --- /dev/null +++ b/lld/test/ELF/fatlto/Inputs/main-LTO.ll @@ -0,0 +1,36 @@ +; ModuleID = 'main-LTO.bc' +source_filename = "main.c" +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 (...) @a() + ret i32 %call +} + +declare i32 @a(...) #1 + +attributes #0 = { noinline nounwind uwtable "frame-pointer"="all" "min-legal-vector-width"="0" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } +attributes #1 = { "frame-pointer"="all" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "tune-cpu"="generic" } + +!llvm.module.flags = !{!0, !1, !2, !3, !4, !5, !6} +!llvm.ident = !{!7} + +!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} +!7 = !{!"clang version 15.0.0 (https://github.com/llvm/llvm-project.git 2288ebbec148ce8000fc2c1f63a4033cd847669e)"} + +^0 = module: (path: "main-LTO.bc", hash: (0, 0, 0, 0, 0)) +^1 = gv: (name: "a") ; guid = 12157170054180749580 +^2 = gv: (name: "main", summaries: (function: (module: ^0, flags: (linkage: external, visibility: default, notEligibleToImport: 1, live: 0, dsoLocal: 1, canAutoHide: 0), insts: 4, funcFlags: (readNone: 0, readOnly: 0, noRecurse: 0, returnDoesNotAlias: 0, noInline: 1, alwaysInline: 0, noUnwind: 1, mayThrow: 0, hasUnknownCall: 0, mustBeUnreachable: 0), calls: ((callee: ^1))))) ; guid = 15822663052811949562 +^3 = flags: 8 +^4 = blockcount: 1 diff --git a/lld/test/ELF/fatlto/fatlto.test b/lld/test/ELF/fatlto/fatlto.test new file mode 100644 --- /dev/null +++ b/lld/test/ELF/fatlto/fatlto.test @@ -0,0 +1,55 @@ +;; Basic FatLTO tests. +; REQUIRES: x86 + +;; Clean up and initialize test dir +; RUN: rm -rf %t +; RUN: mkdir -p %t + +;; Ensure that input files contain .llvm.lto section +; RUN: llc %p/Inputs/a-LTO.ll --filetype=obj -o %t/a-fatLTO.o +; RUN: llvm-as %p/Inputs/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-readobj -S %t/a-fatLTO.o | FileCheck --check-prefix=CHECK-A %s + +; CHECK-A: Name: .llvm.lto + +; RUN: llc %p/Inputs/main-LTO.ll --filetype=obj -o %t/main-fatLTO.o +; RUN: llvm-as %p/Inputs/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-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: ld.lld -o %t/foo-fatLTO %t/a-fatLTO.o %t/main-fatLTO.o -fat-lto-objects +; RUN: llvm-readobj -S %t/foo-fatLTO | 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: ld.lld -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: llvm-as %p/Inputs/a-LTO.ll -o %t/a-LTO.bc +; RUN: llvm-as %p/Inputs/main-LTO.ll -o %t/main-LTO.bc +; RUN: ld.lld -o %t/foo-LTO %t/a-LTO.bc %t/main-LTO.bc +; RUN: obj2yaml %t/foo-fatLTO > %t/foo-fatLTO.yaml +; RUN: obj2yaml %t/foo-LTO > %t/foo-LTO.yaml +; RUN: diff %t/foo-fatLTO.yaml %t/foo-LTO.yaml + +;; Check if the no-LTO target executable produced from FatLTO object file is +;; identical to the one produced from regular object files + +; RUN: llc %p/Inputs/a-LTO.ll --filetype=obj -o %t/a.o +; RUN: llc %p/Inputs/main-LTO.ll --filetype=obj -o %t/main.o + +; RUN: ld.lld -o %t/foo-noLTO %t/a.o %t/main.o +; RUN: obj2yaml %t/foo-fatNoLTO > %t/foo-fatNoLTO.yaml +; RUN: obj2yaml %t/foo-noLTO > %t/foo-noLTO.yaml +; RUN: diff %t/foo-fatNoLTO.yaml %t/foo-noLTO.yaml