Skip to content

Commit cf6c177

Browse files
authored
Fix mem2reg assert to allow phi arguments (#59796)
rdar://96171577
1 parent 1ff1826 commit cf6c177

File tree

2 files changed

+31
-1
lines changed

2 files changed

+31
-1
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ replaceLoad(SILInstruction *inst, SILValue newValue, AllocStackInst *asi,
285285

286286
if (auto *lbi = dyn_cast<LoadBorrowInst>(inst)) {
287287
if (shouldAddLexicalLifetime(asi)) {
288-
assert(isa<BeginBorrowInst>(newValue));
288+
assert(isa<BeginBorrowInst>(newValue) || isa<SILPhiArgument>(newValue));
289289
SmallVector<SILInstruction *, 4> endBorrows;
290290
for (auto *ebi : lbi->getUsersOfType<EndBorrowInst>()) {
291291
endBorrows.push_back(ebi);

test/SILOptimizer/mem2reg_borrows.sil

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -416,3 +416,33 @@ bb3:
416416
%r = tuple ()
417417
return %r : $()
418418
}
419+
420+
// CHECK-LABEL: sil [ossa] @test_control_flow7 :
421+
// CHECK-NOT: alloc_stack
422+
// CHECK-LABEL: } // end sil function 'test_control_flow7'
423+
sil [ossa] @test_control_flow7 : $@convention(thin) (@guaranteed Klass, @guaranteed Klass) -> () {
424+
bb0(%0 : @guaranteed $Klass, %1 : @guaranteed $Klass):
425+
%stk = alloc_stack [lexical] $Klass
426+
cond_br undef, bb1, bb2
427+
428+
bb1:
429+
%copy1 = copy_value %0 : $Klass
430+
store %copy1 to [init] %stk : $*Klass
431+
br bb3
432+
433+
bb2:
434+
%copy2 = copy_value %1 : $Klass
435+
store %copy2 to [init] %stk : $*Klass
436+
br bb3
437+
438+
bb3:
439+
%2 = load_borrow %stk : $*Klass
440+
%3 = function_ref @use_guaranteed : $@convention(thin) (@guaranteed Klass) -> ()
441+
%4 = apply %3(%2) : $@convention(thin) (@guaranteed Klass) -> ()
442+
end_borrow %2 : $Klass
443+
destroy_addr %stk : $*Klass
444+
dealloc_stack %stk : $*Klass
445+
%8 = tuple ()
446+
return %8 : $()
447+
}
448+

0 commit comments

Comments
 (0)