Skip to content

Commit 58030f0

Browse files
committed
[Reachability] NFC: Split initial from barrier.
Although by analogy with def instructions as barrier instructions one could understand how a block where the def appears as a phi could be regarded as a barrier block, the analogy is nonobvious. Reachability knows the difference between an initial block and a barrier block. Although most current clients don't care about this distinction, one does. Here, Reachability calls back with visitInitialBlock for the former and visitBarrierBlock for the latter. Most clients are updated to have the same implementation in both visitBarrierBlock and visitInitialBlock. The findBarriersBackward client is updated to retain the distinction and pass it on to its clients. Its one client, CanonicalizeOSSALifetime is updated to have a simpler handling for barrier edges and to ignore the initial blocks.
1 parent d65f113 commit 58030f0

File tree

6 files changed

+37
-6
lines changed

6 files changed

+37
-6
lines changed

include/swift/SILOptimizer/Analysis/Reachability.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -347,6 +347,7 @@ class IterativeBackwardReachability final {
347347
/// void visitBarrierInstruction(SILInstruction *)
348348
/// void visitBarrierPhi(SILBasicBlock *)
349349
/// void visitBarrierBlock(SILBasicBlock *)
350+
/// void visitInitialBlock(SILBasicBlock *)
350351
/// }
351352
template <typename Visitor>
352353
void findBarriers(Visitor &visitor);
@@ -423,6 +424,7 @@ class IterativeBackwardReachability final {
423424
/// void visitBarrierInstruction(SILInstruction *)
424425
/// void visitBarrierPhi(SILBasicBlock *)
425426
/// void visitBarrierBlock(SILBasicBlock *)
427+
/// void visitInitialBlock(SILBasicBlock *)
426428
/// }
427429
template <typename Visitor>
428430
bool findBarrier(SILInstruction *from, SILBasicBlock *block,
@@ -710,6 +712,10 @@ IterativeBackwardReachability<Effects>::meetOverSuccessors(
710712
/// /// Additionally, this may be invoked with the effective def block if
711713
/// /// no barriers are found between the gens and it.
712714
/// void visitBarrierBlock(SILBasicBlock *)
715+
///
716+
/// /// Invoked with either the function entry block or one of the specified
717+
/// /// initial blocks.
718+
/// void visitInitialBlock(SILBasicBlock *)
713719
/// }
714720
template <typename Effects>
715721
template <typename Visitor>
@@ -765,6 +771,10 @@ void IterativeBackwardReachability<Effects>::findBarriers(Visitor &visitor) {
765771
/// /// Additionally, this may be invoked with the effective def block if
766772
/// /// no barriers are found between the gens and it.
767773
/// void visitBarrierBlock(SILBasicBlock *)
774+
///
775+
/// /// Invoked with either the function entry block or one of the specified
776+
/// /// initial blocks.
777+
/// void visitInitialBlock(SILBasicBlock *)
768778
/// }
769779
template <typename Effects>
770780
template <typename Visitor>
@@ -793,7 +803,7 @@ bool IterativeBackwardReachability<Effects>::findBarrier(SILInstruction *from,
793803
}
794804
assert(result.getEffectForBlock(block) != Effect::Kill());
795805
if (stopAtBlock(block)) {
796-
visitor.visitBarrierBlock(block);
806+
visitor.visitInitialBlock(block);
797807
return true;
798808
}
799809
return false;
@@ -922,6 +932,12 @@ struct ReachableBarriers final {
922932
/// (2) at least one adjacent edge's target is not reachable-at-begin.
923933
llvm::SmallVector<SILBasicBlock *, 4> edges;
924934

935+
/// Terminal blocks that were reached; blocks such that
936+
/// (1) the block is reachable-at-begin
937+
/// (2) it is either the function's entry block or one of the blocks passed
938+
/// to findBarriersBackward's \p initialBlocks parameter.
939+
llvm::SmallVector<SILBasicBlock *, 2> initialBlocks;
940+
925941
ReachableBarriers() {}
926942
ReachableBarriers(ReachableBarriers const &) = delete;
927943
ReachableBarriers &operator=(ReachableBarriers const &) = delete;

lib/SILOptimizer/Analysis/Reachability.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ class FindBarriersBackwardDataflow final {
7070
void visitBarrierBlock(SILBasicBlock *block) {
7171
barriers.edges.push_back(block);
7272
}
73+
74+
void visitInitialBlock(SILBasicBlock *block) {
75+
barriers.initialBlocks.push_back(block);
76+
}
7377
};
7478

7579
FindBarriersBackwardDataflow::Effect

lib/SILOptimizer/Transforms/DestroyAddrHoisting.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,10 @@ class DeinitBarriers final {
318318
result.barrierBlocks.insert(block);
319319
}
320320

321+
void visitInitialBlock(SILBasicBlock *block) {
322+
result.barrierBlocks.insert(block);
323+
}
324+
321325
/// VisitBarrierAccessScopes::Visitor
322326

323327
ArrayRef<SILBasicBlock *> roots();

lib/SILOptimizer/Utils/CanonicalizeOSSALifetime.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -287,16 +287,15 @@ void CanonicalizeOSSALifetime::extendLivenessToDeinitBarriers() {
287287
}
288288
}
289289
for (auto *edge : barriers.edges) {
290-
if (edge == getCurrentDef()->getParentBlock()) {
291-
// A destroy should be inserted at the begin of this block, but that does
292-
// not require adding any instructions to liveness.
293-
continue;
294-
}
295290
auto *predecessor = edge->getSinglePredecessorBlock();
296291
assert(predecessor);
297292
liveness->updateForUse(&predecessor->back(),
298293
/*lifetimeEnding*/ false);
299294
}
295+
// Ignore barriers.initialBlocks. If the collection is non-empty, it
296+
// contains the def-block. Its presence means that no barriers were found
297+
// between lifetime ends and def. In that case, no new instructions need to
298+
// be added to liveness.
300299
}
301300

302301
// Return true if \p inst is an end_access whose access scope overlaps the end

lib/SILOptimizer/Utils/LexicalDestroyHoisting.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,10 @@ class Dataflow final {
184184
void visitBarrierBlock(SILBasicBlock *block) {
185185
barriers.blocks.push_back(block);
186186
}
187+
188+
void visitInitialBlock(SILBasicBlock *block) {
189+
barriers.blocks.push_back(block);
190+
}
187191
};
188192

189193
Dataflow::Classification

lib/SILOptimizer/Utils/ShrinkBorrowScope.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,10 @@ class Dataflow final {
213213
void visitBarrierBlock(SILBasicBlock *block) {
214214
barriers.blocks.push_back(block);
215215
}
216+
217+
void visitInitialBlock(SILBasicBlock *block) {
218+
barriers.blocks.push_back(block);
219+
}
216220
};
217221

218222
/// Whether the specified value is %lifetime or its iterated copy_value.

0 commit comments

Comments
 (0)