Skip to content

Commit c4ebdeb

Browse files
Merge pull request #61945 from nate-chandler/opaque-values/3/20221103
[AddressLowering] Some small fixes.
2 parents b9391c0 + 81d37be commit c4ebdeb

File tree

3 files changed

+85
-5
lines changed

3 files changed

+85
-5
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ void OpaqueStorageAllocation::allocatePhi(PhiValue phi) {
11591159
coalescedPhi.coalesce(phi, pass.valueStorageMap);
11601160

11611161
SmallVector<SILValue, 4> coalescedValues;
1162-
coalescedValues.resize(coalescedPhi.getCoalescedOperands().size());
1162+
coalescedValues.reserve(coalescedPhi.getCoalescedOperands().size());
11631163
for (SILValue value : coalescedPhi.getCoalescedValues())
11641164
coalescedValues.push_back(value);
11651165

@@ -3362,8 +3362,21 @@ static void rewriteFunction(AddressLoweringState &pass) {
33623362
if (valueDef->getType().isAddress())
33633363
continue;
33643364

3365+
SmallPtrSet<Operand *, 8> originalUses;
33653366
SmallVector<Operand *, 8> uses(valueDef->getUses());
3366-
for (Operand *oper : uses) {
3367+
for (auto *oper : uses) {
3368+
originalUses.insert(oper);
3369+
UseRewriter::rewriteUse(oper, pass);
3370+
}
3371+
// Rewrite every new uses that was added.
3372+
uses.clear();
3373+
for (auto *use : valueDef->getUses()) {
3374+
if (originalUses.contains(use))
3375+
continue;
3376+
uses.push_back(use);
3377+
}
3378+
for (auto *oper : uses) {
3379+
assert(isa<DebugValueInst>(oper->getUser()));
33673380
UseRewriter::rewriteUse(oper, pass);
33683381
}
33693382
}
@@ -3396,7 +3409,7 @@ static void filterDeadArgs(OperandValueArrayRef origArgs,
33963409
SmallVectorImpl<SILValue> &newArgs) {
33973410
auto nextDeadArgI = deadArgIndices.begin();
33983411
for (unsigned i : indices(origArgs)) {
3399-
if (i == *nextDeadArgI) {
3412+
if (i == *nextDeadArgI && nextDeadArgI != deadArgIndices.end()) {
34003413
++nextDeadArgI;
34013414
continue;
34023415
}

test/SILOptimizer/address_lowering.sil

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -941,15 +941,16 @@ bb0(%0 : @owned $P):
941941
// CHECK: bb0(%0 : $*any P):
942942
// CHECK: [[ALLOCP:%.*]] = alloc_stack $any P, var, name "q"
943943
// CHECK: copy_addr %0 to [init] [[ALLOCP]] : $*any P
944+
// CHECK: debug_value %0 : $*any P, var, name "q"
944945
// CHECK: [[OPEN:%.*]] = open_existential_addr immutable_access %0 : $*any P to $*@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self
945946
// CHECK: [[OPTIONAL:%.*]] = alloc_stack $Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>
946947
// CHECK: witness_method $@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self, #P.foo : <Self where Self : P> (Self) -> () -> (), [[OPEN]] : $*@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
947948
// CHECK: [[INIT:%.*]] = init_enum_data_addr [[OPTIONAL]] : $*Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>, #Optional.some!enumelt
948949
// CHECK: copy_addr [[OPEN]] to [init] [[INIT]] : $*@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self
949950
// CHECK: inject_enum_addr [[OPTIONAL]] : $*Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>, #Optional.some!enumelt
950951
// CHECK: [[DATA:%.*]] = unchecked_take_enum_data_addr [[OPTIONAL]] : $*Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>, #Optional.some!enumelt
951-
// CHECK: %10 = apply %{{.*}}<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>([[DATA]]) : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
952-
// CHECK: destroy_addr %9 : $*@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self
952+
// CHECK: apply %{{.*}}<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>([[DATA]]) : $@convention(witness_method: P) <τ_0_0 where τ_0_0 : P> (@in_guaranteed τ_0_0) -> ()
953+
// CHECK: destroy_addr [[DATA:%[^,]+]] : $*@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self
953954
// CHECK: destroy_addr [[ALLOCP]] : $*any P
954955
// CHECK: destroy_addr %0 : $*any P
955956
// CHECK: dealloc_stack [[OPTIONAL]] : $*Optional<@opened("EF755EF2-B636-11E7-B7B4-A45E60ECC541", any P) Self>

test/SILOptimizer/address_lowering_phi.sil

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@ enum Optional<T> {
1515
case some(T)
1616
}
1717

18+
struct S {}
19+
1820
struct SRef<T> {
1921
@_hasStorage var object: AnyObject { get set }
2022
@_hasStorage var element: T { get set }
@@ -470,3 +472,67 @@ bb2:
470472
bb3(%phi : @owned $T):
471473
return %phi : $T
472474
}
475+
476+
// Check that the debug_value instruction that is created when deleting a load
477+
// gets rewritten and doesn't obstruct the deletion of the phi.
478+
// CHECK-LABEL: sil [ossa] @f100_store_phi : {{.*}} {
479+
// CHECK: {{bb[0-9]+}}({{%[^,]+}} : $*Value):
480+
// CHECK: [[TEMP:%[^,]+]] = alloc_stack $Value
481+
// CHECK: [[VAR:%[^,]+]] = alloc_stack [lexical] $Value, var, name "value"
482+
// CHECK: cond_br undef, bb2, bb1
483+
// CHECK: bb3:
484+
// CHECK: copy_addr [take] [[TEMP]] to [init] [[VAR]]
485+
// CHECK: debug_value [[TEMP]] : $*Value, var, name "value"
486+
// CHECK-LABEL: } // end sil function 'f100_store_phi'
487+
sil [ossa] @f100_store_phi : $@convention(thin) <Value> (@in Value) -> () {
488+
entry(%instance : @owned $Value):
489+
%14 = alloc_stack [lexical] $Value, var, name "value"
490+
cond_br undef, left, right
491+
492+
left:
493+
br merge(%instance : $Value)
494+
495+
right:
496+
br merge(%instance : $Value)
497+
498+
merge(%34 : @owned $Value):
499+
store %34 to [init] %14 : $*Value
500+
destroy_addr %14 : $*Value
501+
dealloc_stack %14 : $*Value
502+
%43 = tuple ()
503+
return %43 : $()
504+
}
505+
506+
// CHECK-LABEL: sil [ossa] @f105_phi_into_tuple : {{.*}} {
507+
// CHECK: [[STORAGE:%[^,]+]] = alloc_stack $(Self, Builtin.Int1)
508+
// CHECK: [[SELF_ADDR:%[^,]+]] = tuple_element_addr [[STORAGE]]
509+
// CHECK: apply {{%[^,]+}}<Self>([[SELF_ADDR]])
510+
// CHECK-LABEL: } // end sil function 'f105_phi_into_tuple'
511+
sil [ossa] @f105_phi_into_tuple : $@convention(thin) <Self> () -> () {
512+
%getOut = function_ref @getOut : $@convention(thin) <T> () -> @out T
513+
%self = apply %getOut<Self>() : $@convention(thin) <τ_0_0> () -> @out τ_0_0
514+
br exit(%self : $Self)
515+
516+
exit(%self_2 : @owned $Self):
517+
%tuple = tuple (%self_2 : $Self, undef : $Bool)
518+
destroy_value %tuple : $(Self, Bool)
519+
%retval = tuple ()
520+
return %retval : $()
521+
}
522+
523+
// Check deleting the first of multiple block arguments.
524+
// CHECK-LABEL: sil [ossa] @f110_nonfinal_argument : {{.*}} {
525+
// CHECK: bb3({{%[^,]+}} : $S):
526+
// CHECK-LABEL: } // end sil function 'f110_nonfinal_argument'
527+
sil [ossa] @f110_nonfinal_argument : $@convention(thin) <T> (@in T, S) -> () {
528+
entry(%instance : @owned $T, %s : $S):
529+
cond_br undef, left, right
530+
left:
531+
br exit(%instance : $T, %s : $S)
532+
right:
533+
br exit(%instance : $T, %s : $S)
534+
exit(%instance_2 : @owned $T, %s_2 : $S):
535+
destroy_value %instance_2 : $T
536+
%retval = tuple ()
537+
return %retval : $()
538+
}

0 commit comments

Comments
 (0)