36 "bbsections-profile-reader",
37 "Reads and parses a basic block sections profile.",
false,
45 return createProfileParseError(
Twine(
"unable to parse basic block id: '") +
47 unsigned long long BaseBBID;
49 return createProfileParseError(
50 Twine(
"unable to parse BB id: '" + Parts[0]) +
51 "': unsigned integer expected");
52 unsigned long long CloneID = 0;
54 return createProfileParseError(
Twine(
"unable to parse clone id: '") +
55 Parts[1] +
"': unsigned integer expected");
56 return UniqueBBID{
static_cast<unsigned>(BaseBBID),
57 static_cast<unsigned>(CloneID)};
67 auto R = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
68 return R != ProgramPathAndClusterInfo.end() ?
R->second.ClusterInfo
75 auto R = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
76 return R != ProgramPathAndClusterInfo.end()
77 ?
R->second.ClonePaths
84 auto It = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
85 if (It == ProgramPathAndClusterInfo.end())
87 auto NodeIt = It->second.EdgeCounts.find(SrcBBID);
88 if (NodeIt == It->second.EdgeCounts.end())
90 auto EdgeIt = NodeIt->second.find(SinkBBID);
91 if (EdgeIt == NodeIt->second.end())
93 return EdgeIt->second;
151Error BasicBlockSectionsProfileReader::ReadV1Profile() {
152 auto FI = ProgramPathAndClusterInfo.end();
155 unsigned CurrentCluster = 0;
157 unsigned CurrentPosition = 0;
161 DenseSet<UniqueBBID> FuncBBIDs;
165 StringRef DIFilename;
167 for (; !LineIt.is_at_eof(); ++LineIt) {
168 StringRef S(*LineIt);
170 S = S.drop_front().trim();
172 S.split(Values,
' ');
177 if (Values.
size() != 1) {
178 return createProfileParseError(Twine(
"invalid module name value: '") +
184 bool FunctionFound =
any_of(Values, [&](StringRef Alias) {
185 auto It = FunctionNameToDIFilename.find(Alias);
187 if (It == FunctionNameToDIFilename.end())
191 return DIFilename.
empty() || It->second == DIFilename;
193 if (!FunctionFound) {
196 FI = ProgramPathAndClusterInfo.end();
200 for (
size_t i = 1; i < Values.
size(); ++i)
201 FuncAliasMap.try_emplace(Values[i], Values.
front());
205 auto R = ProgramPathAndClusterInfo.try_emplace(Values.
front());
209 return createProfileParseError(
"duplicate profile for function '" +
210 Values.
front() +
"'");
222 if (FI == ProgramPathAndClusterInfo.end())
226 for (
auto BasicBlockIDStr : Values) {
227 auto BasicBlockID = parseUniqueBBID(BasicBlockIDStr);
229 return BasicBlockID.takeError();
230 if (!FuncBBIDs.
insert(*BasicBlockID).second)
231 return createProfileParseError(
232 Twine(
"duplicate basic block id found '") + BasicBlockIDStr +
235 FI->second.ClusterInfo.emplace_back(BBClusterInfo{
236 *std::move(BasicBlockID), CurrentCluster, CurrentPosition++});
243 if (FI == ProgramPathAndClusterInfo.end())
245 SmallSet<unsigned, 5> BBsInPath;
246 FI->second.ClonePaths.push_back({});
247 for (
size_t I = 0;
I < Values.size(); ++
I) {
248 auto BaseBBIDStr = Values[
I];
249 unsigned long long BaseBBID = 0;
251 return createProfileParseError(Twine(
"unsigned integer expected: '") +
253 if (
I != 0 && !BBsInPath.
insert(BaseBBID).second)
254 return createProfileParseError(
255 Twine(
"duplicate cloned block in path: '") + BaseBBIDStr +
"'");
256 FI->second.ClonePaths.back().push_back(BaseBBID);
263 if (FI == ProgramPathAndClusterInfo.end())
267 for (
auto BasicBlockEdgeProfile : Values) {
268 if (BasicBlockEdgeProfile.empty())
271 BasicBlockEdgeProfile.split(NodeEdgeCounts,
',');
273 for (
size_t i = 0; i < NodeEdgeCounts.
size(); ++i) {
274 auto [BBIDStr, CountStr] = NodeEdgeCounts[i].split(
':');
275 auto BBID = parseUniqueBBID(BBIDStr);
277 return BBID.takeError();
278 unsigned long long Count = 0;
280 return createProfileParseError(
281 Twine(
"unsigned integer expected: '") + CountStr +
"'");
284 FI->second.NodeCounts[SrcBBID = *BBID] =
Count;
287 FI->second.EdgeCounts[SrcBBID][*BBID] =
Count;
295 if (FI == ProgramPathAndClusterInfo.end())
297 for (
auto BBIDHashStr : Values) {
298 auto [BBIDStr, HashStr] = BBIDHashStr.split(
':');
299 unsigned long long BBID = 0, Hash = 0;
301 return createProfileParseError(Twine(
"unsigned integer expected: '") +
304 return createProfileParseError(
305 Twine(
"unsigned integer expected in hex format: '") + HashStr +
307 FI->second.BBHashes[BBID] = Hash;
312 return createProfileParseError(Twine(
"invalid specifier: '") +
313 Twine(Specifier) +
"'");
320Error BasicBlockSectionsProfileReader::ReadV0Profile() {
321 auto FI = ProgramPathAndClusterInfo.end();
323 unsigned CurrentCluster = 0;
325 unsigned CurrentPosition = 0;
329 SmallSet<unsigned, 4> FuncBBIDs;
331 for (; !LineIt.is_at_eof(); ++LineIt) {
332 StringRef S(*LineIt);
336 if (!S.consume_front(
"!") || S.empty())
339 if (S.consume_front(
"!")) {
342 if (FI == ProgramPathAndClusterInfo.end())
348 for (
auto BBIDStr : BBIDs) {
349 unsigned long long BBID;
351 return createProfileParseError(Twine(
"unsigned integer expected: '") +
353 if (!FuncBBIDs.
insert(BBID).second)
354 return createProfileParseError(
355 Twine(
"duplicate basic block id found '") + BBIDStr +
"'");
357 FI->second.ClusterInfo.emplace_back(
358 BBClusterInfo({{
static_cast<unsigned>(BBID), 0},
360 CurrentPosition++}));
366 auto [AliasesStr, DIFilenameStr] = S.split(
' ');
367 SmallString<128> DIFilename;
368 if (DIFilenameStr.starts_with(
"M=")) {
371 if (DIFilename.
empty())
372 return createProfileParseError(
"empty module name specifier");
373 }
else if (!DIFilenameStr.empty()) {
374 return createProfileParseError(
"unknown string found: '" +
375 DIFilenameStr +
"'");
381 AliasesStr.
split(Aliases,
'/');
382 bool FunctionFound =
any_of(Aliases, [&](StringRef Alias) {
383 auto It = FunctionNameToDIFilename.find(Alias);
385 if (It == FunctionNameToDIFilename.end())
389 return DIFilename.
empty() || It->second == DIFilename;
391 if (!FunctionFound) {
394 FI = ProgramPathAndClusterInfo.end();
397 for (
size_t i = 1; i < Aliases.
size(); ++i)
398 FuncAliasMap.try_emplace(Aliases[i], Aliases.
front());
402 auto R = ProgramPathAndClusterInfo.try_emplace(Aliases.
front());
406 return createProfileParseError(
"duplicate profile for function '" +
407 Aliases.
front() +
"'");
433Error BasicBlockSectionsProfileReader::ReadProfile() {
436 unsigned long long Version = 0;
437 StringRef FirstLine(*LineIt);
438 if (FirstLine.consume_front(
"v")) {
440 return createProfileParseError(Twine(
"version number expected: '") +
444 return createProfileParseError(Twine(
"invalid profile version: ") +
453 return ReadV0Profile();
455 return ReadV1Profile();
465 BBSPR.FunctionNameToDIFilename.clear();
468 if (
F.isDeclaration())
476 [[maybe_unused]]
bool inserted =
477 BBSPR.FunctionNameToDIFilename.try_emplace(
F.getName(), DIFilename)
481 if (
auto Err =
BBSPR.ReadProfile())
496 return BBSPR.isFunctionHot(FuncName);
502 return BBSPR.getClusterInfoForFunction(FuncName);
508 return BBSPR.getClonePathsForFunction(FuncName);
514 return BBSPR.getEdgeCount(FuncName, SrcBBID, SinkBBID);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
This file defines the DenseSet and SmallDenseSet classes.
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
This file defines the SmallSet class.
This file defines the SmallString class.
This file defines the SmallVector class.
Result run(Function &F, FunctionAnalysisManager &AM)
bool doInitialization(Module &M) override
doInitialization - Virtual method overridden by subclasses to do any necessary initialization before ...
BasicBlockSectionsProfileReader BBSPR
BasicBlockSectionsProfileReader & getBBSPR()
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
SmallVector< BBClusterInfo > getClusterInfoForFunction(StringRef FuncName) const
bool isFunctionHot(StringRef FuncName) const
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &DestBBID) const
bool isFunctionHot(StringRef FuncName) const
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &SinkBBID) const
SmallVector< BBClusterInfo > getClusterInfoForFunction(StringRef FuncName) const
Subprogram description. Uses SubclassData1.
Lightweight error class with error context and mandatory checking.
static ErrorSuccess success()
Create a success value.
Tagged union holding either a T or a Error.
ImmutablePass class - This class is used to provide information that does not need to be run.
This interface provides simple read-only access to a block of memory, and provides simple methods for...
A Module instance is used to store all the information related to an LLVM module.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
constexpr bool empty() const
empty - Check if the string is empty.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::pair< iterator, bool > insert(const ValueT &V)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI StringRef remove_leading_dotslash(StringRef path LLVM_LIFETIME_BOUND, Style style=Style::native)
Remove redundant leading "./" pieces and consecutive separators.
This is an optimization pass for GlobalISel generic memory operations.
ImmutablePass * createBasicBlockSectionsProfileReaderWrapperPass(const MemoryBuffer *Buf)
bool any_of(R &&range, UnaryPredicate P)
Provide wrappers to std::any_of which take ranges instead of having to pass begin/end explicitly.
FunctionAddr VTableAddr uintptr_t uintptr_t Version
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
FunctionAddr VTableAddr Count
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
LLVM_ABI bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)
Helper functions for StringRef::getAsInteger.
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
A special type used by analysis passes to provide an address that identifies that particular analysis...