[SampleProfile] Fix non-determinism in promoteMergeNotInlinedContextSamples()

We're seeing non-determinism with loading sample profiles. It seems to
be related to the order in which we merge FunctionSamples in
promoteMergeNotInlinedContextSamples(). Use a MapVector to iterate over
NonInlinedCallSites in the order entries were inserted.

Reviewed By: wenlei, davidxl

Differential Revision: https://reviews.llvm.org/D131592
This commit is contained in:
Arthur Eubanks 2022-08-10 09:30:30 -07:00
parent 20451cb06b
commit a3ac1cfaed
1 changed files with 10 additions and 9 deletions

View File

@ -25,6 +25,7 @@
#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/ADT/PriorityQueue.h"
#include "llvm/ADT/SCCIterator.h"
#include "llvm/ADT/SmallVector.h"
@ -475,7 +476,7 @@ protected:
const SmallVectorImpl<CallBase *> &Candidates, const Function &F,
bool Hot);
void promoteMergeNotInlinedContextSamples(
DenseMap<CallBase *, const FunctionSamples *> NonInlinedCallSites,
MapVector<CallBase *, const FunctionSamples *> NonInlinedCallSites,
const Function &F);
std::vector<Function *> buildFunctionOrder(Module &M, CallGraph *CG);
std::unique_ptr<ProfiledCallGraph> buildProfiledCallGraph(CallGraph &CG);
@ -1106,7 +1107,7 @@ bool SampleProfileLoader::inlineHotFunctions(
"ProfAccForSymsInList should be false when profile-sample-accurate "
"is enabled");
DenseMap<CallBase *, const FunctionSamples *> LocalNotInlinedCallSites;
MapVector<CallBase *, const FunctionSamples *> LocalNotInlinedCallSites;
bool Changed = false;
bool LocalChanged = true;
while (LocalChanged) {
@ -1126,7 +1127,7 @@ bool SampleProfileLoader::inlineHotFunctions(
AllCandidates.push_back(CB);
if (FS->getHeadSamplesEstimate() > 0 ||
FunctionSamples::ProfileIsCS)
LocalNotInlinedCallSites.try_emplace(CB, FS);
LocalNotInlinedCallSites.insert({CB, FS});
if (callsiteIsHot(FS, PSI, ProfAccForSymsInList))
Hot = true;
else if (shouldInlineColdCallee(*CB))
@ -1409,7 +1410,7 @@ bool SampleProfileLoader::inlineHotFunctionsWithPriority(
if (ExternalInlineAdvisor)
SizeLimit = std::numeric_limits<unsigned>::max();
DenseMap<CallBase *, const FunctionSamples *> LocalNotInlinedCallSites;
MapVector<CallBase *, const FunctionSamples *> LocalNotInlinedCallSites;
// Perform iterative BFS call site prioritized inlining
bool Changed = false;
@ -1466,7 +1467,7 @@ bool SampleProfileLoader::inlineHotFunctionsWithPriority(
ICPCount++;
Changed = true;
} else if (!ContextTracker) {
LocalNotInlinedCallSites.try_emplace(I, FS);
LocalNotInlinedCallSites.insert({I, FS});
}
}
} else if (CalledFunction && CalledFunction->getSubprogram() &&
@ -1479,7 +1480,7 @@ bool SampleProfileLoader::inlineHotFunctionsWithPriority(
}
Changed = true;
} else if (!ContextTracker) {
LocalNotInlinedCallSites.try_emplace(I, Candidate.CalleeSamples);
LocalNotInlinedCallSites.insert({I, Candidate.CalleeSamples});
}
} else if (LTOPhase == ThinOrFullLTOPhase::ThinLTOPreLink) {
findExternalInlineCandidate(I, findCalleeFunctionSamples(*I),
@ -1505,11 +1506,11 @@ bool SampleProfileLoader::inlineHotFunctionsWithPriority(
}
void SampleProfileLoader::promoteMergeNotInlinedContextSamples(
DenseMap<CallBase *, const FunctionSamples *> NonInlinedCallSites,
MapVector<CallBase *, const FunctionSamples *> NonInlinedCallSites,
const Function &F) {
// Accumulate not inlined callsite information into notInlinedSamples
for (const auto &Pair : NonInlinedCallSites) {
CallBase *I = Pair.getFirst();
CallBase *I = Pair.first;
Function *Callee = I->getCalledFunction();
if (!Callee || Callee->isDeclaration())
continue;
@ -1521,7 +1522,7 @@ void SampleProfileLoader::promoteMergeNotInlinedContextSamples(
<< "' into '" << ore::NV("Caller", &F) << "'");
++NumCSNotInlined;
const FunctionSamples *FS = Pair.getSecond();
const FunctionSamples *FS = Pair.second;
if (FS->getTotalSamples() == 0 && FS->getHeadSamplesEstimate() == 0) {
continue;
}