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)};
64std::pair<bool, SmallVector<BBClusterInfo>>
67 auto R = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
68 return R != ProgramPathAndClusterInfo.end()
69 ? std::pair(
true,
R->second.ClusterInfo)
76 return ProgramPathAndClusterInfo.lookup(getAliasName(FuncName)).ClonePaths;
82 auto It = ProgramPathAndClusterInfo.find(getAliasName(FuncName));
83 if (It == ProgramPathAndClusterInfo.end())
85 auto NodeIt = It->second.EdgeCounts.find(SrcBBID);
86 if (NodeIt == It->second.EdgeCounts.end())
88 auto EdgeIt = NodeIt->second.find(SinkBBID);
89 if (EdgeIt == NodeIt->second.end())
91 return EdgeIt->second;
149Error BasicBlockSectionsProfileReader::ReadV1Profile() {
150 auto FI = ProgramPathAndClusterInfo.end();
153 unsigned CurrentCluster = 0;
155 unsigned CurrentPosition = 0;
159 DenseSet<UniqueBBID> FuncBBIDs;
163 StringRef DIFilename;
165 for (; !LineIt.is_at_eof(); ++LineIt) {
166 StringRef S(*LineIt);
168 S = S.drop_front().trim();
170 S.split(Values,
' ');
175 if (Values.
size() != 1) {
176 return createProfileParseError(Twine(
"invalid module name value: '") +
182 bool FunctionFound =
any_of(Values, [&](StringRef Alias) {
183 auto It = FunctionNameToDIFilename.find(Alias);
185 if (It == FunctionNameToDIFilename.end())
189 return DIFilename.
empty() || It->second == DIFilename;
191 if (!FunctionFound) {
194 FI = ProgramPathAndClusterInfo.end();
198 for (
size_t i = 1; i < Values.
size(); ++i)
199 FuncAliasMap.try_emplace(Values[i], Values.
front());
203 auto R = ProgramPathAndClusterInfo.try_emplace(Values.
front());
207 return createProfileParseError(
"duplicate profile for function '" +
208 Values.
front() +
"'");
220 if (FI == ProgramPathAndClusterInfo.end())
224 for (
auto BasicBlockIDStr : Values) {
225 auto BasicBlockID = parseUniqueBBID(BasicBlockIDStr);
227 return BasicBlockID.takeError();
228 if (!FuncBBIDs.
insert(*BasicBlockID).second)
229 return createProfileParseError(
230 Twine(
"duplicate basic block id found '") + BasicBlockIDStr +
233 FI->second.ClusterInfo.emplace_back(BBClusterInfo{
234 *std::move(BasicBlockID), CurrentCluster, CurrentPosition++});
241 if (FI == ProgramPathAndClusterInfo.end())
243 SmallSet<unsigned, 5> BBsInPath;
244 FI->second.ClonePaths.push_back({});
245 for (
size_t I = 0;
I < Values.size(); ++
I) {
246 auto BaseBBIDStr = Values[
I];
247 unsigned long long BaseBBID = 0;
249 return createProfileParseError(Twine(
"unsigned integer expected: '") +
251 if (
I != 0 && !BBsInPath.
insert(BaseBBID).second)
252 return createProfileParseError(
253 Twine(
"duplicate cloned block in path: '") + BaseBBIDStr +
"'");
254 FI->second.ClonePaths.back().push_back(BaseBBID);
261 if (FI == ProgramPathAndClusterInfo.end())
265 for (
auto BasicBlockEdgeProfile : Values) {
266 if (BasicBlockEdgeProfile.empty())
269 BasicBlockEdgeProfile.split(NodeEdgeCounts,
',');
271 for (
size_t i = 0; i < NodeEdgeCounts.
size(); ++i) {
272 auto [BBIDStr, CountStr] = NodeEdgeCounts[i].split(
':');
273 auto BBID = parseUniqueBBID(BBIDStr);
275 return BBID.takeError();
276 unsigned long long Count = 0;
278 return createProfileParseError(
279 Twine(
"unsigned integer expected: '") + CountStr +
"'");
282 FI->second.NodeCounts[SrcBBID = *BBID] =
Count;
285 FI->second.EdgeCounts[SrcBBID][*BBID] =
Count;
291 return createProfileParseError(Twine(
"invalid specifier: '") +
292 Twine(Specifier) +
"'");
299Error BasicBlockSectionsProfileReader::ReadV0Profile() {
300 auto FI = ProgramPathAndClusterInfo.end();
302 unsigned CurrentCluster = 0;
304 unsigned CurrentPosition = 0;
308 SmallSet<unsigned, 4> FuncBBIDs;
310 for (; !LineIt.is_at_eof(); ++LineIt) {
311 StringRef S(*LineIt);
315 if (!S.consume_front(
"!") || S.empty())
318 if (S.consume_front(
"!")) {
321 if (FI == ProgramPathAndClusterInfo.end())
327 for (
auto BBIDStr : BBIDs) {
328 unsigned long long BBID;
330 return createProfileParseError(Twine(
"unsigned integer expected: '") +
332 if (!FuncBBIDs.
insert(BBID).second)
333 return createProfileParseError(
334 Twine(
"duplicate basic block id found '") + BBIDStr +
"'");
336 FI->second.ClusterInfo.emplace_back(
337 BBClusterInfo({{
static_cast<unsigned>(BBID), 0},
339 CurrentPosition++}));
345 auto [AliasesStr, DIFilenameStr] = S.split(
' ');
346 SmallString<128> DIFilename;
347 if (DIFilenameStr.starts_with(
"M=")) {
350 if (DIFilename.
empty())
351 return createProfileParseError(
"empty module name specifier");
352 }
else if (!DIFilenameStr.empty()) {
353 return createProfileParseError(
"unknown string found: '" +
354 DIFilenameStr +
"'");
360 AliasesStr.
split(Aliases,
'/');
361 bool FunctionFound =
any_of(Aliases, [&](StringRef Alias) {
362 auto It = FunctionNameToDIFilename.find(Alias);
364 if (It == FunctionNameToDIFilename.end())
368 return DIFilename.
empty() || It->second == DIFilename;
370 if (!FunctionFound) {
373 FI = ProgramPathAndClusterInfo.end();
376 for (
size_t i = 1; i < Aliases.
size(); ++i)
377 FuncAliasMap.try_emplace(Aliases[i], Aliases.
front());
381 auto R = ProgramPathAndClusterInfo.try_emplace(Aliases.
front());
385 return createProfileParseError(
"duplicate profile for function '" +
386 Aliases.
front() +
"'");
412Error BasicBlockSectionsProfileReader::ReadProfile() {
415 unsigned long long Version = 0;
416 StringRef FirstLine(*LineIt);
417 if (FirstLine.consume_front(
"v")) {
419 return createProfileParseError(Twine(
"version number expected: '") +
423 return createProfileParseError(Twine(
"invalid profile version: ") +
432 return ReadV0Profile();
434 return ReadV1Profile();
444 BBSPR.FunctionNameToDIFilename.clear();
447 if (
F.isDeclaration())
455 [[maybe_unused]]
bool inserted =
456 BBSPR.FunctionNameToDIFilename.try_emplace(
F.getName(), DIFilename)
460 if (
auto Err =
BBSPR.ReadProfile())
475 return BBSPR.isFunctionHot(FuncName);
478std::pair<bool, SmallVector<BBClusterInfo>>
481 return BBSPR.getClusterInfoForFunction(FuncName);
487 return BBSPR.getClonePathsForFunction(FuncName);
493 return BBSPR.getEdgeCount(FuncName, SrcBBID, SinkBBID);
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file defines the StringMap class.
Function Alias Analysis false
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
bool isFunctionHot(StringRef FuncName) const
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &DestBBID) const
std::pair< bool, SmallVector< BBClusterInfo > > getClusterInfoForFunction(StringRef FuncName) const
bool isFunctionHot(StringRef FuncName) const
std::pair< bool, SmallVector< BBClusterInfo > > getClusterInfoForFunction(StringRef FuncName) const
SmallVector< SmallVector< unsigned > > getClonePathsForFunction(StringRef FuncName) const
uint64_t getEdgeCount(StringRef FuncName, const UniqueBBID &SrcBBID, const UniqueBBID &SinkBBID) 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...