Skip to content

Commit f6ce61c

Browse files
committed
SIL: support vector_base_addr in AccessUtils and WalkUtils
This enables alias analysis and optimizations, like, redundant load elimination, to benefit from it.
1 parent 0e790fc commit f6ce61c

File tree

6 files changed

+104
-1
lines changed

6 files changed

+104
-1
lines changed

SwiftCompilerSources/Sources/SIL/Utilities/AccessUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -431,7 +431,7 @@ public struct AccessPath : CustomStringConvertible, Hashable {
431431

432432
private func canBeOperandOfIndexAddr(_ value: Value) -> Bool {
433433
switch value {
434-
case is IndexAddrInst, is RefTailAddrInst, is PointerToAddressInst:
434+
case is IndexAddrInst, is RefTailAddrInst, is PointerToAddressInst, is VectorBaseAddrInst:
435435
return true
436436
default:
437437
return false

SwiftCompilerSources/Sources/SIL/Utilities/WalkUtils.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,12 @@ extension AddressDefUseWalker {
509509
} else {
510510
return unmatchedPath(address: operand, path: path)
511511
}
512+
case let vba as VectorBaseAddrInst:
513+
if let path = path.popIfMatches(.vectorBase, index: 0) {
514+
return walkDownUses(ofAddress: vba, path: path)
515+
} else {
516+
return unmatchedPath(address: operand, path: path)
517+
}
512518
case is InitEnumDataAddrInst, is UncheckedTakeEnumDataAddrInst:
513519
let ei = instruction as! SingleValueInstruction
514520
if let path = path.popIfMatches(.enumCase, index: (instruction as! EnumInstruction).caseIndex) {
@@ -814,6 +820,8 @@ extension AddressUseDefWalker {
814820
return walkUp(address: sea.struct, path: path.push(.structField, index: sea.fieldIndex))
815821
case let tea as TupleElementAddrInst:
816822
return walkUp(address: tea.tuple, path: path.push(.tupleField, index: tea.fieldIndex))
823+
case let vba as VectorBaseAddrInst:
824+
return walkUp(address: vba.vector, path: path.push(.vectorBase, index: 0))
817825
case let ida as InitEnumDataAddrInst:
818826
return walkUp(address: ida.operand.value, path: path.push(.enumCase, index: ida.caseIndex))
819827
case let uteda as UncheckedTakeEnumDataAddrInst:

test/SILOptimizer/accessutils.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ struct Ptr {
2727
var p: Int64
2828
}
2929

30+
struct VectorStruct {
31+
var a: Builtin.FixedArray<100, Int64>
32+
}
33+
3034
class C {}
3135

3236
sil @_getC : $@convention(thin) () -> @owned C
@@ -707,3 +711,20 @@ bb0(%0 : @guaranteed $List):
707711
return %6 : $Int64
708712
}
709713

714+
// CHECK-LABEL: Accesses for vectors
715+
// CHECK: Value: %4 = index_addr %2 : $*Int64, %3 : $Builtin.Int64
716+
// CHECK-NEXT: Scope: base
717+
// CHECK-NEXT: Base: argument - %0 = argument of bb0 : $*VectorStruct
718+
// CHECK-NEXT: Path: "s0.b.i5"
719+
// CHECK-NEXT: no Storage paths
720+
// CHECK-LABEL: End accesses for vectors
721+
sil [ossa] @vectors : $@convention(thin) (@in_guaranteed VectorStruct) -> Int64 {
722+
bb0(%0 : $*VectorStruct):
723+
%1 = struct_element_addr %0, #VectorStruct.a
724+
%2 = vector_base_addr %1
725+
%3 = integer_literal $Builtin.Int64, 5
726+
%4 = index_addr %2, %3
727+
%5 = load [trivial] %4
728+
return %5
729+
}
730+

test/SILOptimizer/alias-analysis.sil

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,3 +99,44 @@ bb0(%0 : $*MyStruct, %1 : $Builtin.Word):
9999
%99 = tuple ()
100100
return %99 : $()
101101
}
102+
103+
// CHECK-LABEL: @testVectorBaseAddr
104+
// CHECK: PAIR #18.
105+
// CHECK-NEXT: %2 = vector_base_addr %0 : $*Builtin.FixedArray<10, Int>
106+
// CHECK-NEXT: %3 = index_addr %2 : $*Int, %1 : $Builtin.Word
107+
// CHECK-NEXT: MayAlias
108+
// CHECK: PAIR #21.
109+
// CHECK-NEXT: %2 = vector_base_addr %0 : $*Builtin.FixedArray<10, Int>
110+
// CHECK-NEXT: %6 = index_addr %2 : $*Int, %4 : $Builtin.Word
111+
// CHECK-NEXT: NoAlias
112+
// CHECK: PAIR #22.
113+
// CHECK-NEXT: %2 = vector_base_addr %0 : $*Builtin.FixedArray<10, Int>
114+
// CHECK-NEXT: %7 = index_addr %2 : $*Int, %5 : $Builtin.Word
115+
// CHECK-NEXT: NoAlias
116+
// CHECK: PAIR #27.
117+
// CHECK-NEXT: %3 = index_addr %2 : $*Int, %1 : $Builtin.Word
118+
// CHECK-NEXT: %6 = index_addr %2 : $*Int, %4 : $Builtin.Word
119+
// CHECK-NEXT: MayAlias
120+
// CHECK: PAIR #28.
121+
// CHECK-NEXT: %3 = index_addr %2 : $*Int, %1 : $Builtin.Word
122+
// CHECK-NEXT: %7 = index_addr %2 : $*Int, %5 : $Builtin.Word
123+
// CHECK-NEXT: MayAlias
124+
// CHECK: PAIR #40.
125+
// CHECK-NEXT: %6 = index_addr %2 : $*Int, %4 : $Builtin.Word
126+
// CHECK-NEXT: %7 = index_addr %2 : $*Int, %5 : $Builtin.Word
127+
// CHECK-NEXT: NoAlias
128+
sil @testVectorBaseAddr : $@convention(thin) (@inout Builtin.FixedArray<10, Int>, Builtin.Word) -> () {
129+
bb0(%0 : $*Builtin.FixedArray<10, Int>, %1 : $Builtin.Word):
130+
%2 = vector_base_addr %0
131+
%3 = index_addr %2, %1
132+
%4 = integer_literal $Builtin.Word, 1
133+
%5 = integer_literal $Builtin.Word, 2
134+
%6 = index_addr %2, %4
135+
%7 = index_addr %2, %5
136+
fix_lifetime %2
137+
fix_lifetime %3
138+
fix_lifetime %6
139+
fix_lifetime %7
140+
%99 = tuple ()
141+
return %99 : $()
142+
}

test/SILOptimizer/escape_info.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1510,3 +1510,21 @@ bb0:
15101510
dealloc_stack %0 : $*X
15111511
return %4 : $Builtin.RawPointer
15121512
}
1513+
1514+
// CHECK-LABEL: Escape information for test_vector_base_addr_escaping:
1515+
// CHECK: return[]: %1 = alloc_ref $X
1516+
// CHECK: End function test_vector_base_addr_escaping
1517+
sil @test_vector_base_addr_escaping : $@convention(thin) () -> @owned X {
1518+
bb0:
1519+
%0 = alloc_stack $Builtin.FixedArray<10, X>
1520+
%1 = alloc_ref $X
1521+
%2 = vector_base_addr %0
1522+
%3 = integer_literal $Builtin.Int64, 1
1523+
%4 = index_addr %2, %3
1524+
store %1 to %4
1525+
%6 = vector_base_addr %0
1526+
%7 = index_addr %6, %3
1527+
%8 = load %4
1528+
dealloc_stack %0
1529+
return %8
1530+
}

test/SILOptimizer/redundant_load_elim_ossa.sil

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1743,3 +1743,18 @@ bb0(%0 : $Int):
17431743
dealloc_stack %3
17441744
return %6
17451745
}
1746+
1747+
// CHECK-LABEL: sil [ossa] @vector :
1748+
// CHECK: return %1
1749+
// CHECK-LABEL: } // end sil function 'vector'
1750+
sil [ossa] @vector : $@convention(thin) (@inout Builtin.FixedArray<10, Int>, Int, Int) -> Int {
1751+
bb0(%0 : $*Builtin.FixedArray<10, Int>, %1 : $Int, %2 : $Int):
1752+
%3 = vector_base_addr %0
1753+
%4 = integer_literal $Builtin.Word, 1
1754+
%5 = index_addr %3, %4
1755+
store %1 to [trivial] %5
1756+
store %2 to [trivial] %3
1757+
%6 = load [trivial] %5
1758+
return %6
1759+
}
1760+

0 commit comments

Comments
 (0)