Skip to content

Commit 52dd6a5

Browse files
committed
[AddressLowering] Exit range check for args early.
When determining whether a copy_value is part of a copy->store pair, if the value being copied is guaranteed, it is checked whether the store is within the lifetime of all its guaranteed roots. If one of those guaranteed roots is a function argument, the store is certainly within the lifetime, so exit early.
1 parent aad2b6e commit 52dd6a5

File tree

2 files changed

+29
-0
lines changed

2 files changed

+29
-0
lines changed

lib/SILOptimizer/Mandatory/AddressLowering.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -383,6 +383,10 @@ static bool isStoreCopy(SILValue value) {
383383
// - visit borrow introducers via visitBorrowIntroducers
384384
// - call ExtendedLiveness.compute on each borrow introducer
385385
if (llvm::any_of(roots, [&](SILValue root) {
386+
// Nothing is out of range of a function argument.
387+
if (isa<SILFunctionArgument>(root))
388+
return false;
389+
386390
// Handle forwarding phis conservatively rather than recursing.
387391
if (SILArgument::asPhi(root) && !BorrowedValue(root))
388392
return true;

test/SILOptimizer/address_lowering.sil

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1040,6 +1040,31 @@ bb7:
10401040
return %23 : $()
10411041
}
10421042

1043+
// CHECK-LABEL: sil [ossa] @f163_testOpenedArchetype : {{.*}} {
1044+
// CHECK: {{bb[0-9]+}}([[INSTANCE:%[^,]+]] :
1045+
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $Optional<Any>
1046+
// CHECK: [[BORROW:%[^,]+]] = begin_borrow [lexical] [[INSTANCE]]
1047+
// CHECK: [[AN_ERROR:%[^,]+]] = open_existential_box [[BORROW]]
1048+
// CHECK: [[ANY_ADDR:%[^,]+]] = init_enum_data_addr [[ADDR]]
1049+
// CHECK: [[OPENED_ADDR:%[^,]+]] = init_existential_addr [[ANY_ADDR]]
1050+
// CHECK: copy_addr [[AN_ERROR]] to [init] [[OPENED_ADDR]]
1051+
// CHECK: inject_enum_addr [[ADDR]] : $*Optional<Any>, #Optional.some!enumelt
1052+
// CHECK: apply undef<Any>([[ADDR]])
1053+
// CHECK-LABEL: } // end sil function 'f163_testOpenedArchetype'
1054+
sil [ossa] @f163_testOpenedArchetype : $@convention(c) (@owned any Error) -> () {
1055+
block(%instance : @owned $any Error):
1056+
%borrow = begin_borrow [lexical] %instance : $any Error
1057+
%an_error = open_existential_box_value %borrow : $any Error to $@opened("55625EBA-9384-11ED-A0B0-32F16C24A34F", any Error) Self
1058+
%an_owned_error = copy_value %an_error : $@opened("55625EBA-9384-11ED-A0B0-32F16C24A34F", any Error) Self
1059+
%any = init_existential_value %an_owned_error : $@opened("55625EBA-9384-11ED-A0B0-32F16C24A34F", any Error) Self, $@opened("55625EBA-9384-11ED-A0B0-32F16C24A34F", any Error) Self, $Any
1060+
%some_any = enum $Optional<Any>, #Optional.some!enumelt, %any : $Any
1061+
apply undef<Any>(%some_any) : $@convention(thin) <T> (@in Optional<T>) -> ()
1062+
end_borrow %borrow : $any Error
1063+
destroy_value %instance : $any Error
1064+
%retval = tuple ()
1065+
return %retval : $()
1066+
}
1067+
10431068
// CHECK-LABEL: sil [ossa] @f170_compare : $@convention(thin) <T where T : Comparable> (@in_guaranteed T, @in_guaranteed T) -> @out T {
10441069
// CHECK: bb0(%0 : $*T, %1 : $*T, %2 : $*T):
10451070
// CHECK: [[WT:%.*]] = witness_method $T, #Comparable."<" : <Self where Self : Comparable> (Self.Type) -> (Self, Self) -> Builtin.Int1 : $@convention(witness_method: Comparable) <τ_0_0 where τ_0_0 : Comparable> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_0, @thick τ_0_0.Type) -> Builtin.Int1

0 commit comments

Comments
 (0)