Skip to content

Commit f0f0560

Browse files
Merge pull request swiftlang#66303 from nate-chandler/rdar109894792
[Mem2Reg] Recognize store_borrows for debug_value.
2 parents a61fa5a + d928def commit f0f0560

File tree

2 files changed

+90
-2
lines changed

2 files changed

+90
-2
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -300,6 +300,23 @@ replaceDestroy(DestroyAddrInst *dai, SILValue newValue, SILBuilderContext &ctx,
300300
prepareForDeletion(dai, instructionsToDelete);
301301
}
302302

303+
/// Whether the specified debug_value's operand names the address at the
304+
/// indicated alloc_stack.
305+
///
306+
/// If it's a guaranteed alloc_stack (i.e. a store_borrow location), that
307+
/// includes the values produced by any store_borrows whose destinations are the
308+
/// alloc_stack since those values amount to aliases for the alloc_stack's
309+
/// storage.
310+
static bool isDebugValueOfAllocStack(DebugValueInst *dvi, AllocStackInst *asi) {
311+
auto value = dvi->getOperand();
312+
if (value == asi)
313+
return true;
314+
auto *sbi = dyn_cast<StoreBorrowInst>(value);
315+
if (!sbi)
316+
return false;
317+
return sbi->getDest() == asi;
318+
}
319+
303320
/// Promote a DebugValue w/ address value to a DebugValue of non-address value.
304321
static void promoteDebugValueAddr(DebugValueInst *dvai, SILValue value,
305322
SILBuilderContext &ctx,
@@ -1191,7 +1208,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
11911208
// if we have a valid value to use at this point. Otherwise we'll
11921209
// promote this when we deal with hooking up phis.
11931210
if (auto *dvi = DebugValueInst::hasAddrVal(inst)) {
1194-
if (dvi->getOperand() == asi && runningVals)
1211+
if (isDebugValueOfAllocStack(dvi, asi) && runningVals)
11951212
promoteDebugValueAddr(dvi, runningVals->value.replacement(asi, dvi),
11961213
ctx, deleter);
11971214
continue;
@@ -2034,7 +2051,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
20342051
// Replace debug_value w/ address value with debug_value of
20352052
// the promoted value.
20362053
if (auto *dvi = DebugValueInst::hasAddrVal(inst)) {
2037-
if (dvi->getOperand() == asi) {
2054+
if (isDebugValueOfAllocStack(dvi, asi)) {
20382055
if (runningVals) {
20392056
promoteDebugValueAddr(dvi, runningVals->value.replacement(asi, dvi),
20402057
ctx, deleter);

test/SILOptimizer/mem2reg_ossa.sil

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,12 @@ struct LargeCodesizeStruct {
4040
var s5: SmallCodesizeStruct
4141
}
4242

43+
class C {}
44+
45+
struct S {
46+
var field: C
47+
}
48+
4349
///////////
4450
// Tests //
4551
///////////
@@ -657,3 +663,68 @@ bb16:
657663
%retval = tuple ()
658664
return %retval : $()
659665
}
666+
667+
// CHECK-LABEL: sil [ossa] @debug_value_of_store_borrow_addr_multi_block : {{.*}} {
668+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
669+
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[INSTANCE]]
670+
// CHECK: debug_value [[LIFETIME]]
671+
// CHECK-LABEL: } // end sil function 'debug_value_of_store_borrow_addr_multi_block'
672+
sil [ossa] @debug_value_of_store_borrow_addr_multi_block : $@convention(thin) (@owned S) -> () {
673+
entry(%instance : @owned $S):
674+
br header
675+
676+
header:
677+
%lifetime = begin_borrow %instance : $S
678+
%stack = alloc_stack $S
679+
%stack_borrow = store_borrow %lifetime to %stack : $*S
680+
debug_value %stack_borrow : $*S
681+
br body
682+
683+
body:
684+
%field_addr = struct_element_addr %stack_borrow : $*S, #S.field
685+
%field = load [copy] %field_addr : $*C
686+
end_borrow %stack_borrow : $*S
687+
dealloc_stack %stack : $*S
688+
end_borrow %lifetime : $S
689+
destroy_value %field : $C
690+
cond_br undef, backedge, exit
691+
692+
backedge:
693+
br header
694+
695+
exit:
696+
destroy_value %instance : $S
697+
%retval = tuple ()
698+
return %retval : $()
699+
}
700+
701+
// CHECK-LABEL: sil [ossa] @debug_value_of_store_borrow_addr_single_block : {{.*}} {
702+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
703+
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[INSTANCE]]
704+
// CHECK: debug_value [[LIFETIME]]
705+
// CHECK-LABEL: } // end sil function 'debug_value_of_store_borrow_addr_single_block'
706+
sil [ossa] @debug_value_of_store_borrow_addr_single_block : $@convention(thin) (@owned S) -> () {
707+
entry(%instance : @owned $S):
708+
br header
709+
710+
header:
711+
%lifetime = begin_borrow %instance : $S
712+
%stack = alloc_stack $S
713+
%stack_borrow = store_borrow %lifetime to %stack : $*S
714+
debug_value %stack_borrow : $*S
715+
%field_addr = struct_element_addr %stack_borrow : $*S, #S.field
716+
%field = load [copy] %field_addr : $*C
717+
end_borrow %stack_borrow : $*S
718+
dealloc_stack %stack : $*S
719+
end_borrow %lifetime : $S
720+
destroy_value %field : $C
721+
cond_br undef, backedge, exit
722+
723+
backedge:
724+
br header
725+
726+
exit:
727+
destroy_value %instance : $S
728+
%retval = tuple ()
729+
return %retval : $()
730+
}

0 commit comments

Comments
 (0)