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;
134Error BasicBlockSectionsProfileReader::ReadV1Profile() {
135 auto FI = ProgramPathAndClusterInfo.end();
138 unsigned CurrentCluster = 0;
140 unsigned CurrentPosition = 0;
144 DenseSet<UniqueBBID> FuncBBIDs;
148 StringRef DIFilename;
150 for (; !LineIt.is_at_eof(); ++LineIt) {
151 StringRef S(*LineIt);
153 S = S.drop_front().trim();
155 S.split(Values,
' ');
160 if (Values.
size() != 1) {
161 return createProfileParseError(Twine(
"invalid module name value: '") +
167 bool FunctionFound =
any_of(Values, [&](StringRef Alias) {
168 auto It = FunctionNameToDIFilename.find(Alias);
170 if (It == FunctionNameToDIFilename.end())
174 return DIFilename.
empty() || It->second == DIFilename;
176 if (!FunctionFound) {
179 FI = ProgramPathAndClusterInfo.end();
183 for (
size_t i = 1; i < Values.
size(); ++i)
184 FuncAliasMap.try_emplace(Values[i], Values.
front());
188 auto R = ProgramPathAndClusterInfo.try_emplace(Values.
front());
192 return createProfileParseError(
"duplicate profile for function '" +
193 Values.
front() +
"'");
205 if (FI == ProgramPathAndClusterInfo.end())
209 for (
auto BasicBlockIDStr : Values) {
210 auto BasicBlockID = parseUniqueBBID(BasicBlockIDStr);
212 return BasicBlockID.takeError();
213 if (!FuncBBIDs.
insert(*BasicBlockID).second)
214 return createProfileParseError(
215 Twine(
"duplicate basic block id found '") + BasicBlockIDStr +
218 FI->second.ClusterInfo.emplace_back(BBClusterInfo{
219 *std::move(BasicBlockID), CurrentCluster, CurrentPosition++});
226 if (FI == ProgramPathAndClusterInfo.end())
228 SmallSet<unsigned, 5> BBsInPath;
229 FI->second.ClonePaths.push_back({});
230 for (
size_t I = 0;
I < Values.size(); ++
I) {
231 auto BaseBBIDStr = Values[
I];
232 unsigned long long BaseBBID = 0;
234 return createProfileParseError(Twine(
"unsigned integer expected: '") +
236 if (
I != 0 && !BBsInPath.
insert(BaseBBID).second)
237 return createProfileParseError(
238 Twine(
"duplicate cloned block in path: '") + BaseBBIDStr +
"'");
239 FI->second.ClonePaths.back().push_back(BaseBBID);
244 return createProfileParseError(Twine(
"invalid specifier: '") +
245 Twine(Specifier) +
"'");
252Error BasicBlockSectionsProfileReader::ReadV0Profile() {
253 auto FI = ProgramPathAndClusterInfo.end();
255 unsigned CurrentCluster = 0;
257 unsigned CurrentPosition = 0;
261 SmallSet<unsigned, 4> FuncBBIDs;
263 for (; !LineIt.is_at_eof(); ++LineIt) {
264 StringRef S(*LineIt);
268 if (!S.consume_front(
"!") || S.empty())
271 if (S.consume_front(
"!")) {
274 if (FI == ProgramPathAndClusterInfo.end())
280 for (
auto BBIDStr : BBIDs) {
281 unsigned long long BBID;
283 return createProfileParseError(Twine(
"unsigned integer expected: '") +
285 if (!FuncBBIDs.
insert(BBID).second)
286 return createProfileParseError(
287 Twine(
"duplicate basic block id found '") + BBIDStr +
"'");
289 FI->second.ClusterInfo.emplace_back(
290 BBClusterInfo({{
static_cast<unsigned>(BBID), 0},
292 CurrentPosition++}));
298 auto [AliasesStr, DIFilenameStr] = S.split(
' ');
299 SmallString<128> DIFilename;
300 if (DIFilenameStr.starts_with(
"M=")) {
303 if (DIFilename.
empty())
304 return createProfileParseError(
"empty module name specifier");
305 }
else if (!DIFilenameStr.empty()) {
306 return createProfileParseError(
"unknown string found: '" +
307 DIFilenameStr +
"'");
313 AliasesStr.
split(Aliases,
'/');
314 bool FunctionFound =
any_of(Aliases, [&](StringRef Alias) {
315 auto It = FunctionNameToDIFilename.find(Alias);
317 if (It == FunctionNameToDIFilename.end())
321 return DIFilename.
empty() || It->second == DIFilename;
323 if (!FunctionFound) {
326 FI = ProgramPathAndClusterInfo.end();
329 for (
size_t i = 1; i < Aliases.
size(); ++i)
330 FuncAliasMap.try_emplace(Aliases[i], Aliases.
front());
334 auto R = ProgramPathAndClusterInfo.try_emplace(Aliases.
front());
338 return createProfileParseError(
"duplicate profile for function '" +
339 Aliases.
front() +
"'");
365Error BasicBlockSectionsProfileReader::ReadProfile() {
368 unsigned long long Version = 0;
369 StringRef FirstLine(*LineIt);
370 if (FirstLine.consume_front(
"v")) {
372 return createProfileParseError(Twine(
"version number expected: '") +
376 return createProfileParseError(Twine(
"invalid profile version: ") +
385 return ReadV0Profile();
387 return ReadV1Profile();
397 BBSPR.FunctionNameToDIFilename.clear();
400 if (
F.isDeclaration())
408 [[maybe_unused]]
bool inserted =
409 BBSPR.FunctionNameToDIFilename.try_emplace(
F.getName(), DIFilename)
413 if (
auto Err =
BBSPR.ReadProfile())
428 return BBSPR.isFunctionHot(FuncName);
431std::pair<bool, SmallVector<BBClusterInfo>>
434 return BBSPR.getClusterInfoForFunction(FuncName);
440 return BBSPR.getClonePathsForFunction(FuncName);
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
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
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)
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...