Skip to content

Commit 17fe37d

Browse files
committed
Use a separate valueenumerator for alias cache and memory behavior cache
If we use a shared valueenumerator, imagine the case when one of the AAcache or MBcache is cleared and we clear the valueenumerator. This could give rise to collisions (false positives) in the not-yet-cleared cache!
1 parent b5e4197 commit 17fe37d

File tree

3 files changed

+21
-13
lines changed

3 files changed

+21
-13
lines changed

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -112,12 +112,19 @@ class AliasAnalysis : public SILAnalysis {
112112
/// The computeMemoryBehavior() method uses this map to cache queries.
113113
llvm::DenseMap<MemBehaviorKeyTy, MemoryBehavior> MemoryBehaviorCache;
114114

115-
/// The AliasAnalysis/MemoryBehavior cache can't directly map a pair of
116-
/// ValueBase pointers to alias/memorybehavior results because we'd like to
117-
/// be able to remove deleted pointers without having to scan the whole map.
118-
/// So, instead of storing pointers we map pointers to indices and store the
119-
/// indices.
120-
ValueEnumerator<ValueBase*> ValueBaseToIndex;
115+
/// The AliasAnalysis cache can't directly map a pair of ValueBase pointers
116+
/// to alias results because we'd like to be able to remove deleted pointers
117+
/// without having to scan the whole map. So, instead of storing pointers we
118+
/// map pointers to indices and store the indices.
119+
ValueEnumerator<ValueBase*> AliasValueBaseToIndex;
120+
121+
/// Same as AliasValueBaseToIndex, map a pointer to the indices for
122+
/// MemoryBehaviorCache.
123+
///
124+
/// NOTE: we do not use the same ValueEnumerator for the alias cache,
125+
/// as when either cache is cleared, we can not clear the ValueEnumerator
126+
/// because doing so could give rise to collisions in the other cache.
127+
ValueEnumerator<ValueBase*> MemoryBehaviorValueBaseToIndex;
121128

122129
AliasResult aliasAddressProjection(SILValue V1, SILValue V2,
123130
SILValue O1, SILValue O2);
@@ -134,7 +141,8 @@ class AliasAnalysis : public SILAnalysis {
134141
// The pointer I is going away. We can't scan the whole cache and remove
135142
// all of the occurrences of the pointer. Instead we remove the pointer
136143
// from the cache the translates pointers to indices.
137-
ValueBaseToIndex.invalidateValue(I);
144+
AliasValueBaseToIndex.invalidateValue(I);
145+
MemoryBehaviorValueBaseToIndex.invalidateValue(I);
138146
}
139147

140148
virtual bool needsNotifications() override { return true; }

lib/SILOptimizer/Analysis/AliasAnalysis.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -539,7 +539,7 @@ AliasResult AliasAnalysis::alias(SILValue V1, SILValue V2,
539539
// Flush the cache if the size of the cache is too large.
540540
if (AliasCache.size() > AliasAnalysisMaxCacheSize) {
541541
AliasCache.clear();
542-
ValueBaseToIndex.clear();
542+
AliasValueBaseToIndex.clear();
543543
}
544544

545545
// Calculate the aliasing result and store it in the cache.
@@ -710,8 +710,8 @@ SILAnalysis *swift::createAliasAnalysis(SILModule *M) {
710710

711711
AliasKeyTy AliasAnalysis::toAliasKey(SILValue V1, SILValue V2,
712712
SILType Type1, SILType Type2) {
713-
size_t idx1 = ValueBaseToIndex.getIndex(V1.getDef());
714-
size_t idx2 = ValueBaseToIndex.getIndex(V2.getDef());
713+
size_t idx1 = AliasValueBaseToIndex.getIndex(V1.getDef());
714+
size_t idx2 = AliasValueBaseToIndex.getIndex(V2.getDef());
715715
unsigned R1 = V1.getResultNumber();
716716
unsigned R2 = V2.getResultNumber();
717717
void *t1 = Type1.getOpaqueValue();

lib/SILOptimizer/Analysis/MemoryBehavior.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,7 @@ AliasAnalysis::computeMemoryBehavior(SILInstruction *Inst, SILValue V,
313313
// Flush the cache if the size of the cache is too large.
314314
if (MemoryBehaviorCache.size() > MemoryBehaviorAnalysisMaxCacheSize) {
315315
MemoryBehaviorCache.clear();
316-
ValueBaseToIndex.clear();
316+
MemoryBehaviorValueBaseToIndex.clear();
317317
}
318318

319319
// Calculate the aliasing result and store it in the cache.
@@ -333,8 +333,8 @@ AliasAnalysis::computeMemoryBehaviorInner(SILInstruction *Inst, SILValue V,
333333

334334
MemBehaviorKeyTy AliasAnalysis::toMemoryBehaviorKey(SILValue V1, SILValue V2,
335335
RetainObserveKind M) {
336-
size_t idx1 = ValueBaseToIndex.getIndex(V1.getDef());
337-
size_t idx2 = ValueBaseToIndex.getIndex(V2.getDef());
336+
size_t idx1 = MemoryBehaviorValueBaseToIndex.getIndex(V1.getDef());
337+
size_t idx2 = MemoryBehaviorValueBaseToIndex.getIndex(V2.getDef());
338338
unsigned R2 = V2.getResultNumber();
339339
return {idx1, idx2, R2, M};
340340
}

0 commit comments

Comments
 (0)