Skip to content

Commit a6e8277

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 11144ec commit a6e8277

File tree

1 file changed

+303
-0
lines changed

1 file changed

+303
-0
lines changed
Lines changed: 303 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,303 @@
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+
llvm::DenseMap<SILBasicBlock *, SILInstruction *> genForRoot;
121+
for (auto *instruction : effects.gens()) {
122+
if (effects.isLocalGen(instruction))
123+
continue;
124+
auto *block = instruction->getParent();
125+
rootBlocks.push_back(block);
126+
// If none of this block's successors are in the region, then we don't
127+
// need to visit the whole block--just the portion starting from this gen.
128+
if (!llvm::any_of(block->getSuccessorBlocks(),
129+
[&](SILBasicBlock *successor) {
130+
return visitor.isInRegion(successor);
131+
})) {
132+
genForRoot[block] = instruction;
133+
}
134+
}
135+
// Then do a backward DFS from those roots, and visit the blocks in reverse
136+
// post-order.
137+
SILCFGBackwardDFS<This> dfs(*this, rootBlocks);
138+
for (auto *block : dfs.reversePostOrder()) {
139+
// Check whether this block contains a non-local gen.
140+
auto iterator = genForRoot.find(block);
141+
if (iterator != genForRoot.end()) {
142+
visitBlockFromGen(iterator->getSecond());
143+
} else {
144+
visitBlock(block);
145+
}
146+
}
147+
// Finally, visit local gens which weren't visited already.
148+
for (auto *instruction : effects.localGens()) {
149+
auto *block = instruction->getParent();
150+
auto isInRegion = dfs.cachedVisited && dfs.cachedVisited->contains(block);
151+
auto visitedFullBlock = genForRoot.find(block) == genForRoot.end();
152+
if (isInRegion && visitedFullBlock)
153+
continue;
154+
// This local gen is either entirely outside the blocks that define the
155+
// region (!isInRegion) or it is in one of the bottom (i.e. one none of
156+
// whose successors are in the region) blocks in the region only the top
157+
// of which was already visited. Either way, the instructions between the
158+
// local gen and its kill have not yet been visited. Visit them now.
159+
auto foundLocalKill = visitBlockFromGenUntilBegin(instruction);
160+
assert(foundLocalKill && "local gen without local kill?!");
161+
(void)foundLocalKill;
162+
}
163+
}
164+
165+
private:
166+
/// Entry points for visiting: they visit increasingly large portions of a
167+
/// block.
168+
/// - visitBlockFromGenUntilBegin: Instructions and phi until a kill.
169+
/// - visitBlockFromGen: Instructions, phi, and begin.
170+
/// - visitBlock: End, instructions, phi, and begin.
171+
172+
/// Visit instructions and phis starting from the specified gen until a kill
173+
/// is found.
174+
bool visitBlockFromGenUntilBegin(SILInstruction *from) {
175+
assert(effects.effectForInstruction(from) == Effects::Effect::Gen());
176+
for (auto *instruction = from; instruction;
177+
instruction = instruction->getPreviousInstruction()) {
178+
if (visitInstruction(instruction))
179+
return true;
180+
}
181+
auto *block = from->getParent();
182+
if (block->hasPhi()) {
183+
if (visitPhi(block))
184+
return true;
185+
}
186+
return false;
187+
}
188+
189+
/// Visit a block from a non-local gen which begins the region.
190+
///
191+
/// Avoids visiting the portion of the block occurring after an initial gen.
192+
void visitBlockFromGen(SILInstruction *from) {
193+
auto *block = from->getParent();
194+
195+
assert(effects.effectForInstruction(from) == Effects::Effect::Gen());
196+
assert(!llvm::any_of(
197+
block->getSuccessorBlocks(),
198+
[&](SILBasicBlock *successor) { return visited.contains(successor); }));
199+
200+
visited.insert(block);
201+
bool foundLocalKill = visitBlockFromGenUntilBegin(from);
202+
assert(!foundLocalKill && "found local kill for non-local gen?!");
203+
(void)foundLocalKill;
204+
visitBlockBegin(block);
205+
}
206+
207+
/// Visit a block fully; its end, its body, its phi, and its begin.
208+
void visitBlock(SILBasicBlock *block) {
209+
visitBlockEnd(block);
210+
for (auto &instruction : llvm::reverse(*block)) {
211+
visitInstruction(&instruction);
212+
}
213+
if (block->hasPhi())
214+
visitPhi(block);
215+
visitBlockBegin(block);
216+
}
217+
218+
/// Visit block components:
219+
/// - block end
220+
/// - instruction
221+
/// - phi
222+
/// - block begin
223+
224+
/// Visit an instruction. Returns whether it is a barrier.
225+
bool visitInstruction(SILInstruction *instruction) {
226+
if (auto *eai = dyn_cast<EndAccessInst>(instruction)) {
227+
runningLiveAccessScopes.insert(eai->getBeginAccess());
228+
} else if (auto *bai = dyn_cast<BeginAccessInst>(instruction)) {
229+
runningLiveAccessScopes.erase(bai);
230+
}
231+
return handleEffect(effects.effectForInstruction(instruction));
232+
}
233+
234+
/// Visit a phi. Returns whether it is a barrier.
235+
bool visitPhi(SILBasicBlock *block) {
236+
return handleEffect(effects.effectForPhi(block));
237+
}
238+
239+
/// Visit a block begin. If any access scopes are live, record them for use
240+
/// (unioning) when the end of a predecessor is visited.
241+
void visitBlockBegin(SILBasicBlock *block) {
242+
if (!runningLiveAccessScopes.empty()) {
243+
liveInAccessScopes[block] = runningLiveAccessScopes;
244+
}
245+
}
246+
247+
/// Visit a block end. Set the running access scopes for the block to be the
248+
/// union of all access scopes live at the begin of successor blocks. Returns
249+
/// whether the block is a barrier.
250+
bool visitBlockEnd(SILBasicBlock *block) {
251+
visited.insert(block);
252+
runningLiveAccessScopes.clear();
253+
for (auto *successor : block->getSuccessorBlocks()) {
254+
auto iterator = liveInAccessScopes.find(successor);
255+
if (iterator != liveInAccessScopes.end()) {
256+
for (auto *bai : iterator->getSecond()) {
257+
runningLiveAccessScopes.insert(bai);
258+
}
259+
}
260+
}
261+
// If any of this block's predecessors haven't already been visited, it
262+
// means that they aren't in the region and consequently this block is a
263+
// barrier block.
264+
if (llvm::any_of(block->getSuccessorBlocks(), [&](SILBasicBlock *block) {
265+
return !visited.contains(block);
266+
})) {
267+
handleBarrier();
268+
return true;
269+
}
270+
271+
return false;
272+
}
273+
274+
/// Effect procecessing
275+
276+
bool handleEffect(typename Effects::Effect effect) {
277+
switch (effect.value) {
278+
case Effects::Effect::Value::NoEffect:
279+
return false;
280+
case Effects::Effect::Value::Gen:
281+
runningLiveAccessScopes.clear();
282+
return false;
283+
case Effects::Effect::Value::Kill:
284+
handleBarrier();
285+
return true;
286+
}
287+
}
288+
289+
void handleBarrier() {
290+
for (auto *scope : runningLiveAccessScopes) {
291+
visitor.visitBarrierAccessScope(scope);
292+
}
293+
}
294+
295+
/// SILCFGBackwardDFS::Visitor
296+
friend struct SILCFGBackwardDFS<This>;
297+
298+
/// Whether the block is in the region of interest. Just a passhthrough to
299+
/// our visitor.
300+
bool isInRegion(SILBasicBlock *block) { return visitor.isInRegion(block); }
301+
};
302+
303+
} // end namespace swift

0 commit comments

Comments
 (0)