Index: llvm/include/llvm/CodeGen/CommandFlags.inc =================================================================== --- llvm/include/llvm/CodeGen/CommandFlags.inc +++ llvm/include/llvm/CodeGen/CommandFlags.inc @@ -238,6 +238,12 @@ cl::desc("Emit functions into separate sections"), cl::init(false)); +static cl::opt + BBSections("basicblock-sections", + cl::desc("Emit basic blocks into separate sections"), + cl::value_desc("all | | none"), + cl::init("none")); + static cl::opt TLSSize("tls-size", cl::desc("Bit size of immediate TLS offsets"), cl::init(0)); @@ -251,6 +257,11 @@ cl::desc("Give unique names to every section"), cl::init(true)); +static cl::opt UniqueBBSectionNames( + "unique-bb-section-names", + cl::desc("Give unique names to every basic block section"), + cl::init(false)); + static cl::opt EABIVersion("meabi", cl::desc("Set EABI type (default depends on triple):"), cl::init(EABI::Default), @@ -291,6 +302,27 @@ cl::desc("Always emit a debug frame section."), cl::init(false)); +static llvm::BasicBlockSection +getBBSectionsMode(llvm::TargetOptions &Options) { + if (BBSections == "all") + return BasicBlockSection::All; + else if (BBSections == "labels") + return BasicBlockSection::Labels; + else if (BBSections == "none") + return BasicBlockSection::None; + else { + ErrorOr> MBOrErr = + MemoryBuffer::getFile(BBSections); + if (!MBOrErr) { + errs() << "Error loading basic block sections function list file: " + << MBOrErr.getError().message() << "\n"; + } else { + Options.BBSectionsFuncListBuf = std::move(*MBOrErr); + } + return BasicBlockSection::List; + } +} + // Common utility function tightly tied to the options listed here. Initializes // a TargetOptions object with CodeGen flags and returns it. static TargetOptions InitTargetOptionsFromCodeGenFlags() { @@ -314,7 +346,9 @@ Options.RelaxELFRelocations = RelaxELFRelocations; Options.DataSections = DataSections; Options.FunctionSections = FunctionSections; + Options.BBSections = getBBSectionsMode(Options); Options.UniqueSectionNames = UniqueSectionNames; + Options.UniqueBBSectionNames = UniqueBBSectionNames; Options.TLSSize = TLSSize; Options.EmulatedTLS = EmulatedTLS; Options.ExplicitEmulatedTLS = EmulatedTLS.getNumOccurrences() > 0; Index: llvm/include/llvm/Target/TargetMachine.h =================================================================== --- llvm/include/llvm/Target/TargetMachine.h +++ llvm/include/llvm/Target/TargetMachine.h @@ -245,6 +245,9 @@ bool getUniqueSectionNames() const { return Options.UniqueSectionNames; } + /// Return true if unique basic block section names must be generated. + bool getUniqueBBSectionNames() const { return Options.UniqueBBSectionNames; } + /// Return true if data objects should be emitted into their own section, /// corresponds to -fdata-sections. bool getDataSections() const { @@ -257,6 +260,17 @@ return Options.FunctionSections; } + /// If basic blocks should be emitted into their own section, + /// corresponding to -fbasicblock-sections. + llvm::BasicBlockSection getBBSectionsType() const { + return Options.BBSections; + } + + /// Get the list of functions and basic block ids that need unique sections. + const MemoryBuffer *getBBSectionsFuncListBuf() const { + return Options.BBSectionsFuncListBuf.get(); + } + /// Get a \c TargetIRAnalysis appropriate for the target. /// /// This is used to construct the new pass manager's target IR analysis pass, Index: llvm/include/llvm/Target/TargetOptions.h =================================================================== --- llvm/include/llvm/Target/TargetOptions.h +++ llvm/include/llvm/Target/TargetOptions.h @@ -18,6 +18,7 @@ namespace llvm { class MachineFunction; + class MemoryBuffer; class Module; namespace FloatABI { @@ -63,6 +64,18 @@ }; } + enum class BasicBlockSection { + All, // Use Basic Block Sections for all basic blocks. A section + // for every basic block can significantly bloat object file sizes. + List, // Get list of functions & BBs from a file. Selectively enables + // basic block sections for a subset of basic blocks which can be + // used to control object size bloats from creating sections. + Labels, // Do not use Basic Block Sections but label basic blocks. This + // is useful when associating profile counts from virtual addresses + // to basic blocks. + None // Do not use Basic Block Sections. + }; + enum class EABI { Unknown, Default, // Default means not specified @@ -114,9 +127,9 @@ EnableFastISel(false), EnableGlobalISel(false), UseInitArray(false), DisableIntegratedAS(false), RelaxELFRelocations(false), FunctionSections(false), DataSections(false), - UniqueSectionNames(true), TrapUnreachable(false), - NoTrapAfterNoreturn(false), TLSSize(0), EmulatedTLS(false), - ExplicitEmulatedTLS(false), EnableIPRA(false), + UniqueSectionNames(true), UniqueBBSectionNames(false), + TrapUnreachable(false), NoTrapAfterNoreturn(false), TLSSize(0), + EmulatedTLS(false), ExplicitEmulatedTLS(false), EnableIPRA(false), EmitStackSizeSection(false), EnableMachineOutliner(false), SupportsDefaultOutlining(false), EmitAddrsig(false), EmitCallSiteInfo(false), SupportsDebugEntryValues(false), @@ -225,6 +238,9 @@ unsigned UniqueSectionNames : 1; + /// Use unique names for basic block sections. + unsigned UniqueBBSectionNames : 1; + /// Emit target-specific trap instruction for 'unreachable' IR instructions. unsigned TrapUnreachable : 1; @@ -257,6 +273,13 @@ /// Emit address-significance table. unsigned EmitAddrsig : 1; + /// Emit basic blocks into separate sections. + BasicBlockSection BBSections = BasicBlockSection::None; + + /// Memory Buffer that contains information on sampled basic blocks and used + /// to selectively generate basic block sections. + std::shared_ptr BBSectionsFuncListBuf; + /// The flag enables call site info production. It is used only for debug /// info, and it is restricted only to optimized code. This can be used for /// something else, so that should be controlled in the frontend.