Skip to content

Commit 481a561

Browse files
authored
Merge pull request #60820 from meg-gupta/fixmem2regsbi
Fix SILMem2Reg while marking end_borrow of store_borrow as deinit point
2 parents 98e5025 + e0f8711 commit 481a561

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -795,14 +795,15 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
795795
continue;
796796
}
797797
if (!runningVals.hasValue()) {
798-
assert(!deinitializationPoints[blockPromotingWithin]);
799-
deinitializationPoints[blockPromotingWithin] = inst;
800798
continue;
801799
}
802800
if (sbi->getSrc() != runningVals->value.stored) {
803801
continue;
804802
}
803+
// Mark storage as invalid and mark end_borrow as a deinit point.
805804
runningVals->isStorageValid = false;
805+
assert(!deinitializationPoints[blockPromotingWithin]);
806+
deinitializationPoints[blockPromotingWithin] = inst;
806807
if (!canEndLexicalLifetime(runningVals->value)) {
807808
continue;
808809
}

test/SILOptimizer/mem2reg_lifetime_borrows.sil

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -452,3 +452,35 @@ bb0:
452452
%r = tuple ()
453453
return %r : $()
454454
}
455+
456+
// CHECK-LABEL: sil [ossa] @test_deinit_point :
457+
// CHECK: alloc_stack
458+
// CHECK-NOT: alloc_stack [lexical]
459+
// CHECK-LABEL: } // end sil function 'test_deinit_point'
460+
sil [ossa] @test_deinit_point : $@convention(thin) (@guaranteed Klass) -> () {
461+
bb0(%0 : @guaranteed $Klass):
462+
%stk = alloc_stack [lexical] $Klass
463+
cond_br undef, bb1, bb2
464+
465+
bb1:
466+
%tmp = alloc_stack $Klass
467+
%sb = store_borrow %0 to %tmp : $*Klass
468+
%f = function_ref @use_inguaranteed : $@convention(thin) (@in_guaranteed Klass) -> ()
469+
%c = apply %f(%sb) : $@convention(thin) (@in_guaranteed Klass) -> ()
470+
end_borrow %sb : $*Klass
471+
dealloc_stack %tmp : $*Klass
472+
%copy = copy_value %0 : $Klass
473+
store %copy to [init] %stk : $*Klass
474+
%l = load [take] %stk : $*Klass
475+
destroy_value %l : $Klass
476+
br bb3
477+
478+
bb2:
479+
br bb3
480+
481+
bb3:
482+
dealloc_stack %stk : $*Klass
483+
%r = tuple ()
484+
return %r : $()
485+
}
486+

0 commit comments

Comments
 (0)