Skip to content

Commit 9a2df55

Browse files
authored
[InstrProf] No linkage prefixes in IRPGO names (#76994)
Change the format of IRPGO counter names to `[<filepath>;]<mangled-name>` which is computed by `GlobalValue::getGlobalIdentifier()` to fix #74565. In fe05193 (https://reviews.llvm.org/D156569) the format of IRPGO counter names was changed to be `[<filepath>;]<linkage-name>` where `<linkage-name>` is basically `F.getName()` with some prefix, e.g., `_` or `l_` on Mach-O (yes, it is confusing that `<linkage-name>` is computed with `Mangler().getNameWithPrefix()` while `<mangled-name>` is just `F.getName()`). We discovered in #74565 that this causes some missed import issues on some targets and #74008 is a partial fix. Since `<mangled-name>` may not match the `<linkage-name>` on some targets like Mach-O, we will need to post-process the output of `llvm-profdata order` before passing to the linker via `-order_file`. Profiles generated after fe05193 will become stale after this diff, but I think this is acceptable since that patch landed after the LLVM 18 cut which hasn't been released yet.
1 parent 786cf76 commit 9a2df55

File tree

5 files changed

+16
-51
lines changed

5 files changed

+16
-51
lines changed

compiler-rt/test/profile/instrprof-thinlto-indirect-call-promotion.cpp

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,6 @@
2828
// LTO if default linker is GNU ld or gold anyway.
2929
// REQUIRES: lld-available
3030

31-
// Test should fail where linkage-name and mangled-name diverges, see issue https://github.com/llvm/llvm-project/issues/74565).
32-
// Currently, this name divergence happens on Mach-O object file format, or on
33-
// many (but not all) 32-bit Windows systems.
34-
//
35-
// XFAIL: system-darwin
36-
//
37-
// Mark 32-bit Windows as UNSUPPORTED for now as opposed to XFAIL. This test
38-
// should fail on many (but not all) 32-bit Windows systems and succeed on the
39-
// rest. The flexibility in triple string parsing makes it tricky to capture
40-
// both sets accurately. i[3-9]86 specifies arch as Triple::ArchType::x86, (win32|windows)
41-
// specifies OS as Triple::OS::Win32
42-
//
43-
// UNSUPPORTED: target={{i.86.*windows.*}}
44-
4531
// RUN: rm -rf %t && split-file %s %t && cd %t
4632

4733
// Do setup work for all below tests.

lld/test/MachO/pgo-warn-mismatch.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ entry:
2424

2525
;--- cs.proftext
2626
:csir
27-
_foo
27+
foo
2828
# Func Hash:
2929
2277602155505015273
3030
# Num Counters:

llvm/lib/ProfileData/InstrProf.cpp

Lines changed: 8 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "llvm/ProfileData/InstrProf.h"
1515
#include "llvm/ADT/ArrayRef.h"
1616
#include "llvm/ADT/SetVector.h"
17-
#include "llvm/ADT/SmallString.h"
1817
#include "llvm/ADT/SmallVector.h"
1918
#include "llvm/ADT/StringExtras.h"
2019
#include "llvm/ADT/StringRef.h"
@@ -27,7 +26,6 @@
2726
#include "llvm/IR/Instruction.h"
2827
#include "llvm/IR/LLVMContext.h"
2928
#include "llvm/IR/MDBuilder.h"
30-
#include "llvm/IR/Mangler.h"
3129
#include "llvm/IR/Metadata.h"
3230
#include "llvm/IR/Module.h"
3331
#include "llvm/IR/Type.h"
@@ -297,29 +295,20 @@ static StringRef getStrippedSourceFileName(const GlobalObject &GO) {
297295
return FileName;
298296
}
299297

300-
// The PGO name has the format [<filepath>;]<linkage-name> where <filepath>; is
301-
// provided if linkage is local and <linkage-name> is the mangled function
302-
// name. The filepath is used to discriminate possibly identical function names.
303-
// ; is used because it is unlikely to be found in either <filepath> or
304-
// <linkage-name>.
298+
// The PGO name has the format [<filepath>;]<mangled-name> where <filepath>; is
299+
// provided if linkage is local and is used to discriminate possibly identical
300+
// mangled names. ";" is used because it is unlikely to be found in either
301+
// <filepath> or <mangled-name>.
305302
//
306303
// Older compilers used getPGOFuncName() which has the format
307-
// [<filepath>:]<function-name>. <filepath> is used to discriminate between
308-
// possibly identical function names when linkage is local and <function-name>
309-
// simply comes from F.getName(). This caused trouble for Objective-C functions
310-
// which commonly have :'s in their names. Also, since <function-name> is not
311-
// mangled, they cannot be passed to Mach-O linkers via -order_file. We still
312-
// need to compute this name to lookup functions from profiles built by older
313-
// compilers.
304+
// [<filepath>:]<mangled-name>. This caused trouble for Objective-C functions
305+
// which commonly have :'s in their names. We still need to compute this name to
306+
// lookup functions from profiles built by older compilers.
314307
static std::string
315308
getIRPGONameForGlobalObject(const GlobalObject &GO,
316309
GlobalValue::LinkageTypes Linkage,
317310
StringRef FileName) {
318-
SmallString<64> Name;
319-
// FIXME: Mangler's handling is kept outside of `getGlobalIdentifier` for now.
320-
// For more details please check issue #74565.
321-
Mangler().getNameWithPrefix(Name, &GO, /*CannotUsePrivateLabel=*/true);
322-
return GlobalValue::getGlobalIdentifier(Name, Linkage, FileName);
311+
return GlobalValue::getGlobalIdentifier(GO.getName(), Linkage, FileName);
323312
}
324313

325314
static std::optional<std::string> lookupPGONameFromMetadata(MDNode *MD) {

llvm/tools/llvm-profdata/llvm-profdata.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3158,7 +3158,11 @@ static int order_main(int argc, const char *argv[]) {
31583158
BalancedPartitioning BP(Config);
31593159
BP.run(Nodes);
31603160

3161-
WithColor::note() << "# Ordered " << Nodes.size() << " functions\n";
3161+
OS << "# Ordered " << Nodes.size() << " functions\n";
3162+
OS << "# Warning: Mach-O may prefix symbols with \"_\" depending on the "
3163+
"linkage and this output does not take that into account. Some "
3164+
"post-processing may be required before passing to the linker via "
3165+
"-order_file.\n";
31623166
for (auto &N : Nodes) {
31633167
auto [Filename, ParsedFuncName] =
31643168
getParsedIRPGOFuncName(Reader->getSymtab().getFuncOrVarName(N.Id));

llvm/unittests/ProfileData/InstrProfTest.cpp

Lines changed: 2 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -542,22 +542,14 @@ TEST_F(InstrProfTest, test_memprof_merge) {
542542
TEST_F(InstrProfTest, test_irpgo_function_name) {
543543
LLVMContext Ctx;
544544
auto M = std::make_unique<Module>("MyModule.cpp", Ctx);
545-
// Use Mach-O mangling so that non-private symbols get a `_` prefix.
546-
M->setDataLayout(DataLayout("m:o"));
547545
auto *FTy = FunctionType::get(Type::getVoidTy(Ctx), /*isVarArg=*/false);
548546

549547
std::vector<std::tuple<StringRef, Function::LinkageTypes, StringRef>> Data;
550-
Data.emplace_back("ExternalFoo", Function::ExternalLinkage, "_ExternalFoo");
548+
Data.emplace_back("ExternalFoo", Function::ExternalLinkage, "ExternalFoo");
551549
Data.emplace_back("InternalFoo", Function::InternalLinkage,
552-
"MyModule.cpp;_InternalFoo");
553-
Data.emplace_back("PrivateFoo", Function::PrivateLinkage,
554-
"MyModule.cpp;l_PrivateFoo");
555-
Data.emplace_back("WeakODRFoo", Function::WeakODRLinkage, "_WeakODRFoo");
556-
// Test Objective-C symbols
550+
"MyModule.cpp;InternalFoo");
557551
Data.emplace_back("\01-[C dynamicFoo:]", Function::ExternalLinkage,
558552
"-[C dynamicFoo:]");
559-
Data.emplace_back("-<C directFoo:>", Function::ExternalLinkage,
560-
"_-<C directFoo:>");
561553
Data.emplace_back("\01-[C internalFoo:]", Function::InternalLinkage,
562554
"MyModule.cpp;-[C internalFoo:]");
563555

@@ -590,10 +582,6 @@ TEST_F(InstrProfTest, test_pgo_function_name) {
590582
Data.emplace_back("ExternalFoo", Function::ExternalLinkage, "ExternalFoo");
591583
Data.emplace_back("InternalFoo", Function::InternalLinkage,
592584
"MyModule.cpp:InternalFoo");
593-
Data.emplace_back("PrivateFoo", Function::PrivateLinkage,
594-
"MyModule.cpp:PrivateFoo");
595-
Data.emplace_back("WeakODRFoo", Function::WeakODRLinkage, "WeakODRFoo");
596-
// Test Objective-C symbols
597585
Data.emplace_back("\01-[C externalFoo:]", Function::ExternalLinkage,
598586
"-[C externalFoo:]");
599587
Data.emplace_back("\01-[C internalFoo:]", Function::InternalLinkage,
@@ -611,8 +599,6 @@ TEST_F(InstrProfTest, test_pgo_function_name) {
611599
TEST_F(InstrProfTest, test_irpgo_read_deprecated_names) {
612600
LLVMContext Ctx;
613601
auto M = std::make_unique<Module>("MyModule.cpp", Ctx);
614-
// Use Mach-O mangling so that non-private symbols get a `_` prefix.
615-
M->setDataLayout(DataLayout("m:o"));
616602
auto *FTy = FunctionType::get(Type::getVoidTy(Ctx), /*isVarArg=*/false);
617603
auto *InternalFooF =
618604
Function::Create(FTy, Function::InternalLinkage, "InternalFoo", M.get());

0 commit comments

Comments
 (0)