Skip to content

Commit 0f8c2fb

Browse files
committed
TempRValueOpt: fix a compile time crash in tryOptimizeStoreIntoTemp
Bail if there is any kind of user which is not handled in this transformation.
1 parent 4cd68ed commit 0f8c2fb

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/SILOptimizer/Transforms/TempRValueElimination.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,16 @@ TempRValueOptPass::tryOptimizeStoreIntoTemp(StoreInst *si) {
628628
// We pass in SILValue() since we do not have a source address.
629629
if (!collectLoads(useOper, user, tempObj, SILValue(), loadInsts))
630630
return {std::next(si->getIterator()), false};
631+
632+
// Bail if there is any kind of user which is not handled in the code below.
633+
switch (user->getKind()) {
634+
case SILInstructionKind::CopyAddrInst:
635+
case SILInstructionKind::FixLifetimeInst:
636+
case SILInstructionKind::MarkDependenceInst:
637+
continue;
638+
default:
639+
return {std::next(si->getIterator()), false};
640+
}
631641
}
632642

633643
// Since store is always a consuming operation, we do not need to worry about

test/SILOptimizer/temp_rvalue_opt_ossa.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,3 +1159,22 @@ bb0(%0 : $*Klass):
11591159
%9999 = tuple()
11601160
return %9999 : $()
11611161
}
1162+
1163+
// Check that we don't crash with this.
1164+
// CHECK-LABEL: sil [ossa] @unhandled_user : $@convention(thin) (@owned Klass) -> @owned Klass {
1165+
// CHECK: alloc_stack
1166+
// CHECK: store
1167+
// CHECK: begin_access
1168+
// CHECK: load
1169+
// CHECK: } // end sil function 'unhandled_user'
1170+
sil [ossa] @unhandled_user : $@convention(thin) (@owned Klass) -> @owned Klass {
1171+
bb0(%0 : @owned $Klass):
1172+
%5 = alloc_stack $Klass
1173+
store %0 to [init] %5 : $*Klass
1174+
%104 = begin_access [read] [static] %5 : $*Klass
1175+
%105 = load [take] %104 : $*Klass
1176+
end_access %104 : $*Klass
1177+
dealloc_stack %5 : $*Klass
1178+
return %105 : $Klass
1179+
}
1180+

0 commit comments

Comments
 (0)