paulkirth updated this revision to Diff 499913.
paulkirth retitled this revision from "[WIP][Do NOT review] LLD related changes 
for -ffat-lto-objects support" to "[clang][llvm][lld] FatLTO Prototype".
paulkirth edited the summary of this revision.
paulkirth added a comment.

Rebase revision. Abandon parent revision on unified lto.

- Small changes to the LLD implementation were required.

Note: I've only rebased this change so far, I've made no attempt to improve its 
quality.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D131618/new/

https://reviews.llvm.org/D131618

Files:
  clang/include/clang/Basic/CodeGenOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/BackendUtil.cpp
  clang/lib/Driver/Driver.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/test/CodeGen/embed-lto-fatlto.c
  clang/test/Driver/clang_f_opts.c
  clang/test/Driver/fatlto-objects.c
  lld/ELF/Config.h
  lld/ELF/Driver.cpp
  lld/ELF/Options.td
  lld/test/ELF/fatlto/Inputs/a-LTO.ll
  lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml
  lld/test/ELF/fatlto/Inputs/a.c
  lld/test/ELF/fatlto/Inputs/a.h
  lld/test/ELF/fatlto/Inputs/a.yaml
  lld/test/ELF/fatlto/Inputs/main-LTO.ll
  lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml
  lld/test/ELF/fatlto/Inputs/main.c
  lld/test/ELF/fatlto/Inputs/main.yaml
  lld/test/ELF/fatlto/fatlto.test
  llvm/include/llvm/Bitcode/EmbedBitcodePass.h
  llvm/lib/Bitcode/Writer/CMakeLists.txt
  llvm/lib/Bitcode/Writer/EmbedBitcodePass.cpp
  llvm/lib/Object/ObjectFile.cpp
  llvm/lib/Passes/PassBuilder.cpp
  llvm/lib/Passes/PassRegistry.def
  llvm/test/Bitcode/embed-multiple.ll
  llvm/test/Bitcode/embed-unsupported-object-format.ll
  llvm/test/Bitcode/embed.ll

Index: llvm/test/Bitcode/embed.ll
===================================================================
--- /dev/null
+++ llvm/test/Bitcode/embed.ll
@@ -0,0 +1,7 @@
+; RUN: opt --mtriple x86_64-unknown-linux-gnu < %s -passes=embed-bitcode -S | FileCheck %s
+
+@a = global i32 1
+
+; CHECK: @a = global i32 1
+; CHECK: @llvm.embedded.module = private constant {{.*}}, section ".llvm.lto", align 1
+; CHECK: @llvm.compiler.used = appending global [1 x ptr] [ptr @llvm.embedded.module], section "llvm.metadata"
Index: llvm/test/Bitcode/embed-unsupported-object-format.ll
===================================================================
--- /dev/null
+++ llvm/test/Bitcode/embed-unsupported-object-format.ll
@@ -0,0 +1,5 @@
+; RUN: not --crash opt --mtriple powerpc64-unknown-aix < %s -passes=embed-bitcode -S 2>&1 | FileCheck %s
+
+@a = global i32 1
+
+; CHECK: LLVM ERROR: Embed bitcode pass currently only supports ELF object format
Index: llvm/test/Bitcode/embed-multiple.ll
===================================================================
--- /dev/null
+++ llvm/test/Bitcode/embed-multiple.ll
@@ -0,0 +1,6 @@
+; RUN: not --crash opt --mtriple x86_64-unknown-linux-gnu < %s -passes=embed-bitcode -S 2>&1 | FileCheck %s
+
+@a = global i32 1
+@llvm.embedded.module = private constant [4 x i8] c"BC\C0\DE"
+
+; CHECK: LLVM ERROR: Can only embed the module once.
Index: llvm/lib/Passes/PassRegistry.def
===================================================================
--- llvm/lib/Passes/PassRegistry.def
+++ llvm/lib/Passes/PassRegistry.def
@@ -58,6 +58,7 @@
 MODULE_PASS("dot-callgraph", CallGraphDOTPrinterPass())
 MODULE_PASS("elim-avail-extern", EliminateAvailableExternallyPass())
 MODULE_PASS("extract-blocks", BlockExtractorPass({}, false))
+MODULE_PASS("embed-bitcode", EmbedBitcodePass())
 MODULE_PASS("forceattrs", ForceFunctionAttrsPass())
 MODULE_PASS("function-import", FunctionImportPass())
 MODULE_PASS("globaldce", GlobalDCEPass())
Index: llvm/lib/Passes/PassBuilder.cpp
===================================================================
--- llvm/lib/Passes/PassBuilder.cpp
+++ llvm/lib/Passes/PassBuilder.cpp
@@ -73,6 +73,7 @@
 #include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/Analysis/TypeBasedAliasAnalysis.h"
 #include "llvm/Analysis/UniformityAnalysis.h"
+#include "llvm/Bitcode/EmbedBitcodePass.h"
 #include "llvm/CodeGen/HardwareLoops.h"
 #include "llvm/CodeGen/TypePromotion.h"
 #include "llvm/IR/DebugInfo.h"
Index: llvm/lib/Object/ObjectFile.cpp
===================================================================
--- llvm/lib/Object/ObjectFile.cpp
+++ llvm/lib/Object/ObjectFile.cpp
@@ -79,7 +79,7 @@
 bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const {
   Expected<StringRef> NameOrErr = getSectionName(Sec);
   if (NameOrErr)
-    return *NameOrErr == ".llvmbc";
+    return *NameOrErr == ".llvmbc" || *NameOrErr == ".llvm.lto";
   consumeError(NameOrErr.takeError());
   return false;
 }
Index: llvm/lib/Bitcode/Writer/EmbedBitcodePass.cpp
===================================================================
--- /dev/null
+++ llvm/lib/Bitcode/Writer/EmbedBitcodePass.cpp
@@ -0,0 +1,52 @@
+//===- EmbedBitcodePass.cpp - Pass that embeds the bitcode into a global---===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// EmbedBitcodePass implementation.
+//
+//===----------------------------------------------------------------------===//
+//
+#include "llvm/Bitcode/EmbedBitcodePass.h"
+
+#include "llvm/Bitcode/BitcodeWriter.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/PassManager.h"
+#include "llvm/InitializePasses.h"
+#include "llvm/Pass.h"
+#include "llvm/Support/ErrorHandling.h"
+#include "llvm/TargetParser/Triple.h"
+#include "llvm/Transforms/Utils/ModuleUtils.h"
+
+using namespace llvm;
+
+PreservedAnalyses EmbedBitcodePass::run(Module &M, ModuleAnalysisManager &AM) {
+  if (M.getGlobalVariable("llvm.embedded.module", true))
+    report_fatal_error("Can only embed the module once.");
+
+  Triple T(M.getTargetTriple());
+  if (T.getObjectFormat() != Triple::ELF)
+    report_fatal_error(
+        "Embed bitcode pass currently only supports ELF object format.");
+
+  std::string Data;
+  raw_string_ostream OS(Data);
+  WriteBitcodeToFile(M, OS, /* ShouldPreserveUseListOrder */ true);
+  ArrayRef<uint8_t> ModuleData((const uint8_t *)OS.str().data(),
+                               OS.str().size());
+  Constant *ModuleConstant = ConstantDataArray::get(M.getContext(), ModuleData);
+
+  llvm::GlobalVariable *EM = new llvm::GlobalVariable(
+      M, ModuleConstant->getType(), true, llvm::GlobalValue::PrivateLinkage,
+      ModuleConstant);
+
+  EM->setSection(".llvm.lto");
+  EM->setAlignment(Align(1));
+  EM->setName("llvm.embedded.module");
+  appendToCompilerUsed(M, {EM});
+
+  return PreservedAnalyses::all();
+}
Index: llvm/lib/Bitcode/Writer/CMakeLists.txt
===================================================================
--- llvm/lib/Bitcode/Writer/CMakeLists.txt
+++ llvm/lib/Bitcode/Writer/CMakeLists.txt
@@ -2,6 +2,7 @@
   BitWriter.cpp
   BitcodeWriter.cpp
   BitcodeWriterPass.cpp
+  EmbedBitcodePass.cpp
   ValueEnumerator.cpp
 
   DEPENDS
Index: llvm/include/llvm/Bitcode/EmbedBitcodePass.h
===================================================================
--- /dev/null
+++ llvm/include/llvm/Bitcode/EmbedBitcodePass.h
@@ -0,0 +1,36 @@
+//===-- EmbedBitcodePass.h - Embeds bitcode into global ---------*- C++ -*-===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+/// \file
+///
+/// This file provides apass which embeds the bitcode into a global variable.
+///
+//===----------------------------------------------------------------------===//
+//
+#ifndef LLVM_BITCODE_EMBEDBITCODEPASS_H
+#define LLVM_BITCODE_EMBEDBITCODEPASS_H
+
+#include "llvm/IR/PassManager.h"
+
+namespace llvm {
+class Module;
+class ModulePass;
+class Pass;
+
+/// Pass embeds the current module into a global variable.
+class EmbedBitcodePass : public PassInfoMixin<EmbedBitcodePass> {
+public:
+  EmbedBitcodePass(){};
+
+  PreservedAnalyses run(Module &M, ModuleAnalysisManager &);
+
+  static bool isRequired() { return true; }
+};
+
+} // end namespace llvm.
+
+#endif
Index: lld/test/ELF/fatlto/fatlto.test
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/fatlto.test
@@ -0,0 +1,38 @@
+; REQUIRES: x86
+
+; Basic FatLTO tests.
+; Check that if input files contain .llvm.lto section
+; RUN: yaml2obj %p/Inputs/a-fatLTO.yaml > %ta-fatLTO.o
+; RUN: yaml2obj %p/Inputs/main-fatLTO.yaml > %tmain-fatLTO.o
+; RUN: llvm-readobj -S %ta-fatLTO.o | FileCheck --check-prefix=CHECK-A %s
+; CHECK-A: Name: .llvm.lto
+; RUN: llvm-readobj -S %tmain-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 %tfoo-fatLTO %ta-fatLTO.o %tmain-fatLTO.o -fat-lto-objects
+; RUN: llvm-readobj -S %tfoo-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 %tfoo-fatNoLTO %ta-fatLTO.o %tmain-fatLTO.o
+; RUN: llvm-readobj -S %tfoo-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 %ta-LTO.bc
+; RUN: llvm-as %p/Inputs/main-LTO.ll -o %tmain-LTO.bc
+; RUN: ld.lld -o %tfoo-LTO %ta-LTO.bc %tmain-LTO.bc
+; RUN: obj2yaml %tfoo-fatLTO > %tfoo-fatLTO.yaml
+; RUN: obj2yaml %tfoo-LTO > %tfoo-LTO.yaml
+; RUN: diff %tfoo-fatLTO.yaml %tfoo-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: yaml2obj %p/Inputs/a.yaml > %ta.o
+; RUN: yaml2obj %p/Inputs/main.yaml > %tmain.o
+; RUN: ld.lld -o %tfoo-noLTO %ta.o %tmain.o
+; RUN: obj2yaml %tfoo-fatNoLTO > %tfoo-fatNoLTO.yaml
+; RUN: obj2yaml %tfoo-noLTO > %tfoo-noLTO.yaml
+; RUN: diff %tfoo-fatNoLTO.yaml %tfoo-noLTO.yaml
Index: lld/test/ELF/fatlto/Inputs/main.yaml
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/main.yaml
@@ -0,0 +1,80 @@
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+  SectionHeaderStringTable: .strtab
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x10
+    Content:         554889E54883EC10C745FC00000000B000E8000000004883C4105DC3
+  - Name:            .comment
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x1
+    EntSize:         0x1
+    Content:         00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420323238386562626563313438636538303030666332633166363361343033336364383437363639652900
+  - Name:            .note.GNU-stack
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x1
+  - Name:            .eh_frame
+    Type:            SHT_X86_64_UNWIND
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x8
+    Content:         1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000001C00000000410E108602430D06570C0708000000
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x8
+    Info:            .text
+    Relocations:
+      - Offset:          0x12
+        Symbol:          a
+        Type:            R_X86_64_PLT32
+        Addend:          -4
+  - Name:            .rela.eh_frame
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x8
+    Info:            .eh_frame
+    Relocations:
+      - Offset:          0x20
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+  - Name:            .llvm_addrsig
+    Type:            SHT_LLVM_ADDRSIG
+    Flags:           [ SHF_EXCLUDE ]
+    Link:            .symtab
+    AddressAlign:    0x1
+    Symbols:         [ a ]
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .strtab
+      - Name:            .text
+      - Name:            .rela.text
+      - Name:            .comment
+      - Name:            .note.GNU-stack
+      - Name:            .eh_frame
+      - Name:            .rela.eh_frame
+      - Name:            .llvm_addrsig
+      - Name:            .symtab
+Symbols:
+  - Name:            main.c
+    Type:            STT_FILE
+    Index:           SHN_ABS
+  - Name:            .text
+    Type:            STT_SECTION
+    Section:         .text
+  - Name:            main
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+    Size:            0x1C
+  - Name:            a
+    Binding:         STB_GLOBAL
+...
Index: lld/test/ELF/fatlto/Inputs/main.c
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/main.c
@@ -0,0 +1,3 @@
+int a();
+
+int main() { return a(); }
Index: lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml
@@ -0,0 +1,86 @@
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+  SectionHeaderStringTable: .strtab
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x10
+    Content:         554889E54883EC10C745FC00000000B000E8000000004883C4105DC3
+  - Name:            .llvm.lto
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x1
+    Content:         4243C0DE3514000005000000620C30244D59BE669DFBB44F1BC8244401320500210C0000220200000B02210002000000160000000781239141C80449061032399201840C250508191E048B62800C450242920B42641032143808184B0A3232884870C421234412878C1041920264C808B1142043468820C901323284182A282A90317CB05C9120C3C8000000892000000C0000003222C80820624600212B24980C212524980C19270C85A4906032645C20246382C0A839023030416C908180110000000051180000810000001B4823F8FFFFFFFF017000092883200C04C21CE4211CDAA11CDA001EDE211DDC811ECA411E807060077600887648077768037628877308077668037B288771A08777908736B8877420077A4007000E00C21DDEA10DE8411EC2011EE0211DDCE11CDAA01DC2811ED001A00779A88772008879A08770188775680378908777A0877218077A780779680371A80773308772908736988774D0877200F00020E8211CE4E11CCA811EDA601CE0A11E007CC0033B68033BA00380A0877090877328077A680373288770A0877A9087729807600DC6011FF0800DD6C01CF0611EE4810DD6A01DDA011FD8600DE6611ECA810DD6601EE6A11CE4800DD6001FF0E00E00821EEAC11DCAA10DC6011EEA01388772708772908774180760435808FFFFFFFF3F10E6200FE1D00EE5D006F0F00EE9E00EF4500EF20084033BB00340B8C33BB4013DC84338C0033CA4833B9C431BB44338D0033A00F4200FF5500E00310FF4100EE3B00E6D000FF2F00EF4500EE3400FEF200F6D200EF5600EE6500EF2D006F3900EFA500E001E00043D84833C9C4339D0431B8C033CD403800F7860076D6007740010F4100EF2700EE5400F6D600EE5100EF4500FF2500EF300ACC138E0031EB0C11A98033ECC833CB0C11AB4433BE0031BACC13CCC4339B0C11ACCC33C94831CB0C11AE0031EDC0140D0433DB84339B4C138C0433D00E7500EEE500EF2900EE300000000004918000002000000138260422000000013307CC0033BF8053BA08336A8077758077778877B70873660877470877AC08736380777A8870DA6500E6DD00E7A500E6D000F72700770A0077320077A300772D006F020077710077A300772A0077320076D000F72700772A0077640077A600774D006E9600774A0077640076D600E7800077A10077280076DE00E78A0077160077A300772A0077640076D300B71200778A011C2900603208D2F0CA9004200002000000000000000800020802115110C4000080000000000000000000420B141A0A889000040160807000000321E980C19114C908C092647C604434A0914423914C1080000000000B1180000A10000003308801CC4E11C6614013D88433884C38C4280077978077398710CE6000FED100EF4800E330C421EC2C11DCEA11C6630053D88433884831BCC033DC8433D8C033DCC788C7470077B08077948877070077A700376788770208719CC110EEC900EE1300F6E300FE3F00EF0500E3310C41DDE211CD8211DC2611E6630893BBC833BD04339B4033CBC833C84033BCCF0147660077B680737688772680737808770908770600776280776F8057678877780875F08877118877298877998812CEEF00EEEE00EF5C00EEC300362C8A11CE4A11CCCA11CE4A11CDC611CCA211CC4811DCA6106D6904339C84339984339C84339B8C33894433888033B94C32FBC833CFC823BD4033BB0C30CC7698770588772708374680778608774188774A08719CE530FEE000FF2500EE4900EE3400FE1200EEC500E3320281DDCC11EC2411ED2211CDC811EDCE01CE4E11DEA011E66185138B0433A9C833BCC50247660077B68073760877778077898514CF4900FF0500E331E6A1ECA611CE8211DDEC11D7E011EE4A11CCC211DF0610654858338CCC33BB0433DD04339FCC23CE4433B88C33BB0C38CC50A877998877718877408077A28077298815CE3100EECC00EE5500EF33023C1D2411EE4E117D8E11DDE011E6648193BB0833DB4831B84C3388C4339CCC33CB8C139C8C33BD4033CCC48B471080776600771088771588719DBC60EEC600FEDE006F0200FE5300FE5200FF6500E6E100EE3300EE5300FF3E006E9E00EE4500EF83023E2EC611CC2811DD8E117EC211DE6211DC4211DD8211DE8211F66209D3BBC433DB80339948339CC58BC7070077778077A08077A488777708719CBE70EEF300FE1E00EE9400FE9A00FE530C3010373A8077718875F988770708774A08774D0877200007920000050000000721E482043880C19097232482023818C9191D144A01028643C3132428E9021A3882079014A921CCD21A1030077636861725F73697A65504943204C6576656C504945204C6576656C75777461626C656672616D652D706F696E7465725468696E4C544F456E61626C6553706C69744C544F556E6974636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E6769742032323838656262656331343863653830303066633263316636336134303333636438343736363965290000002308813082100C2308013182101423088131C39004CA0CC32230330CCBC0CC302C0433C3B014CC0C436234330CC991CC1020321298A08CD8D8ECDA5CDADEC8EAD8CA5CCCD8C2CEE646391AE78122694A858DCDAECD258DACCC8D6E9480020000A9180000250000000B0A7228877780077A587098433DB8C338B04339D0C382E61CC6A10DE8411EC2C11DE6211DE8211DDEC11D1634E3600EE7500FE1200FE4400FE1200FE7500EF4B08081077928877060077678877108077A28077258709CC338B4013BA4833D94C3026B1CD8211CDCE11CDC201CE4611CDC201CE8811EC2611CD0A11CC8611CC2811DD861C1010FF4200FE1500FF4800E00000000D11000000600000007CC3CA4833B9C033B94033DA0833C94433890C301000000612000000E0000001304C14C441008A3306C4004C2008C1810000882C1706C100E04000005000000075010CD1461864008C0B2D881111153052C0000000000007120000003000000320E1022840096040000000000000000650C00002500000012039428010000000300000005000000320000004C0000000100000058000000000000005800000002000000880000000000000037000000180000004F000000060000000400000000000000880000000000000000000000020000000000000000000000040000000000000004000000FFFFFFFF0024000004000000010000000400000001000000FFFFFFFF08240000000000005D0C000019000000120394B5000000006D61696E6131352E302E3067697420323238386562626563313438636538303030666332633166363361343033336364383437363639657838365F36342D756E6B6E6F776E2D6C696E75782D676E756D61696E2E6300000000000000
+  - Name:            .comment
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x1
+    EntSize:         0x1
+    Content:         00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420323238386562626563313438636538303030666332633166363361343033336364383437363639652900
+  - Name:            .note.GNU-stack
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x1
+  - Name:            .eh_frame
+    Type:            SHT_X86_64_UNWIND
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x8
+    Content:         1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000001C00000000410E108602430D06570C0708000000
+  - Name:            .rela.text
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x8
+    Info:            .text
+    Relocations:
+      - Offset:          0x12
+        Symbol:          a
+        Type:            R_X86_64_PLT32
+        Addend:          -4
+  - Name:            .rela.eh_frame
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x8
+    Info:            .eh_frame
+    Relocations:
+      - Offset:          0x20
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+  - Name:            .llvm_addrsig
+    Type:            SHT_LLVM_ADDRSIG
+    Flags:           [ SHF_EXCLUDE ]
+    Link:            .symtab
+    AddressAlign:    0x1
+    Symbols:         [ a ]
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .strtab
+      - Name:            .text
+      - Name:            .rela.text
+      - Name:            .llvm.lto
+      - Name:            .comment
+      - Name:            .note.GNU-stack
+      - Name:            .eh_frame
+      - Name:            .rela.eh_frame
+      - Name:            .llvm_addrsig
+      - Name:            .symtab
+Symbols:
+  - Name:            main.c
+    Type:            STT_FILE
+    Index:           SHN_ABS
+  - Name:            .text
+    Type:            STT_SECTION
+    Section:         .text
+  - Name:            main
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+    Size:            0x1C
+  - Name:            a
+    Binding:         STB_GLOBAL
+...
Index: lld/test/ELF/fatlto/Inputs/main-LTO.ll
===================================================================
--- /dev/null
+++ 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 optnone 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 optnone 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
Index: lld/test/ELF/fatlto/Inputs/a.yaml
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/a.yaml
@@ -0,0 +1,67 @@
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+  SectionHeaderStringTable: .strtab
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x10
+    Content:         554889E531C05DC3
+  - Name:            .comment
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x1
+    EntSize:         0x1
+    Content:         00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420323238386562626563313438636538303030666332633166363361343033336364383437363639652900
+  - Name:            .note.GNU-stack
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x1
+  - Name:            .eh_frame
+    Type:            SHT_X86_64_UNWIND
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x8
+    Content:         1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000000800000000410E108602430D06430C0708000000
+  - Name:            .rela.eh_frame
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x8
+    Info:            .eh_frame
+    Relocations:
+      - Offset:          0x20
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+  - Name:            .llvm_addrsig
+    Type:            SHT_LLVM_ADDRSIG
+    Flags:           [ SHF_EXCLUDE ]
+    Link:            .symtab
+    AddressAlign:    0x1
+    Offset:          0x168
+    Symbols:         [  ]
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .strtab
+      - Name:            .text
+      - Name:            .comment
+      - Name:            .note.GNU-stack
+      - Name:            .eh_frame
+      - Name:            .rela.eh_frame
+      - Name:            .llvm_addrsig
+      - Name:            .symtab
+Symbols:
+  - Name:            a.c
+    Type:            STT_FILE
+    Index:           SHN_ABS
+  - Name:            .text
+    Type:            STT_SECTION
+    Section:         .text
+  - Name:            a
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+    Size:            0x8
+...
Index: lld/test/ELF/fatlto/Inputs/a.h
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/a.h
@@ -0,0 +1,3 @@
+extern int foo1(void);
+extern void foo2(void);
+extern void foo4(void);
Index: lld/test/ELF/fatlto/Inputs/a.c
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/a.c
@@ -0,0 +1 @@
+int a(void) { return 0; }
Index: lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml
===================================================================
--- /dev/null
+++ lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml
@@ -0,0 +1,73 @@
+--- !ELF
+FileHeader:
+  Class:           ELFCLASS64
+  Data:            ELFDATA2LSB
+  Type:            ET_REL
+  Machine:         EM_X86_64
+  SectionHeaderStringTable: .strtab
+Sections:
+  - Name:            .text
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC, SHF_EXECINSTR ]
+    AddressAlign:    0x10
+    Content:         554889E531C05DC3
+  - Name:            .llvm.lto
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x1
+    Content:         4243C0DE3514000005000000620C30244D59BE669DFBB44F1BC8244401320500210C0000D80100000B02210002000000160000000781239141C80449061032399201840C250508191E048B62800C450242920B42641032143808184B0A3232884870C421234412878C1041920264C808B1142043468820C901323284182A282A90317CB05C9120C3C8000000892000000B0000003222C80820624600212B24980C212524980C19270C85A4906032645C20246382A0A83902303041060246000051180000460000001B4823F8FFFFFFFF017000092883200C04C21CE4211CDAA11CDA001EDE211DDC811ECA411E807060077600887648077768037628877308077668037B288771A08777908736B8877420077A4007000E00C21DDEA10DE8411EC2011EE0211DDCE11CDAA01DC2811ED001A00779A88772008879A08770188775680378908777A0877218077A780779680371A80773308772908736988774D0877200F00020E8211CE4E11CCA811EDA601CE0A11E007CC0033B68033BA00380A0877090877328077A680373288770A0877A9087729807600DC6011FF0800DD6C01CF0611EE4810DD6A01DDA011FD8600DE6611ECA810DD6601EE6A11CE4800DD6001FF0E00E00821EEAC11DCAA10DC6011EEA013887727087729087741807000049180000010000001382000013307CC0033BF8053BA08336A8077758077778877B70873660877470877AC08736380777A8870DA6500E6DD00E7A500E6D000F72700770A0077320077A300772D006F020077710077A300772A0077320076D000F72700772A0077640077A600774D006E9600774A0077640076D600E7800077A10077280076DE00E78A0077160077A300772A0077640076D300B71200778A011C29003E00B432A2010000008000000000000002000024062834051EE0000802C1007000000321E980C19114C908C092647C604434A0914423914C1080000000000B1180000A10000003308801CC4E11C6614013D88433884C38C4280077978077398710CE6000FED100EF4800E330C421EC2C11DCEA11C6630053D88433884831BCC033DC8433D8C033DCC788C7470077B08077948877070077A700376788770208719CC110EEC900EE1300F6E300FE3F00EF0500E3310C41DDE211CD8211DC2611E6630893BBC833BD04339B4033CBC833C84033BCCF0147660077B680737688772680737808770908770600776280776F8057678877780875F08877118877298877998812CEEF00EEEE00EF5C00EEC300362C8A11CE4A11CCCA11CE4A11CDC611CCA211CC4811DCA6106D6904339C84339984339C84339B8C33894433888033B94C32FBC833CFC823BD4033BB0C30CC7698770588772708374680778608774188774A08719CE530FEE000FF2500EE4900EE3400FE1200EEC500E3320281DDCC11EC2411ED2211CDC811EDCE01CE4E11DEA011E66185138B0433A9C833BCC50247660077B68073760877778077898514CF4900FF0500E331E6A1ECA611CE8211DDEC11D7E011EE4A11CCC211DF0610654858338CCC33BB0433DD04339FCC23CE4433B88C33BB0C38CC50A877998877718877408077A28077298815CE3100EECC00EE5500EF33023C1D2411EE4E117D8E11DDE011E6648193BB0833DB4831B84C3388C4339CCC33CB8C139C8C33BD4033CCC48B471080776600771088771588719DBC60EEC600FEDE006F0200FE5300FE5200FF6500E6E100EE3300EE5300FF3E006E9E00EE4500EF83023E2EC611CC2811DD8E117EC211DE6211DC4211DD8211DE8211F66209D3BBC433DB80339948339CC58BC7070077778077A08077A488777708719CBE70EEF300FE1E00EE9400FE9A00FE530C3010373A8077718875F988770708774A08774D0877200007920000050000000721E482043880C19097232482023818C9191D144A01028643C3132428E9021A3882079014A921CCD21A1030077636861725F73697A65504943204C6576656C504945204C6576656C75777461626C656672616D652D706F696E7465725468696E4C544F456E61626C6553706C69744C544F556E6974636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E676974203232383865626265633134386365383030306663326331663633613430333363643834373636396529000000230841308210082308C13082101023084131C39004CA0CC32230330CCBC0CC302C0433C3B014CC0C436234330CC991CC1020321298A08CD8D8ECDA5CDADEC8EAD8CA5CCCD8C2CEE646391AE78122694A858DCDAECD258DACCC8D6E9480020000A9180000250000000B0A7228877780077A587098433DB8C338B04339D0C382E61CC6A10DE8411EC2C11DE6211DE8211DDEC11D1634E3600EE7500FE1200FE4400FE1200FE7500EF4B08081077928877060077678877108077A28077258709CC338B4013BA4833D94C3026B1CD8211CDCE11CDC201CE4611CDC201CE8811EC2611CD0A11CC8611CC2811DD861C1010FF4200FE1500FF4800E00000000D11000000600000007CC3CA4833B9C033B94033DA0833C94433890C30100000061200000060000001304C106E140000002000000075010CD14610000000000007120000003000000320E10228400D4030000000000000000650C00001F000000120394F0000000000300000001000000320000004C0000000100000058000000000000005800000001000000700000000000000033000000180000004B000000030000000100000000000000700000000000000000000000010000000000000000000000010000000000000001000000FFFFFFFF00240000000000005D0C000017000000120394AE000000006131352E302E3067697420323238386562626563313438636538303030666332633166363361343033336364383437363639657838365F36342D756E6B6E6F776E2D6C696E75782D676E75612E63000000000000
+  - Name:            .comment
+    Type:            SHT_PROGBITS
+    Flags:           [ SHF_MERGE, SHF_STRINGS ]
+    AddressAlign:    0x1
+    EntSize:         0x1
+    Content:         00636C616E672076657273696F6E2031352E302E30202868747470733A2F2F6769746875622E636F6D2F6C6C766D2F6C6C766D2D70726F6A6563742E67697420323238386562626563313438636538303030666332633166363361343033336364383437363639652900
+  - Name:            .note.GNU-stack
+    Type:            SHT_PROGBITS
+    AddressAlign:    0x1
+  - Name:            .eh_frame
+    Type:            SHT_X86_64_UNWIND
+    Flags:           [ SHF_ALLOC ]
+    AddressAlign:    0x8
+    Content:         1400000000000000017A5200017810011B0C0708900100001C0000001C000000000000000800000000410E108602430D06430C0708000000
+  - Name:            .rela.eh_frame
+    Type:            SHT_RELA
+    Flags:           [ SHF_INFO_LINK ]
+    Link:            .symtab
+    AddressAlign:    0x8
+    Info:            .eh_frame
+    Relocations:
+      - Offset:          0x20
+        Symbol:          .text
+        Type:            R_X86_64_PC32
+  - Name:            .llvm_addrsig
+    Type:            SHT_LLVM_ADDRSIG
+    Flags:           [ SHF_EXCLUDE ]
+    Link:            .symtab
+    AddressAlign:    0x1
+    Offset:          0x9D8
+    Symbols:         [  ]
+  - Type:            SectionHeaderTable
+    Sections:
+      - Name:            .strtab
+      - Name:            .text
+      - Name:            .llvm.lto
+      - Name:            .comment
+      - Name:            .note.GNU-stack
+      - Name:            .eh_frame
+      - Name:            .rela.eh_frame
+      - Name:            .llvm_addrsig
+      - Name:            .symtab
+Symbols:
+  - Name:            a.c
+    Type:            STT_FILE
+    Index:           SHN_ABS
+  - Name:            .text
+    Type:            STT_SECTION
+    Section:         .text
+  - Name:            a
+    Type:            STT_FUNC
+    Section:         .text
+    Binding:         STB_GLOBAL
+    Size:            0x8
+...
Index: lld/test/ELF/fatlto/Inputs/a-LTO.ll
===================================================================
--- /dev/null
+++ 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 optnone uwtable
+define dso_local i32 @a() #0 {
+entry:
+  ret i32 0
+}
+
+attributes #0 = { noinline nounwind optnone 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
Index: lld/ELF/Options.td
===================================================================
--- lld/ELF/Options.td
+++ lld/ELF/Options.td
@@ -615,6 +615,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<lto_O>, HelpText<"Alias for --lto-O">;
 def: F<"plugin-opt=debug-pass-manager">,
   Alias<lto_debug_pass_manager>, HelpText<"Alias for --lto-debug-pass-manager">;
Index: lld/ELF/Driver.cpp
===================================================================
--- lld/ELF/Driver.cpp
+++ 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"
@@ -306,7 +307,15 @@
     files.push_back(make<BitcodeFile>(mbref, "", 0, inLib));
     break;
   case file_magic::elf_relocatable:
-    files.push_back(createObjFile(mbref, "", inLib));
+    if (config->fatLTOObjects) {
+      Expected<MemoryBufferRef> fatLTOData =
+          IRObjectFile::findBitcodeInMemBuffer(mbref);
+      if (!errorToBool(fatLTOData.takeError()))
+        files.push_back(make<BitcodeFile>(*fatLTOData, "", 0, inLib));
+      else
+        files.push_back(createObjFile(mbref, "", inLib));
+    } else
+      files.push_back(createObjFile(mbref, "", inLib));
     break;
   default:
     error(path + ": unknown file type");
@@ -1066,6 +1075,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 =
@@ -2721,6 +2732,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<EhInputSection>(s)))
           ctx.ehInputSections.push_back(cast<EhInputSection>(s));
         else
Index: lld/ELF/Config.h
===================================================================
--- lld/ELF/Config.h
+++ lld/ELF/Config.h
@@ -187,6 +187,7 @@
                   uint64_t>
       callGraphProfile;
   bool allowMultipleDefinition;
+  bool fatLTOObjects;
   bool androidPackDynRelocs = false;
   bool armHasBlx = false;
   bool armHasMovtMovw = false;
Index: clang/test/Driver/fatlto-objects.c
===================================================================
--- /dev/null
+++ clang/test/Driver/fatlto-objects.c
@@ -0,0 +1,10 @@
+// REQUIRES: x86_64-linux
+// RUN: %clang -target x86_64-unknown-linux-gnu -flto -ffat-lto-objects -fintegrated-as -### %s -c 2>&1 | FileCheck %s -check-prefix=CHECK-CC
+// CHECK-CC: -cc1
+// CHECK-CC: -emit-obj
+// CHECK-CC: -ffat-lto-objects
+
+// RUN: %clang -target x86_64-unknown-linux-gnu -ffat-lto-objects -fintegrated-as -### %s -c 2>&1 | FileCheck %s -check-prefix=CHECK-CC-NOLTO
+// CHECK-CC-NOLTO: -cc1
+// CHECK-CC-NOLTO: -emit-obj
+// CHECK-CC-NOLTO-NOT: -ffat-lto-objects
Index: clang/test/Driver/clang_f_opts.c
===================================================================
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -421,7 +421,6 @@
 // CHECK-WARNING-DAG: optimization flag '-fwhole-program' is not supported
 // CHECK-WARNING-DAG: optimization flag '-fcaller-saves' is not supported
 // CHECK-WARNING-DAG: optimization flag '-freorder-blocks' is not supported
-// CHECK-WARNING-DAG: optimization flag '-ffat-lto-objects' is not supported
 // CHECK-WARNING-DAG: optimization flag '-fmerge-constants' is not supported
 // CHECK-WARNING-DAG: optimization flag '-finline-small-functions' is not supported
 // CHECK-WARNING-DAG: optimization flag '-ftree-dce' is not supported
Index: clang/test/CodeGen/embed-lto-fatlto.c
===================================================================
--- /dev/null
+++ clang/test/CodeGen/embed-lto-fatlto.c
@@ -0,0 +1,9 @@
+// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -S -flto=full -ffat-lto-objects -emit-llvm < %s  | FileCheck %s
+// RUN: %clang -cc1 -triple x86_64-unknown-linux-gnu -S -flto=full -ffat-lto-objects -emit-llvm < %s  | FileCheck %s
+//
+// CHECK: !{{[0-9]+}} = !{i32 1, !"ThinLTO", i32 0}
+// CHECK: !{{[0-9]+}} = !{i32 1, !"EnableSplitLTOUnit", i32 1}
+
+int test(void) {
+  return 0xabcd;
+}
Index: clang/lib/Driver/ToolChains/Clang.cpp
===================================================================
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -7291,6 +7291,9 @@
     }
   }
 
+  if (IsUsingLTO && Args.getLastArg(options::OPT_ffat_lto_objects))
+    CmdArgs.push_back("-ffat-lto-objects");
+
   if (Args.hasArg(options::OPT_forder_file_instrumentation)) {
      CmdArgs.push_back("-forder-file-instrumentation");
      // Enable order file instrumentation when ThinLTO is not on. When ThinLTO is
Index: clang/lib/Driver/Driver.cpp
===================================================================
--- clang/lib/Driver/Driver.cpp
+++ clang/lib/Driver/Driver.cpp
@@ -4632,6 +4632,8 @@
     if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) {
       types::ID Output =
           Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC;
+      if (Args.hasArg(options::OPT_ffat_lto_objects))
+        Output = types::TY_PP_Asm;
       return C.MakeAction<BackendJobAction>(Input, Output);
     }
     if (isUsingLTO(/* IsOffload */ true) &&
Index: clang/lib/CodeGen/BackendUtil.cpp
===================================================================
--- clang/lib/CodeGen/BackendUtil.cpp
+++ clang/lib/CodeGen/BackendUtil.cpp
@@ -24,6 +24,7 @@
 #include "llvm/Bitcode/BitcodeReader.h"
 #include "llvm/Bitcode/BitcodeWriter.h"
 #include "llvm/Bitcode/BitcodeWriterPass.h"
+#include "llvm/Bitcode/EmbedBitcodePass.h"
 #include "llvm/CodeGen/RegAllocRegistry.h"
 #include "llvm/CodeGen/SchedulerRegistry.h"
 #include "llvm/CodeGen/TargetSubtargetInfo.h"
@@ -1035,6 +1036,17 @@
     }
   }
 
+  // Run pass to embed the bitcode into the FatLTO module to preserve for
+  // LTO usage.
+  if (CodeGenOpts.FatLTO) {
+    if (!TheModule->getModuleFlag("ThinLTO"))
+      TheModule->addModuleFlag(Module::Error, "ThinLTO", uint32_t(0));
+    if (!TheModule->getModuleFlag("EnableSplitLTOUnit"))
+      TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit",
+                               uint32_t(1));
+    MPM.addPass(EmbedBitcodePass());
+  }
+
   // Now that we have all of the passes ready, run them.
   {
     PrettyStackTraceString CrashInfo("Optimizer");
Index: clang/include/clang/Driver/Options.td
===================================================================
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -2265,6 +2265,11 @@
   Flags<[CoreOption, CC1Option]>, Group<f_Group>,
   HelpText<"Write minimized bitcode to <file> for the ThinLTO thin link only">,
   MarshallingInfoString<CodeGenOpts<"ThinLinkBitcodeFile">>;
+defm fat_lto_objects : BoolFOption<"fat-lto-objects",
+  CodeGenOpts<"FatLTO">, DefaultFalse,
+  PosFlag<SetTrue, [], "Enable fat LTO object support">,
+  NegFlag<SetFalse, [], "Disable fat LTO object support">,
+  BothFlags<[CC1Option], "">>;
 def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">,
                                 Group<f_Group>, Flags<[NoXarchOption, CoreOption]>;
 defm merge_all_constants : BoolFOption<"merge-all-constants",
@@ -4908,7 +4913,6 @@
 defm reorder_blocks : BooleanFFlag<"reorder-blocks">, Group<clang_ignored_gcc_optimization_f_Group>;
 defm branch_count_reg : BooleanFFlag<"branch-count-reg">, Group<clang_ignored_gcc_optimization_f_Group>;
 defm default_inline : BooleanFFlag<"default-inline">, Group<clang_ignored_gcc_optimization_f_Group>;
-defm fat_lto_objects : BooleanFFlag<"fat-lto-objects">, Group<clang_ignored_gcc_optimization_f_Group>;
 defm float_store : BooleanFFlag<"float-store">, Group<clang_ignored_gcc_optimization_f_Group>;
 defm friend_injection : BooleanFFlag<"friend-injection">, Group<clang_ignored_f_Group>;
 defm function_attribute_list : BooleanFFlag<"function-attribute-list">, Group<clang_ignored_f_Group>;
Index: clang/include/clang/Basic/CodeGenOptions.def
===================================================================
--- clang/include/clang/Basic/CodeGenOptions.def
+++ clang/include/clang/Basic/CodeGenOptions.def
@@ -162,6 +162,7 @@
                                      ///< compile step.
 CODEGENOPT(LTOUnit, 1, 0) ///< Emit IR to support LTO unit features (CFI, whole
                           ///< program vtable opt).
+CODEGENOPT(FatLTO, 1, 0) ///< Set when -ffat-lto-objects is enabled.
 CODEGENOPT(EnableSplitLTOUnit, 1, 0) ///< Enable LTO unit splitting to support
 				     /// CFI and traditional whole program
 				     /// devirtualization that require whole
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to