Skip to content

Commit 412107f

Browse files
authored
Merge pull request #83967 from eeckstein/fix-tail-addr-aliasing
AliasAnalysis: fix aliasing of `ref_tail_addr` access bases
2 parents f0bfdf6 + e5081a5 commit 412107f

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

SwiftCompilerSources/Sources/SIL/Utilities/AccessUtils.swift

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -323,8 +323,19 @@ public enum AccessBase : CustomStringConvertible, Hashable {
323323
isDifferentAllocation(rea.instance, otherRea.instance) ||
324324
hasDifferentType(rea.instance, otherRea.instance)
325325
case (.tail(let rta), .tail(let otherRta)):
326-
return isDifferentAllocation(rta.instance, otherRta.instance) ||
327-
hasDifferentType(rta.instance, otherRta.instance)
326+
if isDifferentAllocation(rta.instance, otherRta.instance) {
327+
return true
328+
}
329+
if hasDifferentType(rta.instance, otherRta.instance),
330+
// In contrast to `ref_element_addr`, tail addresses can also be obtained via a superclass
331+
// (in case the derived class doesn't add any stored properties).
332+
// Therefore if the instance types differ by sub-superclass relationship, the base is _not_ different.
333+
!rta.instance.type.isExactSuperclass(of: otherRta.instance.type),
334+
!otherRta.instance.type.isExactSuperclass(of: rta.instance.type)
335+
{
336+
return true
337+
}
338+
return false
328339
case (.argument(let arg), .argument(let otherArg)):
329340
return (arg.convention.isExclusiveIndirect || otherArg.convention.isExclusiveIndirect) && arg != otherArg
330341

test/SILOptimizer/basic-aa.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -665,6 +665,25 @@ bb0:
665665
%99 = tuple ()
666666
return %99 : $()
667667
}
668+
669+
class Derived: X {}
670+
671+
// CHECK-LABEL: @test_tail_addr_and_upcast
672+
// CHECK: PAIR #10.
673+
// CHECK-NEXT: %2 = ref_tail_addr %1 : $X, $Int32
674+
// CHECK-NEXT: %3 = ref_tail_addr %0 : $Derived, $Int32
675+
// CHECK-NEXT: MayAlias
676+
sil @test_tail_addr_and_upcast : $@convention(thin) () -> () {
677+
bb0:
678+
%0 = alloc_ref $Derived
679+
%1 = upcast %0 to $X
680+
%2 = ref_tail_addr %1, $Int32
681+
%3 = ref_tail_addr %0, $Int32
682+
fix_lifetime %2
683+
fix_lifetime %3
684+
%r = tuple ()
685+
return %r
686+
}
668687

669688
class GenericClass<T> {
670689
var x : T

0 commit comments

Comments
 (0)