//===-- BasicBlockSectionsProfileReader.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 // //===----------------------------------------------------------------------===// // // Implementation of the basic block sections profile reader pass. It parses // and stores the basic block sections profile file (which is specified via the // `-basic-block-sections` flag). // //===----------------------------------------------------------------------===// #include "llvm/CodeGen/BasicBlockSectionsProfileReader.h" #include "llvm/ADT/DenseSet.h" #include "llvm/ADT/SmallSet.h" #include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallVector.h" #include "llvm/ADT/StringMap.h" #include "llvm/ADT/StringRef.h" #include "llvm/IR/DebugInfoMetadata.h" #include "llvm/Pass.h" #include "llvm/Support/Error.h" #include "llvm/Support/ErrorHandling.h" #include "llvm/Support/LineIterator.h" #include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/Path.h" #include <llvm/ADT/STLExtras.h> usingnamespacellvm; char BasicBlockSectionsProfileReaderWrapperPass::ID = …; INITIALIZE_PASS(…) Expected<UniqueBBID> BasicBlockSectionsProfileReader::parseUniqueBBID(StringRef S) const { … } bool BasicBlockSectionsProfileReader::isFunctionHot(StringRef FuncName) const { … } std::pair<bool, SmallVector<BBClusterInfo>> BasicBlockSectionsProfileReader::getClusterInfoForFunction( StringRef FuncName) const { … } SmallVector<SmallVector<unsigned>> BasicBlockSectionsProfileReader::getClonePathsForFunction( StringRef FuncName) const { … } // Reads the version 1 basic block sections profile. Profile for each function // is encoded as follows: // m <module_name> // f <function_name_1> <function_name_2> ... // c <bb_id_1> <bb_id_2> <bb_id_3> // c <bb_id_4> <bb_id_5> // ... // Module name specifier (starting with 'm') is optional and allows // distinguishing profile for internal-linkage functions with the same name. If // not specified, it will apply to any function with the same name. Function // name specifier (starting with 'f') can specify multiple function name // aliases. Basic block clusters are specified by 'c' and specify the cluster of // basic blocks, and the internal order in which they must be placed in the same // section. // This profile can also specify cloning paths which instruct the compiler to // clone basic blocks along a path. The cloned blocks are then specified in the // cluster information. // The following profile lists two cloning paths (starting with 'p') for // function bar and places the total 9 blocks within two clusters. The first two // blocks of a cloning path specify the edge along which the path is cloned. For // instance, path 1 (1 -> 3 -> 4) instructs that 3 and 4 must be cloned along // the edge 1->3. Within the given clusters, each cloned block is identified by // "<original block id>.<clone id>". For instance, 3.1 represents the first // clone of block 3. Original blocks are specified just with their block ids. A // block cloned multiple times appears with distinct clone ids. The CFG for bar // is shown below before and after cloning with its final clusters labeled. // // f main // f bar // p 1 3 4 # cloning path 1 // p 4 2 # cloning path 2 // c 1 3.1 4.1 6 # basic block cluster 1 // c 0 2 3 4 2.1 5 # basic block cluster 2 // **************************************************************************** // function bar before and after cloning with basic block clusters shown. // **************************************************************************** // .... .............. // 0 -------+ : 0 :---->: 1 ---> 3.1 : // | | : | : :........ | : // v v : v : : v : // +--> 2 --> 5 1 ~~~~~~> +---: 2 : : 4.1: clsuter 1 // | | | | : | : : | : // | v | | : v ....... : v : // | 3 <------+ | : 3 <--+ : : 6 : // | | | : | | : :....: // | v | : v | : // +--- 4 ---> 6 | : 4 | : // | : | | : // | : v | : // | :2.1---+ : cluster 2 // | : | ......: // | : v : // +-->: 5 : // .... // **************************************************************************** Error BasicBlockSectionsProfileReader::ReadV1Profile() { … } Error BasicBlockSectionsProfileReader::ReadV0Profile() { … } // Basic Block Sections can be enabled for a subset of machine basic blocks. // This is done by passing a file containing names of functions for which basic // block sections are desired. Additionally, machine basic block ids of the // functions can also be specified for a finer granularity. Moreover, a cluster // of basic blocks could be assigned to the same section. // Optionally, a debug-info filename can be specified for each function to allow // distinguishing internal-linkage functions of the same name. // A file with basic block sections for all of function main and three blocks // for function foo (of which 1 and 2 are placed in a cluster) looks like this: // (Profile for function foo is only loaded when its debug-info filename // matches 'path/to/foo_file.cc'). // ---------------------------- // list.txt: // !main // !foo M=path/to/foo_file.cc // !!1 2 // !!4 Error BasicBlockSectionsProfileReader::ReadProfile() { … } bool BasicBlockSectionsProfileReaderWrapperPass::doInitialization(Module &M) { … } AnalysisKey BasicBlockSectionsProfileReaderAnalysis::Key; BasicBlockSectionsProfileReader BasicBlockSectionsProfileReaderAnalysis::run(Function &F, FunctionAnalysisManager &AM) { … } bool BasicBlockSectionsProfileReaderWrapperPass::isFunctionHot( StringRef FuncName) const { … } std::pair<bool, SmallVector<BBClusterInfo>> BasicBlockSectionsProfileReaderWrapperPass::getClusterInfoForFunction( StringRef FuncName) const { … } SmallVector<SmallVector<unsigned>> BasicBlockSectionsProfileReaderWrapperPass::getClonePathsForFunction( StringRef FuncName) const { … } BasicBlockSectionsProfileReader & BasicBlockSectionsProfileReaderWrapperPass::getBBSPR() { … } ImmutablePass *llvm::createBasicBlockSectionsProfileReaderWrapperPass( const MemoryBuffer *Buf) { … }