//===-- BasicBlockSections.cpp ---=========--------------------------------===// // // 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 // //===----------------------------------------------------------------------===// // // BasicBlockSections implementation. // // The purpose of this pass is to assign sections to basic blocks when // -fbasic-block-sections= option is used. Further, with profile information // only the subset of basic blocks with profiles are placed in separate sections // and the rest are grouped in a cold section. The exception handling blocks are // treated specially to ensure they are all in one seciton. // // Basic Block Sections // ==================== // // With option, -fbasic-block-sections=list, every function may be split into // clusters of basic blocks. Every cluster will be emitted into a separate // section with its basic blocks sequenced in the given order. To get the // optimized performance, the clusters must form an optimal BB layout for the // function. We insert a symbol at the beginning of every cluster's section to // allow the linker to reorder the sections in any arbitrary sequence. A global // order of these sections would encapsulate the function layout. // For example, consider the following clusters for a function foo (consisting // of 6 basic blocks 0, 1, ..., 5). // // 0 2 // 1 3 5 // // * Basic blocks 0 and 2 are placed in one section with symbol `foo` // referencing the beginning of this section. // * Basic blocks 1, 3, 5 are placed in a separate section. A new symbol // `foo.__part.1` will reference the beginning of this section. // * Basic block 4 (note that it is not referenced in the list) is placed in // one section, and a new symbol `foo.cold` will point to it. // // There are a couple of challenges to be addressed: // // 1. The last basic block of every cluster should not have any implicit // fallthrough to its next basic block, as it can be reordered by the linker. // The compiler should make these fallthroughs explicit by adding // unconditional jumps.. // // 2. All inter-cluster branch targets would now need to be resolved by the // linker as they cannot be calculated during compile time. This is done // using static relocations. Further, the compiler tries to use short branch // instructions on some ISAs for small branch offsets. This is not possible // for inter-cluster branches as the offset is not determined at compile // time, and therefore, long branch instructions have to be used for those. // // 3. Debug Information (DebugInfo) and Call Frame Information (CFI) emission // needs special handling with basic block sections. DebugInfo needs to be // emitted with more relocations as basic block sections can break a // function into potentially several disjoint pieces, and CFI needs to be // emitted per cluster. This also bloats the object file and binary sizes. // // Basic Block Address Map // ================== // // With -fbasic-block-address-map, we emit the offsets of BB addresses of // every function into the .llvm_bb_addr_map section. Along with the function // symbols, this allows for mapping of virtual addresses in PMU profiles back to // the corresponding basic blocks. This logic is implemented in AsmPrinter. This // pass only assigns the BBSectionType of every function to ``labels``. // //===----------------------------------------------------------------------===// #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringRef.h" #include "llvm/CodeGen/BasicBlockSectionUtils.h" #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h" #include "llvm/CodeGen/MachineDominators.h" #include "llvm/CodeGen/MachineFunction.h" #include "llvm/CodeGen/MachineFunctionPass.h" #include "llvm/CodeGen/MachinePostDominators.h" #include "llvm/CodeGen/Passes.h" #include "llvm/CodeGen/TargetInstrInfo.h" #include "llvm/InitializePasses.h" #include "llvm/Target/TargetMachine.h" #include <optional> usingnamespacellvm; // Placing the cold clusters in a separate section mitigates against poor // profiles and allows optimizations such as hugepage mapping to be applied at a // section granularity. Defaults to ".text.split." which is recognized by lld // via the `-z keep-text-section-prefix` flag. cl::opt<std::string> llvm::BBSectionsColdTextPrefix( "bbsections-cold-text-prefix", cl::desc("The text prefix to use for cold basic block clusters"), cl::init(".text.split."), cl::Hidden); static cl::opt<bool> BBSectionsDetectSourceDrift( "bbsections-detect-source-drift", cl::desc("This checks if there is a fdo instr. profile hash " "mismatch for this function"), cl::init(true), cl::Hidden); namespace { class BasicBlockSections : public MachineFunctionPass { … }; } // end anonymous namespace char BasicBlockSections::ID = …; INITIALIZE_PASS_BEGIN( BasicBlockSections, "bbsections-prepare", "Prepares for basic block sections, by splitting functions " "into clusters of basic blocks.", false, false) INITIALIZE_PASS_DEPENDENCY(BasicBlockSectionsProfileReaderWrapperPass) INITIALIZE_PASS_END(BasicBlockSections, "bbsections-prepare", "Prepares for basic block sections, by splitting functions " "into clusters of basic blocks.", false, false) // This function updates and optimizes the branching instructions of every basic // block in a given function to account for changes in the layout. static void updateBranches(MachineFunction &MF, const SmallVector<MachineBasicBlock *> &PreLayoutFallThroughs) { … } // 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. // Additionally, if exception handling landing pads end up in more than one // 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. // 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<UniqueBBID, BBClusterInfo> &FuncClusterInfo) { … } void llvm::sortBasicBlocksAndUpdateBranches( MachineFunction &MF, MachineBasicBlockComparator MBBCmp) { … } // If the exception section begins with a landing pad, that landing pad will // assume a zero offset (relative to @LPStart) in the LSDA. However, a value of // zero implies "no landing pad." This function inserts a NOP just before the EH // pad label to ensure a nonzero offset. void llvm::avoidZeroOffsetLandingPad(MachineFunction &MF) { … } bool llvm::hasInstrProfHashMismatch(MachineFunction &MF) { … } // Identify, arrange, and modify basic blocks which need separate sections // according to the specification provided by the -fbasic-block-sections flag. bool BasicBlockSections::handleBBSections(MachineFunction &MF) { … } // When the BB address map needs to be generated, this renumbers basic blocks to // make them appear in increasing order of their IDs in the function. This // avoids the need to store basic block IDs in the BB address map section, since // they can be determined implicitly. bool BasicBlockSections::handleBBAddrMap(MachineFunction &MF) { … } bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) { … } void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU) const { … } MachineFunctionPass *llvm::createBasicBlockSectionsPass() { … }