Skip to content

Commit 2ff12fb

Browse files
committed
Bridging: APIs for PackSpecialization pass
1 parent 5417f00 commit 2ff12fb

File tree

18 files changed

+425
-50
lines changed

18 files changed

+425
-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: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -134,17 +134,31 @@ public struct ResultInfo : CustomStringConvertible {
134134
/// calling convention of the parameter.
135135
///
136136
/// TODO: For most purposes, you probably want \c returnValueType.
137-
public let type: BridgedASTType
137+
public let type: CanonicalType
138138
public let convention: ResultConvention
139+
public let options: UInt8
139140
public let hasLoweredAddresses: Bool
140141

142+
// Must be kept consistent with 'SILResultInfo::Flag'
143+
public enum Flag : UInt8 {
144+
case notDifferentiable = 0x1
145+
case isSending = 0x2
146+
};
147+
148+
public init(type: CanonicalType, convention: ResultConvention, options: UInt8, hasLoweredAddresses: Bool) {
149+
self.type = type
150+
self.convention = convention
151+
self.options = options
152+
self.hasLoweredAddresses = hasLoweredAddresses
153+
}
154+
141155
/// Is this result returned indirectly in SIL? Most formally
142156
/// indirect results can be returned directly in SIL. This depends
143157
/// on whether the calling function has lowered addresses.
144158
public var isSILIndirect: Bool {
145159
switch convention {
146160
case .indirect:
147-
return hasLoweredAddresses || type.isExistentialArchetypeWithError()
161+
return hasLoweredAddresses || type.isExistentialArchetypeWithError
148162
case .pack:
149163
return true
150164
case .owned, .unowned, .unownedInnerPointer, .autoreleased, .guaranteed, .guaranteedAddress:
@@ -153,8 +167,11 @@ public struct ResultInfo : CustomStringConvertible {
153167
}
154168

155169
public var description: String {
156-
convention.description + ": "
157-
+ String(taking: type.getDebugDescription())
170+
convention.description + ": " + type.description
171+
}
172+
173+
public func getReturnValueType(function: Function) -> CanonicalType {
174+
CanonicalType(bridged: self._bridged.getReturnValueType(function.bridged))
158175
}
159176
}
160177

@@ -228,6 +245,10 @@ public struct ParameterInfo : CustomStringConvertible {
228245
public func hasOption(_ flag: Flag) -> Bool {
229246
return options & flag.rawValue != 0
230247
}
248+
249+
public func getArgumentType(function: Function) -> CanonicalType {
250+
CanonicalType(bridged: self._bridged.getArgumentType(function.bridged))
251+
}
231252
}
232253

233254
extension FunctionConvention {
@@ -431,17 +452,22 @@ public enum ResultConvention : CustomStringConvertible {
431452

432453
extension ResultInfo {
433454
init(bridged: BridgedResultInfo, hasLoweredAddresses: Bool) {
434-
self.type = BridgedASTType(type: bridged.type)
455+
self.type = CanonicalType(bridged: bridged.type)
435456
self.convention = ResultConvention(bridged: bridged.convention)
436457
self.hasLoweredAddresses = hasLoweredAddresses
458+
self.options = bridged.options
437459
}
438460
init?(bridged: OptionalBridgedResultInfo, hasLoweredAddresses: Bool) {
439-
guard let t = bridged.type else {
461+
if bridged.type.getRawType().type == nil {
440462
return nil
441463
}
442-
self.type = BridgedASTType(type: t)
464+
self.type = CanonicalType(bridged: bridged.type)
443465
self.convention = ResultConvention(bridged: bridged.convention)
444466
self.hasLoweredAddresses = hasLoweredAddresses
467+
self.options = bridged.options
468+
}
469+
public var _bridged: BridgedResultInfo {
470+
BridgedResultInfo(type.bridged, convention.bridged, options)
445471
}
446472
}
447473

@@ -460,6 +486,19 @@ extension ResultConvention {
460486
fatalError("unsupported result convention")
461487
}
462488
}
489+
490+
var bridged: BridgedResultConvention {
491+
switch self {
492+
case .indirect: return .Indirect
493+
case .owned: return .Owned
494+
case .unowned: return .Unowned
495+
case .unownedInnerPointer: return .UnownedInnerPointer
496+
case .autoreleased: return .Autoreleased
497+
case .pack: return .Pack
498+
case .guaranteed: return .Guaranteed
499+
case .guaranteedAddress: return .GuaranteedAddress
500+
}
501+
}
463502
}
464503

465504
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
}
@@ -1812,10 +1812,29 @@ final public class DeallocPackInst : Instruction, UnaryInstruction, Deallocation
18121812
final public class DeallocPackMetadataInst : Instruction, Deallocation {}
18131813

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

18201839
final public class TuplePackExtractInst: SingleValueInstruction {
18211840
public var indexOperand: Operand { operands[0] }
@@ -1840,7 +1859,7 @@ public class TermInst : Instruction {
18401859
let succArray = bridged.TermInst_getSuccessors()
18411860
return SuccessorArray(base: succArray.base, count: succArray.count)
18421861
}
1843-
1862+
18441863
public var isFunctionExiting: Bool { false }
18451864

18461865
public final func replaceBranchTarget(from fromBlock: BasicBlock, to toBlock: BasicBlock, _ context: some MutatingContext) {
@@ -1870,6 +1889,9 @@ final public class YieldInst : TermInst {
18701889
public func convention(of operand: Operand) -> ArgumentConvention {
18711890
return bridged.YieldInst_getConvention(operand.bridged).convention
18721891
}
1892+
1893+
public var resumeBlock: BasicBlock { bridged.YieldInst_getResumeBB().block }
1894+
public var unwindBlock: BasicBlock { bridged.YieldInst_getUnwindBB().block }
18731895
}
18741896

18751897
final public class UnwindInst : TermInst {
@@ -1979,11 +2001,11 @@ final public class AwaitAsyncContinuationInst : TermInst, UnaryInstruction {
19792001

19802002
public struct CheckedCastInstOptions {
19812003
var storage: UInt8 = 0
1982-
2004+
19832005
var bridged: BridgedInstruction.CheckedCastInstOptions {
19842006
.init(storage: storage)
19852007
}
1986-
2008+
19872009
var isolatedConformances: CastingIsolatedConformances {
19882010
return (storage & 0x01) != 0 ? .prohibit : .allow
19892011
}

0 commit comments

Comments
 (0)