Skip to content

Commit a4258d4

Browse files
authored
Merge pull request #8083 from eeckstein/silanalysis-invalidation-api
2 parents 25653ac + 0e068ea commit a4258d4

31 files changed

+375
-252
lines changed

include/swift/SILOptimizer/Analysis/AliasAnalysis.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -263,15 +263,24 @@ class AliasAnalysis : public SILAnalysis {
263263
/// Encodes the memory behavior query as a MemBehaviorKeyTy.
264264
MemBehaviorKeyTy toMemoryBehaviorKey(SILValue V1, SILValue V2, RetainObserveKind K);
265265

266-
virtual void invalidate(SILAnalysis::InvalidationKind K) override {
266+
virtual void invalidate() override {
267267
AliasCache.clear();
268268
MemoryBehaviorCache.clear();
269269
}
270270

271271
virtual void invalidate(SILFunction *,
272272
SILAnalysis::InvalidationKind K) override {
273-
invalidate(K);
273+
invalidate();
274274
}
275+
276+
/// Notify the analysis about a newly created function.
277+
virtual void notifyAddFunction(SILFunction *F) override { }
278+
279+
/// Notify the analysis about a function which will be deleted from the
280+
/// module.
281+
virtual void notifyDeleteFunction(SILFunction *F) override { }
282+
283+
virtual void invalidateFunctionTables() override { }
275284
};
276285

277286

include/swift/SILOptimizer/Analysis/Analysis.h

Lines changed: 35 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -55,21 +55,14 @@ namespace swift {
5555
/// has been modified.
5656
Branches = 0x4,
5757

58-
/// The pass delete or created new functions.
59-
///
60-
/// The intent behind this is so that analyses that cache
61-
/// SILFunction* to be able to be invalidated and later
62-
/// recomputed so that they are not holding dangling pointers.
63-
Functions = 0x8,
64-
6558
/// Convenience states:
6659
FunctionBody = Calls | Branches | Instructions,
6760

6861
CallsAndInstructions = Calls | Instructions,
6962

7063
BranchesAndInstructions = Branches | Instructions,
7164

72-
Everything = Functions | Calls | Branches | Instructions,
65+
Everything = Calls | Branches | Instructions,
7366
};
7467

7568
/// A list of the known analysis.
@@ -112,22 +105,20 @@ namespace swift {
112105
bool isLocked() { return invalidationLock; }
113106

114107
/// Invalidate all information in this analysis.
115-
virtual void invalidate(InvalidationKind K) {}
108+
virtual void invalidate() = 0;
116109

117110
/// Invalidate all of the information for a specific function.
118-
virtual void invalidate(SILFunction *F, InvalidationKind K) {}
119-
120-
/// Invalidate all of the information for a specific function. Also, we
121-
/// know that this function is a dead function and going to be deleted from
122-
/// the module.
123-
virtual void invalidateForDeadFunction(SILFunction *F, InvalidationKind K) {
124-
// Call the normal invalidate function unless overridden by specific
125-
// analysis.
126-
invalidate(F, K);
127-
}
128-
111+
virtual void invalidate(SILFunction *F, InvalidationKind K) = 0;
112+
129113
/// Notify the analysis about a newly created function.
130-
virtual void notifyAnalysisOfFunction(SILFunction *F) {}
114+
virtual void notifyAddFunction(SILFunction *F) = 0;
115+
116+
/// Notify the analysis about a function which will be deleted from the
117+
/// module.
118+
virtual void notifyDeleteFunction(SILFunction *F) = 0;
119+
120+
/// Notify the analysis about changed witness or vtables.
121+
virtual void invalidateFunctionTables() = 0;
131122

132123
/// Verify the state of this analysis.
133124
virtual void verify() const {}
@@ -190,26 +181,39 @@ namespace swift {
190181
return it.second;
191182
}
192183

193-
virtual void invalidate(SILAnalysis::InvalidationKind K) override {
194-
if (!shouldInvalidate(K)) return;
195-
196-
for (auto D : Storage)
197-
delete D.second;
198-
184+
/// Invalidate all information in this analysis.
185+
virtual void invalidate() override {
199186
Storage.clear();
200187
}
201188

202-
virtual void invalidate(SILFunction *F,
203-
SILAnalysis::InvalidationKind K) override {
204-
if (!shouldInvalidate(K)) return;
205-
189+
/// Helper function to remove the analysis data for a function.
190+
void invalidateFunction(SILFunction *F) {
206191
auto &it = Storage.FindAndConstruct(F);
207192
if (it.second) {
208193
delete it.second;
209194
it.second = nullptr;
210195
}
211196
}
212197

198+
/// Invalidate all of the information for a specific function.
199+
virtual void invalidate(SILFunction *F,
200+
SILAnalysis::InvalidationKind K) override {
201+
if (shouldInvalidate(K))
202+
invalidateFunction(F);
203+
}
204+
205+
/// Notify the analysis about a newly created function.
206+
virtual void notifyAddFunction(SILFunction *F) override { }
207+
208+
/// Notify the analysis about a function which will be deleted from the
209+
/// module.
210+
virtual void notifyDeleteFunction(SILFunction *F) override {
211+
invalidateFunction(F);
212+
}
213+
214+
/// Notify the analysis about changed witness or vtables.
215+
virtual void invalidateFunctionTables() override { }
216+
213217
FunctionAnalysisBase() {}
214218
virtual ~FunctionAnalysisBase() {
215219
for (auto D : Storage)

include/swift/SILOptimizer/Analysis/BasicCalleeAnalysis.h

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,34 @@ class BasicCalleeAnalysis : public SILAnalysis {
132132
return S->getKind() == AnalysisKind::BasicCallee;
133133
}
134134

135-
virtual void invalidate(SILAnalysis::InvalidationKind K) {
136-
if (K & InvalidationKind::Functions)
137-
Cache.reset();
135+
/// Invalidate all information in this analysis.
136+
virtual void invalidate() override {
137+
Cache.reset();
138138
}
139139

140-
virtual void invalidate(SILFunction *F, InvalidationKind K) { invalidate(K); }
140+
/// Invalidate all of the information for a specific function.
141+
virtual void invalidate(SILFunction *F, InvalidationKind K) override {
142+
// No invalidation needed because the analysis does not cache anything
143+
// per call-site in functions.
144+
}
145+
146+
/// Notify the analysis about a newly created function.
147+
virtual void notifyAddFunction(SILFunction *F) override {
148+
// Nothing to be done because the analysis does not cache anything
149+
// per call-site in functions.
150+
}
151+
152+
/// Notify the analysis about a function which will be deleted from the
153+
/// module.
154+
virtual void notifyDeleteFunction(SILFunction *F) override {
155+
// No invalidation needed because the analysis does not cache anything
156+
// per call-site in functions.
157+
};
158+
159+
/// Notify the analysis about changed witness or vtables.
160+
virtual void invalidateFunctionTables() override {
161+
Cache.reset();
162+
}
141163

142164
CalleeList getCalleeList(FullApplySite FAS) {
143165
if (!Cache)

include/swift/SILOptimizer/Analysis/CallerAnalysis.h

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -121,11 +121,17 @@ class CallerAnalysis : public SILAnalysis {
121121
return S->getKind() == AnalysisKind::Caller;
122122
}
123123

124-
virtual void notifyAnalysisOfFunction(SILFunction *F) {
125-
RecomputeFunctionList.insert(F);
124+
/// Invalidate all information in this analysis.
125+
virtual void invalidate() override {
126+
FuncInfos.clear();
127+
RecomputeFunctionList.clear();
128+
for (auto &F : Mod) {
129+
RecomputeFunctionList.insert(&F);
130+
}
126131
}
127132

128-
virtual void invalidate(SILFunction *F, InvalidationKind K) {
133+
/// Invalidate all of the information for a specific function.
134+
virtual void invalidate(SILFunction *F, InvalidationKind K) override {
129135
// Should we invalidate based on the invalidation kind.
130136
bool shouldInvalidate = K & InvalidationKind::CallsAndInstructions;
131137
if (!shouldInvalidate)
@@ -138,23 +144,20 @@ class CallerAnalysis : public SILAnalysis {
138144
RecomputeFunctionList.insert(F);
139145
}
140146

141-
virtual void invalidateForDeadFunction(SILFunction *F, InvalidationKind K) {
147+
/// Notify the analysis about a newly created function.
148+
virtual void notifyAddFunction(SILFunction *F) override {
149+
RecomputeFunctionList.insert(F);
150+
}
151+
152+
/// Notify the analysis about a function which will be deleted from the
153+
/// module.
154+
virtual void notifyDeleteFunction(SILFunction *F) override {
142155
invalidateExistingCalleeRelation(F);
143156
RecomputeFunctionList.remove(F);
144157
}
145158

146-
virtual void invalidate(InvalidationKind K) {
147-
// Should we invalidate based on the invalidation kind.
148-
bool shouldInvalidate = K & InvalidationKind::Calls;
149-
if (!shouldInvalidate)
150-
return;
151-
152-
FuncInfos.clear();
153-
RecomputeFunctionList.clear();
154-
for (auto &F : Mod) {
155-
RecomputeFunctionList.insert(&F);
156-
}
157-
}
159+
/// Notify the analysis about changed witness or vtables.
160+
virtual void invalidateFunctionTables() override { }
158161

159162
const FunctionInfo &getCallerInfo(SILFunction *F) {
160163
// Recompute every function in the invalidated function list and empty the

include/swift/SILOptimizer/Analysis/ClassHierarchyAnalysis.h

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,24 @@ class ClassHierarchyAnalysis : public SILAnalysis {
4444
return S->getKind() == AnalysisKind::ClassHierarchy;
4545
}
4646

47-
virtual void invalidate(SILAnalysis::InvalidationKind K) {
48-
// Nothing can invalidate the ClassHierarchyAnalysis!
47+
/// Invalidate all information in this analysis.
48+
virtual void invalidate() override {
49+
// Nothing can invalidate, because types are static and cannot be changed
50+
// during the SIL pass pipeline.
4951
}
5052

53+
/// Invalidate all of the information for a specific function.
54+
virtual void invalidate(SILFunction *F, InvalidationKind K) override { }
55+
56+
/// Notify the analysis about a newly created function.
57+
virtual void notifyAddFunction(SILFunction *F) override { }
58+
59+
/// Notify the analysis about a function which will be deleted from the
60+
/// module.
61+
virtual void notifyDeleteFunction(SILFunction *F) override { }
62+
63+
/// Notify the analysis about changed witness or vtables.
64+
virtual void invalidateFunctionTables() override { }
5165

5266
/// Returns a list of the known direct subclasses of a class \p C in
5367
/// the current module.
@@ -87,10 +101,6 @@ class ClassHierarchyAnalysis : public SILAnalysis {
87101
return ProtocolImplementationsCache.count(C);
88102
}
89103

90-
virtual void invalidate(SILFunction *F, SILAnalysis::InvalidationKind K) {
91-
invalidate(K);
92-
}
93-
94104
private:
95105
/// Compute inheritance properties.
96106
void init();

include/swift/SILOptimizer/Analysis/DestructorAnalysis.h

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,28 @@ class DestructorAnalysis : public SILAnalysis {
3434
/// Returns true if destruction of T may store to memory.
3535
bool mayStoreToMemoryOnDestruction(SILType T);
3636

37+
/// No invalidation is needed.
38+
virtual void invalidate() override {
39+
// Nothing can invalidate, because types are static and cannot be changed
40+
// during the SIL pass pipeline.
41+
}
42+
43+
/// No invalidation is needed.
44+
virtual void invalidate(SILFunction *F, InvalidationKind K) override {
45+
// Nothing can invalidate, because types are static and cannot be changed
46+
// during the SIL pass pipeline.
47+
}
48+
49+
/// Notify the analysis about a newly created function.
50+
virtual void notifyAddFunction(SILFunction *F) override { }
51+
52+
/// Notify the analysis about a function which will be deleted from the
53+
/// module.
54+
virtual void notifyDeleteFunction(SILFunction *F) override { }
55+
56+
/// Notify the analysis about changed witness or vtables.
57+
virtual void invalidateFunctionTables() override { }
58+
3759
protected:
3860
bool cacheResult(CanType Type, bool Result);
3961
bool isSafeType(CanType Ty);

include/swift/SILOptimizer/Analysis/EscapeAnalysis.h

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -791,10 +791,24 @@ class EscapeAnalysis : public BottomUpIPAnalysis {
791791
/// node, the pointers do not alias.
792792
bool canPointToSameMemory(SILValue V1, SILValue V2);
793793

794-
virtual void invalidate(InvalidationKind K) override;
794+
/// Invalidate all information in this analysis.
795+
virtual void invalidate() override;
795796

797+
/// Invalidate all of the information for a specific function.
796798
virtual void invalidate(SILFunction *F, InvalidationKind K) override;
797799

800+
/// Notify the analysis about a newly created function.
801+
virtual void notifyAddFunction(SILFunction *F) override { }
802+
803+
/// Notify the analysis about a function which will be deleted from the
804+
/// module.
805+
virtual void notifyDeleteFunction(SILFunction *F) override {
806+
invalidate(F, InvalidationKind::Nothing);
807+
}
808+
809+
/// Notify the analysis about changed witness or vtables.
810+
virtual void invalidateFunctionTables() override { }
811+
798812
virtual void handleDeleteNotification(ValueBase *I) override;
799813

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

include/swift/SILOptimizer/Analysis/SideEffectAnalysis.h

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -379,11 +379,23 @@ class SideEffectAnalysis : public BottomUpIPAnalysis {
379379
/// Get the side-effects of a call site.
380380
void getEffects(FunctionEffects &ApplyEffects, FullApplySite FAS);
381381

382-
/// No invalidation is needed. See comment for SideEffectAnalysis.
383-
virtual void invalidate(InvalidationKind K) override;
382+
/// Invalidate all information in this analysis.
383+
virtual void invalidate() override;
384384

385-
/// No invalidation is needed. See comment for SideEffectAnalysis.
385+
/// Invalidate all of the information for a specific function.
386386
virtual void invalidate(SILFunction *F, InvalidationKind K) override;
387+
388+
/// Notify the analysis about a newly created function.
389+
virtual void notifyAddFunction(SILFunction *F) override { }
390+
391+
/// Notify the analysis about a function which will be deleted from the
392+
/// module.
393+
virtual void notifyDeleteFunction(SILFunction *F) override {
394+
invalidate(F, InvalidationKind::Nothing);
395+
}
396+
397+
/// Notify the analysis about changed witness or vtables.
398+
virtual void invalidateFunctionTables() override { }
387399
};
388400

389401
} // end namespace swift

include/swift/SILOptimizer/Analysis/TypeExpansionAnalysis.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,25 @@ class TypeExpansionAnalysis : public SILAnalysis {
3333

3434
/// Return ProjectionPath to every leaf or intermediate node of the given type.
3535
const ProjectionPathList &getTypeExpansion(SILType B, SILModule *Mod);
36+
37+
/// Invalidate all information in this analysis.
38+
virtual void invalidate() override {
39+
// Nothing can invalidate, because types are static and cannot be changed
40+
// during the SIL pass pipeline.
41+
}
42+
43+
/// Invalidate all of the information for a specific function.
44+
virtual void invalidate(SILFunction *F, InvalidationKind K) override { }
45+
46+
/// Notify the analysis about a newly created function.
47+
virtual void notifyAddFunction(SILFunction *F) override { }
48+
49+
/// Notify the analysis about a function which will be deleted from the
50+
/// module.
51+
virtual void notifyDeleteFunction(SILFunction *F) override { }
52+
53+
/// Notify the analysis about changed witness or vtables.
54+
virtual void invalidateFunctionTables() override { }
3655
};
3756

3857
}

0 commit comments

Comments
 (0)