Skip to content

Commit 7b8d88e

Browse files
committed
[SILOpt] Added VisitBarrierAccessScopes utility.
The new utility finds access scopes which are barriers by finding access scopes which themselves contain barriers. This is necessary to (1) allow hoisting through access scopes when possible (i.e. not simply treating all end_access instructions as barriers) and (2) not hoist into access scopes that contain barriers and in so doing introduce exclusivity violations.
1 parent ccc8840 commit 7b8d88e

File tree

1 file changed

+287
-0
lines changed

1 file changed

+287
-0
lines changed
Lines changed: 287 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,287 @@
1+
//===--- VisitBarrierAccessScopes.h - Find access scopes with barriers ----===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2022 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
//
13+
// Given a region of a function, backwards-reachable from some set of roots, on
14+
// which gen/kill effects can be determined, determines which access scopes must
15+
// also be treated as kills in view of the rule that a kill within an access
16+
// scope makes the access scope itself a kill.
17+
//
18+
//===----------------------------------------------------------------------===//
19+
20+
#include "swift/SIL/BasicBlockDatastructures.h"
21+
#include "swift/SIL/BasicBlockUtils.h"
22+
#include "llvm/ADT/DenseMap.h"
23+
#include "llvm/ADT/Optional.h"
24+
#include "llvm/ADT/STLExtras.h"
25+
#include "llvm/ADT/SmallPtrSet.h"
26+
27+
namespace swift {
28+
29+
class BeginAccessInst;
30+
class EndAccessInst;
31+
class SILInstruction;
32+
class SILBasicBlock;
33+
class SILInstruction;
34+
35+
/// Visits the begin_access instructions corresponding to access scopes that
36+
/// must be regarded as barriers (in particular, their end_access instructions
37+
/// must be) because they contain other barriers.
38+
///
39+
/// interface Effects {
40+
/// typename Effect
41+
///
42+
/// /// The root instructions from which to walk.
43+
/// iterable gens()
44+
///
45+
/// /// The gens which are killed within the block where they occur.
46+
/// iterable localGens()
47+
///
48+
/// /// Whether the indicated instruction is killed within the specified
49+
/// /// block.
50+
/// bool isLocalGen(SILInstruction *)
51+
///
52+
/// /// The effect, if any, of the specified instruction.
53+
/// Effects::Effect effectForPhi(SILBasicBlock *)
54+
///
55+
/// /// The effect, if any, of the phis of the specified block.
56+
/// Effects::Effect effectForInstruction(SILInstruction *)
57+
/// }
58+
/// interface Visitor {
59+
/// /// Whether the indicated basic block is within the region of the graph
60+
/// /// that should be traversed.
61+
/// bool isInRegion(SILBasicBlock *)
62+
///
63+
/// /// Visit each discovered begin_access instruction which defines a barrier
64+
/// /// scope.
65+
/// void visitBarrierAccessScope(BeginAccessInst *)
66+
/// }
67+
///
68+
/// Implements SILCFGBackwardDFS::Visitor
69+
template <typename Effects, typename Visitor>
70+
class VisitBarrierAccessScopes {
71+
using This = VisitBarrierAccessScopes<Effects, Visitor>;
72+
/// Describes the effect, if any, of each instruction and phi.
73+
Effects &effects;
74+
/// Describes the search region and visits the found barriers.
75+
Visitor &visitor;
76+
/// The function in which to find barrier access scopes.
77+
SILFunction *function;
78+
/// The blocks which have already been visited. Used to determine whether a
79+
/// block being visited is a barrier block.
80+
BasicBlockSet visited;
81+
82+
/// The access scopes that are live at the begin of each block after visiting
83+
/// all the block's predecessors and its instructions.
84+
llvm::DenseMap<SILBasicBlock *, llvm::SmallPtrSet<BeginAccessInst *, 2>>
85+
liveInAccessScopes;
86+
/// While visiting a block's instructions, the access scopes that are
87+
/// currently live.
88+
llvm::SmallPtrSet<BeginAccessInst *, 2> runningLiveAccessScopes;
89+
90+
public:
91+
VisitBarrierAccessScopes(SILFunction *function, Effects &effects,
92+
Visitor &visitor)
93+
: effects(effects), visitor(visitor), function(function),
94+
visited(function){};
95+
96+
/// Visit the begin_access instructions.
97+
///
98+
/// Sort the backwards-reachable, in-region blocks topologically. Then visit
99+
/// them in the reverse of that order, so that when any block is visited, all
100+
/// of its non-backedge* successors will already have been visited.
101+
///
102+
/// While visiting, which access scopes are live within blocks is tracked,
103+
/// storing the access scopes that are live at the begin of a block, and
104+
/// noting that every access scope live at the begin of any successor is also
105+
/// live at the end of a block.
106+
///
107+
/// * These are backedges in the reversed graph, that are ignored in a reverse
108+
/// depth-first search.
109+
///
110+
/// Finally, look through the instructions between local gens and their kills
111+
/// for further barrier access scopes.
112+
void visit() {
113+
// First, collect the gens whose blocks should be used as the roots of the
114+
// region--those that are non-local.
115+
//
116+
// Keep track of which gens are at the beginning of the region (i.e. none of
117+
// whose successors are in the region) so that we can avoid iterating over
118+
// the instructions below those gens.
119+
SmallVector<SILBasicBlock *, 32> rootBlocks;
120+
for (auto *instruction : effects.gens()) {
121+
if (effects.isLocalGen(instruction))
122+
continue;
123+
auto *block = instruction->getParent();
124+
rootBlocks.push_back(block);
125+
}
126+
// Then do a backward DFS from those roots, and visit the blocks in reverse
127+
// post-order.
128+
SILCFGBackwardDFS<This> dfs(*this, rootBlocks);
129+
for (auto *block : dfs.reversePostOrder()) {
130+
visitBlock(block);
131+
}
132+
// Finally, visit local gens which weren't visited already.
133+
for (auto *instruction : effects.localGens()) {
134+
auto *block = instruction->getParent();
135+
auto isInRegion = dfs.cachedVisited && dfs.cachedVisited->contains(block);
136+
if (isInRegion)
137+
continue;
138+
// This local gen is either entirely outside the blocks that define the
139+
// region (!isInRegion) or it is in one of the bottom (i.e. one none of
140+
// whose successors are in the region) blocks in the region only the top
141+
// of which was already visited. Either way, the instructions between the
142+
// local gen and its kill have not yet been visited. Visit them now.
143+
auto foundLocalKill = visitBlockFromGenUntilBegin(instruction);
144+
assert(foundLocalKill && "local gen without local kill?!");
145+
(void)foundLocalKill;
146+
}
147+
}
148+
149+
private:
150+
/// Entry points for visiting: they visit increasingly large portions of a
151+
/// block.
152+
/// - visitBlockFromGenUntilBegin: Instructions and phi until a kill.
153+
/// - visitBlockFromGen: Instructions, phi, and begin.
154+
/// - visitBlock: End, instructions, phi, and begin.
155+
156+
/// Visit instructions and phis starting from the specified gen until a kill
157+
/// is found.
158+
bool visitBlockFromGenUntilBegin(SILInstruction *from) {
159+
assert(effects.effectForInstruction(from) == Effects::Effect::Gen());
160+
for (auto *instruction = from; instruction;
161+
instruction = instruction->getPreviousInstruction()) {
162+
if (visitInstruction(instruction))
163+
return true;
164+
}
165+
auto *block = from->getParent();
166+
if (block->hasPhi()) {
167+
if (visitPhi(block))
168+
return true;
169+
}
170+
return false;
171+
}
172+
173+
/// Visit a block from a non-local gen which begins the region.
174+
///
175+
/// Avoids visiting the portion of the block occurring after an initial gen.
176+
void visitBlockFromGen(SILInstruction *from) {
177+
auto *block = from->getParent();
178+
179+
assert(effects.effectForInstruction(from) == Effects::Effect::Gen());
180+
assert(!llvm::any_of(
181+
block->getSuccessorBlocks(),
182+
[&](SILBasicBlock *successor) { return visited.contains(successor); }));
183+
184+
visited.insert(block);
185+
bool foundLocalKill = visitBlockFromGenUntilBegin(from);
186+
assert(!foundLocalKill && "found local kill for non-local gen?!");
187+
(void)foundLocalKill;
188+
visitBlockBegin(block);
189+
}
190+
191+
/// Visit a block fully; its end, its body, its phi, and its begin.
192+
void visitBlock(SILBasicBlock *block) {
193+
visitBlockEnd(block);
194+
for (auto &instruction : llvm::reverse(*block)) {
195+
visitInstruction(&instruction);
196+
}
197+
if (block->hasPhi())
198+
visitPhi(block);
199+
visitBlockBegin(block);
200+
}
201+
202+
/// Visit block components:
203+
/// - block end
204+
/// - instruction
205+
/// - phi
206+
/// - block begin
207+
208+
/// Visit an instruction. Returns whether it is a barrier.
209+
bool visitInstruction(SILInstruction *instruction) {
210+
if (auto *eai = dyn_cast<EndAccessInst>(instruction)) {
211+
runningLiveAccessScopes.insert(eai->getBeginAccess());
212+
} else if (auto *bai = dyn_cast<BeginAccessInst>(instruction)) {
213+
runningLiveAccessScopes.erase(bai);
214+
}
215+
return handleEffect(effects.effectForInstruction(instruction));
216+
}
217+
218+
/// Visit a phi. Returns whether it is a barrier.
219+
bool visitPhi(SILBasicBlock *block) {
220+
return handleEffect(effects.effectForPhi(block));
221+
}
222+
223+
/// Visit a block begin. If any access scopes are live, record them for use
224+
/// (unioning) when the end of a predecessor is visited.
225+
void visitBlockBegin(SILBasicBlock *block) {
226+
if (!runningLiveAccessScopes.empty()) {
227+
liveInAccessScopes[block] = runningLiveAccessScopes;
228+
}
229+
}
230+
231+
/// Visit a block end. Set the running access scopes for the block to be the
232+
/// union of all access scopes live at the begin of successor blocks. Returns
233+
/// whether the block is a barrier.
234+
bool visitBlockEnd(SILBasicBlock *block) {
235+
visited.insert(block);
236+
runningLiveAccessScopes.clear();
237+
for (auto *successor : block->getSuccessorBlocks()) {
238+
auto iterator = liveInAccessScopes.find(successor);
239+
if (iterator != liveInAccessScopes.end()) {
240+
for (auto *bai : iterator->getSecond()) {
241+
runningLiveAccessScopes.insert(bai);
242+
}
243+
}
244+
}
245+
// If any of this block's predecessors haven't already been visited, it
246+
// means that they aren't in the region and consequently this block is a
247+
// barrier block.
248+
if (llvm::any_of(block->getSuccessorBlocks(), [&](SILBasicBlock *block) {
249+
return !visited.contains(block);
250+
})) {
251+
handleBarrier();
252+
return true;
253+
}
254+
255+
return false;
256+
}
257+
258+
/// Effect procecessing
259+
260+
bool handleEffect(typename Effects::Effect effect) {
261+
switch (effect.value) {
262+
case Effects::Effect::Value::NoEffect:
263+
return false;
264+
case Effects::Effect::Value::Gen:
265+
runningLiveAccessScopes.clear();
266+
return false;
267+
case Effects::Effect::Value::Kill:
268+
handleBarrier();
269+
return true;
270+
}
271+
}
272+
273+
void handleBarrier() {
274+
for (auto *scope : runningLiveAccessScopes) {
275+
visitor.visitBarrierAccessScope(scope);
276+
}
277+
}
278+
279+
/// SILCFGBackwardDFS::Visitor
280+
friend struct SILCFGBackwardDFS<This>;
281+
282+
/// Whether the block is in the region of interest. Just a passhthrough to
283+
/// our visitor.
284+
bool isInRegion(SILBasicBlock *block) { return visitor.isInRegion(block); }
285+
};
286+
287+
} // end namespace swift

0 commit comments

Comments
 (0)