Skip to content

Commit 1897f26

Browse files
Merge pull request #66345 from nate-chandler/cherrypick/release/5.9/rdar109894792
5.9: [Mem2Reg] Recognize store_borrows for debug_value.
2 parents 91d659b + ccb5983 commit 1897f26

File tree

2 files changed

+89
-2
lines changed

2 files changed

+89
-2
lines changed

lib/SILOptimizer/Transforms/SILMem2Reg.cpp

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

295+
/// Whether the specified debug_value's operand names the address at the
296+
/// indicated alloc_stack.
297+
///
298+
/// If it's a guaranteed alloc_stack (i.e. a store_borrow location), that
299+
/// includes the values produced by any store_borrows whose destinations are the
300+
/// alloc_stack since those values amount to aliases for the alloc_stack's
301+
/// storage.
302+
static bool isDebugValueOfAllocStack(DebugValueInst *dvi, AllocStackInst *asi) {
303+
auto value = dvi->getOperand();
304+
if (value == asi)
305+
return true;
306+
auto *sbi = dyn_cast<StoreBorrowInst>(value);
307+
if (!sbi)
308+
return false;
309+
return sbi->getDest() == asi;
310+
}
311+
295312
/// Promote a DebugValue w/ address value to a DebugValue of non-address value.
296313
static void promoteDebugValueAddr(DebugValueInst *dvai, SILValue value,
297314
SILBuilderContext &ctx,
@@ -979,7 +996,7 @@ SILInstruction *StackAllocationPromoter::promoteAllocationInBlock(
979996
// if we have a valid value to use at this point. Otherwise we'll
980997
// promote this when we deal with hooking up phis.
981998
if (auto *dvi = DebugValueInst::hasAddrVal(inst)) {
982-
if (dvi->getOperand() == asi && runningVals)
999+
if (isDebugValueOfAllocStack(dvi, asi) && runningVals)
9831000
promoteDebugValueAddr(dvi, runningVals->value.replacement(asi, dvi),
9841001
ctx, deleter);
9851002
continue;
@@ -1983,7 +2000,7 @@ void MemoryToRegisters::removeSingleBlockAllocation(AllocStackInst *asi) {
19832000
// Replace debug_value w/ address value with debug_value of
19842001
// the promoted value.
19852002
if (auto *dvi = DebugValueInst::hasAddrVal(inst)) {
1986-
if (dvi->getOperand() == asi) {
2003+
if (isDebugValueOfAllocStack(dvi, asi)) {
19872004
if (runningVals) {
19882005
promoteDebugValueAddr(dvi, runningVals->value.replacement(asi, dvi),
19892006
ctx, deleter);

test/SILOptimizer/mem2reg_ossa.sil

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,12 @@ struct LargeCodesizeStruct {
2222
var s5: SmallCodesizeStruct
2323
}
2424

25+
class C {}
26+
27+
struct S {
28+
var field: C
29+
}
30+
2531
///////////
2632
// Tests //
2733
///////////
@@ -538,3 +544,67 @@ bb7:
538544
return %r : $()
539545
}
540546

547+
// CHECK-LABEL: sil [ossa] @debug_value_of_store_borrow_addr_multi_block : {{.*}} {
548+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
549+
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[INSTANCE]]
550+
// CHECK: debug_value [[LIFETIME]]
551+
// CHECK-LABEL: } // end sil function 'debug_value_of_store_borrow_addr_multi_block'
552+
sil [ossa] @debug_value_of_store_borrow_addr_multi_block : $@convention(thin) (@owned S) -> () {
553+
entry(%instance : @owned $S):
554+
br header
555+
556+
header:
557+
%lifetime = begin_borrow %instance : $S
558+
%stack = alloc_stack $S
559+
%stack_borrow = store_borrow %lifetime to %stack : $*S
560+
debug_value %stack_borrow : $*S
561+
br body
562+
563+
body:
564+
%field_addr = struct_element_addr %stack_borrow : $*S, #S.field
565+
%field = load [copy] %field_addr : $*C
566+
end_borrow %stack_borrow : $*S
567+
dealloc_stack %stack : $*S
568+
end_borrow %lifetime : $S
569+
destroy_value %field : $C
570+
cond_br undef, backedge, exit
571+
572+
backedge:
573+
br header
574+
575+
exit:
576+
destroy_value %instance : $S
577+
%retval = tuple ()
578+
return %retval : $()
579+
}
580+
581+
// CHECK-LABEL: sil [ossa] @debug_value_of_store_borrow_addr_single_block : {{.*}} {
582+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
583+
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[INSTANCE]]
584+
// CHECK: debug_value [[LIFETIME]]
585+
// CHECK-LABEL: } // end sil function 'debug_value_of_store_borrow_addr_single_block'
586+
sil [ossa] @debug_value_of_store_borrow_addr_single_block : $@convention(thin) (@owned S) -> () {
587+
entry(%instance : @owned $S):
588+
br header
589+
590+
header:
591+
%lifetime = begin_borrow %instance : $S
592+
%stack = alloc_stack $S
593+
%stack_borrow = store_borrow %lifetime to %stack : $*S
594+
debug_value %stack_borrow : $*S
595+
%field_addr = struct_element_addr %stack_borrow : $*S, #S.field
596+
%field = load [copy] %field_addr : $*C
597+
end_borrow %stack_borrow : $*S
598+
dealloc_stack %stack : $*S
599+
end_borrow %lifetime : $S
600+
destroy_value %field : $C
601+
cond_br undef, backedge, exit
602+
603+
backedge:
604+
br header
605+
606+
exit:
607+
destroy_value %instance : $S
608+
%retval = tuple ()
609+
return %retval : $()
610+
}

0 commit comments

Comments
 (0)