diff options
| author | Dimitry Andric <dim@FreeBSD.org> | 2023-12-18 20:30:12 +0000 |
|---|---|---|
| committer | Dimitry Andric <dim@FreeBSD.org> | 2024-04-19 21:12:03 +0000 |
| commit | c9157d925c489f07ba9c0b2ce47e5149b75969a5 (patch) | |
| tree | 08bc4a3d9cad3f9ebffa558ddf140b9d9257b219 /contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp | |
| parent | 2a66844f606a35d68ad8a8061f4bea204274b3bc (diff) | |
Diffstat (limited to 'contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp')
| -rw-r--r-- | contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp | 93 |
1 files changed, 29 insertions, 64 deletions
diff --git a/contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp b/contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp index 6967ca5160c0..42997d2287d6 100644 --- a/contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp +++ b/contrib/llvm-project/llvm/lib/CodeGen/BasicBlockSections.cpp @@ -168,31 +168,6 @@ updateBranches(MachineFunction &MF, } } -// This function provides the BBCluster information associated with a function. -// Returns true if a valid association exists and false otherwise. -bool getBBClusterInfoForFunction( - const MachineFunction &MF, - BasicBlockSectionsProfileReader *BBSectionsProfileReader, - DenseMap<unsigned, BBClusterInfo> &V) { - - // Find the assoicated cluster information. - std::pair<bool, SmallVector<BBClusterInfo, 4>> P = - BBSectionsProfileReader->getBBClusterInfoForFunction(MF.getName()); - if (!P.first) - return false; - - if (P.second.empty()) { - // This indicates that sections are desired for all basic blocks of this - // function. We clear the BBClusterInfo vector to denote this. - V.clear(); - return true; - } - - for (const BBClusterInfo &BBCI : P.second) - V[BBCI.BBID] = BBCI; - return true; -} - // This function sorts basic blocks according to the cluster's information. // All explicitly specified clusters of basic blocks will be ordered // accordingly. All non-specified BBs go into a separate "Cold" section. @@ -200,12 +175,12 @@ bool getBBClusterInfoForFunction( // clusters, they are moved into a single "Exception" section. Eventually, // clusters are ordered in increasing order of their IDs, with the "Exception" // and "Cold" succeeding all other clusters. -// FuncBBClusterInfo represent the cluster information for basic blocks. It +// FuncClusterInfo represents the cluster information for basic blocks. It // maps from BBID of basic blocks to their cluster information. If this is // empty, it means unique sections for all basic blocks in the function. static void assignSections(MachineFunction &MF, - const DenseMap<unsigned, BBClusterInfo> &FuncBBClusterInfo) { + const DenseMap<UniqueBBID, BBClusterInfo> &FuncClusterInfo) { assert(MF.hasBBSections() && "BB Sections is not set for function."); // This variable stores the section ID of the cluster containing eh_pads (if // all eh_pads are one cluster). If more than one cluster contain eh_pads, we @@ -216,19 +191,17 @@ assignSections(MachineFunction &MF, // With the 'all' option, every basic block is placed in a unique section. // With the 'list' option, every basic block is placed in a section // associated with its cluster, unless we want individual unique sections - // for every basic block in this function (if FuncBBClusterInfo is empty). + // for every basic block in this function (if FuncClusterInfo is empty). if (MF.getTarget().getBBSectionsType() == llvm::BasicBlockSection::All || - FuncBBClusterInfo.empty()) { + FuncClusterInfo.empty()) { // If unique sections are desired for all basic blocks of the function, we // set every basic block's section ID equal to its original position in // the layout (which is equal to its number). This ensures that basic // blocks are ordered canonically. MBB.setSectionID(MBB.getNumber()); } else { - // TODO: Replace `getBBIDOrNumber` with `getBBID` once version 1 is - // deprecated. - auto I = FuncBBClusterInfo.find(MBB.getBBIDOrNumber()); - if (I != FuncBBClusterInfo.end()) { + auto I = FuncClusterInfo.find(*MBB.getBBID()); + if (I != FuncClusterInfo.end()) { MBB.setSectionID(I->second.ClusterID); } else { // BB goes into the special cold section if it is not specified in the @@ -260,7 +233,8 @@ void llvm::sortBasicBlocksAndUpdateBranches( [[maybe_unused]] const MachineBasicBlock *EntryBlock = &MF.front(); SmallVector<MachineBasicBlock *> PreLayoutFallThroughs(MF.getNumBlockIDs()); for (auto &MBB : MF) - PreLayoutFallThroughs[MBB.getNumber()] = MBB.getFallThrough(); + PreLayoutFallThroughs[MBB.getNumber()] = + MBB.getFallThrough(/*JumpToFallThrough=*/false); MF.sort(MBBCmp); assert(&MF.front() == EntryBlock && @@ -285,19 +259,12 @@ void llvm::avoidZeroOffsetLandingPad(MachineFunction &MF) { MachineBasicBlock::iterator MI = MBB.begin(); while (!MI->isEHLabel()) ++MI; - MCInst Nop = MF.getSubtarget().getInstrInfo()->getNop(); - BuildMI(MBB, MI, DebugLoc(), - MF.getSubtarget().getInstrInfo()->get(Nop.getOpcode())); + MF.getSubtarget().getInstrInfo()->insertNoop(MBB, MI); } } } -// This checks if the source of this function has drifted since this binary was -// profiled previously. For now, we are piggy backing on what PGO does to -// detect this with instrumented profiles. PGO emits an hash of the IR and -// checks if the hash has changed. Advanced basic block layout is usually done -// on top of PGO optimized binaries and hence this check works well in practice. -static bool hasInstrProfHashMismatch(MachineFunction &MF) { +bool llvm::hasInstrProfHashMismatch(MachineFunction &MF) { if (!BBSectionsDetectSourceDrift) return false; @@ -318,7 +285,7 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) { assert(BBSectionsType != BasicBlockSection::None && "BB Sections not enabled!"); - // Check for source drift. If the source has changed since the profiles + // Check for source drift. If the source has changed since the profiles // were obtained, optimizing basic blocks might be sub-optimal. // This only applies to BasicBlockSection::List as it creates // clusters of basic blocks using basic block ids. Source drift can @@ -326,32 +293,30 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) { // regards to performance. if (BBSectionsType == BasicBlockSection::List && hasInstrProfHashMismatch(MF)) - return true; - // Renumber blocks before sorting them. This is useful during sorting, - // basic blocks in the same section will retain the default order. - // This renumbering should also be done for basic block labels to match the - // profiles with the correct blocks. - // For LLVM_BB_ADDR_MAP versions 2 and higher, this renumbering serves - // the different purpose of accessing the original layout positions and - // finding the original fallthroughs. - // TODO: Change the above comment accordingly when version 1 is deprecated. + return false; + // Renumber blocks before sorting them. This is useful for accessing the + // original layout positions and finding the original fallthroughs. MF.RenumberBlocks(); if (BBSectionsType == BasicBlockSection::Labels) { MF.setBBSectionsType(BBSectionsType); - return true; + return false; } - BBSectionsProfileReader = &getAnalysis<BasicBlockSectionsProfileReader>(); + DenseMap<UniqueBBID, BBClusterInfo> FuncClusterInfo; + if (BBSectionsType == BasicBlockSection::List) { + auto [HasProfile, ClusterInfo] = + getAnalysis<BasicBlockSectionsProfileReader>() + .getClusterInfoForFunction(MF.getName()); + if (!HasProfile) + return false; + for (auto &BBClusterInfo : ClusterInfo) { + FuncClusterInfo.try_emplace(BBClusterInfo.BBID, BBClusterInfo); + } + } - // Map from BBID of blocks to their cluster information. - DenseMap<unsigned, BBClusterInfo> FuncBBClusterInfo; - if (BBSectionsType == BasicBlockSection::List && - !getBBClusterInfoForFunction(MF, BBSectionsProfileReader, - FuncBBClusterInfo)) - return true; MF.setBBSectionsType(BBSectionsType); - assignSections(MF, FuncBBClusterInfo); + assignSections(MF, FuncClusterInfo); // We make sure that the cluster including the entry basic block precedes all // other clusters. @@ -385,8 +350,8 @@ bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) { // If the two basic block are in the same section, the order is decided by // their position within the section. if (XSectionID.Type == MBBSectionID::SectionType::Default) - return FuncBBClusterInfo.lookup(X.getBBIDOrNumber()).PositionInCluster < - FuncBBClusterInfo.lookup(Y.getBBIDOrNumber()).PositionInCluster; + return FuncClusterInfo.lookup(*X.getBBID()).PositionInCluster < + FuncClusterInfo.lookup(*Y.getBBID()).PositionInCluster; return X.getNumber() < Y.getNumber(); }; |
