Skip to content

Commit ee360e0

Browse files
authored
Merge pull request swiftlang#79484 from eeckstein/fix-simplify-cfg
SimplifyCFG: remove all borrowed-from uses from arguments when merging blocks
2 parents 3aae44d + 5ef3b96 commit ee360e0

File tree

2 files changed

+52
-2
lines changed

2 files changed

+52
-2
lines changed

lib/SILOptimizer/Transforms/SimplifyCFG.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1293,9 +1293,9 @@ bool SimplifyCFG::simplifyBranchBlock(BranchInst *BI) {
12931293
if (auto *bfi = getBorrowedFromUser(arg)) {
12941294
bfi->replaceAllUsesWith(Val);
12951295
bfi->eraseFromParent();
1296-
} else {
1297-
arg->replaceAllUsesWith(Val);
12981296
}
1297+
arg->replaceAllUsesWith(Val);
1298+
12991299
if (!isVeryLargeFunction) {
13001300
if (auto *I = dyn_cast<SingleValueInstruction>(Val)) {
13011301
// Replacing operands may trigger constant folding which then could

test/SILOptimizer/simplify_cfg_ossa.sil

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ struct NonTrivial {
5959
var object: AnyObject
6060
}
6161

62+
struct S {
63+
var a: Int
64+
var b: NonTrivial
65+
}
66+
6267
///////////
6368
// Tests //
6469
///////////
@@ -1971,3 +1976,48 @@ bb6(%15: @guaranteed $E, %16 : @reborrow $B):
19711976
%r = tuple()
19721977
return %r
19731978
}
1979+
1980+
// CHECK-LABEL: sil [ossa] @dont_crash_when_merging_blocks_with_borrowed_from :
1981+
// CHECK: } // end sil function 'dont_crash_when_merging_blocks_with_borrowed_from'
1982+
sil [ossa] @dont_crash_when_merging_blocks_with_borrowed_from : $@convention(thin) (@owned Optional<S>) -> () {
1983+
bb0(%0 : @owned $Optional<S>):
1984+
cond_br undef, bb1, bb2
1985+
1986+
bb1:
1987+
%2 = integer_literal $Builtin.Int1, -1
1988+
br bb3(%2)
1989+
1990+
bb2:
1991+
%4 = integer_literal $Builtin.Int1, 0
1992+
br bb3(%4)
1993+
1994+
bb3(%6 : $Builtin.Int1):
1995+
%7 = begin_borrow %0
1996+
cond_br %6, bb4, bb5
1997+
1998+
bb4:
1999+
switch_enum %7, case #Optional.some!enumelt: bb6, case #Optional.none!enumelt: bb7
2000+
2001+
bb5:
2002+
br bb9
2003+
2004+
bb6(%11 : @guaranteed $S):
2005+
(%12, %13) = destructure_struct %11
2006+
%14 = enum $Optional<NonTrivial>, #Optional.some!enumelt, %13
2007+
br bb8(%14)
2008+
2009+
bb7:
2010+
%16 = enum $Optional<NonTrivial>, #Optional.none!enumelt
2011+
br bb8(%16)
2012+
2013+
bb8(%18 : @guaranteed $Optional<NonTrivial>):
2014+
%19 = borrowed %18 from (%7)
2015+
br bb9
2016+
2017+
bb9:
2018+
end_borrow %7
2019+
destroy_value %0
2020+
%23 = tuple ()
2021+
return %23
2022+
}
2023+

0 commit comments

Comments
 (0)