Skip to content

[PrunedLiveness] Addressed boundary TODO. #61567

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 11 additions & 9 deletions lib/SIL/Utils/PrunedLiveness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,28 +229,30 @@ void PrunedLivenessBoundary::dump() const {
print(llvm::dbgs());
}

// TODO: with guaranteed phis, it will be possible to hit this assert:
// assert(succ->getSinglePredecessorBlock() == predBB);
//
// Once it's possible to test dead guaranteed phis, replace the assert with a
// set to avoid multiple insertions at a merge point:
// // Control flow merge blocks used as insertion points.
// BasicBlockSet mergeBlocks;
//
void PrunedLivenessBoundary::visitInsertionPoints(
llvm::function_ref<void(SILBasicBlock::iterator insertPt)> visitor,
DeadEndBlocks *deBlocks) {
// Control flow merge blocks used as insertion points.
SmallPtrSet<SILBasicBlock *, 4> mergeBlocks;

for (SILInstruction *user : lastUsers) {
if (!isa<TermInst>(user)) {
visitor(std::next(user->getIterator()));
continue;
}
auto *predBB = user->getParent();
for (SILBasicBlock *succ : predBB->getSuccessors()) {
if (!succ->getSinglePredecessorBlock()) {
assert(predBB->getSingleSuccessorBlock() == succ);
if (!mergeBlocks.insert(succ).second) {
continue;
}
} else {
assert(succ->getSinglePredecessorBlock() == predBB);
}
if (deBlocks && deBlocks->isDeadEnd(succ))
continue;

assert(succ->getSinglePredecessorBlock() == predBB);
visitor(succ->begin());
}
}
Expand Down
22 changes: 22 additions & 0 deletions lib/SILOptimizer/UtilityPasses/UnitTestRunner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@

#include "swift/AST/Type.h"
#include "swift/Basic/TaggedUnion.h"
#include "swift/SIL/PrunedLiveness.h"
#include "swift/SIL/SILArgumentArrayRef.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILBridging.h"
Expand Down Expand Up @@ -217,6 +218,24 @@ struct VisitAdjacentReborrowsOfPhiTest : UnitTest {
}
};

// Arguments:
// - variadic list of - instruction: a last user
// Dumps:
// - the insertion points
struct PrunedLivenessBoundaryWithListOfLastUsersInsertionPointsTest : UnitTest {
PrunedLivenessBoundaryWithListOfLastUsersInsertionPointsTest(
UnitTestRunner *pass)
: UnitTest(pass) {}
void invoke(Arguments &arguments) override {
PrunedLivenessBoundary boundary;
while (arguments.hasUntaken()) {
boundary.lastUsers.push_back(arguments.takeInstruction());
}
boundary.visitInsertionPoints(
[](SILBasicBlock::iterator point) { point->dump(); });
}
};

// Arguments: NONE
// Dumps:
// - the function
Expand Down Expand Up @@ -273,6 +292,9 @@ class UnitTestRunner : public SILFunctionTransform {
VisitAdjacentReborrowsOfPhiTest)
ADD_UNIT_TEST_SUBCLASS("function-get-self-argument-index",
FunctionGetSelfArgumentIndex)
ADD_UNIT_TEST_SUBCLASS(
"pruned-liveness-boundary-with-list-of-last-users-insertion-points",
PrunedLivenessBoundaryWithListOfLastUsersInsertionPointsTest)
/// [new_tests] Add the new mapping from string to subclass above this line.

#undef ADD_UNIT_TEST_SUBCLASS
Expand Down
22 changes: 22 additions & 0 deletions test/SILOptimizer/pruned_liveness_boundary.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %target-sil-opt -unit-test-runner %s 2>&1 | %FileCheck %s

// CHECK: begin running test 1 of {{[^,]+}} on last_uses_merge_points: dump-function
// CHECK: [[REGISTER_3:%[^,]+]] = tuple ()
// CHECK: return [[REGISTER_3]]
// CHECK: end running test 1 of {{[^,]+}} on last_uses_merge_points: dump-function
// CHECK: begin running test 2 of {{[^,]+}} on last_uses_merge_points: pruned-liveness-boundary-with-list-of-last-users-insertion-points
// CHECK: [[REGISTER_3]] = tuple ()
// CHECK: end running test 2 of {{[^,]+}} on last_uses_merge_points: pruned-liveness-boundary-with-list-of-last-users-insertion-points
sil [ossa] @last_uses_merge_points : $@convention(thin) () -> () {
entry:
test_specification "dump-function"
test_specification "pruned-liveness-boundary-with-list-of-last-users-insertion-points @block[1].instruction[0] @block[2].instruction[0]"
cond_br undef, left, right
left:
br bottom
right:
br bottom
bottom:
%retval = tuple ()
return %retval : $()
}