From 6f375aa6b65c4441babcfaf09b463532965308c1 Mon Sep 17 00:00:00 2001 From: Meghana Gupta Date: Fri, 10 Jan 2025 11:30:09 -0800 Subject: [PATCH] Look through forwarding instructions while querying ownership root Fixes rdar://142507746 --- include/swift/SIL/MemAccessUtils.h | 7 +++++++ lib/SIL/Utils/OwnershipUtils.cpp | 2 +- test/SILOptimizer/cse_ossa_nontrivial.sil | 23 +++++++++++++++++++++++ 3 files changed, 31 insertions(+), 1 deletion(-) diff --git a/include/swift/SIL/MemAccessUtils.h b/include/swift/SIL/MemAccessUtils.h index 84fabd54487f9..07a0002e37559 100644 --- a/include/swift/SIL/MemAccessUtils.h +++ b/include/swift/SIL/MemAccessUtils.h @@ -638,6 +638,13 @@ class AccessBase : public AccessRepresentation { return findOwnershipReferenceRoot(getReference()); } + /// Return the OSSA root of the reference being accessed + /// looking through struct_extract, tuple_extract, etc. + /// Precondition: isReference() is true. + SILValue getOwnershipReferenceAggregate() const { + return findOwnershipReferenceAggregate(getReference()); + } + /// Return the storage root of the reference being accessed. /// /// Precondition: isReference() is true. diff --git a/lib/SIL/Utils/OwnershipUtils.cpp b/lib/SIL/Utils/OwnershipUtils.cpp index 561ef1dee9392..3e6942f6e9ac0 100644 --- a/lib/SIL/Utils/OwnershipUtils.cpp +++ b/lib/SIL/Utils/OwnershipUtils.cpp @@ -1092,7 +1092,7 @@ bool AddressOwnership::areUsesWithinLifetime( if (!base.hasLocalOwnershipLifetime()) return true; - SILValue root = base.getOwnershipReferenceRoot(); + SILValue root = base.getOwnershipReferenceAggregate(); BorrowedValue borrow(root); if (borrow) return borrow.areUsesWithinExtendedScope(uses, &deadEndBlocks); diff --git a/test/SILOptimizer/cse_ossa_nontrivial.sil b/test/SILOptimizer/cse_ossa_nontrivial.sil index e3f92c17c7cce..df46d83983769 100644 --- a/test/SILOptimizer/cse_ossa_nontrivial.sil +++ b/test/SILOptimizer/cse_ossa_nontrivial.sil @@ -1010,3 +1010,26 @@ bb0: return %t : $() } +// CHECK-LABEL: sil [ossa] @test_aggreate_tail_addr : +// CHECK: ref_tail_addr +// CHECK: ref_tail_addr +// CHECK-LABEL: } // end sil function 'test_aggreate_tail_addr' +sil [ossa] @test_aggreate_tail_addr : $@convention(thin) (@owned _NativeDictionary) -> () { +bb0(%0 : @owned $_NativeDictionary): + %49 = move_value [lexical] %0 : $_NativeDictionary + %50 = begin_borrow %49 + %51 = struct_extract %50, #_NativeDictionary._storage + %52 = copy_value %51 + %53 = ref_tail_addr %51, $_UnsafeBitset.Word + %59 = load [trivial] %53 + end_borrow %50 + %72 = begin_borrow %52 + %73 = ref_tail_addr %72, $_UnsafeBitset.Word + %74 = struct_element_addr %73, #_UnsafeBitset.Word.value + %75 = struct_element_addr %74, #UInt._value + %76 = load [trivial] %75 + end_borrow %72 + destroy_value %52 + destroy_value %49 + unreachable +}