1 //===-- BasicBlockSections.cpp ---=========--------------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 // 9 // BasicBlockSections implementation. 10 // 11 // The purpose of this pass is to assign sections to basic blocks when 12 // -fbasic-block-sections= option is used. Further, with profile information 13 // only the subset of basic blocks with profiles are placed in separate sections 14 // and the rest are grouped in a cold section. The exception handling blocks are 15 // treated specially to ensure they are all in one seciton. 16 // 17 // Basic Block Sections 18 // ==================== 19 // 20 // With option, -fbasic-block-sections=list, every function may be split into 21 // clusters of basic blocks. Every cluster will be emitted into a separate 22 // section with its basic blocks sequenced in the given order. To get the 23 // optimized performance, the clusters must form an optimal BB layout for the 24 // function. We insert a symbol at the beginning of every cluster's section to 25 // allow the linker to reorder the sections in any arbitrary sequence. A global 26 // order of these sections would encapsulate the function layout. 27 // For example, consider the following clusters for a function foo (consisting 28 // of 6 basic blocks 0, 1, ..., 5). 29 // 30 // 0 2 31 // 1 3 5 32 // 33 // * Basic blocks 0 and 2 are placed in one section with symbol `foo` 34 // referencing the beginning of this section. 35 // * Basic blocks 1, 3, 5 are placed in a separate section. A new symbol 36 // `foo.__part.1` will reference the beginning of this section. 37 // * Basic block 4 (note that it is not referenced in the list) is placed in 38 // one section, and a new symbol `foo.cold` will point to it. 39 // 40 // There are a couple of challenges to be addressed: 41 // 42 // 1. The last basic block of every cluster should not have any implicit 43 // fallthrough to its next basic block, as it can be reordered by the linker. 44 // The compiler should make these fallthroughs explicit by adding 45 // unconditional jumps.. 46 // 47 // 2. All inter-cluster branch targets would now need to be resolved by the 48 // linker as they cannot be calculated during compile time. This is done 49 // using static relocations. Further, the compiler tries to use short branch 50 // instructions on some ISAs for small branch offsets. This is not possible 51 // for inter-cluster branches as the offset is not determined at compile 52 // time, and therefore, long branch instructions have to be used for those. 53 // 54 // 3. Debug Information (DebugInfo) and Call Frame Information (CFI) emission 55 // needs special handling with basic block sections. DebugInfo needs to be 56 // emitted with more relocations as basic block sections can break a 57 // function into potentially several disjoint pieces, and CFI needs to be 58 // emitted per cluster. This also bloats the object file and binary sizes. 59 // 60 // Basic Block Labels 61 // ================== 62 // 63 // With -fbasic-block-sections=labels, we emit the offsets of BB addresses of 64 // every function into the .llvm_bb_addr_map section. Along with the function 65 // symbols, this allows for mapping of virtual addresses in PMU profiles back to 66 // the corresponding basic blocks. This logic is implemented in AsmPrinter. This 67 // pass only assigns the BBSectionType of every function to ``labels``. 68 // 69 //===----------------------------------------------------------------------===// 70 71 #include "llvm/ADT/Optional.h" 72 #include "llvm/ADT/SmallSet.h" 73 #include "llvm/ADT/SmallVector.h" 74 #include "llvm/ADT/StringMap.h" 75 #include "llvm/ADT/StringRef.h" 76 #include "llvm/CodeGen/BasicBlockSectionUtils.h" 77 #include "llvm/CodeGen/MachineFunction.h" 78 #include "llvm/CodeGen/MachineFunctionPass.h" 79 #include "llvm/CodeGen/Passes.h" 80 #include "llvm/CodeGen/TargetInstrInfo.h" 81 #include "llvm/InitializePasses.h" 82 #include "llvm/Support/Error.h" 83 #include "llvm/Support/LineIterator.h" 84 #include "llvm/Support/MemoryBuffer.h" 85 #include "llvm/Target/TargetMachine.h" 86 87 using llvm::SmallSet; 88 using llvm::SmallVector; 89 using llvm::StringMap; 90 using llvm::StringRef; 91 using namespace llvm; 92 93 // Placing the cold clusters in a separate section mitigates against poor 94 // profiles and allows optimizations such as hugepage mapping to be applied at a 95 // section granularity. Defaults to ".text.split." which is recognized by lld 96 // via the `-z keep-text-section-prefix` flag. 97 cl::opt<std::string> llvm::BBSectionsColdTextPrefix( 98 "bbsections-cold-text-prefix", 99 cl::desc("The text prefix to use for cold basic block clusters"), 100 cl::init(".text.split."), cl::Hidden); 101 102 cl::opt<bool> BBSectionsDetectSourceDrift( 103 "bbsections-detect-source-drift", 104 cl::desc("This checks if there is a fdo instr. profile hash " 105 "mismatch for this function"), 106 cl::init(true), cl::Hidden); 107 108 namespace { 109 110 // This struct represents the cluster information for a machine basic block. 111 struct BBClusterInfo { 112 // MachineBasicBlock ID. 113 unsigned MBBNumber; 114 // Cluster ID this basic block belongs to. 115 unsigned ClusterID; 116 // Position of basic block within the cluster. 117 unsigned PositionInCluster; 118 }; 119 120 using ProgramBBClusterInfoMapTy = StringMap<SmallVector<BBClusterInfo, 4>>; 121 122 class BasicBlockSections : public MachineFunctionPass { 123 public: 124 static char ID; 125 126 // This contains the basic-block-sections profile. 127 const MemoryBuffer *MBuf = nullptr; 128 129 // This encapsulates the BB cluster information for the whole program. 130 // 131 // For every function name, it contains the cluster information for (all or 132 // some of) its basic blocks. The cluster information for every basic block 133 // includes its cluster ID along with the position of the basic block in that 134 // cluster. 135 ProgramBBClusterInfoMapTy ProgramBBClusterInfo; 136 137 // Some functions have alias names. We use this map to find the main alias 138 // name for which we have mapping in ProgramBBClusterInfo. 139 StringMap<StringRef> FuncAliasMap; 140 141 BasicBlockSections(const MemoryBuffer *Buf) 142 : MachineFunctionPass(ID), MBuf(Buf) { 143 initializeBasicBlockSectionsPass(*PassRegistry::getPassRegistry()); 144 }; 145 146 BasicBlockSections() : MachineFunctionPass(ID) { 147 initializeBasicBlockSectionsPass(*PassRegistry::getPassRegistry()); 148 } 149 150 StringRef getPassName() const override { 151 return "Basic Block Sections Analysis"; 152 } 153 154 void getAnalysisUsage(AnalysisUsage &AU) const override; 155 156 /// Read profiles of basic blocks if available here. 157 bool doInitialization(Module &M) override; 158 159 /// Identify basic blocks that need separate sections and prepare to emit them 160 /// accordingly. 161 bool runOnMachineFunction(MachineFunction &MF) override; 162 }; 163 164 } // end anonymous namespace 165 166 char BasicBlockSections::ID = 0; 167 INITIALIZE_PASS(BasicBlockSections, "bbsections-prepare", 168 "Prepares for basic block sections, by splitting functions " 169 "into clusters of basic blocks.", 170 false, false) 171 172 // This function updates and optimizes the branching instructions of every basic 173 // block in a given function to account for changes in the layout. 174 static void updateBranches( 175 MachineFunction &MF, 176 const SmallVector<MachineBasicBlock *, 4> &PreLayoutFallThroughs) { 177 const TargetInstrInfo *TII = MF.getSubtarget().getInstrInfo(); 178 SmallVector<MachineOperand, 4> Cond; 179 for (auto &MBB : MF) { 180 auto NextMBBI = std::next(MBB.getIterator()); 181 auto *FTMBB = PreLayoutFallThroughs[MBB.getNumber()]; 182 // If this block had a fallthrough before we need an explicit unconditional 183 // branch to that block if either 184 // 1- the block ends a section, which means its next block may be 185 // reorderd by the linker, or 186 // 2- the fallthrough block is not adjacent to the block in the new 187 // order. 188 if (FTMBB && (MBB.isEndSection() || &*NextMBBI != FTMBB)) 189 TII->insertUnconditionalBranch(MBB, FTMBB, MBB.findBranchDebugLoc()); 190 191 // We do not optimize branches for machine basic blocks ending sections, as 192 // their adjacent block might be reordered by the linker. 193 if (MBB.isEndSection()) 194 continue; 195 196 // It might be possible to optimize branches by flipping the branch 197 // condition. 198 Cond.clear(); 199 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; // For analyzeBranch. 200 if (TII->analyzeBranch(MBB, TBB, FBB, Cond)) 201 continue; 202 MBB.updateTerminator(FTMBB); 203 } 204 } 205 206 // This function provides the BBCluster information associated with a function. 207 // Returns true if a valid association exists and false otherwise. 208 static bool getBBClusterInfoForFunction( 209 const MachineFunction &MF, const StringMap<StringRef> FuncAliasMap, 210 const ProgramBBClusterInfoMapTy &ProgramBBClusterInfo, 211 std::vector<Optional<BBClusterInfo>> &V) { 212 // Get the main alias name for the function. 213 auto FuncName = MF.getName(); 214 auto R = FuncAliasMap.find(FuncName); 215 StringRef AliasName = R == FuncAliasMap.end() ? FuncName : R->second; 216 217 // Find the assoicated cluster information. 218 auto P = ProgramBBClusterInfo.find(AliasName); 219 if (P == ProgramBBClusterInfo.end()) 220 return false; 221 222 if (P->second.empty()) { 223 // This indicates that sections are desired for all basic blocks of this 224 // function. We clear the BBClusterInfo vector to denote this. 225 V.clear(); 226 return true; 227 } 228 229 V.resize(MF.getNumBlockIDs()); 230 for (auto bbClusterInfo : P->second) { 231 // Bail out if the cluster information contains invalid MBB numbers. 232 if (bbClusterInfo.MBBNumber >= MF.getNumBlockIDs()) 233 return false; 234 V[bbClusterInfo.MBBNumber] = bbClusterInfo; 235 } 236 return true; 237 } 238 239 // This function sorts basic blocks according to the cluster's information. 240 // All explicitly specified clusters of basic blocks will be ordered 241 // accordingly. All non-specified BBs go into a separate "Cold" section. 242 // Additionally, if exception handling landing pads end up in more than one 243 // clusters, they are moved into a single "Exception" section. Eventually, 244 // clusters are ordered in increasing order of their IDs, with the "Exception" 245 // and "Cold" succeeding all other clusters. 246 // FuncBBClusterInfo represent the cluster information for basic blocks. If this 247 // is empty, it means unique sections for all basic blocks in the function. 248 static void 249 assignSections(MachineFunction &MF, 250 const std::vector<Optional<BBClusterInfo>> &FuncBBClusterInfo) { 251 assert(MF.hasBBSections() && "BB Sections is not set for function."); 252 // This variable stores the section ID of the cluster containing eh_pads (if 253 // all eh_pads are one cluster). If more than one cluster contain eh_pads, we 254 // set it equal to ExceptionSectionID. 255 Optional<MBBSectionID> EHPadsSectionID; 256 257 for (auto &MBB : MF) { 258 // With the 'all' option, every basic block is placed in a unique section. 259 // With the 'list' option, every basic block is placed in a section 260 // associated with its cluster, unless we want individual unique sections 261 // for every basic block in this function (if FuncBBClusterInfo is empty). 262 if (MF.getTarget().getBBSectionsType() == llvm::BasicBlockSection::All || 263 FuncBBClusterInfo.empty()) { 264 // If unique sections are desired for all basic blocks of the function, we 265 // set every basic block's section ID equal to its number (basic block 266 // id). This further ensures that basic blocks are ordered canonically. 267 MBB.setSectionID({static_cast<unsigned int>(MBB.getNumber())}); 268 } else if (FuncBBClusterInfo[MBB.getNumber()].hasValue()) 269 MBB.setSectionID(FuncBBClusterInfo[MBB.getNumber()]->ClusterID); 270 else { 271 // BB goes into the special cold section if it is not specified in the 272 // cluster info map. 273 MBB.setSectionID(MBBSectionID::ColdSectionID); 274 } 275 276 if (MBB.isEHPad() && EHPadsSectionID != MBB.getSectionID() && 277 EHPadsSectionID != MBBSectionID::ExceptionSectionID) { 278 // If we already have one cluster containing eh_pads, this must be updated 279 // to ExceptionSectionID. Otherwise, we set it equal to the current 280 // section ID. 281 EHPadsSectionID = EHPadsSectionID.hasValue() 282 ? MBBSectionID::ExceptionSectionID 283 : MBB.getSectionID(); 284 } 285 } 286 287 // If EHPads are in more than one section, this places all of them in the 288 // special exception section. 289 if (EHPadsSectionID == MBBSectionID::ExceptionSectionID) 290 for (auto &MBB : MF) 291 if (MBB.isEHPad()) 292 MBB.setSectionID(EHPadsSectionID.getValue()); 293 } 294 295 void llvm::sortBasicBlocksAndUpdateBranches( 296 MachineFunction &MF, MachineBasicBlockComparator MBBCmp) { 297 SmallVector<MachineBasicBlock *, 4> PreLayoutFallThroughs( 298 MF.getNumBlockIDs()); 299 for (auto &MBB : MF) 300 PreLayoutFallThroughs[MBB.getNumber()] = MBB.getFallThrough(); 301 302 MF.sort(MBBCmp); 303 304 // Set IsBeginSection and IsEndSection according to the assigned section IDs. 305 MF.assignBeginEndSections(); 306 307 // After reordering basic blocks, we must update basic block branches to 308 // insert explicit fallthrough branches when required and optimize branches 309 // when possible. 310 updateBranches(MF, PreLayoutFallThroughs); 311 } 312 313 // If the exception section begins with a landing pad, that landing pad will 314 // assume a zero offset (relative to @LPStart) in the LSDA. However, a value of 315 // zero implies "no landing pad." This function inserts a NOP just before the EH 316 // pad label to ensure a nonzero offset. Returns true if padding is not needed. 317 static bool avoidZeroOffsetLandingPad(MachineFunction &MF) { 318 for (auto &MBB : MF) { 319 if (MBB.isBeginSection() && MBB.isEHPad()) { 320 MachineBasicBlock::iterator MI = MBB.begin(); 321 while (!MI->isEHLabel()) 322 ++MI; 323 MCInst Nop = MF.getSubtarget().getInstrInfo()->getNop(); 324 BuildMI(MBB, MI, DebugLoc(), 325 MF.getSubtarget().getInstrInfo()->get(Nop.getOpcode())); 326 return false; 327 } 328 } 329 return true; 330 } 331 332 // This checks if the source of this function has drifted since this binary was 333 // profiled previously. For now, we are piggy backing on what PGO does to 334 // detect this with instrumented profiles. PGO emits an hash of the IR and 335 // checks if the hash has changed. Advanced basic block layout is usually done 336 // on top of PGO optimized binaries and hence this check works well in practice. 337 static bool hasInstrProfHashMismatch(MachineFunction &MF) { 338 if (!BBSectionsDetectSourceDrift) 339 return false; 340 341 const char MetadataName[] = "instr_prof_hash_mismatch"; 342 auto *Existing = MF.getFunction().getMetadata(LLVMContext::MD_annotation); 343 if (Existing) { 344 MDTuple *Tuple = cast<MDTuple>(Existing); 345 for (auto &N : Tuple->operands()) 346 if (cast<MDString>(N.get())->getString() == MetadataName) 347 return true; 348 } 349 350 return false; 351 } 352 353 bool BasicBlockSections::runOnMachineFunction(MachineFunction &MF) { 354 auto BBSectionsType = MF.getTarget().getBBSectionsType(); 355 assert(BBSectionsType != BasicBlockSection::None && 356 "BB Sections not enabled!"); 357 358 // Check for source drift. If the source has changed since the profiles 359 // were obtained, optimizing basic blocks might be sub-optimal. 360 // This only applies to BasicBlockSection::List as it creates 361 // clusters of basic blocks using basic block ids. Source drift can 362 // invalidate these groupings leading to sub-optimal code generation with 363 // regards to performance. 364 if (BBSectionsType == BasicBlockSection::List && 365 hasInstrProfHashMismatch(MF)) 366 return true; 367 368 // Renumber blocks before sorting them for basic block sections. This is 369 // useful during sorting, basic blocks in the same section will retain the 370 // default order. This renumbering should also be done for basic block 371 // labels to match the profiles with the correct blocks. 372 MF.RenumberBlocks(); 373 374 if (BBSectionsType == BasicBlockSection::Labels) { 375 MF.setBBSectionsType(BBSectionsType); 376 return true; 377 } 378 379 std::vector<Optional<BBClusterInfo>> FuncBBClusterInfo; 380 if (BBSectionsType == BasicBlockSection::List && 381 !getBBClusterInfoForFunction(MF, FuncAliasMap, ProgramBBClusterInfo, 382 FuncBBClusterInfo)) 383 return true; 384 MF.setBBSectionsType(BBSectionsType); 385 assignSections(MF, FuncBBClusterInfo); 386 387 // We make sure that the cluster including the entry basic block precedes all 388 // other clusters. 389 auto EntryBBSectionID = MF.front().getSectionID(); 390 391 // Helper function for ordering BB sections as follows: 392 // * Entry section (section including the entry block). 393 // * Regular sections (in increasing order of their Number). 394 // ... 395 // * Exception section 396 // * Cold section 397 auto MBBSectionOrder = [EntryBBSectionID](const MBBSectionID &LHS, 398 const MBBSectionID &RHS) { 399 // We make sure that the section containing the entry block precedes all the 400 // other sections. 401 if (LHS == EntryBBSectionID || RHS == EntryBBSectionID) 402 return LHS == EntryBBSectionID; 403 return LHS.Type == RHS.Type ? LHS.Number < RHS.Number : LHS.Type < RHS.Type; 404 }; 405 406 // We sort all basic blocks to make sure the basic blocks of every cluster are 407 // contiguous and ordered accordingly. Furthermore, clusters are ordered in 408 // increasing order of their section IDs, with the exception and the 409 // cold section placed at the end of the function. 410 auto Comparator = [&](const MachineBasicBlock &X, 411 const MachineBasicBlock &Y) { 412 auto XSectionID = X.getSectionID(); 413 auto YSectionID = Y.getSectionID(); 414 if (XSectionID != YSectionID) 415 return MBBSectionOrder(XSectionID, YSectionID); 416 // If the two basic block are in the same section, the order is decided by 417 // their position within the section. 418 if (XSectionID.Type == MBBSectionID::SectionType::Default) 419 return FuncBBClusterInfo[X.getNumber()]->PositionInCluster < 420 FuncBBClusterInfo[Y.getNumber()]->PositionInCluster; 421 return X.getNumber() < Y.getNumber(); 422 }; 423 424 sortBasicBlocksAndUpdateBranches(MF, Comparator); 425 avoidZeroOffsetLandingPad(MF); 426 return true; 427 } 428 429 // Basic Block Sections can be enabled for a subset of machine basic blocks. 430 // This is done by passing a file containing names of functions for which basic 431 // block sections are desired. Additionally, machine basic block ids of the 432 // functions can also be specified for a finer granularity. Moreover, a cluster 433 // of basic blocks could be assigned to the same section. 434 // A file with basic block sections for all of function main and three blocks 435 // for function foo (of which 1 and 2 are placed in a cluster) looks like this: 436 // ---------------------------- 437 // list.txt: 438 // !main 439 // !foo 440 // !!1 2 441 // !!4 442 static Error getBBClusterInfo(const MemoryBuffer *MBuf, 443 ProgramBBClusterInfoMapTy &ProgramBBClusterInfo, 444 StringMap<StringRef> &FuncAliasMap) { 445 assert(MBuf); 446 line_iterator LineIt(*MBuf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'); 447 448 auto invalidProfileError = [&](auto Message) { 449 return make_error<StringError>( 450 Twine("Invalid profile " + MBuf->getBufferIdentifier() + " at line " + 451 Twine(LineIt.line_number()) + ": " + Message), 452 inconvertibleErrorCode()); 453 }; 454 455 auto FI = ProgramBBClusterInfo.end(); 456 457 // Current cluster ID corresponding to this function. 458 unsigned CurrentCluster = 0; 459 // Current position in the current cluster. 460 unsigned CurrentPosition = 0; 461 462 // Temporary set to ensure every basic block ID appears once in the clusters 463 // of a function. 464 SmallSet<unsigned, 4> FuncBBIDs; 465 466 for (; !LineIt.is_at_eof(); ++LineIt) { 467 StringRef S(*LineIt); 468 if (S[0] == '@') 469 continue; 470 // Check for the leading "!" 471 if (!S.consume_front("!") || S.empty()) 472 break; 473 // Check for second "!" which indicates a cluster of basic blocks. 474 if (S.consume_front("!")) { 475 if (FI == ProgramBBClusterInfo.end()) 476 return invalidProfileError( 477 "Cluster list does not follow a function name specifier."); 478 SmallVector<StringRef, 4> BBIndexes; 479 S.split(BBIndexes, ' '); 480 // Reset current cluster position. 481 CurrentPosition = 0; 482 for (auto BBIndexStr : BBIndexes) { 483 unsigned long long BBIndex; 484 if (getAsUnsignedInteger(BBIndexStr, 10, BBIndex)) 485 return invalidProfileError(Twine("Unsigned integer expected: '") + 486 BBIndexStr + "'."); 487 if (!FuncBBIDs.insert(BBIndex).second) 488 return invalidProfileError(Twine("Duplicate basic block id found '") + 489 BBIndexStr + "'."); 490 if (!BBIndex && CurrentPosition) 491 return invalidProfileError("Entry BB (0) does not begin a cluster."); 492 493 FI->second.emplace_back(BBClusterInfo{ 494 ((unsigned)BBIndex), CurrentCluster, CurrentPosition++}); 495 } 496 CurrentCluster++; 497 } else { // This is a function name specifier. 498 // Function aliases are separated using '/'. We use the first function 499 // name for the cluster info mapping and delegate all other aliases to 500 // this one. 501 SmallVector<StringRef, 4> Aliases; 502 S.split(Aliases, '/'); 503 for (size_t i = 1; i < Aliases.size(); ++i) 504 FuncAliasMap.try_emplace(Aliases[i], Aliases.front()); 505 506 // Prepare for parsing clusters of this function name. 507 // Start a new cluster map for this function name. 508 FI = ProgramBBClusterInfo.try_emplace(Aliases.front()).first; 509 CurrentCluster = 0; 510 FuncBBIDs.clear(); 511 } 512 } 513 return Error::success(); 514 } 515 516 bool BasicBlockSections::doInitialization(Module &M) { 517 if (!MBuf) 518 return false; 519 if (auto Err = getBBClusterInfo(MBuf, ProgramBBClusterInfo, FuncAliasMap)) 520 report_fatal_error(std::move(Err)); 521 return false; 522 } 523 524 void BasicBlockSections::getAnalysisUsage(AnalysisUsage &AU) const { 525 AU.setPreservesAll(); 526 MachineFunctionPass::getAnalysisUsage(AU); 527 } 528 529 MachineFunctionPass * 530 llvm::createBasicBlockSectionsPass(const MemoryBuffer *Buf) { 531 return new BasicBlockSections(Buf); 532 } 533