Commit df082ac4 authored by Sriraman Tallam's avatar Sriraman Tallam
Browse files

Basic Block Sections support in LLVM.

This is the second patch in a series of patches to enable basic block
sections support.

This patch adds support for:

* Creating direct jumps at the end of basic blocks that have fall
through instructions.
* New pass, bbsections-prepare, that analyzes placement of basic blocks
in sections.
* Actual placing of a basic block in a unique section with special
handling of exception handling blocks.
* Supports placing a subset of basic blocks in a unique section.
* Support for MIR serialization and deserialization with basic block
sections.

Parent patch : D68063
Differential Revision: https://reviews.llvm.org/D73674
parent 30dc342f
Loading
Loading
Loading
Loading
+43 −0
Original line number Diff line number Diff line
@@ -46,6 +46,19 @@ class raw_ostream;
class TargetRegisterClass;
class TargetRegisterInfo;

enum MachineBasicBlockSection : unsigned {
  ///  This is also the order of sections in a function.  Basic blocks that are
  ///  part of the original function section (entry block) come first, followed
  ///  by exception handling basic blocks, cold basic blocks and finally basic
  //   blocks that need unique sections.
  MBBS_Entry,
  MBBS_Exception,
  MBBS_Cold,
  MBBS_Unique,
  ///  None implies no sections for any basic block, the default.
  MBBS_None,
};

template <> struct ilist_traits<MachineInstr> {
private:
  friend class MachineBasicBlock; // Set by the owning MachineBasicBlock.
@@ -130,6 +143,9 @@ private:
  /// Indicate that this basic block is the entry block of a cleanup funclet.
  bool IsCleanupFuncletEntry = false;

  /// Stores the Section type of the basic block with basic block sections.
  MachineBasicBlockSection SectionType = MBBS_None;

  /// Default target of the callbr of a basic block.
  bool InlineAsmBrDefaultTarget = false;

@@ -140,6 +156,9 @@ private:
  /// is only computed once and is cached.
  mutable MCSymbol *CachedMCSymbol = nullptr;

  /// Used during basic block sections to mark the end of a basic block.
  MCSymbol *EndMCSymbol = nullptr;

  // Intrusive list support
  MachineBasicBlock() = default;

@@ -415,6 +434,18 @@ public:
  /// Indicates if this is the entry block of a cleanup funclet.
  void setIsCleanupFuncletEntry(bool V = true) { IsCleanupFuncletEntry = V; }

  /// Returns true if this block begins any section.
  bool isBeginSection() const;

  /// Returns true if this block ends any section.
  bool isEndSection() const;

  /// Returns the type of section this basic block belongs to.
  MachineBasicBlockSection getSectionType() const { return SectionType; }

  /// Indicate that the basic block belongs to a Section Type.
  void setSectionType(MachineBasicBlockSection V) { SectionType = V; }

  /// Returns true if this is the indirect dest of an INLINEASM_BR.
  bool isInlineAsmBrIndirectTarget(const MachineBasicBlock *Tgt) const {
    return InlineAsmBrIndirectTargets.count(Tgt);
@@ -453,6 +484,12 @@ public:
  void moveBefore(MachineBasicBlock *NewAfter);
  void moveAfter(MachineBasicBlock *NewBefore);

  /// Returns true if this and MBB belong to the same section.
  bool sameSection(const MachineBasicBlock *MBB) const;

  /// Returns the basic block that ends the section which contains this one.
  const MachineBasicBlock *getSectionEndMBB() const;

  /// Update the terminator instructions in block to account for changes to the
  /// layout. If the block previously used a fallthrough, it may now need a
  /// branch, and if it previously used branching it may now be able to use a
@@ -839,6 +876,12 @@ public:
  /// Return the MCSymbol for this basic block.
  MCSymbol *getSymbol() const;

  /// Sets the MCSymbol corresponding to the end of this basic block.
  void setEndMCSymbol(MCSymbol *Sym) { EndMCSymbol = Sym; }

  /// Returns the MCSymbol corresponding to the end of this basic block.
  MCSymbol *getEndMCSymbol() const { return EndMCSymbol; }

  Optional<uint64_t> getIrrLoopHeaderWeight() const {
    return IrrLoopHeaderWeight;
  }
+59 −0
Original line number Diff line number Diff line
@@ -37,6 +37,7 @@
#include "llvm/Support/Compiler.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/Recycler.h"
#include "llvm/Target/TargetOptions.h"
#include <cassert>
#include <cstdint>
#include <memory>
@@ -64,6 +65,7 @@ class MachineRegisterInfo;
class MCContext;
class MCInstrDesc;
class MCSymbol;
class MCSection;
class Pass;
class PseudoSourceValueManager;
class raw_ostream;
@@ -244,6 +246,9 @@ class MachineFunction {
  // Keep track of jump tables for switch instructions
  MachineJumpTableInfo *JumpTableInfo;

  // Keep track of the function section.
  MCSection *Section = nullptr;

  // Keeps track of Wasm exception handling related data. This will be null for
  // functions that aren't using a wasm EH personality.
  WasmEHFuncInfo *WasmEHInfo = nullptr;
@@ -257,6 +262,12 @@ class MachineFunction {
  // numbered and this vector keeps track of the mapping from ID's to MBB's.
  std::vector<MachineBasicBlock*> MBBNumbering;

  // Unary encoding of basic block symbols is used to reduce size of ".strtab".
  // Basic block number 'i' gets a prefix of length 'i'.  The ith character also
  // denotes the type of basic block number 'i'.  Return blocks are marked with
  // 'r', landing pads with 'l' and regular blocks with 'a'.
  std::vector<char> BBSectionsSymbolPrefix;

  // Pool-allocate MachineFunction-lifetime and IR objects.
  BumpPtrAllocator Allocator;

@@ -332,6 +343,14 @@ class MachineFunction {
  bool HasEHScopes = false;
  bool HasEHFunclets = false;

  /// Section Type for basic blocks, only relevant with basic block sections.
  BasicBlockSection BBSectionsType = BasicBlockSection::None;

  /// With Basic Block Sections, this stores the bb ranges of cold and
  /// exception sections.
  std::pair<int, int> ColdSectionRange = {-1, -1};
  std::pair<int, int> ExceptionSectionRange = {-1, -1};

  /// List of C++ TypeInfo used.
  std::vector<const GlobalValue *> TypeInfos;

@@ -453,6 +472,12 @@ public:
  MachineModuleInfo &getMMI() const { return MMI; }
  MCContext &getContext() const { return Ctx; }

  /// Returns the Section this function belongs to.
  MCSection *getSection() const { return Section; }

  /// Indicates the Section this function belongs to.
  void setSection(MCSection *S) { Section = S; }

  PseudoSourceValueManager &getPSVManager() const { return *PSVManager; }

  /// Return the DataLayout attached to the Module associated to this MF.
@@ -467,6 +492,35 @@ public:
  /// getFunctionNumber - Return a unique ID for the current function.
  unsigned getFunctionNumber() const { return FunctionNumber; }

  /// Returns true if this function has basic block sections enabled.
  bool hasBBSections() const {
    return (BBSectionsType == BasicBlockSection::All ||
            BBSectionsType == BasicBlockSection::List);
  }

  /// Returns true if basic block labels are to be generated for this function.
  bool hasBBLabels() const {
    return BBSectionsType == BasicBlockSection::Labels;
  }

  void setBBSectionsType(BasicBlockSection V) { BBSectionsType = V; }

  void setSectionRange();

  /// Returns true if this basic block number starts a cold or exception
  /// section.
  bool isSectionStartMBB(int N) const {
    return (N == ColdSectionRange.first || N == ExceptionSectionRange.first);
  }

  /// Returns true if this basic block ends a cold or exception section.
  bool isSectionEndMBB(int N) const {
    return (N == ColdSectionRange.second || N == ExceptionSectionRange.second);
  }

  /// Creates basic block Labels for this function.
  void createBBLabels();

  /// getTarget - Return the target machine this machine code is compiled with
  const LLVMTargetMachine &getTarget() const { return Target; }

@@ -1014,6 +1068,11 @@ public:
  /// of the instruction stream.
  void copyCallSiteInfo(const MachineInstr *Old,
                        const MachineInstr *New);

  const std::vector<char> &getBBSectionsSymbolPrefix() const {
    return BBSectionsSymbolPrefix;
  }

  /// Move the call site info from \p Old to \New call site info. This function
  /// is used when we are replacing one call instruction with another one to
  /// the same callee.
+7 −0
Original line number Diff line number Diff line
@@ -22,6 +22,7 @@ namespace llvm {
class FunctionPass;
class MachineFunction;
class MachineFunctionPass;
class MemoryBuffer;
class ModulePass;
class Pass;
class TargetMachine;
@@ -42,6 +43,12 @@ namespace llvm {
  /// the entry block.
  FunctionPass *createUnreachableBlockEliminationPass();

  /// createBBSectionsPrepare Pass - This pass assigns sections to machine basic
  /// blocks and is enabled with -fbasicblock-sections.
  /// Buf is a memory buffer that contains the list of functions and basic
  /// block ids to selectively enable basic block sections.
  MachineFunctionPass *createBBSectionsPreparePass(const MemoryBuffer *Buf);

  /// MachineFunctionPrinter pass - This pass prints out the machine function to
  /// the given stream as a debugging tool.
  MachineFunctionPass *
+9 −0
Original line number Diff line number Diff line
@@ -65,6 +65,15 @@ public:
  MCSection *getSectionForJumpTable(const Function &F,
                                    const TargetMachine &TM) const override;

  MCSection *
  getSectionForMachineBasicBlock(const Function &F,
                                 const MachineBasicBlock &MBB,
                                 const TargetMachine &TM) const override;

  MCSection *getNamedSectionForMachineBasicBlock(
      const Function &F, const MachineBasicBlock &MBB, const TargetMachine &TM,
      const char *Suffix) const override;

  bool shouldPutJumpTableInFunctionSection(bool UsesLabelDifference,
                                           const Function &F) const override;

+1 −0
Original line number Diff line number Diff line
@@ -77,6 +77,7 @@ void initializeAssumptionCacheTrackerPass(PassRegistry&);
void initializeAtomicExpandPass(PassRegistry&);
void initializeAttributorLegacyPassPass(PassRegistry&);
void initializeAttributorCGSCCLegacyPassPass(PassRegistry &);
void initializeBBSectionsPreparePass(PassRegistry &);
void initializeBDCELegacyPassPass(PassRegistry&);
void initializeBarrierNoopPass(PassRegistry&);
void initializeBasicAAWrapperPassPass(PassRegistry&);
Loading