Skip to content

Commit 6b44cd8

Browse files
Merge pull request #41542 from nate-chandler/lexical_lifetimes/fix_may_access_pointer
[MemAccessUtils] Corrected inverted condition.
2 parents 9ead7d0 + 8a81d41 commit 6b44cd8

File tree

3 files changed

+41
-18
lines changed

3 files changed

+41
-18
lines changed

lib/SIL/Utils/MemAccessUtils.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -405,13 +405,13 @@ static bool isBarrierApply(FullApplySite) {
405405
static bool mayAccessPointer(SILInstruction *instruction) {
406406
if (!instruction->mayReadOrWriteMemory())
407407
return false;
408-
bool fail = false;
409-
visitAccessedAddress(instruction, [&fail](Operand *operand) {
408+
bool isUnidentified = false;
409+
visitAccessedAddress(instruction, [&isUnidentified](Operand *operand) {
410410
auto accessStorage = AccessStorage::compute(operand->get());
411-
if (accessStorage.getKind() != AccessRepresentation::Kind::Unidentified)
412-
fail = true;
411+
if (accessStorage.getKind() == AccessRepresentation::Kind::Unidentified)
412+
isUnidentified = true;
413413
});
414-
return fail;
414+
return isUnidentified;
415415
}
416416

417417
static bool mayLoadWeakOrUnowned(SILInstruction *instruction) {

test/SILOptimizer/hoist_destroy_addr.sil

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -327,6 +327,25 @@ exit:
327327
return %321 : $()
328328
}
329329

330+
// Hoist a destroy_addr of an @in argument over a load from an alloc_stack.
331+
//
332+
// CHECK-LABEL: sil [ossa] @test_hoist_over_load_from_stack : {{.*}} {
333+
// CHECK: apply
334+
// CHECK: destroy_addr
335+
// CHECK: load [take]
336+
// CHECK-LABEL: } // end sil function 'test_hoist_over_load_from_stack'
337+
sil [ossa] @test_hoist_over_load_from_stack : $@convention(thin) (@in X) -> @owned X {
338+
entry(%in_addr : $*X):
339+
%stack_addr = alloc_stack $X
340+
copy_addr %in_addr to [initialization] %stack_addr : $*X
341+
%unknown = function_ref @unknown : $@convention(thin) () -> ()
342+
apply %unknown() : $@convention(thin) () -> ()
343+
%retval = load [take] %stack_addr : $*X
344+
destroy_addr %in_addr : $*X
345+
dealloc_stack %stack_addr : $*X
346+
return %retval : $X
347+
}
348+
330349
// Fold destroy_addr and a load [copy] into a load [take] even when that
331350
// load [take] is guarded by an access scope.
332351
//
@@ -442,9 +461,10 @@ entry(%instance : @owned $S):
442461
}
443462

444463
// Don't fold with an unrelated load [copy].
464+
//
445465
// CHECK-LABEL: sil [ossa] @nofold_unrelated_scoped_load_copy : {{.*}} {
446-
// CHECK: load [copy]
447466
// CHECK: destroy_addr
467+
// CHECK: load [copy]
448468
// CHECK: destroy_addr
449469
// CHECK-LABEL: // end sil function 'nofold_unrelated_scoped_load_copy'
450470
sil [ossa] @nofold_unrelated_scoped_load_copy : $@convention(thin) (@owned X) -> (@owned X) {

test/SILOptimizer/shrink_borrow_scope.sil

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -581,18 +581,21 @@ entry(%instance_c: @owned $C, %instance_d: @owned $D):
581581
return %instance_c : $C
582582
}
583583

584-
// Don't hoist over store to an address which itself is (earlier) stored into a
585-
// field of the object being borrowed.
586-
// TODO: Eventually, we should be able to hoist over such a store.
587-
// CHECK-LABEL: sil [ossa] @hoist_over_load : $@convention(thin) () -> () {
588-
// CHECK: [[D:%[^,]+]] = alloc_ref $PointerWrapper
589-
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[D]] : $PointerWrapper
590-
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $PointedTo
591-
// CHECK: [[C:%[^,]+]] = alloc_ref $PointedTo
592-
// CHECK: store [[C]] to [init] [[ADDR]] : $*PointedTo
593-
// CHECK-NEXT: end_borrow [[LIFETIME]] : $PointerWrapper
594-
// CHECK-LABEL: } // end sil function 'hoist_over_load'
595-
sil [ossa] @hoist_over_load : $@convention(thin) () -> () {
584+
// Hoist over store to unrelated stack address. Do not hoist over store to
585+
// field of lifetime.
586+
//
587+
// CHECK-LABEL: sil [ossa] @hoist_over_store : $@convention(thin) () -> () {
588+
// CHECK: [[D:%[^,]+]] = alloc_ref $PointerWrapper
589+
// CHECK: [[LIFETIME:%[^,]+]] = begin_borrow [[D]] : $PointerWrapper
590+
// CHECK: [[ADDR:%[^,]+]] = alloc_stack $PointedTo
591+
// CHECK: [[PTR:%[^,]+]] = address_to_pointer [[ADDR]]
592+
// CHECK: [[FIELD:%[^,]+]] = ref_element_addr [[LIFETIME]]
593+
// CHECK: store [[PTR]] to [trivial] [[FIELD]]
594+
// CHECK: end_borrow [[LIFETIME]] : $PointerWrapper
595+
// CHECK: [[C:%[^,]+]] = alloc_ref $PointedTo
596+
// CHECK: store [[C]] to [init] [[ADDR]] : $*PointedTo
597+
// CHECK-LABEL: } // end sil function 'hoist_over_store'
598+
sil [ossa] @hoist_over_store : $@convention(thin) () -> () {
596599
bb0:
597600
%d = alloc_ref $PointerWrapper
598601
%lifetime = begin_borrow %d : $PointerWrapper

0 commit comments

Comments
 (0)