Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:26

0001 //===-- BasicBlockSectionsProfileReader.h - BB sections profile reader pass ==//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This pass creates the basic block cluster info by reading the basic block
0010 // sections profile. The cluster info will be used by the basic-block-sections
0011 // pass to arrange basic blocks in their sections.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
0016 #define LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H
0017 
0018 #include "llvm/ADT/SmallString.h"
0019 #include "llvm/ADT/SmallVector.h"
0020 #include "llvm/ADT/StringMap.h"
0021 #include "llvm/ADT/StringRef.h"
0022 #include "llvm/CodeGen/MachineBasicBlock.h"
0023 #include "llvm/IR/Module.h"
0024 #include "llvm/IR/PassManager.h"
0025 #include "llvm/InitializePasses.h"
0026 #include "llvm/Pass.h"
0027 #include "llvm/Support/Error.h"
0028 #include "llvm/Support/LineIterator.h"
0029 #include "llvm/Support/MemoryBuffer.h"
0030 #include "llvm/Target/TargetMachine.h"
0031 
0032 namespace llvm {
0033 
0034 // This struct represents the cluster information for a machine basic block,
0035 // which is specifed by a unique ID (`MachineBasicBlock::BBID`).
0036 struct BBClusterInfo {
0037   // Basic block ID.
0038   UniqueBBID BBID;
0039   // Cluster ID this basic block belongs to.
0040   unsigned ClusterID;
0041   // Position of basic block within the cluster.
0042   unsigned PositionInCluster;
0043 };
0044 
0045 // This represents the raw input profile for one function.
0046 struct FunctionPathAndClusterInfo {
0047   // BB Cluster information specified by `UniqueBBID`s.
0048   SmallVector<BBClusterInfo> ClusterInfo;
0049   // Paths to clone. A path a -> b -> c -> d implies cloning b, c, and d along
0050   // the edge a -> b (a is not cloned). The index of the path in this vector
0051   // determines the `UniqueBBID::CloneID` of the cloned blocks in that path.
0052   SmallVector<SmallVector<unsigned>> ClonePaths;
0053 };
0054 
0055 // Provides DenseMapInfo for UniqueBBID.
0056 template <> struct DenseMapInfo<UniqueBBID> {
0057   static inline UniqueBBID getEmptyKey() {
0058     unsigned EmptyKey = DenseMapInfo<unsigned>::getEmptyKey();
0059     return UniqueBBID{EmptyKey, EmptyKey};
0060   }
0061   static inline UniqueBBID getTombstoneKey() {
0062     unsigned TombstoneKey = DenseMapInfo<unsigned>::getTombstoneKey();
0063     return UniqueBBID{TombstoneKey, TombstoneKey};
0064   }
0065   static unsigned getHashValue(const UniqueBBID &Val) {
0066     std::pair<unsigned, unsigned> PairVal =
0067         std::make_pair(Val.BaseID, Val.CloneID);
0068     return DenseMapInfo<std::pair<unsigned, unsigned>>::getHashValue(PairVal);
0069   }
0070   static bool isEqual(const UniqueBBID &LHS, const UniqueBBID &RHS) {
0071     return DenseMapInfo<unsigned>::isEqual(LHS.BaseID, RHS.BaseID) &&
0072            DenseMapInfo<unsigned>::isEqual(LHS.CloneID, RHS.CloneID);
0073   }
0074 };
0075 
0076 class BasicBlockSectionsProfileReader {
0077 public:
0078   friend class BasicBlockSectionsProfileReaderWrapperPass;
0079   BasicBlockSectionsProfileReader(const MemoryBuffer *Buf)
0080       : MBuf(Buf), LineIt(*Buf, /*SkipBlanks=*/true, /*CommentMarker=*/'#'){};
0081 
0082   BasicBlockSectionsProfileReader(){};
0083 
0084   // Returns true if basic block sections profile exist for function \p
0085   // FuncName.
0086   bool isFunctionHot(StringRef FuncName) const;
0087 
0088   // Returns a pair with first element representing whether basic block sections
0089   // profile exist for the function \p FuncName, and the second element
0090   // representing the basic block sections profile (cluster info) for this
0091   // function. If the first element is true and the second element is empty, it
0092   // means unique basic block sections are desired for all basic blocks of the
0093   // function.
0094   std::pair<bool, SmallVector<BBClusterInfo>>
0095   getClusterInfoForFunction(StringRef FuncName) const;
0096 
0097   // Returns the path clonings for the given function.
0098   SmallVector<SmallVector<unsigned>>
0099   getClonePathsForFunction(StringRef FuncName) const;
0100 
0101 private:
0102   StringRef getAliasName(StringRef FuncName) const {
0103     auto R = FuncAliasMap.find(FuncName);
0104     return R == FuncAliasMap.end() ? FuncName : R->second;
0105   }
0106 
0107   // Returns a profile parsing error for the current line.
0108   Error createProfileParseError(Twine Message) const {
0109     return make_error<StringError>(
0110         Twine("invalid profile " + MBuf->getBufferIdentifier() + " at line " +
0111               Twine(LineIt.line_number()) + ": " + Message),
0112         inconvertibleErrorCode());
0113   }
0114 
0115   // Parses a `UniqueBBID` from `S`. `S` must be in the form "<bbid>"
0116   // (representing an original block) or "<bbid>.<cloneid>" (representing a
0117   // cloned block) where bbid is a non-negative integer and cloneid is a
0118   // positive integer.
0119   Expected<UniqueBBID> parseUniqueBBID(StringRef S) const;
0120 
0121   // Reads the basic block sections profile for functions in this module.
0122   Error ReadProfile();
0123 
0124   // Reads version 0 profile.
0125   // TODO: Remove this function once version 0 is deprecated.
0126   Error ReadV0Profile();
0127 
0128   // Reads version 1 profile.
0129   Error ReadV1Profile();
0130 
0131   // This contains the basic-block-sections profile.
0132   const MemoryBuffer *MBuf = nullptr;
0133 
0134   // Iterator to the line being parsed.
0135   line_iterator LineIt;
0136 
0137   // Map from every function name in the module to its debug info filename or
0138   // empty string if no debug info is available.
0139   StringMap<SmallString<128>> FunctionNameToDIFilename;
0140 
0141   // This contains the BB cluster information for the whole program.
0142   //
0143   // For every function name, it contains the cloning and cluster information
0144   // for (all or some of) its basic blocks. The cluster information for every
0145   // basic block includes its cluster ID along with the position of the basic
0146   // block in that cluster.
0147   StringMap<FunctionPathAndClusterInfo> ProgramPathAndClusterInfo;
0148 
0149   // Some functions have alias names. We use this map to find the main alias
0150   // name which appears in ProgramPathAndClusterInfo as a key.
0151   StringMap<StringRef> FuncAliasMap;
0152 };
0153 
0154 // Creates a BasicBlockSectionsProfileReader pass to parse the basic block
0155 // sections profile. \p Buf is a memory buffer that contains the list of
0156 // functions and basic block ids to selectively enable basic block sections.
0157 ImmutablePass *
0158 createBasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf);
0159 
0160 /// Analysis pass providing the \c BasicBlockSectionsProfileReader.
0161 ///
0162 /// Note that this pass's result cannot be invalidated, it is immutable for the
0163 /// life of the module.
0164 class BasicBlockSectionsProfileReaderAnalysis
0165     : public AnalysisInfoMixin<BasicBlockSectionsProfileReaderAnalysis> {
0166 
0167 public:
0168   static AnalysisKey Key;
0169   typedef BasicBlockSectionsProfileReader Result;
0170   BasicBlockSectionsProfileReaderAnalysis(const TargetMachine *TM) : TM(TM) {}
0171 
0172   Result run(Function &F, FunctionAnalysisManager &AM);
0173 
0174 private:
0175   const TargetMachine *TM;
0176 };
0177 
0178 class BasicBlockSectionsProfileReaderWrapperPass : public ImmutablePass {
0179 public:
0180   static char ID;
0181   BasicBlockSectionsProfileReader BBSPR;
0182 
0183   BasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf)
0184       : ImmutablePass(ID), BBSPR(BasicBlockSectionsProfileReader(Buf)) {
0185     initializeBasicBlockSectionsProfileReaderWrapperPassPass(
0186         *PassRegistry::getPassRegistry());
0187   };
0188 
0189   BasicBlockSectionsProfileReaderWrapperPass()
0190       : ImmutablePass(ID), BBSPR(BasicBlockSectionsProfileReader()) {
0191     initializeBasicBlockSectionsProfileReaderWrapperPassPass(
0192         *PassRegistry::getPassRegistry());
0193   }
0194 
0195   StringRef getPassName() const override {
0196     return "Basic Block Sections Profile Reader";
0197   }
0198 
0199   bool isFunctionHot(StringRef FuncName) const;
0200 
0201   std::pair<bool, SmallVector<BBClusterInfo>>
0202   getClusterInfoForFunction(StringRef FuncName) const;
0203 
0204   SmallVector<SmallVector<unsigned>>
0205   getClonePathsForFunction(StringRef FuncName) const;
0206 
0207   // Initializes the FunctionNameToDIFilename map for the current module and
0208   // then reads the profile for the matching functions.
0209   bool doInitialization(Module &M) override;
0210 
0211   BasicBlockSectionsProfileReader &getBBSPR();
0212 };
0213 
0214 } // namespace llvm
0215 #endif // LLVM_CODEGEN_BASICBLOCKSECTIONSPROFILEREADER_H