Files
swift-mirror/test/SILOptimizer/alias-analysis_unit.sil
Erik Eckstein 93a97200a5 SIL: correctly handle index_addr with the new projection flag in various SIL utilities
* SmallProjectionPath: we don't need special handling for index elements anymore
* WalkUtils: only treat `index_addr` with the `projection` flag as index projections
* AccessUtils: remove the special treatment of `index_addr`, except handling `pointer_to_address` - `index_addr` pairs
2026-06-09 16:17:53 +02:00

220 lines
7.9 KiB
Plaintext

// RUN: %target-sil-opt %s -test-runner -o /dev/null | %FileCheck %s
// General black-box alias analysis tests. White-box unit tests are
// specific to the aa-kind, such as basic-aa.sil and typed-access-tb-aa.sil.
// REQUIRES: asserts
sil_stage canonical
import Swift
import SwiftShims
import Builtin
struct MyStruct {
@_hasStorage @_hasInitialValue var i: Int64 { get set }
@_hasStorage @_hasInitialValue var j: Int64 { get set }
}
struct OuterStruct {
@_hasStorage @_hasInitialValue var s: MyStruct { get set }
}
// Test overlapping access on a different path; the user has misused an index offset
// CHECK-LABEL: @testOffsetBehindProjection
// CHECK: PAIR #23.
// CHECK-NEXT: %3 = index_addr %1 : $*Int64, %2 : $Builtin.Word
// CHECK-NEXT: %5 = struct_element_addr %0 : $*MyStruct, #MyStruct.j
// CHECK-NEXT: NoAlias
sil shared @testOffsetBehindProjection : $@convention(thin) (@inout MyStruct) -> () {
bb0(%0 : $*MyStruct):
specify_test "aliasing"
%1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
%2 = integer_literal $Builtin.Word, 1
%3 = index_addr %1 : $*Int64, %2 : $Builtin.Word
%4 = load %3 : $*Int64
// load from a different subobject overlaps
%5 = struct_element_addr %0 : $*MyStruct, #MyStruct.j
%6 = load %5 : $*Int64
%99 = tuple ()
return %99 : $()
}
// CHECK-LABEL: @testOffsetsBehindProjectionOverlap
// CHECK: PAIR #21.
// CHECK-NEXT: %2 = struct_element_addr %1 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: %6 = struct_element_addr %5 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: MayAlias
sil shared @testOffsetsBehindProjectionOverlap : $@convention(thin) (@inout OuterStruct) -> () {
bb0(%0 : $*OuterStruct):
specify_test "aliasing"
%1 = struct_element_addr %0 : $*OuterStruct, #OuterStruct.s
%2 = struct_element_addr %1 : $*MyStruct, #MyStruct.i
%3 = load %2 : $*Int64
// Loading from a different index within the same subobject still appears to overlap.
// Indexing from a subobject is always considered an unknown index.
%4 = integer_literal $Builtin.Word, 1
%5 = index_addr %1 : $*MyStruct, %4 : $Builtin.Word
%6 = struct_element_addr %5 : $*MyStruct, #MyStruct.i
%7 = load %6 : $*Int64
%99 = tuple ()
return %99 : $()
}
// CHECK-LABEL: @testIndexOf0
// CHECK: PAIR #8.
// CHECK-NEXT: %1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: %3 = index_addr %0 : $*MyStruct, %2 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #9.
// CHECK-NEXT: %1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: %4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: MayAlias
sil @testIndexOf0 : $@convention(thin) (@inout MyStruct) -> () {
bb0(%0 : $*MyStruct):
specify_test "aliasing"
%1 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
%2 = integer_literal $Builtin.Word, 0
%3 = index_addr %0 : $*MyStruct, %2 : $Builtin.Word
%4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
fix_lifetime %1 : $*Int64
fix_lifetime %4 : $*Int64
%99 = tuple ()
return %99 : $()
}
// CHECK-LABEL: @testUnknownIndex
// CHECK: PAIR #12.
// CHECK-NEXT: %2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: %3 = index_addr %0 : $*MyStruct, %1 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #13.
// CHECK-NEXT: %2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: %4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
// CHECK-NEXT: MayAlias
sil @testUnknownIndex : $@convention(thin) (@inout MyStruct, Builtin.Word) -> () {
bb0(%0 : $*MyStruct, %1 : $Builtin.Word):
specify_test "aliasing"
%2 = struct_element_addr %0 : $*MyStruct, #MyStruct.i
%3 = index_addr %0 : $*MyStruct, %1 : $Builtin.Word
%4 = struct_element_addr %3 : $*MyStruct, #MyStruct.i
fix_lifetime %2 : $*Int64
fix_lifetime %4 : $*Int64
%99 = tuple ()
return %99 : $()
}
// CHECK-LABEL: @testVectorBaseAddr
// CHECK: PAIR #18.
// CHECK-NEXT: %2 = vector_base_addr %0 : $*Builtin.FixedArray<10, Int>
// CHECK-NEXT: %3 = index_addr [projection] %2 : $*Int, %1 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #21.
// CHECK-NEXT: %2 = vector_base_addr %0 : $*Builtin.FixedArray<10, Int>
// CHECK-NEXT: %6 = index_addr [projection] %2 : $*Int, %4 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #22.
// CHECK-NEXT: %2 = vector_base_addr %0 : $*Builtin.FixedArray<10, Int>
// CHECK-NEXT: %7 = index_addr [projection] %2 : $*Int, %5 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #27.
// CHECK-NEXT: %3 = index_addr [projection] %2 : $*Int, %1 : $Builtin.Word
// CHECK-NEXT: %6 = index_addr [projection] %2 : $*Int, %4 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #28.
// CHECK-NEXT: %3 = index_addr [projection] %2 : $*Int, %1 : $Builtin.Word
// CHECK-NEXT: %7 = index_addr [projection] %2 : $*Int, %5 : $Builtin.Word
// CHECK-NEXT: MayAlias
// CHECK: PAIR #40.
// CHECK-NEXT: %6 = index_addr [projection] %2 : $*Int, %4 : $Builtin.Word
// CHECK-NEXT: %7 = index_addr [projection] %2 : $*Int, %5 : $Builtin.Word
// CHECK-NEXT: NoAlias
sil @testVectorBaseAddr : $@convention(thin) (@inout Builtin.FixedArray<10, Int>, Builtin.Word) -> () {
bb0(%0 : $*Builtin.FixedArray<10, Int>, %1 : $Builtin.Word):
specify_test "aliasing"
%2 = vector_base_addr %0
%3 = index_addr [projection] %2, %1
%4 = integer_literal $Builtin.Word, 1
%5 = integer_literal $Builtin.Word, 2
%6 = index_addr [projection] %2, %4
%7 = index_addr [projection] %2, %5
fix_lifetime %2
fix_lifetime %3
fix_lifetime %6
fix_lifetime %7
%99 = tuple ()
return %99 : $()
}
// CHECK-LABEL: @compensating_index_addr1
// CHECK: PAIR #28.
// CHECK-NEXT: %3 = index_addr %1 : $*Int, %2 : $Builtin.Word
// CHECK-NEXT: %7 = index_addr %5 : $*Int, %6 : $Builtin.Word
// CHECK-NEXT: MayAlias
sil @compensating_index_addr1 : $@convention(thin) (@inout Builtin.FixedArray<10, Int>) -> () {
bb0(%0 : $*Builtin.FixedArray<10, Int>):
specify_test "aliasing"
%1 = vector_base_addr %0
%2 = integer_literal $Builtin.Word, 2
%3 = index_addr %1, %2
%4 = integer_literal $Builtin.Word, 1
%5 = index_addr %1, %4
%6 = integer_literal $Builtin.Word, 1
%7 = index_addr %5, %6
fix_lifetime %3
fix_lifetime %7
%r = tuple ()
return %r
}
// CHECK-LABEL: @compensating_index_addr2
// CHECK: PAIR #36.
// CHECK-NEXT: %3 = index_addr %1 : $*Int, %2 : $Builtin.Word
// CHECK-NEXT: %9 = index_addr %7 : $*Int, %8 : $Builtin.Word
// CHECK-NEXT: MayAlias
sil @compensating_index_addr2 : $@convention(thin) (@inout Builtin.FixedArray<10, Int>) -> () {
bb0(%0 : $*Builtin.FixedArray<10, Int>):
specify_test "aliasing"
%1 = vector_base_addr %0
%2 = integer_literal $Builtin.Word, 3
%3 = index_addr %1, %2
%4 = integer_literal $Builtin.Word, 1
%5 = index_addr %1, %4
%6 = address_to_pointer %5 to $Builtin.RawPointer
%7 = pointer_to_address %6 to $*Int
%8 = integer_literal $Builtin.Word, 2
%9 = index_addr %7, %8
fix_lifetime %3
fix_lifetime %9
%r = tuple ()
return %r
}
struct Container {
@_hasStorage var values: InlineArray<3, Int> { get set }
}
sil_global hidden @g : $Container
// CHECK-LABEL: @testInlineArray
// CHECK: PAIR #9.
// CHECK-NEXT: %1 = global_addr @g : $*Container
// CHECK-NEXT: %4 = vector_base_addr %3 : $*Builtin.FixedArray<3, Int>
// CHECK-NEXT: MayAlias
// CHECK: PAIR #13.
// CHECK-NEXT: %2 = struct_element_addr %1 : $*Container, #Container.values
// CHECK-NEXT: %4 = vector_base_addr %3 : $*Builtin.FixedArray<3, Int>
// CHECK-NEXT: MayAlias
sil [ossa] @testInlineArray : $@convention(thin) (Int) -> () {
bb0(%0 : $Int):
specify_test "aliasing"
%1 = global_addr @g : $*Container
%3 = struct_element_addr %1, #Container.values
%4 = struct_element_addr %3, #InlineArray._storage
%5 = vector_base_addr %4
fix_lifetime %5
%10 = tuple ()
return %10
}