This is an archive of the discontinued LLVM Phabricator instance.

[InstrProf] Encode linkage names in IRPGO counter names
ClosedPublic

Authored by ellis on Jul 28 2023, 1:01 PM.

Details

Summary

Prior to this diff, names in the __llvm_prf_names section had the format [<filepath>:]<function-name>, e.g., main.cpp:foo, bar. <filepath> is used to discriminate between possibly identical function names when linkage is local and <function-name> simply comes from F.getName(). This has two problems:

Instead, this diff changes the format to [<filepath>;]<linkage-name>, e.g., main.cpp;_foo, _bar. The hope is that ; won't realistically be found in either <filepath> or <linkage-name>.

To prevent invalidating all prior IRPGO profiles, we also lookup the prior name format when a record is not found (see InstrProfSymtab::create(), readMemprof(), and getInstrProfRecord()). It seems that Swift and Clang FE-PGO rely on the original getPGOFuncName(), so we cannot simply replace it.

Diff Detail

Event Timeline

ellis created this revision.Jul 28 2023, 1:01 PM
Herald added a project: Restricted Project. · View Herald TranscriptJul 28 2023, 1:01 PM
Herald added subscribers: hoy, wlei, Enna1 and 2 others. · View Herald Transcript
ellis edited the summary of this revision. (Show Details)Jul 28 2023, 1:07 PM
ellis added reviewers: MaskRay, kyulee, wenlei, davidxl, tejohnson.
ellis published this revision for review.Jul 28 2023, 1:12 PM
ellis edited the summary of this revision. (Show Details)
Herald added a project: Restricted Project. · View Herald TranscriptJul 28 2023, 1:12 PM
davidxl added inline comments.Jul 28 2023, 4:47 PM
llvm/lib/ProfileData/InstrProf.cpp
269

and --> is

271

is it better to use '>' to indicate context:

foo.c > bar

or <foo.c> bar

295

Consider sharing code with the legacy version. They are almost identical.

ellis added inline comments.Jul 28 2023, 5:06 PM
llvm/lib/ProfileData/InstrProf.cpp
271

I don't think > would work well because objc_direct methods have <> symbols: -<C foo:>. I thought ; worked well because I don't know of any name mangling that uses it.

davidxl added inline comments.Jul 28 2023, 5:25 PM
llvm/lib/ProfileData/InstrProf.cpp
271

ok. Note the <> is used in the unknown case.

ellis added inline comments.Jul 31 2023, 10:19 AM
llvm/lib/ProfileData/InstrProf.cpp
271

Yep! <unknown>;_blah is a possible case, which is why we still need the ; in between the two. I can't imagine ; in a filepath, and I don't know of a case where ; is in a mangled name.

ellis updated this revision to Diff 546620.Aug 2 2023, 3:19 PM

Rebase and address comments

MaskRay accepted this revision.Aug 3 2023, 10:53 AM

Looks reasonable to me. Thanks for fixing this FIXME

// GlobalValue::getGlobalIdentifier() prefixes the filename if the symbol
// is local. This logic will break if there is a colon in the filename,
// but we cannot use rsplit() because ObjC symbols can have colons.
llvm/lib/ProfileData/InstrProf.cpp
313
358
360
361

contains and split duplicate the work to search ;. You may just call split and check whether the second element is empty.

387

while you are here

llvm/lib/ProfileData/InstrProfReader.cpp
1224
llvm/test/Transforms/PGOProfile/statics_counter_naming.ll
8

This line tests -static-func-strip-dirname-prefix=1 by asserting that the NOPATH line does not occur. This is a bit weak.
It's probably better to rm -rf %t && mkdir -p %t/dir && cd %t; cp %s dir/a.ll; opt ... dir/a.ll so that we can actually check -static-func-strip-dirname-prefix=1 , but such a change is not the scope of this patch.

llvm/unittests/ProfileData/InstrProfTest.cpp
526
// Use Mach-O mangling that non-private symbols get a `_` prefix. 
M->setDataLayout(DataLayout("..."))
537

Add a comment that they are ObjC symbols.

549

Personally I think this ASSERT_NE can be removed as *F expresses the intention.

595
// Use Mach-O mangling that non-private symbols get a `_` prefix. 
M->setDataLayout(DataLayout("..."))
599

Perhaps a non-internal linkage for more variance.

1339
This revision is now accepted and ready to land.Aug 3 2023, 10:53 AM
MaskRay added inline comments.Aug 3 2023, 10:55 AM
llvm/lib/ProfileData/InstrProf.cpp
289

Mach-O linker

ELF linkers use different option names and don't have the mangling issue.

ellis updated this revision to Diff 547301.Aug 4 2023, 11:57 AM

Address comments and rebase

llvm/lib/ProfileData/InstrProf.cpp
313

Unfortunately most of this file is wrapped in namespace llvm {} so this gives a warning

warning: extra qualification on member 'getIRPGOFuncName' [-Wextra-qualification]
This revision was landed with ongoing or failed builds.Aug 7 2023, 10:15 AM
This revision was automatically updated to reflect the committed changes.
thakis added a subscriber: thakis.Aug 7 2023, 11:40 AM

Looks like this breaks tests on macOS: http://45.33.8.238/macm1/66536/step_7.txt

Please take a look and revert for now if it takes a while to fix.

ellis added a comment.Aug 7 2023, 11:43 AM

Looks like this breaks tests on macOS: http://45.33.8.238/macm1/66536/step_7.txt

Please take a look and revert for now if it takes a while to fix.

Thanks. Looking into this.

ellis added a comment.Aug 7 2023, 12:43 PM

Looks like this breaks tests on macOS: http://45.33.8.238/macm1/66536/step_7.txt

Please take a look and revert for now if it takes a while to fix.

Thanks. Looking into this.

This should be fixed by https://reviews.llvm.org/D157321