Skip to content

Commit ca72104

Browse files
committed
[IPO][SampleContextTracker] Use SmallVector to track context profiles to prevent non-determinism.
Use SmallVector instead of SmallSet to track the context profiles mapped. Doing this can help avoid non-determinism caused by iterating over unordered containers. This bug was found with reverse iteration turning on, --extra-llvm-cmake-variables="-DLLVM_REVERSE_ITERATION=ON". Failing LLVM test profile-context-tracker-debug.ll . Reviewed By: MaskRay, wenlei Differential Revision: https://reviews.llvm.org/D99547
1 parent 247ff26 commit ca72104

File tree

2 files changed

+7
-7
lines changed

2 files changed

+7
-7
lines changed

llvm/include/llvm/Transforms/IPO/SampleContextTracker.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#ifndef LLVM_TRANSFORMS_IPO_SAMPLECONTEXTTRACKER_H
1616
#define LLVM_TRANSFORMS_IPO_SAMPLECONTEXTTRACKER_H
1717

18-
#include "llvm/ADT/SmallSet.h"
18+
#include "llvm/ADT/SmallVector.h"
1919
#include "llvm/ADT/StringMap.h"
2020
#include "llvm/ADT/StringRef.h"
2121
#include "llvm/Analysis/CallGraph.h"
@@ -91,7 +91,7 @@ class ContextTrieNode {
9191
// calling context and the context is identified by path from root to the node.
9292
class SampleContextTracker {
9393
public:
94-
using ContextSamplesTy = SmallSet<FunctionSamples *, 16>;
94+
using ContextSamplesTy = SmallVector<FunctionSamples *, 16>;
9595

9696
SampleContextTracker(StringMap<FunctionSamples> &Profiles);
9797
// Query context profile for a specific callee with given name at a given
@@ -144,7 +144,7 @@ class SampleContextTracker {
144144
StringRef ContextStrToRemove);
145145

146146
// Map from function name to context profiles (excluding base profile)
147-
StringMap<ContextSamplesTy> FuncToCtxtProfileSet;
147+
StringMap<ContextSamplesTy> FuncToCtxtProfiles;
148148

149149
// Root node for context trie tree
150150
ContextTrieNode RootContext;

llvm/lib/Transforms/IPO/SampleContextTracker.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ SampleContextTracker::SampleContextTracker(
183183
SampleContext Context(FuncSample.first(), RawContext);
184184
LLVM_DEBUG(dbgs() << "Tracking Context for function: " << Context << "\n");
185185
if (!Context.isBaseContext())
186-
FuncToCtxtProfileSet[Context.getNameWithoutContext()].insert(FSamples);
186+
FuncToCtxtProfiles[Context.getNameWithoutContext()].push_back(FSamples);
187187
ContextTrieNode *NewNode = getOrCreateContextPath(Context, true);
188188
assert(!NewNode->getFunctionSamples() &&
189189
"New node can't have sample profile");
@@ -268,12 +268,12 @@ SampleContextTracker::getContextSamplesFor(const SampleContext &Context) {
268268
SampleContextTracker::ContextSamplesTy &
269269
SampleContextTracker::getAllContextSamplesFor(const Function &Func) {
270270
StringRef CanonName = FunctionSamples::getCanonicalFnName(Func);
271-
return FuncToCtxtProfileSet[CanonName];
271+
return FuncToCtxtProfiles[CanonName];
272272
}
273273

274274
SampleContextTracker::ContextSamplesTy &
275275
SampleContextTracker::getAllContextSamplesFor(StringRef Name) {
276-
return FuncToCtxtProfileSet[Name];
276+
return FuncToCtxtProfiles[Name];
277277
}
278278

279279
FunctionSamples *SampleContextTracker::getBaseSamplesFor(const Function &Func,
@@ -297,7 +297,7 @@ FunctionSamples *SampleContextTracker::getBaseSamplesFor(StringRef Name,
297297
// We have profile for function under different contexts,
298298
// create synthetic base profile and merge context profiles
299299
// into base profile.
300-
for (auto *CSamples : FuncToCtxtProfileSet[Name]) {
300+
for (auto *CSamples : FuncToCtxtProfiles[Name]) {
301301
SampleContext &Context = CSamples->getContext();
302302
ContextTrieNode *FromNode = getContextFor(Context);
303303
if (FromNode == Node)

0 commit comments

Comments
 (0)