Skip to content

[region-isolation] Take into account that Swift's RPO order doesn't include blocks that are dead. #72077

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
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
8 changes: 8 additions & 0 deletions include/swift/SILOptimizer/Analysis/RegionAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,12 @@ class BlockPartitionState {
/// Set if this block in the next iteration needs to be visited.
bool needsUpdate = false;

/// Set if this block is live.
///
/// If the block is not live, then we shouldnt try to emit diagnostics for it
/// since we will not have performed dataflow upon it.
bool isLive = false;

/// The partition of elements into regions at the top of the block.
Partition entryPartition;

Expand All @@ -81,6 +87,8 @@ class BlockPartitionState {
TransferringOperandSetFactory &ptrSetFactory);

public:
bool getLiveness() const { return isLive; }

ArrayRef<PartitionOp> getPartitionOps() const { return blockPartitionOps; }

const Partition &getEntryPartition() const { return entryPartition; }
Expand Down
1 change: 1 addition & 0 deletions lib/SILOptimizer/Analysis/RegionAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3057,6 +3057,7 @@ void RegionAnalysisFunctionInfo::runDataflow() {

for (auto *block : pofi->getReversePostOrder()) {
auto &blockState = (*blockStates)[block];
blockState.isLive = true;

LLVM_DEBUG(llvm::dbgs() << "Block: bb" << block->getDebugID() << "\n");
if (!blockState.needsUpdate) {
Expand Down
6 changes: 6 additions & 0 deletions lib/SILOptimizer/Mandatory/TransferNonSendable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1324,6 +1324,12 @@ void TransferNonSendableImpl::runDiagnosticEvaluator() {
LLVM_DEBUG(llvm::dbgs() << "Walking blocks for diagnostics.\n");
for (auto [block, blockState] : regionInfo->getRange()) {
LLVM_DEBUG(llvm::dbgs() << "|--> Block bb" << block.getDebugID() << "\n");

if (!blockState.getLiveness()) {
LLVM_DEBUG(llvm::dbgs() << "Dead block... skipping!\n");
continue;
}

LLVM_DEBUG(llvm::dbgs() << "Entry Partition: ";
blockState.getEntryPartition().print(llvm::dbgs()));

Expand Down
26 changes: 25 additions & 1 deletion test/Concurrency/transfernonsendable.sil
Original file line number Diff line number Diff line change
Expand Up @@ -174,4 +174,28 @@ bb0:
dealloc_stack %2 : $*NonSendableKlass
%19 = tuple ()
return %19 : $()
}
}

// Dead block crasher
//
// We used to crash here since we attempted to process /all/ blocks for
// diagnostic even for non-dead blocks. This is a problem since the dataflow
// uses a reverse post order traversal to get quick convergence and that skips
// dead blocks.
sil [ossa] @dead_block_crasher : $@convention(thin) @async () -> () {
bb0:
%0 = function_ref @constructNonSendableKlass : $@convention(thin) () -> @owned NonSendableKlass
%1 = apply %0() : $@convention(thin) () -> @owned NonSendableKlass
br bb1

bbDeadBlock:
%transfer = function_ref @transferNonSendableKlass : $@convention(thin) @async (@guaranteed NonSendableKlass) -> ()
apply [caller_isolation=nonisolated] [callee_isolation=global_actor] %transfer(%1) : $@convention(thin) @async (@guaranteed NonSendableKlass) -> ()
br bb1

bb1:
destroy_value %1 : $NonSendableKlass
%9999 = tuple ()
return %9999 : $()
}