Skip to content

Commit 66e07f0

Browse files
authored
Merge pull request #81441 from eeckstein/vector_base_addr
SIL: introduce the `vector_base_addr` instruction and use it in `InlineArray`
2 parents efe53f0 + d28384d commit 66e07f0

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+566
-103
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,8 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
6161

6262
public var builtinVectorElementType: Type { Type(bridged: bridged.getBuiltinVectorElementType()) }
6363

64+
public var builtinFixedArrayElementType: Type { Type(bridged: bridged.getBuiltinFixedArrayElementType()) }
65+
6466
public func subst(with substitutionMap: SubstitutionMap) -> Type {
6567
return Type(bridged: bridged.subst(substitutionMap.bridged))
6668
}
@@ -81,6 +83,8 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti
8183

8284
public var builtinVectorElementType: CanonicalType { rawType.builtinVectorElementType.canonical }
8385

86+
public var builtinFixedArrayElementType: CanonicalType { rawType.builtinFixedArrayElementType.canonical }
87+
8488
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
8589
return rawType.subst(with: substitutionMap).canonical
8690
}
@@ -106,6 +110,7 @@ extension TypeProperties {
106110

107111
public var isBuiltinFloat: Bool { rawType.bridged.isBuiltinFloat() }
108112
public var isBuiltinVector: Bool { rawType.bridged.isBuiltinVector() }
113+
public var isBuiltinFixedArray: Bool { rawType.bridged.isBuiltinFixedArray() }
109114

110115
public var isClass: Bool {
111116
if let nominal = nominal, nominal is ClassDecl {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/InitializeStaticGlobals.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,7 +175,7 @@ private func lowerInlineArray(array: InlineArray, _ context: FunctionPassContext
175175
///
176176
private func getInlineArrayInfo(of allocStack: AllocStackInst) -> InlineArray? {
177177
var arrayLoad: LoadInst? = nil
178-
var elementStorage: UncheckedAddrCastInst? = nil
178+
var elementStorage: VectorBaseAddrInst? = nil
179179

180180
for use in allocStack.uses {
181181
switch use.instruction {
@@ -188,11 +188,11 @@ private func getInlineArrayInfo(of allocStack: AllocStackInst) -> InlineArray? {
188188
arrayLoad = load
189189
case is DeallocStackInst:
190190
break
191-
case let addrCastToElement as UncheckedAddrCastInst:
191+
case let baseAddr as VectorBaseAddrInst:
192192
if elementStorage != nil {
193193
return nil
194194
}
195-
elementStorage = addrCastToElement
195+
elementStorage = baseAddr
196196
default:
197197
return nil
198198
}

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ swift_compiler_sources(Optimizer
3939
SimplifySwitchEnum.swift
4040
SimplifyTuple.swift
4141
SimplifyTupleExtract.swift
42+
SimplifyUncheckedAddrCast.swift
4243
SimplifyUncheckedEnumData.swift
4344
SimplifyValueToBridgeObject.swift
4445
SimplifyWitnessMethod.swift)
Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
//===--- SimplifyUncheckedAddrCast.swift ----------------------------------===//
2+
//
3+
// This source file is part of the Swift.org open source project
4+
//
5+
// Copyright (c) 2014 - 2025 Apple Inc. and the Swift project authors
6+
// Licensed under Apache License v2.0 with Runtime Library Exception
7+
//
8+
// See https://swift.org/LICENSE.txt for license information
9+
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
import SIL
14+
15+
extension UncheckedAddrCastInst : OnoneSimplifiable, SILCombineSimplifiable {
16+
17+
func simplify(_ context: SimplifyContext) {
18+
// ```
19+
// %1 = unchecked_addr_cast %0 : $*T to $*T
20+
// ```
21+
// ->
22+
// replace %1 with %0
23+
//
24+
if optimizeSameTypeCast(context) {
25+
return
26+
}
27+
28+
// ```
29+
// %1 = unchecked_addr_cast %0 : $*U to $*V
30+
// %2 = unchecked_addr_cast %1 : $*V to $*T
31+
// ```
32+
// ->
33+
// ```
34+
// %2 = unchecked_addr_cast %0: $*U to $*T
35+
// ```
36+
if optimizeDoubleCast(context) {
37+
return
38+
}
39+
40+
// ```
41+
// %1 = unchecked_addr_cast %0 : $*Builtin.FixedArray<N, Element> to $*Element
42+
// ```
43+
// ->
44+
// ```
45+
// %1 = vector_base_addr %0 : $*Builtin.FixedArray<N, Element>
46+
// ```
47+
_ = optimizeVectorBaseCast(context)
48+
}
49+
}
50+
51+
private extension UncheckedAddrCastInst {
52+
func optimizeSameTypeCast(_ context: SimplifyContext) -> Bool {
53+
if fromAddress.type == type {
54+
self.replace(with: fromAddress, context)
55+
return true
56+
}
57+
return false
58+
}
59+
60+
func optimizeDoubleCast(_ context: SimplifyContext) -> Bool {
61+
if let firstCast = fromAddress as? UncheckedAddrCastInst {
62+
let builder = Builder(before: self, context)
63+
let newCast = builder.createUncheckedAddrCast(from: firstCast.fromAddress, to: type)
64+
self.replace(with: newCast, context)
65+
return true
66+
}
67+
return false
68+
}
69+
70+
func optimizeVectorBaseCast(_ context: SimplifyContext) -> Bool {
71+
if fromAddress.type.isBuiltinFixedArray,
72+
fromAddress.type.builtinFixedArrayElementType(in: parentFunction, maximallyAbstracted: true).addressType == type
73+
{
74+
let builder = Builder(before: self, context)
75+
let vectorBase = builder.createVectorBaseAddr(vector: fromAddress)
76+
self.replace(with: vectorBase, context)
77+
return true
78+
}
79+
return false
80+
}
81+
}

SwiftCompilerSources/Sources/Optimizer/PassManager/PassRegistration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,7 @@ private func registerSwiftPasses() {
125125
registerForSILCombine(PointerToAddressInst.self, { run(PointerToAddressInst.self, $0) })
126126
registerForSILCombine(UncheckedEnumDataInst.self, { run(UncheckedEnumDataInst.self, $0) })
127127
registerForSILCombine(WitnessMethodInst.self, { run(WitnessMethodInst.self, $0) })
128+
registerForSILCombine(UncheckedAddrCastInst.self, { run(UncheckedAddrCastInst.self, $0) })
128129
registerForSILCombine(UnconditionalCheckedCastInst.self, { run(UnconditionalCheckedCastInst.self, $0) })
129130
registerForSILCombine(AllocStackInst.self, { run(AllocStackInst.self, $0) })
130131
registerForSILCombine(ApplyInst.self, { run(ApplyInst.self, $0) })

SwiftCompilerSources/Sources/Optimizer/Utilities/OptUtils.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -387,7 +387,7 @@ extension Instruction {
387387
case let bi as BuiltinInst:
388388
switch bi.id {
389389
case .ZeroInitializer:
390-
let type = bi.type.isBuiltinVector ? bi.type.builtinVectorElementType : bi.type
390+
let type = bi.type.isBuiltinVector ? bi.type.builtinVectorElementType(in: parentFunction) : bi.type
391391
return type.isBuiltinInteger || type.isBuiltinFloat
392392
case .PtrToInt:
393393
return bi.operands[0].value is StringLiteralInst

SwiftCompilerSources/Sources/Optimizer/Utilities/Verifier.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,16 @@ extension LoadBorrowInst : VerifiableInstruction {
148148
}
149149
}
150150

151+
extension VectorBaseAddrInst : VerifiableInstruction {
152+
func verify(_ context: FunctionPassContext) {
153+
require(vector.type.isBuiltinFixedArray,
154+
"vector operand of vector_element_addr must be a Builtin.FixedArray")
155+
require(type == vector.type.builtinFixedArrayElementType(in: parentFunction,
156+
maximallyAbstracted: true).addressType,
157+
"result of vector_element_addr has wrong type")
158+
}
159+
}
160+
151161
// Used to check if any instruction is mutating the memory location within the liverange of a `load_borrow`.
152162
// Note that it is not checking if an instruction _may_ mutate the memory, but it's checking if any instruction
153163
// _definitely_ will mutate the memory.

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -487,6 +487,10 @@ public struct Builder {
487487
return notifyNew(vectorInst.getAs(VectorInst.self))
488488
}
489489

490+
public func createVectorBaseAddr(vector: Value) -> VectorBaseAddrInst {
491+
return notifyNew(bridged.createVectorBaseAddr(vector.bridged).getAs(VectorBaseAddrInst.self))
492+
}
493+
490494
public func createGlobalAddr(global: GlobalVariable, dependencyToken: Value?) -> GlobalAddrInst {
491495
return notifyNew(bridged.createGlobalAddr(global.bridged, dependencyToken.bridged).getAs(GlobalAddrInst.self))
492496
}

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1350,6 +1350,10 @@ final public class ObjectInst : SingleValueInstruction {
13501350
final public class VectorInst : SingleValueInstruction {
13511351
}
13521352

1353+
final public class VectorBaseAddrInst : SingleValueInstruction, UnaryInstruction {
1354+
public var vector: Value { operand.value }
1355+
}
1356+
13531357
final public class DifferentiableFunctionInst: SingleValueInstruction {}
13541358

13551359
final public class LinearFunctionInst: SingleValueInstruction {}

SwiftCompilerSources/Sources/SIL/Registration.swift

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public func registerSILClasses() {
118118
register(MoveOnlyWrapperToCopyableAddrInst.self)
119119
register(ObjectInst.self)
120120
register(VectorInst.self)
121+
register(VectorBaseAddrInst.self)
121122
register(TuplePackExtractInst.self)
122123
register(TuplePackElementAddrInst.self)
123124
register(PackElementGetInst.self)

0 commit comments

Comments
 (0)