diff --git a/clang/include/clang/Basic/CodeGenOptions.def b/clang/include/clang/Basic/CodeGenOptions.def --- a/clang/include/clang/Basic/CodeGenOptions.def +++ b/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 diff --git a/clang/include/clang/Driver/Options.td b/clang/include/clang/Driver/Options.td --- a/clang/include/clang/Driver/Options.td +++ b/clang/include/clang/Driver/Options.td @@ -2265,6 +2265,11 @@ Flags<[CoreOption, CC1Option]>, Group, HelpText<"Write minimized bitcode to for the ThinLTO thin link only">, MarshallingInfoString>; +defm fat_lto_objects : BoolFOption<"fat-lto-objects", + CodeGenOpts<"FatLTO">, DefaultFalse, + PosFlag, + NegFlag, + BothFlags<[CC1Option], "">>; def fmacro_backtrace_limit_EQ : Joined<["-"], "fmacro-backtrace-limit=">, Group, Flags<[NoXarchOption, CoreOption]>; defm merge_all_constants : BoolFOption<"merge-all-constants", @@ -4908,7 +4913,6 @@ defm reorder_blocks : BooleanFFlag<"reorder-blocks">, Group; defm branch_count_reg : BooleanFFlag<"branch-count-reg">, Group; defm default_inline : BooleanFFlag<"default-inline">, Group; -defm fat_lto_objects : BooleanFFlag<"fat-lto-objects">, Group; defm float_store : BooleanFFlag<"float-store">, Group; defm friend_injection : BooleanFFlag<"friend-injection">, Group; defm function_attribute_list : BooleanFFlag<"function-attribute-list">, Group; diff --git a/clang/lib/CodeGen/BackendUtil.cpp b/clang/lib/CodeGen/BackendUtil.cpp --- a/clang/lib/CodeGen/BackendUtil.cpp +++ b/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" @@ -977,6 +978,10 @@ if (CodeGenOpts.OptimizationLevel == 0) { MPM = PB.buildO0DefaultPipeline(Level, IsLTO || IsThinLTO); + } else if (CodeGenOpts.FatLTO) { + MPM = PB.buildFatLTODefaultPipeline( + Level, IsThinLTO, CodeGenOpts.EmitLLVMUseLists, + IsThinLTO || shouldEmitRegularLTOSummary()); } else if (IsThinLTO) { MPM = PB.buildThinLTOPreLinkDefaultPipeline(Level); } else if (IsLTO) { @@ -1035,6 +1040,13 @@ } } + if (CodeGenOpts.FatLTO) { + // Set EnableSplitLTOUnit, since the config above won't + if (!TheModule->getModuleFlag("EnableSplitLTOUnit")) + TheModule->addModuleFlag(Module::Error, "EnableSplitLTOUnit", + uint32_t(CodeGenOpts.EnableSplitLTOUnit)); + } + // Now that we have all of the passes ready, run them. { PrettyStackTraceString CrashInfo("Optimizer"); diff --git a/clang/lib/Driver/Driver.cpp b/clang/lib/Driver/Driver.cpp --- a/clang/lib/Driver/Driver.cpp +++ b/clang/lib/Driver/Driver.cpp @@ -4630,8 +4630,13 @@ } case phases::Backend: { if (isUsingLTO() && TargetDeviceOffloadKind == Action::OFK_None) { - types::ID Output = - Args.hasArg(options::OPT_S) ? types::TY_LTO_IR : types::TY_LTO_BC; + types::ID Output; + if (Args.hasArg(options::OPT_S)) + Output = types::TY_LTO_IR; + else if (Args.hasArg(options::OPT_ffat_lto_objects)) + Output = types::TY_PP_Asm; + else + Output = types::TY_LTO_BC; return C.MakeAction(Input, Output); } if (isUsingLTO(/* IsOffload */ true) && diff --git a/clang/lib/Driver/ToolChains/Clang.cpp b/clang/lib/Driver/ToolChains/Clang.cpp --- a/clang/lib/Driver/ToolChains/Clang.cpp +++ b/clang/lib/Driver/ToolChains/Clang.cpp @@ -7291,6 +7291,14 @@ } } + if (IsUsingLTO && Args.getLastArg(options::OPT_ffat_lto_objects)) { + assert(LTOMode == LTOK_Full || LTOMode == LTOK_Thin); + CmdArgs.push_back(Args.MakeArgString( + Twine("-flto=") + (LTOMode == LTOK_Thin ? "thin" : "full"))); + CmdArgs.push_back("-flto-unit"); + 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 diff --git a/clang/test/CodeGen/embed-lto-fatlto.c b/clang/test/CodeGen/embed-lto-fatlto.c new file mode 100644 --- /dev/null +++ b/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; +} diff --git a/clang/test/Driver/clang_f_opts.c b/clang/test/Driver/clang_f_opts.c --- a/clang/test/Driver/clang_f_opts.c +++ b/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 diff --git a/clang/test/Driver/fatlto-objects.c b/clang/test/Driver/fatlto-objects.c new file mode 100644 --- /dev/null +++ b/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 diff --git a/lld/ELF/Config.h b/lld/ELF/Config.h --- a/lld/ELF/Config.h +++ b/lld/ELF/Config.h @@ -187,6 +187,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" @@ -306,6 +307,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: @@ -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(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 @@ -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, HelpText<"Alias for --lto-O">; def: F<"plugin-opt=debug-pass-manager">, Alias, HelpText<"Alias for --lto-debug-pass-manager">; 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/a-fatLTO.yaml b/lld/test/ELF/fatlto/Inputs/a-fatLTO.yaml new file mode 100644 --- /dev/null +++ b/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 +... diff --git a/lld/test/ELF/fatlto/Inputs/a.yaml b/lld/test/ELF/fatlto/Inputs/a.yaml new file mode 100644 --- /dev/null +++ b/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 +... 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/Inputs/main-fatLTO.yaml b/lld/test/ELF/fatlto/Inputs/main-fatLTO.yaml new file mode 100644 --- /dev/null +++ b/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 +... diff --git a/lld/test/ELF/fatlto/Inputs/main.yaml b/lld/test/ELF/fatlto/Inputs/main.yaml new file mode 100644 --- /dev/null +++ b/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 +... 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 %/tmain-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 diff --git a/llvm/include/llvm/Bitcode/EmbedBitcodePass.h b/llvm/include/llvm/Bitcode/EmbedBitcodePass.h new file mode 100644 --- /dev/null +++ b/llvm/include/llvm/Bitcode/EmbedBitcodePass.h @@ -0,0 +1,42 @@ +//===-- 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 { + bool IsThinLTO; + bool EmitLLVMUSeLists; + bool EmitLTOSummary; + +public: + EmbedBitcodePass(bool IsThinLTO, bool EmitLLVMUSeLists, bool EmitLTOSummary) + : IsThinLTO(IsThinLTO), EmitLLVMUSeLists(EmitLLVMUSeLists), + EmitLTOSummary(EmitLTOSummary){}; + + PreservedAnalyses run(Module &M, ModuleAnalysisManager &); + + static bool isRequired() { return true; } +}; + +} // end namespace llvm. + +#endif diff --git a/llvm/include/llvm/Passes/PassBuilder.h b/llvm/include/llvm/Passes/PassBuilder.h --- a/llvm/include/llvm/Passes/PassBuilder.h +++ b/llvm/include/llvm/Passes/PassBuilder.h @@ -239,6 +239,21 @@ ModulePassManager buildPerModuleDefaultPipeline(OptimizationLevel Level, bool LTOPreLink = false); + /// Build a fat object default optimization pipeline. + /// + /// This builds a pipeline that runs the LTO/ThinLTO pipeline, and emits a + /// section containing the bitcode along size the object code generated by the + /// PerModuleDefaultPipeline. + /// + /// Note that \p Level cannot be `O0` here. The pipelines produced are + /// only intended for use when attempting to optimize code. If frontends + /// require some transformations for semantic reasons, they should explicitly + /// build them. + ModulePassManager buildFatLTODefaultPipeline(OptimizationLevel Level, + bool ThinLTOPreLink, + bool EmitUseList, + bool EmitSummary); + /// Build a pre-link, ThinLTO-targeting default optimization pipeline to /// a pass manager. /// @@ -591,6 +606,10 @@ void addVectorPasses(OptimizationLevel Level, FunctionPassManager &FPM, bool IsFullLTO); + void addPerModuleDefaultPipelinePasses(ModulePassManager &MPM, + OptimizationLevel Level, + bool LTOPreLink = false); + static std::optional> parsePipelineText(StringRef Text); diff --git a/llvm/lib/Bitcode/Writer/CMakeLists.txt b/llvm/lib/Bitcode/Writer/CMakeLists.txt --- a/llvm/lib/Bitcode/Writer/CMakeLists.txt +++ b/llvm/lib/Bitcode/Writer/CMakeLists.txt @@ -2,6 +2,7 @@ BitWriter.cpp BitcodeWriter.cpp BitcodeWriterPass.cpp + EmbedBitcodePass.cpp ValueEnumerator.cpp DEPENDS diff --git a/llvm/lib/Bitcode/Writer/EmbedBitcodePass.cpp b/llvm/lib/Bitcode/Writer/EmbedBitcodePass.cpp new file mode 100644 --- /dev/null +++ b/llvm/lib/Bitcode/Writer/EmbedBitcodePass.cpp @@ -0,0 +1,59 @@ +//===- 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/Bitcode/BitcodeWriterPass.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/IPO/ThinLTOBitcodeWriter.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); + + if (IsThinLTO) + ThinLTOBitcodeWriterPass(OS, nullptr).run(M, AM); + else + BitcodeWriterPass(OS, EmitLLVMUSeLists, EmitLTOSummary).run(M, AM); + + ArrayRef 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(); +} diff --git a/llvm/lib/Object/ObjectFile.cpp b/llvm/lib/Object/ObjectFile.cpp --- a/llvm/lib/Object/ObjectFile.cpp +++ b/llvm/lib/Object/ObjectFile.cpp @@ -79,7 +79,7 @@ bool ObjectFile::isSectionBitcode(DataRefImpl Sec) const { Expected NameOrErr = getSectionName(Sec); if (NameOrErr) - return *NameOrErr == ".llvmbc"; + return *NameOrErr == ".llvmbc" || *NameOrErr == ".llvm.lto"; consumeError(NameOrErr.takeError()); return false; } diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp --- a/llvm/lib/Passes/PassBuilder.cpp +++ b/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" diff --git a/llvm/lib/Passes/PassBuilderPipelines.cpp b/llvm/lib/Passes/PassBuilderPipelines.cpp --- a/llvm/lib/Passes/PassBuilderPipelines.cpp +++ b/llvm/lib/Passes/PassBuilderPipelines.cpp @@ -23,6 +23,7 @@ #include "llvm/Analysis/ProfileSummaryInfo.h" #include "llvm/Analysis/ScopedNoAliasAA.h" #include "llvm/Analysis/TypeBasedAliasAnalysis.h" +#include "llvm/Bitcode/EmbedBitcodePass.h" #include "llvm/IR/PassManager.h" #include "llvm/Passes/OptimizationLevel.h" #include "llvm/Passes/PassBuilder.h" @@ -1395,15 +1396,22 @@ ModulePassManager PassBuilder::buildPerModuleDefaultPipeline(OptimizationLevel Level, bool LTOPreLink) { + ModulePassManager MPM; + addPerModuleDefaultPipelinePasses(MPM, Level, LTOPreLink); + return MPM; +} + +void PassBuilder::addPerModuleDefaultPipelinePasses(ModulePassManager &MPM, + OptimizationLevel Level, + bool LTOPreLink) { assert(Level != OptimizationLevel::O0 && "Must request optimizations for the default pipeline!"); - ModulePassManager MPM; - // Convert @llvm.global.annotations to !annotation metadata. MPM.addPass(Annotation2MetadataPass()); - // Force any function attributes we want the rest of the pipeline to observe. + // Force any function attributes we want the rest of the pipeline to + // observe. MPM.addPass(ForceFunctionAttrsPass()); // Apply module pipeline start EP callback. @@ -1431,7 +1439,19 @@ if (LTOPreLink) addRequiredLTOPreLinkPasses(MPM); +} +ModulePassManager +PassBuilder::buildFatLTODefaultPipeline(OptimizationLevel Level, + bool ThinLTOPreLink, bool EmitUseList, + bool EmitSummary) { + assert(Level != OptimizationLevel::O0 && + "Must request optimizations for the default pipeline!"); + ModulePassManager MPM = ThinLTOPreLink + ? buildThinLTOPreLinkDefaultPipeline(Level) + : buildLTOPreLinkDefaultPipeline(Level); + MPM.addPass(EmbedBitcodePass(ThinLTOPreLink, EmitUseList, EmitSummary)); + addPerModuleDefaultPipelinePasses(MPM, Level, /*LTOPreLink=*/false); return MPM; } diff --git a/llvm/lib/Passes/PassRegistry.def b/llvm/lib/Passes/PassRegistry.def --- a/llvm/lib/Passes/PassRegistry.def +++ b/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(true, true, true)) MODULE_PASS("forceattrs", ForceFunctionAttrsPass()) MODULE_PASS("function-import", FunctionImportPass()) MODULE_PASS("globaldce", GlobalDCEPass()) diff --git a/llvm/test/Bitcode/embed-multiple.ll b/llvm/test/Bitcode/embed-multiple.ll new file mode 100644 --- /dev/null +++ b/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. diff --git a/llvm/test/Bitcode/embed-unsupported-object-format.ll b/llvm/test/Bitcode/embed-unsupported-object-format.ll new file mode 100644 --- /dev/null +++ b/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 diff --git a/llvm/test/Bitcode/embed.ll b/llvm/test/Bitcode/embed.ll new file mode 100644 --- /dev/null +++ b/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"