Skip to content

Commit 8b1e257

Browse files
authored
Merge pull request swiftlang#76277 from eeckstein/fix-borrowed-from-updating
SIL: fix `Phi.init` for arguments in unreachable blocks
2 parents 5490c50 + d4221f4 commit 8b1e257

File tree

2 files changed

+34
-4
lines changed

2 files changed

+34
-4
lines changed

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,22 @@ public struct Phi {
8383
// is only included here for compatibility with .sil tests that have
8484
// not been migrated.
8585
public init?(_ value: Value) {
86-
guard let argument = value as? Argument else { return nil }
86+
guard let argument = value as? Argument else {
87+
return nil
88+
}
8789
var preds = argument.parentBlock.predecessors
88-
guard let pred = preds.next() else { return nil }
89-
let term = pred.terminator
90-
guard term is BranchInst || term is CondBranchInst else { return nil }
90+
if let pred = preds.next() {
91+
let term = pred.terminator
92+
guard term is BranchInst || term is CondBranchInst else {
93+
return nil
94+
}
95+
} else {
96+
// No predecessors indicates an unreachable block (except for function arguments).
97+
// Treat this like a degenerate phi so we don't consider it a terminator result.
98+
if argument is FunctionArgument {
99+
return nil
100+
}
101+
}
91102
self.value = argument
92103
}
93104

test/SILOptimizer/borrowed_from_updater.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -286,3 +286,22 @@ bb2(%8 : @reborrow @guaranteed $PairC, %9 : @guaranteed $C):
286286
return %99 : $()
287287
}
288288

289+
// CHECK-LABEL: sil [ossa] @unreachable_block :
290+
// CHECK: bb1([[A1:%.*]] : @reborrow @guaranteed $C):
291+
// CHECK-NEXT: = borrowed [[A1]] : $C from ()
292+
// CHECK: bb2([[A2:%.*]] : @reborrow @guaranteed $C):
293+
// CHECK-NEXT: = borrowed [[A2]] : $C from (%0 : $C)
294+
// CHECK: } // end sil function 'unreachable_block'
295+
sil [ossa] @unreachable_block : $@convention(thin) (@owned C) -> @owned C {
296+
bb0(%0 : @owned $C):
297+
%1 = begin_borrow %0 : $C
298+
br bb2(%1 : $C)
299+
300+
bb1(%3 : @guaranteed $C):
301+
br bb2(%3 : $C)
302+
303+
bb2(%5 : @guaranteed $C):
304+
end_borrow %5 : $C
305+
return %0 : $C
306+
}
307+

0 commit comments

Comments
 (0)