File indexing completed on 2026-05-10 08:43:26
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
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
0035
0036 struct BBClusterInfo {
0037
0038 UniqueBBID BBID;
0039
0040 unsigned ClusterID;
0041
0042 unsigned PositionInCluster;
0043 };
0044
0045
0046 struct FunctionPathAndClusterInfo {
0047
0048 SmallVector<BBClusterInfo> ClusterInfo;
0049
0050
0051
0052 SmallVector<SmallVector<unsigned>> ClonePaths;
0053 };
0054
0055
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, true, '#'){};
0081
0082 BasicBlockSectionsProfileReader(){};
0083
0084
0085
0086 bool isFunctionHot(StringRef FuncName) const;
0087
0088
0089
0090
0091
0092
0093
0094 std::pair<bool, SmallVector<BBClusterInfo>>
0095 getClusterInfoForFunction(StringRef FuncName) const;
0096
0097
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
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
0116
0117
0118
0119 Expected<UniqueBBID> parseUniqueBBID(StringRef S) const;
0120
0121
0122 Error ReadProfile();
0123
0124
0125
0126 Error ReadV0Profile();
0127
0128
0129 Error ReadV1Profile();
0130
0131
0132 const MemoryBuffer *MBuf = nullptr;
0133
0134
0135 line_iterator LineIt;
0136
0137
0138
0139 StringMap<SmallString<128>> FunctionNameToDIFilename;
0140
0141
0142
0143
0144
0145
0146
0147 StringMap<FunctionPathAndClusterInfo> ProgramPathAndClusterInfo;
0148
0149
0150
0151 StringMap<StringRef> FuncAliasMap;
0152 };
0153
0154
0155
0156
0157 ImmutablePass *
0158 createBasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf);
0159
0160
0161
0162
0163
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
0208
0209 bool doInitialization(Module &M) override;
0210
0211 BasicBlockSectionsProfileReader &getBBSPR();
0212 };
0213
0214 }
0215 #endif