Skip to content

Commit 9bfd7aa

Browse files
authored
Merge pull request swiftlang#35899 from meg-gupta/removerauwerror
Remove an llvm_unreachable in ownership rauw
2 parents 87a403e + 8807d44 commit 9bfd7aa

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

lib/SILOptimizer/Utils/OwnershipOptUtils.cpp

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -264,24 +264,16 @@ struct OwnershipLifetimeExtender {
264264
CopyValueInst *
265265
OwnershipLifetimeExtender::createPlusOneCopy(SILValue value,
266266
SILInstruction *consumingPoint) {
267-
auto *newValInsertPt = value->getDefiningInsertionPoint();
268-
assert(newValInsertPt);
269-
CopyValueInst *copy;
270-
if (!isa<SILArgument>(value)) {
271-
SILBuilderWithScope::insertAfter(newValInsertPt, [&](SILBuilder &builder) {
272-
copy = builder.createCopyValue(builder.getInsertionPointLoc(), value);
273-
});
274-
} else {
275-
SILBuilderWithScope builder(newValInsertPt);
276-
copy = builder.createCopyValue(newValInsertPt->getLoc(), value);
277-
}
267+
auto *copyPoint = value->getNextInstruction();
268+
auto loc = copyPoint->getLoc();
269+
auto *copy = SILBuilderWithScope(copyPoint).createCopyValue(loc, value);
278270

279271
auto &callbacks = ctx.callbacks;
280272
callbacks.createdNewInst(copy);
281273

282274
auto *result = copy;
283275
findJointPostDominatingSet(
284-
newValInsertPt->getParent(), consumingPoint->getParent(),
276+
copyPoint->getParent(), consumingPoint->getParent(),
285277
// inputBlocksFoundDuringWalk.
286278
[&](SILBasicBlock *loopBlock) {
287279
// This must be consumingPoint->getParent() since we only have one
@@ -291,10 +283,16 @@ OwnershipLifetimeExtender::createPlusOneCopy(SILValue value,
291283
assert(loopBlock == consumingPoint->getParent());
292284
auto front = loopBlock->begin();
293285
SILBuilderWithScope newBuilder(front);
286+
287+
// Create an extra copy when the consuming point is inside a
288+
// loop and both copyPoint and the destroy points are outside the
289+
// loop. This copy will be consumed in the same block. The original
290+
// value will be destroyed on all paths exiting the loop.
291+
//
292+
// Since copyPoint dominates consumingPoint, it must be outside the
293+
// loop. Otherwise backward traversal would have stopped at copyPoint.
294294
result = newBuilder.createCopyValue(front->getLoc(), copy);
295295
callbacks.createdNewInst(result);
296-
297-
llvm_unreachable("Should never visit this!");
298296
},
299297
// Input blocks in joint post dom set. We don't care about thse.
300298
[&](SILBasicBlock *postDomBlock) {

test/SILOptimizer/cse_ossa_nontrivial.sil

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ import Swift
77
///////////////
88
// CSE Tests //
99
///////////////
10-
class Klass {
11-
}
10+
class SuperKlass {}
11+
12+
class Klass : SuperKlass {}
1213

1314
struct NonTrivialStruct {
1415
var val:Klass
@@ -34,6 +35,7 @@ struct StructWithEnum2 {
3435

3536
sil @use_nontrivialstruct1 : $@convention(thin) (@guaranteed NonTrivialStruct) -> ()
3637
sil @use_nontrivialstruct2 : $@convention(thin) (@owned NonTrivialStruct) -> ()
38+
sil @use_superklass1 : $@convention(thin) (@owned SuperKlass) -> ()
3739

3840
// CHECK-LABEL: sil [ossa] @structliteral1 :
3941
// CHECK: struct $NonTrivialStruct
@@ -767,3 +769,30 @@ bb0(%0 : @guaranteed $WrapperKlass):
767769
return %res : $()
768770
}
769771

772+
// CHECK-LABEL: sil [ossa] @cse_upcast_loop :
773+
// CHECK: upcast
774+
// CHECK-NOT: upcast
775+
// CHECK-LABEL: } // end sil function 'cse_upcast_loop'
776+
sil [ossa] @cse_upcast_loop : $@convention(thin) (@owned Klass) -> () {
777+
bb0(%0 : @owned $Klass):
778+
%func = function_ref @use_superklass1 : $@convention(thin) (@owned SuperKlass) -> ()
779+
%copy0 = copy_value %0 : $Klass
780+
%1 = upcast %copy0 : $Klass to $SuperKlass
781+
apply %func(%1) : $@convention(thin) (@owned SuperKlass) -> ()
782+
br bb1
783+
784+
bb1:
785+
cond_br undef, bb2, bb3
786+
787+
bb2:
788+
%copy1 = copy_value %0 : $Klass
789+
%2 = upcast %copy1 : $Klass to $SuperKlass
790+
apply %func(%2) : $@convention(thin) (@owned SuperKlass) -> ()
791+
br bb1
792+
793+
bb3:
794+
destroy_value %0 : $Klass
795+
%res = tuple ()
796+
return %res : $()
797+
}
798+

0 commit comments

Comments
 (0)