Skip to content

[nfc]Generalize PGOFuncName helper methods for general global objects #73570

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

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions clang/lib/CodeGen/CodeGenPGO.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using namespace CodeGen;
void CodeGenPGO::setFuncName(StringRef Name,
llvm::GlobalValue::LinkageTypes Linkage) {
llvm::IndexedInstrProfReader *PGOReader = CGM.getPGOReader();
FuncName = llvm::getPGOFuncName(
FuncName = llvm::getPGOObjectName(
Name, Linkage, CGM.getCodeGenOpts().MainFileName,
PGOReader ? PGOReader->getVersion() : llvm::IndexedInstrProf::Version);

Expand All @@ -45,8 +45,8 @@ void CodeGenPGO::setFuncName(StringRef Name,

void CodeGenPGO::setFuncName(llvm::Function *Fn) {
setFuncName(Fn->getName(), Fn->getLinkage());
// Create PGOFuncName meta data.
llvm::createPGOFuncNameMetadata(*Fn, FuncName);
// Create PGOName meta data.
llvm::createPGONameMetadata(*Fn, FuncName);
}

/// The version of the PGO hash algorithm.
Expand Down
21 changes: 9 additions & 12 deletions llvm/include/llvm/ProfileData/InstrProf.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,10 @@ std::string getPGOFuncName(const Function &F, bool InLTO = false,
/// used the key for profile lookup. The function's original
/// name is \c RawFuncName and has linkage of type \c Linkage.
/// The function is defined in module \c FileName.
std::string getPGOFuncName(StringRef RawFuncName,
GlobalValue::LinkageTypes Linkage,
StringRef FileName,
uint64_t Version = INSTR_PROF_INDEX_VERSION);
std::string getPGOObjectName(StringRef RawFuncName,
GlobalValue::LinkageTypes Linkage,
StringRef FileName,
uint64_t Version = INSTR_PROF_INDEX_VERSION);

/// \return the modified name for function \c F suitable to be
/// used as the key for IRPGO profile lookup. \c InLTO indicates if this is
Expand Down Expand Up @@ -279,15 +279,12 @@ bool getValueProfDataFromInst(const Instruction &Inst,
uint32_t &ActualNumValueData, uint64_t &TotalC,
bool GetNoICPValue = false);

inline StringRef getPGOFuncNameMetadataName() { return "PGOFuncName"; }

/// Return the PGOFuncName meta data associated with a function.
MDNode *getPGOFuncNameMetadata(const Function &F);
inline StringRef getPGONameMetadataName() { return "PGOName"; }

/// Create the PGOFuncName meta data if PGOFuncName is different from
/// function's raw name. This should only apply to internal linkage functions
/// declared by users only.
void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName);
/// Create the PGOName meta data if PGOName is different from the object's raw
/// name. This should only apply to internal linkage objects declared by users
/// only (i.e., not internalized by LTO).
void createPGONameMetadata(GlobalObject &GO, StringRef PGOName);

/// Check if we can use Comdat for profile variables. This will eliminate
/// the duplicated profile variables for Comdat functions.
Expand Down
86 changes: 47 additions & 39 deletions llvm/lib/ProfileData/InstrProf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,11 +246,11 @@ std::string InstrProfError::message() const {

char InstrProfError::ID = 0;

std::string getPGOFuncName(StringRef RawFuncName,
GlobalValue::LinkageTypes Linkage,
StringRef FileName,
uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
return GlobalValue::getGlobalIdentifier(RawFuncName, Linkage, FileName);
std::string getPGOObjectName(StringRef RawName,
GlobalValue::LinkageTypes Linkage,
StringRef FileName,
uint64_t Version LLVM_ATTRIBUTE_UNUSED) {
return GlobalValue::getGlobalIdentifier(RawName, Linkage, FileName);
}

// Strip NumPrefix level of directory name from PathNameStr. If the number of
Expand Down Expand Up @@ -287,11 +287,11 @@ static StringRef getStrippedSourceFileName(const GlobalObject &GO) {
// ; is used because it is unlikely to be found in either <filepath> or
// <linkage-name>.
//
// Older compilers used getPGOFuncName() which has the format
// [<filepath>:]<function-name>. <filepath> is used to discriminate between
// possibly identical function names when linkage is local and <function-name>
// simply comes from F.getName(). This caused trouble for Objective-C functions
// which commonly have :'s in their names. Also, since <function-name> is not
// Older compilers used getPGOObjectName() which has the format
// [<filepath>:]<object-name>. <filepath> is used to discriminate between
// possibly identical object names when linkage is local and <object-name>
// simply comes from GO.getName(). This caused trouble for Objective-C functions
// which commonly have :'s in their names. Also, since <object-name> is not
// mangled, they cannot be passed to Mach-O linkers via -order_file. We still
// need to compute this name to lookup functions from profiles built by older
// compilers.
Expand Down Expand Up @@ -325,9 +325,9 @@ static std::optional<std::string> lookupPGONameFromMetadata(MDNode *MD) {
// and renamed. We need to ensure that the original internal PGO name is
// used when computing the GUID that is compared against the profiled GUIDs.
// To differentiate compiler generated internal symbols from original ones,
// PGOFuncName meta data are created and attached to the original internal
// PGOName meta data are created and attached to the original internal
// symbols in the value profile annotation step
// (PGOUseFunc::annotateIndirectCallSites). If a symbol does not have the meta
// (PGOUseFunc::annotateValueSites). If a symbol does not have the meta
// data, its original linkage must be non-internal.
static std::string getIRPGOObjectName(const GlobalObject &GO, bool InLTO,
MDNode *PGONameMetadata) {
Expand All @@ -337,39 +337,51 @@ static std::string getIRPGOObjectName(const GlobalObject &GO, bool InLTO,
}

// In LTO mode (when InLTO is true), first check if there is a meta data.
if (auto IRPGOFuncName = lookupPGONameFromMetadata(PGONameMetadata))
return *IRPGOFuncName;
if (auto IRPGOName = lookupPGONameFromMetadata(PGONameMetadata))
return *IRPGOName;

// If there is no meta data, the function must be a global before the value
// profile annotation pass. Its current linkage may be internal if it is
// If there is no meta data, the global object must be a global before the
// value profile annotation pass. Its current linkage may be internal if it is
// internalized in LTO mode.
return getIRPGONameForGlobalObject(GO, GlobalValue::ExternalLinkage, "");
}

/// Return the PGOName meta data associated with a global object.
static MDNode *getPGONameMetadata(const GlobalObject &GO) {
return GO.getMetadata(getPGONameMetadataName());
}

// Returns the IRPGO function name and does special handling when called
// in LTO optimization. See the comments of `getIRPGOObjectName` for details.
std::string getIRPGOFuncName(const Function &F, bool InLTO) {
return getIRPGOObjectName(F, InLTO, getPGOFuncNameMetadata(F));
return getIRPGOObjectName(F, InLTO, getPGONameMetadata(F));
}

// This is similar to `getIRPGOFuncName` except that this function calls
// 'getPGOFuncName' to get a name and `getIRPGOFuncName` calls
// This is similar to `getIRPGOObjectName` except that this function calls
// 'getPGOObjectName' to get a name and `getIRPGOObjectName` calls
// 'getIRPGONameForGlobalObject'. See the difference between two callees in the
// comments of `getIRPGONameForGlobalObject`.
std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
static std::string getPGOObjectName(const GlobalObject &GO, bool InLTO,
uint64_t Version) {
if (!InLTO) {
auto FileName = getStrippedSourceFileName(F);
return getPGOFuncName(F.getName(), F.getLinkage(), FileName, Version);
auto FileName = getStrippedSourceFileName(GO);
return getPGOObjectName(GO.getName(), GO.getLinkage(), FileName, Version);
}

// In LTO mode (when InLTO is true), first check if there is a meta data.
if (auto PGOFuncName = lookupPGONameFromMetadata(getPGOFuncNameMetadata(F)))
if (auto PGOFuncName = lookupPGONameFromMetadata(getPGONameMetadata(GO)))
return *PGOFuncName;

// If there is no meta data, the function must be a global before the value
// profile annotation pass. Its current linkage may be internal if it is
// internalized in LTO mode.
return getPGOFuncName(F.getName(), GlobalValue::ExternalLinkage, "");
// If there is no meta data, the function or global variable must be a global
// before the value profile annotation pass. Its current linkage may be
// internal if it is internalized in LTO mode.
return getPGOObjectName(GO.getName(), GlobalValue::ExternalLinkage, "");
}

// Returns the PGO function name and does special handling when called in LTO
// optimization. See the comments of `getPGOObjectName` for details.
std::string getPGOFuncName(const Function &F, bool InLTO, uint64_t Version) {
return getPGOObjectName(F, InLTO, Version);
}

// See getIRPGOFuncName() for a discription of the format.
Expand Down Expand Up @@ -1255,20 +1267,16 @@ bool getValueProfDataFromInst(const Instruction &Inst,
return true;
}

MDNode *getPGOFuncNameMetadata(const Function &F) {
return F.getMetadata(getPGOFuncNameMetadataName());
}

void createPGOFuncNameMetadata(Function &F, StringRef PGOFuncName) {
// Only for internal linkage functions.
if (PGOFuncName == F.getName())
return;
void createPGONameMetadata(GlobalObject &GO, StringRef PGOName) {
// Only for internal linkage functions or global variables.
if (PGOName == GO.getName())
return;
// Don't create duplicated meta-data.
if (getPGOFuncNameMetadata(F))
if (getPGONameMetadata(GO))
return;
LLVMContext &C = F.getContext();
MDNode *N = MDNode::get(C, MDString::get(C, PGOFuncName));
F.setMetadata(getPGOFuncNameMetadataName(), N);
LLVMContext &C = GO.getContext();
MDNode *N = MDNode::get(C, MDString::get(C, PGOName));
GO.setMetadata(getPGONameMetadataName(), N);
}

bool needsComdatForCounter(const Function &F, const Module &M) {
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Transforms/Instrumentation/PGOInstrumentation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1700,8 +1700,8 @@ void PGOUseFunc::annotateValueSites() {
if (DisableValueProfiling)
return;

// Create the PGOFuncName meta data.
createPGOFuncNameMetadata(F, FuncInfo.FuncName);
// Create the PGOName meta data.
createPGONameMetadata(F, FuncInfo.FuncName);

for (uint32_t Kind = IPVK_First; Kind <= IPVK_Last; ++Kind)
annotateValueSites(Kind);
Expand Down
12 changes: 6 additions & 6 deletions llvm/test/Transforms/DeadArgElim/func_metadata.ll
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,17 @@ target triple = "x86_64-unknown-linux-gnu"

@s = common dso_local local_unnamed_addr global i32 0, align 4

define internal i32 @va_func(i32 %num, ...) !prof !28 !PGOFuncName !29{
; CHECK: define internal void @va_func(i32 %num) !prof ![[ENTRYCOUNT:[0-9]+]] !PGOFuncName ![[PGOFUNCNAME1:[0-9]+]] {
define internal i32 @va_func(i32 %num, ...) !prof !28 !PGOName !29{
; CHECK: define internal void @va_func(i32 %num) !prof ![[ENTRYCOUNT:[0-9]+]] !PGOName ![[PGOName1:[0-9]+]] {
entry:
%0 = load i32, ptr @s, align 4, !tbaa !31
%add = add nsw i32 %0, %num
store i32 %add, ptr @s, align 4, !tbaa !31
ret i32 0
}

define internal fastcc i32 @foo() unnamed_addr !prof !28 !PGOFuncName !30 {
; CHECK: define internal fastcc void @foo() unnamed_addr !prof ![[ENTRYCOUNT:[0-9]+]] !PGOFuncName ![[PGOFUNCNAME2:[0-9]+]] {
define internal fastcc i32 @foo() unnamed_addr !prof !28 !PGOName !30 {
; CHECK: define internal fastcc void @foo() unnamed_addr !prof ![[ENTRYCOUNT:[0-9]+]] !PGOName ![[PGOName2:[0-9]+]] {
entry:
%0 = load i32, ptr @s, align 4, !tbaa !31
%add = add nsw i32 %0, 8
Expand Down Expand Up @@ -58,9 +58,9 @@ entry:
!28 = !{!"function_entry_count", i64 1}
; CHECK: ![[ENTRYCOUNT]] = !{!"function_entry_count", i64 1}
!29 = !{!"foo.c:va_func"}
; CHECK: ![[PGOFUNCNAME1]] = !{!"foo.c:va_func"}
; CHECK: ![[PGOName1]] = !{!"foo.c:va_func"}
!30 = !{!"foo.c:foo"}
; CHECK: ![[PGOFUNCNAME2]] = !{!"foo.c:foo"}
; CHECK: ![[PGOName2]] = !{!"foo.c:foo"}
!31 = !{!32, !32, i64 0}
!32 = !{!"int", !33, i64 0}
!33 = !{!"omnipotent char", !34, i64 0}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/JumpThreading/thread-prob-1.ll
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
; Make sure that we set the branch probability for the newly created
; basic block.

define void @foo(i1 %cond1, i1 %cond2) !prof !0 !PGOFuncName !1 {
define void @foo(i1 %cond1, i1 %cond2) !prof !0 !PGOName !1 {
entry:
br i1 %cond1, label %bb.f1, label %bb.f2, !prof !2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ if.end:
ret void
}

define internal fastcc i32 @cond(i32 %i) #1 !prof !29 !PGOFuncName !35 {
define internal fastcc i32 @cond(i32 %i) #1 !prof !29 !PGOName !35 {
entry:
%rem = srem i32 %i, 2
ret i32 %rem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ if.end:

declare void @clobber()

define internal fastcc i32 @cond(i32 %i) #1 !prof !29 !PGOFuncName !35 {
define internal fastcc i32 @cond(i32 %i) #1 !prof !29 !PGOName !35 {
entry:
%rem = srem i32 %i, 2
ret i32 %rem
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ entry:
ret void
}

define internal void @c() !PGOFuncName !1 {
define internal void @c() !PGOName !1 {
entry:
ret void
}
Expand Down
4 changes: 2 additions & 2 deletions llvm/test/Transforms/PGOProfile/icp_invoke.ll
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@ target triple = "x86_64-unknown-linux-gnu"
@foo2 = global ptr null, align 8
@_ZTIi = external constant ptr

define internal void @_ZL4bar1v() !PGOFuncName !0 {
define internal void @_ZL4bar1v() !PGOName !0 {
entry:
ret void
}

define internal i32 @_ZL4bar2v() !PGOFuncName !1 {
define internal i32 @_ZL4bar2v() !PGOName !1 {
entry:
ret i32 100
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/Transforms/PGOProfile/icp_invoke_nouse.ll
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ target triple = "x86_64-unknown-linux-gnu"
@_ZTISt9exception = external constant ptr
@pfptr = global ptr null, align 8

define internal i32 @_ZL4bar1v() !PGOFuncName !0 {
define internal i32 @_ZL4bar1v() !PGOName !0 {
entry:
ret i32 100
}
Expand Down
2 changes: 1 addition & 1 deletion llvm/test/tools/gold/X86/Inputs/thinlto_cspgo_bar.ll
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ for.inc.3:
ret void
}

define internal fastcc i32 @cond(i32 %i) #1 !prof !29 !PGOFuncName !36 {
define internal fastcc i32 @cond(i32 %i) #1 !prof !29 !PGOName !36 {
entry:
%rem = srem i32 %i, 2
ret i32 %rem
Expand Down