Skip to content

Commit 7a5fe1d

Browse files
committed
Change the analysis invalidation message from "preserve" to "invalidate".
This commit changes the way passes invalidate analysis. Passes now report the list of traits that they invalidate. We went back and forth on this a few times and we are now going back to the invalidation mode. In a few places in the optimizer passes had to record the fact that they deleted a call or a branch and had to construct the enum that will contain the preserve list. Now passes can either use the new enum states that are the intersection of traits or even send multiple invalidation message. We are making this change now because Mark added a new kind of invalidation trait ("function"). Adopting this new invalidation trait required that we inspect all of the invalidation sites anyway. This commit includes a more efficient use of the 'function' attribute, and our function passes don't invalidate the 'function' attribute anymore.
1 parent 02e08e2 commit 7a5fe1d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

60 files changed

+225
-218
lines changed

include/swift/SILAnalysis/AliasAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,9 +136,9 @@ class AliasAnalysis : public SILAnalysis {
136136
return getMemoryBehavior(Inst, V) == MemoryBehavior::MayHaveSideEffects;
137137
}
138138

139-
virtual void invalidate(SILAnalysis::PreserveKind K) { AliasCache.clear(); }
139+
virtual void invalidate(SILAnalysis::InvalidationKind K) { AliasCache.clear(); }
140140

141-
virtual void invalidate(SILFunction *, SILAnalysis::PreserveKind K) {
141+
virtual void invalidate(SILFunction *, SILAnalysis::InvalidationKind K) {
142142
invalidate(K);
143143
}
144144
};

include/swift/SILAnalysis/Analysis.h

Lines changed: 36 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -29,60 +29,46 @@ namespace swift {
2929
class SILAnalysis {
3030
public:
3131
/// This is a list of values that allow passes to communicate to analysis
32-
/// which traits of the code were preserved. Based on this information
32+
/// which traits of the code were invalidated. Based on this information
3333
/// the analysis can decide if it needs to be invalidated. This information
34-
/// may refer to a specific function or to a list of functions depending
35-
/// on the context in which it is used.
36-
enum PreserveKind : unsigned {
37-
/// The pass does not preserve any analysis trait.
34+
/// may refer to a specific function or to the whole module depending on
35+
/// the context in which it is used.
36+
enum InvalidationKind : unsigned {
37+
/// The pass does not change anithing.
3838
Nothing = 0x0,
3939

40-
/// The pass did not modify any call instructions.
41-
///
42-
/// The intention of this preserve kind is twofold:
43-
///
44-
/// 1. Allow analyses like the CallGraphAnalysis that store pointers to
45-
/// apply instructions and SILFunctions to invalidate their stored
46-
/// information, preventing dangling pointers.
47-
/// 2. Allow analyses like the CallGraphAnalysis to know to search for new
48-
/// edges in the callgraph.
49-
Calls = 0x1,
50-
51-
/// The pass did not modify any branch edges in the CFG beyond reordering
52-
/// them in the successor or predecessor list of a BB.
40+
/// The pass created, deleted or rearranged some instructions in a
41+
/// function.
42+
Instructions = 0x1,
43+
44+
/// The pass modified some calls (apply instructions).
5345
///
54-
/// The intention of this preserve kind is to tell analyses like the
55-
/// Dominance Analysis and the Post Order Analysis that the underlying CFG
56-
/// has been changed up to reordering of branch edges in the successor or
57-
/// predecessor lists of a BB. Unlike the "Calls" preservation kind this
58-
/// is not meant to prevent dangling pointers. This is because all CFG
59-
/// related analyses are able to store basic blocks instead of
60-
/// terminators. This allows for certain useful transformations to occur
61-
/// without requiring recomputation of the dominator tree or CFG post
62-
/// order. Some of these transformations are:
46+
/// The intention of this invalidation kind is to allow analysis that
47+
/// rely on a specific call graph structure to recompute themselves.
48+
Calls = 0x2,
49+
50+
/// A pass has invalidated some branches in the program.
6351
///
64-
/// 1. Converting a branch from one type of branch to another type that
65-
/// preserves the CFG. Ex: Convering a checked_cast_addr_br =>
66-
/// checked_cast_br.
67-
/// 2. Canonicalizing a conditional branch by inverting its branch
68-
/// condition.
69-
Branches = 0x2,
70-
71-
/// This is a list of combined traits that is defined to make the use of
72-
/// the invalidation API more convenient.
73-
ProgramFlow = Calls | Branches, // The pass changed some instructions but
74-
// did not change the overall flow
75-
// of the code.
76-
77-
/// The pass does not delete any functions.
52+
/// The intention of this invalidation kind is to tell analyses like the
53+
/// Dominance Analysis and the PostOrder Analysis that the underlying CFG
54+
/// has been modified.
55+
Branches = 0x4,
56+
57+
/// The pass delete or created new functions.
7858
///
7959
/// The intent behind this is so that analyses that cache
80-
/// SILFunction * to be able to be invalidated and later
60+
/// SILFunction* to be able to be invalidated and later
8161
/// recomputed so that they are not holding dangling pointers.
82-
Functions = 0x4,
62+
Functions = 0x8,
63+
64+
/// Convenience states:
65+
WholeFunction = Calls | Branches | Instructions,
66+
67+
CallsAndInstructions = Calls | Instructions,
68+
69+
BranchesAndInstructions = Branches | Instructions,
8370

84-
/// This Top in case we add a different top besides ProgramFlow.
85-
All = ProgramFlow,
71+
Everything = Functions | Calls | Branches | Instructions,
8672
};
8773

8874
/// A list of the known analysis.
@@ -125,10 +111,10 @@ namespace swift {
125111
bool isLocked() { return invalidationLock; }
126112

127113
/// Invalidate all information in this analysis.
128-
virtual void invalidate(PreserveKind K) {}
114+
virtual void invalidate(InvalidationKind K) {}
129115

130116
/// Invalidate all of the information for a specific function.
131-
virtual void invalidate(SILFunction *F, PreserveKind K) {}
117+
virtual void invalidate(SILFunction *F, InvalidationKind K) {}
132118

133119
/// Verify the state of this analysis.
134120
virtual void verify() const {}
@@ -157,7 +143,7 @@ namespace swift {
157143

158144
/// Return True if the analysis should be invalidated given trait \K is
159145
/// preserved.
160-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) = 0;
146+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) = 0;
161147

162148
/// A stub function that verifies the specific AnalysisTy \p A. This is
163149
/// meant to be overridden by subclasses.
@@ -172,7 +158,7 @@ namespace swift {
172158
return it.second;
173159
}
174160

175-
virtual void invalidate(SILAnalysis::PreserveKind K) override {
161+
virtual void invalidate(SILAnalysis::InvalidationKind K) override {
176162
if (!shouldInvalidate(K)) return;
177163

178164
for (auto D : Storage)
@@ -182,7 +168,7 @@ namespace swift {
182168
}
183169

184170
virtual void invalidate(SILFunction *F,
185-
SILAnalysis::PreserveKind K) override {
171+
SILAnalysis::InvalidationKind K) override {
186172
if (!shouldInvalidate(K)) return;
187173

188174
auto &it = Storage.FindAndConstruct(F);

include/swift/SILAnalysis/BasicCalleeAnalysis.h

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,15 +124,14 @@ class BasicCalleeAnalysis : public SILAnalysis {
124124
return S->getKind() == AnalysisKind::BasicCallee;
125125
}
126126

127-
virtual void invalidate(SILAnalysis::PreserveKind K) {
128-
if (K & PreserveKind::Functions)
129-
return;
130-
131-
delete Cache;
132-
Cache = nullptr;
127+
virtual void invalidate(SILAnalysis::InvalidationKind K) {
128+
if (K & InvalidationKind::Functions) {
129+
delete Cache;
130+
Cache = nullptr;
131+
}
133132
}
134133

135-
virtual void invalidate(SILFunction *F, PreserveKind K) { invalidate(K); }
134+
virtual void invalidate(SILFunction *F, InvalidationKind K) { invalidate(K); }
136135

137136
CalleeList getCalleeList(FullApplySite FAS) {
138137
if (!Cache)

include/swift/SILAnalysis/CallGraphAnalysis.h

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -50,14 +50,14 @@ class CallGraphAnalysis : public SILAnalysis {
5050
return *CG;
5151
}
5252

53-
virtual void invalidate(SILAnalysis::PreserveKind K) {
54-
if (K & PreserveKind::Calls) return;
55-
56-
delete CG;
57-
CG = nullptr;
53+
virtual void invalidate(SILAnalysis::InvalidationKind K) {
54+
if (K & InvalidationKind::Calls) {
55+
delete CG;
56+
CG = nullptr;
57+
}
5858
}
5959

60-
virtual void invalidate(SILFunction *, SILAnalysis::PreserveKind K) {
60+
virtual void invalidate(SILFunction *, SILAnalysis::InvalidationKind K) {
6161
invalidate(K);
6262
}
6363

include/swift/SILAnalysis/ClassHierarchyAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ class ClassHierarchyAnalysis : public SILAnalysis {
4545
return S->getKind() == AnalysisKind::ClassHierarchy;
4646
}
4747

48-
virtual void invalidate(SILAnalysis::PreserveKind K) {
48+
virtual void invalidate(SILAnalysis::InvalidationKind K) {
4949
// Nothing can invalidate the ClassHierarchyAnalysis!
5050
}
5151

@@ -88,7 +88,7 @@ class ClassHierarchyAnalysis : public SILAnalysis {
8888
return ProtocolImplementationsCache.count(C);
8989
}
9090

91-
virtual void invalidate(SILFunction *F, SILAnalysis::PreserveKind K) {
91+
virtual void invalidate(SILFunction *F, SILAnalysis::InvalidationKind K) {
9292
invalidate(K);
9393
}
9494

include/swift/SILAnalysis/DominanceAnalysis.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,8 @@ class DominanceAnalysis : public FunctionAnalysisBase<DominanceInfo> {
4646
return new DominanceInfo(F);
4747
}
4848

49-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
50-
bool branchesPreserved = K & PreserveKind::Branches;
51-
return !branchesPreserved;
49+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
50+
return K & InvalidationKind::Branches;
5251
}
5352
};
5453

@@ -75,9 +74,8 @@ class PostDominanceAnalysis : public FunctionAnalysisBase<PostDominanceInfo> {
7574
return new PostDominanceInfo(F);
7675
}
7776

78-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
79-
bool branchesPreserved = K & PreserveKind::Branches;
80-
return !branchesPreserved;
77+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
78+
return K & InvalidationKind::Branches;
8179
}
8280
};
8381

include/swift/SILAnalysis/EscapeAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -660,13 +660,13 @@ class EscapeAnalysis : public SILAnalysis {
660660
/// Recomputes the connection graphs for all functions the module.
661661
void recompute();
662662

663-
virtual void invalidate(PreserveKind K) {
663+
virtual void invalidate(InvalidationKind K) {
664664
Function2Info.clear();
665665
Allocator.DestroyAll();
666666
shouldRecompute = true;
667667
}
668668

669-
virtual void invalidate(SILFunction *F, PreserveKind K) {
669+
virtual void invalidate(SILFunction *F, InvalidationKind K) {
670670
if (FunctionInfo *FInfo = Function2Info.lookup(F)) {
671671
FInfo->Graph.clear();
672672
FInfo->KnownCallees.clear();

include/swift/SILAnalysis/IVAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ class IVAnalysis final : public FunctionAnalysisBase<IVInfo> {
9595
}
9696

9797
/// For now we always invalidate.
98-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
98+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
9999
return true;
100100
}
101101
};

include/swift/SILAnalysis/LoopAnalysis.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,9 +38,8 @@ class SILLoopAnalysis : public FunctionAnalysisBase<SILLoopInfo> {
3838
return S->getKind() == AnalysisKind::Loop;
3939
}
4040

41-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
42-
bool branchesPreserved = K & PreserveKind::Branches;
43-
return !branchesPreserved;
41+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
42+
return K & InvalidationKind::Branches;
4443
}
4544

4645
// Computes loop information for the given function using dominance

include/swift/SILAnalysis/LoopRegionAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,8 @@ class LoopRegionAnalysis : public FunctionAnalysisBase<LoopRegionFunctionInfo> {
916916
return new LoopRegionFunctionInfo(F, POA->get(F), SLA->get(F));
917917
}
918918

919-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
920-
return !(K & PreserveKind::Branches);
919+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
920+
return K & InvalidationKind::Branches;
921921
}
922922
};
923923

include/swift/SILAnalysis/PostOrderAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,8 +97,8 @@ class PostOrderAnalysis : public FunctionAnalysisBase<PostOrderFunctionInfo> {
9797
return new PostOrderFunctionInfo(F);
9898
}
9999

100-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
101-
return !(K & PreserveKind::Branches);
100+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
101+
return K & InvalidationKind::Branches;
102102
}
103103

104104
public:

include/swift/SILAnalysis/RCIdentityAnalysis.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class RCIdentityAnalysis : public FunctionAnalysisBase<RCIdentityFunctionInfo> {
8181
return new RCIdentityFunctionInfo(DA);
8282
}
8383

84-
virtual bool shouldInvalidate(SILAnalysis::PreserveKind K) override {
84+
virtual bool shouldInvalidate(SILAnalysis::InvalidationKind K) override {
8585
return true;
8686
}
8787

include/swift/SILAnalysis/SideEffectAnalysis.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -332,12 +332,12 @@ class SideEffectAnalysis : public SILAnalysis {
332332
void getEffects(FunctionEffects &FE, FullApplySite FAS);
333333

334334
/// No invalidation is needed. See comment for SideEffectAnalysis.
335-
virtual void invalidate(PreserveKind K) {
335+
virtual void invalidate(InvalidationKind K) {
336336
shouldRecompute = true;
337337
}
338338

339339
/// No invalidation is needed. See comment for SideEffectAnalysis.
340-
virtual void invalidate(SILFunction *F, PreserveKind K) {
340+
virtual void invalidate(SILFunction *F, InvalidationKind K) {
341341
invalidate(K);
342342
}
343343
};

include/swift/SILPasses/PassManager.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ class SILPassManager {
8888
void runOneIteration();
8989

9090
/// \brief Broadcast the invalidation of the module to all analysis.
91-
void invalidateAnalysis(SILAnalysis::PreserveKind K) {
91+
void invalidateAnalysis(SILAnalysis::InvalidationKind K) {
9292
for (auto AP : Analysis)
9393
if (!AP->isLocked())
9494
AP->invalidate(K);
@@ -101,7 +101,7 @@ class SILPassManager {
101101

102102
/// \brief Broadcast the invalidation of the function to all analysis.
103103
void invalidateAnalysis(SILFunction *F,
104-
SILAnalysis::PreserveKind K) {
104+
SILAnalysis::InvalidationKind K) {
105105
// Invalidate the analysis (unless they are locked)
106106
for (auto AP : Analysis)
107107
if (!AP->isLocked())

include/swift/SILPasses/Transforms.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,7 @@ namespace swift {
9797
protected:
9898
SILFunction *getFunction() { return F; }
9999

100-
void invalidateAnalysis(SILAnalysis::PreserveKind K) {
100+
void invalidateAnalysis(SILAnalysis::InvalidationKind K) {
101101
PM->invalidateAnalysis(F, K);
102102
}
103103
};
@@ -123,12 +123,12 @@ namespace swift {
123123

124124
/// Invalidate all of functions in the module, using invalidation
125125
/// information \p K.
126-
void invalidateAnalysis(SILAnalysis::PreserveKind K) {
126+
void invalidateAnalysis(SILAnalysis::InvalidationKind K) {
127127
PM->invalidateAnalysis(K);
128128
}
129129

130130
/// Invalidate only the function \p F, using invalidation information \p K.
131-
void invalidateAnalysis(SILFunction *F, SILAnalysis::PreserveKind K) {
131+
void invalidateAnalysis(SILFunction *F, SILAnalysis::InvalidationKind K) {
132132
PM->invalidateAnalysis(F, K);
133133
}
134134

0 commit comments

Comments
 (0)