This is an archive of the discontinued LLVM Phabricator instance.

[llvm-profgen] Fixing a context attribure update issue due to a non-derministic processing order on different platforms.
ClosedPublic

Authored by hoy on Mar 31 2022, 12:36 PM.

Details

Summary

A context can be created by invoking the getFunctionProfileForContext function in two ways:

  • by using a probe and its calling context.
  • by removing the leaf frame from an existing contexts. The first way is used when generating a function profile for a given LBR range, and the input WasLeafInlined is computed depending on the actually probe in the LBR range. The second way is used when using the entry count of an inlinee function profile to update its inliner callsite count, so WasLeafInlined is unknown for the inliner frame.

The two invocations can happen in different order on different platforms, since the lbr ranges are stored in an unordered_map, and we are making sure ContextWasInlined is always set correctly.

This should fix the random test failure introduced by D121655

Diff Detail

Event Timeline

hoy created this revision.Mar 31 2022, 12:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 31 2022, 12:36 PM
Herald added subscribers: modimo, wenlei. · View Herald Transcript
hoy requested review of this revision.Mar 31 2022, 12:36 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 31 2022, 12:36 PM
hoy updated this revision to Diff 419549.Mar 31 2022, 12:50 PM

Updating D122844: [llvm-profgen] Fixing a context attribure update issue due to a non-derministic processing order on different platforms.

wenlei added inline comments.Mar 31 2022, 1:40 PM
llvm/tools/llvm-profgen/ProfileGenerator.cpp
729

This fix assumes that the 1st is always going to be called for given context, and it's just a matter of order. Is that actually true?

What is the exact call path for both cases?

hoy added inline comments.Mar 31 2022, 1:44 PM
llvm/tools/llvm-profgen/ProfileGenerator.cpp
729

Yes. Since we go through the lbr samples in an unorderd way (see CSProfileGenerator::populateBodySamplesWithProbes, ProbeCounter is an unordered_map), #1 and #2 can be called in arbitrary order on different platforms. If #1 is called before #2, the context will have the correct attribute set even without this fix. On the contrary, we need the fix for #1 to update already existing context.

wenlei added inline comments.Mar 31 2022, 1:50 PM
llvm/tools/llvm-profgen/ProfileGenerator.cpp
729

Sorry if the question wasn't clear, and not sure if you answered the question:

  • 1st is always going to be called for given context? asking because if in some cases we only call 2nd, then the flag will be missing despite being deterministic.
  • What is the exact call path for both cases?
hoy added inline comments.Mar 31 2022, 2:06 PM
llvm/tools/llvm-profgen/ProfileGenerator.cpp
729

1st is always going to be called for given context?

That's not always true. The context could never be executed but its inlinee context can be. Yes, the flag could be missing in this case.

What is the exact call path for both cases?

For #1, when the inliner frame has a probe executed. Something like

llvm::sampleprof::CSProfileGenerator::getFunctionProfileForContext(llvm::SmallVector<llvm::sampleprof::SampleContextFrame, 1u> const&, bool) (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:710)
llvm::sampleprof::CSProfileGenerator::getFunctionProfileForLeafProbe(llvm::ArrayRef<llvm::sampleprof::SampleContextFrame>, llvm::MCDecodedPseudoProbe const*) (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:1131)
llvm::sampleprof::CSProfileGenerator::populateBodySamplesWithProbes(std::map<std::pair<unsigned long, unsigned long>, unsigned long, std::less<std::pair<unsigned long, unsigned long> >, std::allocator<std::pair<std::pair<unsigned long, unsigned long> const, unsigned long> > > const&, llvm::ArrayRef<llvm::sampleprof::SampleContextFrame>) (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:1041)
llvm::sampleprof::CSProfileGenerator::generateProbeBasedProfile() (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:1018)
llvm::sampleprof::CSProfileGenerator::generateProfile() (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:729)
main (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/llvm-profgen.cpp:183)

For #2, when an inlinee's probe is executed and the probe is an entry probe. The entry probe count is then used to update the inliner's callsite count.

llvm::sampleprof::CSProfileGenerator::getFunctionProfileForContext(llvm::SmallVector<llvm::sampleprof::SampleContextFrame, 1u> const&, bool) (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:728)
llvm::sampleprof::CSProfileGenerator::populateBodySamplesWithProbes(std::map<std::pair<unsigned long, unsigned long>, unsigned long, std::less<std::pair<unsigned long, unsigned long> >, std::allocator<std::pair<std::pair<unsigned long, unsigned long> const, unsigned long> > > const&, llvm::ArrayRef<llvm::sampleprof::SampleContextFrame>) (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/**ProfileGenerator.cpp:1081**)
llvm::sampleprof::CSProfileGenerator::generateProbeBasedProfile() (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:1034)
llvm::sampleprof::CSProfileGenerator::generateProfile() (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/ProfileGenerator.cpp:745)
main (/home/hoy/src/llvm-project/llvm/tools/llvm-profgen/llvm-profgen.cpp:183)
__libc_start_main (:61)
_start (:13)
wenlei accepted this revision.Mar 31 2022, 5:08 PM
wenlei added inline comments.
llvm/tools/llvm-profgen/ProfileGenerator.cpp
729

Ok, please point out the answer to my 1st question in the comment, and mention the current fix is a hack (with a FIXME or TODO), to be fixed later. Please also try to make the rest of the comment more concise.

This revision is now accepted and ready to land.Mar 31 2022, 5:08 PM
hoy updated this revision to Diff 419590.Mar 31 2022, 5:31 PM

Updating D122844: [llvm-profgen] Fixing a context attribure update issue due to a non-derministic processing order on different platforms.

This revision was landed with ongoing or failed builds.Mar 31 2022, 5:34 PM
This revision was automatically updated to reflect the committed changes.