Skip to content

Commit c7af4c5

Browse files
committed
Bridging: APIs for PackSpecialization pass
1 parent a0d33c3 commit c7af4c5

File tree

18 files changed

+426
-50
lines changed

18 files changed

+426
-50
lines changed

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,8 @@ extension TypeProperties {
146146
public var isExistentialMetatype: Bool { rawType.bridged.isExistentialMetatypeType() }
147147
public var isDynamicSelf: Bool { rawType.bridged.isDynamicSelf()}
148148
public var isBox: Bool { rawType.bridged.isBox() }
149+
public var isPack: Bool { rawType.bridged.isPack() }
150+
public var isSILPack: Bool { rawType.bridged.isSILPack() }
149151

150152
public var canBeClass: Type.TraitResult { rawType.bridged.canBeClass().result }
151153

@@ -263,6 +265,14 @@ extension TypeProperties {
263265
public func checkConformance(to protocol: ProtocolDecl) -> Conformance {
264266
return Conformance(bridged: rawType.bridged.checkConformance(`protocol`.bridged))
265267
}
268+
269+
public var containsSILPackExpansionType: Bool {
270+
return rawType.bridged.containsSILPackExpansionType()
271+
}
272+
273+
public var isSILPackElementAddress: Bool {
274+
return rawType.bridged.isSILPackElementAddress()
275+
}
266276
}
267277

268278
public struct TypeArray : RandomAccessCollection, CustomReflectable {

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/ConstantCapturePropagation.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,8 @@ private func specializeClosure(specializedName: String,
152152
newParams.append(contentsOf: nonConstantArguments.map { partialApply.parameter(for: $0)! })
153153

154154
let isGeneric = newParams.contains { $0.type.hasTypeParameter } ||
155-
callee.convention.results.contains { $0.type.hasTypeParameter() } ||
156-
callee.convention.errorResult?.type.hasTypeParameter() ?? false
155+
callee.convention.results.contains { $0.type.hasTypeParameter } ||
156+
callee.convention.errorResult?.type.hasTypeParameter ?? false
157157

158158
let specializedClosure = context.createSpecializedFunctionDeclaration(from: callee,
159159
withName: specializedName,

SwiftCompilerSources/Sources/Optimizer/PassManager/FunctionPassContext.swift

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -158,19 +158,43 @@ struct FunctionPassContext : MutatingContext {
158158
}
159159
}
160160

161-
func createSpecializedFunctionDeclaration(from original: Function, withName specializedFunctionName: String,
162-
withParams specializedParameters: [ParameterInfo],
163-
makeThin: Bool = false,
164-
makeBare: Bool = false,
165-
preserveGenericSignature: Bool = true) -> Function
166-
{
161+
func mangle(withExplodedPackArguments argIndices: [Int], from original: Function) -> String {
162+
return argIndices.withBridgedArrayRef { bridgedArgIndices in
163+
String(taking: bridgedPassContext.mangleWithExplodedPackArgs(bridgedArgIndices, original.bridged))
164+
}
165+
}
166+
167+
func createSpecializedFunctionDeclaration(
168+
from original: Function, withName specializedFunctionName: String,
169+
withParams specializedParameters: [ParameterInfo],
170+
withResults specializedResults: [ResultInfo]? = nil,
171+
makeThin: Bool = false,
172+
makeBare: Bool = false,
173+
preserveGenericSignature: Bool = true
174+
) -> Function {
167175
return specializedFunctionName._withBridgedStringRef { nameRef in
168176
let bridgedParamInfos = specializedParameters.map { $0._bridged }
169177

170178
return bridgedParamInfos.withUnsafeBufferPointer { paramBuf in
171-
bridgedPassContext.createSpecializedFunctionDeclaration(nameRef, paramBuf.baseAddress, paramBuf.count,
172-
original.bridged, makeThin, makeBare,
173-
preserveGenericSignature).function
179+
180+
if let bridgedResultInfos = specializedResults?.map({ $0._bridged }) {
181+
182+
return bridgedResultInfos.withUnsafeBufferPointer { resultBuf in
183+
return bridgedPassContext.createSpecializedFunctionDeclaration(
184+
nameRef, paramBuf.baseAddress, paramBuf.count,
185+
resultBuf.baseAddress, resultBuf.count,
186+
original.bridged, makeThin, makeBare,
187+
preserveGenericSignature
188+
).function
189+
}
190+
} else {
191+
return bridgedPassContext.createSpecializedFunctionDeclaration(
192+
nameRef, paramBuf.baseAddress, paramBuf.count,
193+
nil, 0,
194+
original.bridged, makeThin, makeBare,
195+
preserveGenericSignature
196+
).function
197+
}
174198
}
175199
}
176200
}

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -248,12 +248,33 @@ public struct Builder {
248248
return notifyNew(allocStack.getAs(AllocStackInst.self))
249249
}
250250

251+
public func createAllocPack(_ packType: Type) -> AllocPackInst {
252+
let allocPack = bridged.createAllocPack(packType.bridged)
253+
return notifyNew(allocPack.getAs(AllocPackInst.self))
254+
}
255+
256+
public func createAllocPackMetadata() -> AllocPackMetadataInst {
257+
let allocPackMetadata = bridged.createAllocPackMetadata()
258+
return notifyNew(allocPackMetadata.getAs(AllocPackMetadataInst.self))
259+
}
260+
261+
public func createAllocPackMetadata(_ packType: Type) -> AllocPackMetadataInst {
262+
let allocPackMetadata = bridged.createAllocPackMetadata(packType.bridged)
263+
return notifyNew(allocPackMetadata.getAs(AllocPackMetadataInst.self))
264+
}
265+
251266
@discardableResult
252267
public func createDeallocStack(_ operand: Value) -> DeallocStackInst {
253268
let dr = bridged.createDeallocStack(operand.bridged)
254269
return notifyNew(dr.getAs(DeallocStackInst.self))
255270
}
256271

272+
@discardableResult
273+
public func createDeallocPack(_ operand: Value) -> DeallocPackInst {
274+
let dr = bridged.createDeallocPack(operand.bridged)
275+
return notifyNew(dr.getAs(DeallocPackInst.self))
276+
}
277+
257278
@discardableResult
258279
public func createDeallocStackRef(_ operand: Value) -> DeallocStackRefInst {
259280
let dr = bridged.createDeallocStackRef(operand.bridged)
@@ -689,6 +710,11 @@ public struct Builder {
689710
return notifyNew(store.getAs(StoreInst.self))
690711
}
691712

713+
public func createStoreBorrow(source: Value, destination: Value) -> StoreBorrowInst {
714+
let storeBorrow = bridged.createStoreBorrow(source.bridged, destination.bridged)
715+
return notifyNew(storeBorrow.getAs(StoreBorrowInst.self))
716+
}
717+
692718
public func createInitExistentialRef(instance: Value,
693719
existentialType: Type,
694720
formalConcreteType: CanonicalType,
@@ -713,6 +739,22 @@ public struct Builder {
713739
return notifyNew(initExistential.getAs(InitExistentialMetatypeInst.self))
714740
}
715741

742+
public func createScalarPackIndex(componentIndex: Int, indexedPackType: CanonicalType) -> ScalarPackIndexInst {
743+
let scalarPackIndex = bridged.createScalarPackIndex(SwiftInt(componentIndex), indexedPackType.bridged)
744+
return notifyNew(scalarPackIndex.getAs(ScalarPackIndexInst.self))
745+
}
746+
747+
public func createPackElementGet(packIndex: Value, pack: Value, elementType: Type) -> PackElementGetInst {
748+
let packElementGet = bridged.createPackElementGet(packIndex.bridged, pack.bridged, elementType.bridged)
749+
return notifyNew(packElementGet.getAs(PackElementGetInst.self))
750+
}
751+
752+
@discardableResult
753+
public func createPackElementSet(elementValue: Value, packIndex: Value, pack: Value) -> PackElementSetInst {
754+
let packElementSet = bridged.createPackElementSet(elementValue.bridged, packIndex.bridged, pack.bridged)
755+
return notifyNew(packElementSet.getAs(PackElementSetInst.self))
756+
}
757+
716758
public func createMetatype(
717759
ofInstanceType instanceType: CanonicalType,
718760
representation: AST.`Type`.MetatypeRepresentation

SwiftCompilerSources/Sources/SIL/FunctionConvention.swift

Lines changed: 47 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -140,17 +140,31 @@ public struct ResultInfo : CustomStringConvertible {
140140
/// calling convention of the parameter.
141141
///
142142
/// TODO: For most purposes, you probably want \c returnValueType.
143-
public let type: BridgedASTType
143+
public let type: CanonicalType
144144
public let convention: ResultConvention
145+
public let options: UInt8
145146
public let hasLoweredAddresses: Bool
146147

148+
// Must be kept consistent with 'SILResultInfo::Flag'
149+
public enum Flag : UInt8 {
150+
case notDifferentiable = 0x1
151+
case isSending = 0x2
152+
};
153+
154+
public init(type: CanonicalType, convention: ResultConvention, options: UInt8, hasLoweredAddresses: Bool) {
155+
self.type = type
156+
self.convention = convention
157+
self.options = options
158+
self.hasLoweredAddresses = hasLoweredAddresses
159+
}
160+
147161
/// Is this result returned indirectly in SIL? Most formally
148162
/// indirect results can be returned directly in SIL. This depends
149163
/// on whether the calling function has lowered addresses.
150164
public var isSILIndirect: Bool {
151165
switch convention {
152166
case .indirect:
153-
return hasLoweredAddresses || type.isExistentialArchetypeWithError()
167+
return hasLoweredAddresses || type.isExistentialArchetypeWithError
154168
case .pack:
155169
return true
156170
case .owned, .unowned, .unownedInnerPointer, .autoreleased, .guaranteed, .guaranteedAddress, .inout:
@@ -159,8 +173,11 @@ public struct ResultInfo : CustomStringConvertible {
159173
}
160174

161175
public var description: String {
162-
convention.description + ": "
163-
+ String(taking: type.getDebugDescription())
176+
convention.description + ": " + type.description
177+
}
178+
179+
public func getReturnValueType(function: Function) -> CanonicalType {
180+
CanonicalType(bridged: self._bridged.getReturnValueType(function.bridged))
164181
}
165182
}
166183

@@ -234,6 +251,10 @@ public struct ParameterInfo : CustomStringConvertible {
234251
public func hasOption(_ flag: Flag) -> Bool {
235252
return options & flag.rawValue != 0
236253
}
254+
255+
public func getArgumentType(function: Function) -> CanonicalType {
256+
CanonicalType(bridged: self._bridged.getArgumentType(function.bridged))
257+
}
237258
}
238259

239260
extension FunctionConvention {
@@ -443,17 +464,22 @@ public enum ResultConvention : CustomStringConvertible {
443464

444465
extension ResultInfo {
445466
init(bridged: BridgedResultInfo, hasLoweredAddresses: Bool) {
446-
self.type = BridgedASTType(type: bridged.type)
467+
self.type = CanonicalType(bridged: bridged.type)
447468
self.convention = ResultConvention(bridged: bridged.convention)
448469
self.hasLoweredAddresses = hasLoweredAddresses
470+
self.options = bridged.options
449471
}
450472
init?(bridged: OptionalBridgedResultInfo, hasLoweredAddresses: Bool) {
451-
guard let t = bridged.type else {
473+
if bridged.type.getRawType().type == nil {
452474
return nil
453475
}
454-
self.type = BridgedASTType(type: t)
476+
self.type = CanonicalType(bridged: bridged.type)
455477
self.convention = ResultConvention(bridged: bridged.convention)
456478
self.hasLoweredAddresses = hasLoweredAddresses
479+
self.options = bridged.options
480+
}
481+
public var _bridged: BridgedResultInfo {
482+
BridgedResultInfo(type.bridged, convention.bridged, options)
457483
}
458484
}
459485

@@ -473,6 +499,20 @@ extension ResultConvention {
473499
fatalError("unsupported result convention")
474500
}
475501
}
502+
503+
var bridged: BridgedResultConvention {
504+
switch self {
505+
case .indirect: return .Indirect
506+
case .owned: return .Owned
507+
case .unowned: return .Unowned
508+
case .unownedInnerPointer: return .UnownedInnerPointer
509+
case .autoreleased: return .Autoreleased
510+
case .pack: return .Pack
511+
case .guaranteed: return .Guaranteed
512+
case .guaranteedAddress: return .GuaranteedAddress
513+
case .inout: return .Inout
514+
}
515+
}
476516
}
477517

478518
extension ParameterInfo {

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ public class Instruction : CustomStringConvertible, Hashable {
9696
BridgedContext.moveInstructionBefore(bridged, otherInstruction.bridged)
9797
context.notifyInstructionsChanged()
9898
}
99-
99+
100100
public final func copy(before otherInstruction: Instruction, _ context: some MutatingContext) {
101101
BridgedContext.copyInstructionBefore(bridged, otherInstruction.bridged)
102102
context.notifyInstructionsChanged()
@@ -182,7 +182,7 @@ public class Instruction : CustomStringConvertible, Hashable {
182182
public static func ==(lhs: Instruction, rhs: Instruction) -> Bool {
183183
lhs === rhs
184184
}
185-
185+
186186
public func isIdenticalTo(_ otherInst: Instruction) -> Bool {
187187
return bridged.isIdenticalTo(otherInst.bridged)
188188
}
@@ -1094,7 +1094,7 @@ final public class RefElementAddrInst : SingleValueInstruction, UnaryInstruction
10941094
public var fieldIsLet: Bool { bridged.RefElementAddrInst_fieldIsLet() }
10951095

10961096
public var isImmutable: Bool { bridged.RefElementAddrInst_isImmutable() }
1097-
1097+
10981098
public func set(isImmutable: Bool, _ context: some MutatingContext) {
10991099
context.notifyInstructionsChanged()
11001100
bridged.RefElementAddrInst_setImmutable(isImmutable)
@@ -1141,7 +1141,7 @@ final public class KeyPathInst : SingleValueInstruction {
11411141
final public
11421142
class UnconditionalCheckedCastInst : SingleValueInstruction, UnaryInstruction {
11431143
public override var mayTrap: Bool { true }
1144-
1144+
11451145
public var sourceFormalType: CanonicalType {
11461146
CanonicalType(bridged: bridged.UnconditionalCheckedCast_getSourceFormalType())
11471147
}
@@ -1814,10 +1814,29 @@ final public class DeallocPackInst : Instruction, UnaryInstruction, Deallocation
18141814
final public class DeallocPackMetadataInst : Instruction, Deallocation {}
18151815

18161816
final public class OpenPackElementInst : SingleValueInstruction {}
1817-
final public class PackLengthInst : SingleValueInstruction {}
1818-
final public class DynamicPackIndexInst : SingleValueInstruction {}
1819-
final public class PackPackIndexInst : SingleValueInstruction {}
1820-
final public class ScalarPackIndexInst : SingleValueInstruction {}
1817+
final public class PackLengthInst : SingleValueInstruction {
1818+
public var packType: CanonicalType {
1819+
CanonicalType(bridged: bridged.PackLengthInst_getPackType())
1820+
}
1821+
}
1822+
1823+
public protocol AnyPackIndexInst : SingleValueInstruction {
1824+
var indexedPackType: CanonicalType { get }
1825+
}
1826+
1827+
extension AnyPackIndexInst {
1828+
public var indexedPackType: CanonicalType {
1829+
CanonicalType(bridged: bridged.AnyPackIndexInst_getIndexedPackType())
1830+
}
1831+
}
1832+
1833+
final public class DynamicPackIndexInst : SingleValueInstruction, AnyPackIndexInst {}
1834+
final public class PackPackIndexInst : SingleValueInstruction, AnyPackIndexInst {}
1835+
final public class ScalarPackIndexInst : SingleValueInstruction, AnyPackIndexInst {
1836+
public var componentIndex: Int {
1837+
Int(bridged.ScalarPackIndexInst_getComponentIndex())
1838+
}
1839+
}
18211840

18221841
final public class TuplePackExtractInst: SingleValueInstruction {
18231842
public var indexOperand: Operand { operands[0] }
@@ -1842,7 +1861,7 @@ public class TermInst : Instruction {
18421861
let succArray = bridged.TermInst_getSuccessors()
18431862
return SuccessorArray(base: succArray.base, count: succArray.count)
18441863
}
1845-
1864+
18461865
public var isFunctionExiting: Bool { false }
18471866

18481867
public final func replaceBranchTarget(from fromBlock: BasicBlock, to toBlock: BasicBlock, _ context: some MutatingContext) {
@@ -1888,6 +1907,9 @@ final public class YieldInst : TermInst {
18881907
public func convention(of operand: Operand) -> ArgumentConvention {
18891908
return bridged.YieldInst_getConvention(operand.bridged).convention
18901909
}
1910+
1911+
public var resumeBlock: BasicBlock { bridged.YieldInst_getResumeBB().block }
1912+
public var unwindBlock: BasicBlock { bridged.YieldInst_getUnwindBB().block }
18911913
}
18921914

18931915
final public class UnwindInst : TermInst {
@@ -1997,11 +2019,11 @@ final public class AwaitAsyncContinuationInst : TermInst, UnaryInstruction {
19972019

19982020
public struct CheckedCastInstOptions {
19992021
var storage: UInt8 = 0
2000-
2022+
20012023
var bridged: BridgedInstruction.CheckedCastInstOptions {
20022024
.init(storage: storage)
20032025
}
2004-
2026+
20052027
var isolatedConformances: CastingIsolatedConformances {
20062028
return (storage & 0x01) != 0 ? .prohibit : .allow
20072029
}

0 commit comments

Comments
 (0)