Skip to content

Commit dfe2f43

Browse files
authored
Merge pull request #5390 from eeckstein/fix-simplify-cfg
SIL: fix a bug which can cause the stack nesting to get broken by SimplifyCFG's jump threading
2 parents 6bfba48 + 49f9058 commit dfe2f43

File tree

3 files changed

+46
-2
lines changed

3 files changed

+46
-2
lines changed

lib/SIL/LoopInfo.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,6 @@ bool SILLoop::canDuplicate(SILInstruction *I) const {
7676
return false;
7777
}
7878

79-
assert(I->isTriviallyDuplicatable() &&
80-
"Code here must match isTriviallyDuplicatable in SILInstruction");
8179
return true;
8280
}
8381

lib/SIL/SILInstruction.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,10 @@ bool SILInstruction::isTriviallyDuplicatable() const {
983983
if (isa<AllocStackInst>(this) || isa<DeallocStackInst>(this)) {
984984
return false;
985985
}
986+
if (auto *ARI = dyn_cast<AllocRefInst>(this)) {
987+
if (ARI->canAllocOnStack())
988+
return false;
989+
}
986990

987991
if (isa<OpenExistentialAddrInst>(this) ||
988992
isa<OpenExistentialRefInst>(this) ||

test/SILOptimizer/simplify_cfg.sil

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1246,6 +1246,48 @@ bb7:
12461246
br bb1
12471247
}
12481248

1249+
sil @unknown2 : $@convention(thin) () -> ()
1250+
1251+
// CHECK-LABEL: no_checked_cast_br_threading_with_alloc_ref_stack
1252+
// CHECK: checked_cast_br
1253+
// CHECK: apply
1254+
// CHECK: apply
1255+
// CHECK: checked_cast_br
1256+
// CHECK: apply
1257+
// CHECK: apply
1258+
// CHECK: return
1259+
sil @no_checked_cast_br_threading_with_alloc_ref_stack : $@convention(method) (@guaranteed Base) -> () {
1260+
bb0(%0 : $Base):
1261+
%fu = function_ref @unknown : $@convention(thin) () -> ()
1262+
%fu2 = function_ref @unknown2 : $@convention(thin) () -> ()
1263+
checked_cast_br [exact] %0 : $Base to $Base, bb1, bb2
1264+
1265+
bb1(%1 : $Base):
1266+
apply %fu() : $@convention(thin) () -> ()
1267+
br bb3
1268+
1269+
bb2:
1270+
apply %fu2() : $@convention(thin) () -> ()
1271+
br bb3
1272+
1273+
bb3:
1274+
%a = alloc_ref [stack] $Base
1275+
checked_cast_br [exact] %0 : $Base to $Base, bb4, bb5
1276+
1277+
bb4(%2 : $Base):
1278+
apply %fu() : $@convention(thin) () -> ()
1279+
br bb6
1280+
1281+
bb5:
1282+
apply %fu2() : $@convention(thin) () -> ()
1283+
br bb6
1284+
1285+
bb6:
1286+
dealloc_ref [stack] %a : $Base
1287+
%r = tuple()
1288+
return %r : $()
1289+
}
1290+
12491291

12501292
// CHECK-LABEL: sil @jumpthread_switch_enum
12511293
// CHECK-NOT: switch_enum

0 commit comments

Comments
 (0)