Skip to content

[NFC][InstrProf]Factor out getCanonicalName to compute the canonical name given a pgo name. #81547

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 6 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions llvm/include/llvm/ProfileData/InstrProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,17 @@ class InstrProfSymtab {
return "** External Symbol **";
}

// Returns the canonial name of the given PGOName. In a canonical name, all
// suffixes that begins with "." except ".__uniq." are stripped.
// FIXME: Unify this with `FunctionSamples::getCanonicalFnName`.
static StringRef getCanonicalName(StringRef PGOName);

// Add the function into the symbol table, by creating the following
// map entries:
// name-set = {PGOFuncName} + {getCanonicalName(PGOFuncName)} if the canonical
// name is different from pgo name
// - In MD5NameMap: <MD5Hash(name), name> for name in name-set
// - In MD5FuncMap: <MD5Hash(name), &F> for name in name-set
Error addFuncWithName(Function &F, StringRef PGOFuncName);

// If the symtab is created by a series of calls to \c addFuncName, \c
Expand Down
49 changes: 30 additions & 19 deletions llvm/lib/ProfileData/InstrProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -517,35 +517,46 @@ Error InstrProfSymtab::create(StringRef NameStrings) {
std::bind(&InstrProfSymtab::addFuncName, this, std::placeholders::_1));
}

Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
if (Error E = addFuncName(PGOFuncName))
return E;
MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName) {
// In ThinLTO, local function may have been promoted to global and have
// suffix ".llvm." added to the function name. We need to add the
// stripped function name to the symbol table so that we can find a match
// from profile.
//
// We may have other suffixes similar as ".llvm." which are needed to
// be stripped before the matching, but ".__uniq." suffix which is used
// to differentiate internal linkage functions in different modules
// should be kept. Now this is the only suffix with the pattern ".xxx"
// which is kept before matching.
// ".__uniq." suffix is used to differentiate internal linkage functions in
// different modules and should be kept. This is the only suffix with the
// pattern ".xxx" which is kept before matching, other suffixes similar as
// ".llvm." will be stripped.
const std::string UniqSuffix = ".__uniq.";
auto pos = PGOFuncName.find(UniqSuffix);
// Search '.' after ".__uniq." if ".__uniq." exists, otherwise
// search '.' from the beginning.
if (pos != std::string::npos)
size_t pos = PGOName.find(UniqSuffix);
if (pos != StringRef::npos)
pos += UniqSuffix.length();
else
pos = 0;
pos = PGOFuncName.find('.', pos);
if (pos != std::string::npos && pos != 0) {
StringRef OtherFuncName = PGOFuncName.substr(0, pos);
if (Error E = addFuncName(OtherFuncName))

// Search '.' after ".__uniq." if ".__uniq." exists, otherwise search '.' from
// the beginning.
pos = PGOName.find('.', pos);
if (pos != StringRef::npos && pos != 0)
return PGOName.substr(0, pos);

return PGOName;
}

Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
auto mapName = [&](StringRef Name) -> Error {
if (Error E = addFuncName(Name))
return E;
MD5FuncMap.emplace_back(Function::getGUID(OtherFuncName), &F);
}
MD5FuncMap.emplace_back(Function::getGUID(Name), &F);
return Error::success();
};
if (Error E = mapName(PGOFuncName))
return E;

StringRef CanonicalFuncName = getCanonicalName(PGOFuncName);
if (CanonicalFuncName != PGOFuncName)
return mapName(CanonicalFuncName);

return Error::success();
}

Expand Down