Skip to content

Commit 2422e96

Browse files
[NFC][InstrProf]Factor out getCanonicalName to compute the canonical name given a pgo name. (#81547)
- Also update the `InstrProf::addFuncWithName` to call the newly added `getCanonicalName`.
1 parent c830c12 commit 2422e96

File tree

2 files changed

+41
-19
lines changed

2 files changed

+41
-19
lines changed

llvm/include/llvm/ProfileData/InstrProf.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -449,6 +449,17 @@ class InstrProfSymtab {
449449
return "** External Symbol **";
450450
}
451451

452+
// Returns the canonial name of the given PGOName. In a canonical name, all
453+
// suffixes that begins with "." except ".__uniq." are stripped.
454+
// FIXME: Unify this with `FunctionSamples::getCanonicalFnName`.
455+
static StringRef getCanonicalName(StringRef PGOName);
456+
457+
// Add the function into the symbol table, by creating the following
458+
// map entries:
459+
// name-set = {PGOFuncName} + {getCanonicalName(PGOFuncName)} if the canonical
460+
// name is different from pgo name
461+
// - In MD5NameMap: <MD5Hash(name), name> for name in name-set
462+
// - In MD5FuncMap: <MD5Hash(name), &F> for name in name-set
452463
Error addFuncWithName(Function &F, StringRef PGOFuncName);
453464

454465
// If the symtab is created by a series of calls to \c addFuncName, \c

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -517,35 +517,46 @@ Error InstrProfSymtab::create(StringRef NameStrings) {
517517
std::bind(&InstrProfSymtab::addFuncName, this, std::placeholders::_1));
518518
}
519519

520-
Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
521-
if (Error E = addFuncName(PGOFuncName))
522-
return E;
523-
MD5FuncMap.emplace_back(Function::getGUID(PGOFuncName), &F);
520+
StringRef InstrProfSymtab::getCanonicalName(StringRef PGOName) {
524521
// In ThinLTO, local function may have been promoted to global and have
525522
// suffix ".llvm." added to the function name. We need to add the
526523
// stripped function name to the symbol table so that we can find a match
527524
// from profile.
528525
//
529-
// We may have other suffixes similar as ".llvm." which are needed to
530-
// be stripped before the matching, but ".__uniq." suffix which is used
531-
// to differentiate internal linkage functions in different modules
532-
// should be kept. Now this is the only suffix with the pattern ".xxx"
533-
// which is kept before matching.
526+
// ".__uniq." suffix is used to differentiate internal linkage functions in
527+
// different modules and should be kept. This is the only suffix with the
528+
// pattern ".xxx" which is kept before matching, other suffixes similar as
529+
// ".llvm." will be stripped.
534530
const std::string UniqSuffix = ".__uniq.";
535-
auto pos = PGOFuncName.find(UniqSuffix);
536-
// Search '.' after ".__uniq." if ".__uniq." exists, otherwise
537-
// search '.' from the beginning.
538-
if (pos != std::string::npos)
531+
size_t pos = PGOName.find(UniqSuffix);
532+
if (pos != StringRef::npos)
539533
pos += UniqSuffix.length();
540534
else
541535
pos = 0;
542-
pos = PGOFuncName.find('.', pos);
543-
if (pos != std::string::npos && pos != 0) {
544-
StringRef OtherFuncName = PGOFuncName.substr(0, pos);
545-
if (Error E = addFuncName(OtherFuncName))
536+
537+
// Search '.' after ".__uniq." if ".__uniq." exists, otherwise search '.' from
538+
// the beginning.
539+
pos = PGOName.find('.', pos);
540+
if (pos != StringRef::npos && pos != 0)
541+
return PGOName.substr(0, pos);
542+
543+
return PGOName;
544+
}
545+
546+
Error InstrProfSymtab::addFuncWithName(Function &F, StringRef PGOFuncName) {
547+
auto mapName = [&](StringRef Name) -> Error {
548+
if (Error E = addFuncName(Name))
546549
return E;
547-
MD5FuncMap.emplace_back(Function::getGUID(OtherFuncName), &F);
548-
}
550+
MD5FuncMap.emplace_back(Function::getGUID(Name), &F);
551+
return Error::success();
552+
};
553+
if (Error E = mapName(PGOFuncName))
554+
return E;
555+
556+
StringRef CanonicalFuncName = getCanonicalName(PGOFuncName);
557+
if (CanonicalFuncName != PGOFuncName)
558+
return mapName(CanonicalFuncName);
559+
549560
return Error::success();
550561
}
551562

0 commit comments

Comments
 (0)