Skip to content

Commit b3d31c7

Browse files
committed
Bridging: Implement bridges required for ongoing AutoDiff changes
In #83926, part of the changes resolving #68944 is submitted. The AutoDiff closure specialization optimizer pass relies on several bridges not implemented yet. This patch introduces these missing bridges. See the list of the changes below. **AST:** * Define `.asValueDecl` on each BridgedXXXDecl type that's also a ValueDecl. * Define `.asNominalTypeDecl` on each BridgedXXXDecl type that's also a NominalTypeDecl. * Define `.asGenericContext` on each BridgedXXXDecl type that's also a GenericContext. * `class BridgedSourceFile`: - Make nullable - `func addTopLevelDecl(_ decl: BridgedDecl)` * `class BridgedFileUnit`: - `func castToSourceFile() -> BridgedNullableSourceFile` * `class BridgedDecl`: - `func getDeclContext() -> BridgedDeclContext` * `class BridgedParamDecl`: - `func cloneWithoutType() -> BridgedParamDecl` - `func setInterfaceType(_ type : BridgedASTType)` * Define `BridgedDecl.setImplicit()` instead of `BridgedParamDecl.setImplicit()` * `class BridgedGenericContext`: - `setGenericSignature(_ genSig: BridgedGenericSignature)` * Change return type of `BridgedEnumDecl.createParsed(/*...*/)` from `BridgedNominalTypeDecl` to `BridgedEnumDecl` * `class BridgedValueDecl`: - `func setAccess(_ accessLevel: swift.AccessLevel)` * `class BridgedNominalTypeDecl`: - `func addMember(_ member: BridgedDecl)` * `class BridgedGenericTypeParamDecl`: - `func createImplicit(declContext: BridgedDeclContext, name: swift.Identifier, depth: UInt, index: UInt, paramKind: swift.GenericTypeParamKind)` * `class ValueDecl`: - `var baseIdentifier: swift.Identifier` * `class NominalTypeDecl`: - `var declaredInterfaceType: Type` * `class EnumElementDecl`: - `var parameterList: BridgedParameterList` - `var nameStr: StringRef` * `struct GenericSignature`: - `var canonicalSignature: CanGenericSignature` * `struct CanGenericSignature`: - `var isEmpty: Bool` - `var genericSignature: GenericSignature` * `struct Type`: - `func mapTypeOutOfContext() -> Type` - `func getReducedType(sig: GenericSignature) -> CanonicalType` - `func GenericTypeParam_getName() -> swift.Identifier` - `func GenericTypeParam_getDepth() -> UInt` - `func GenericTypeParam_getIndex() -> UInt` - `func GenericTypeParam_getParamKind() -> swift.GenericTypeParamKind` * `struct CanonicalType`: - `func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature` - `func loweredType(in function: SIL.Function) -> SIL.Type` **SIL:** * `class Argument`: - `func replaceAllUsesWith(newArg: Argument)` * `class BasicBlock`: - `func insertPhiArgument(atPosition: Int, type: Type, ownership: Ownership, _ context: some MutatingContext) -> Argument` * `struct Builder`: - `func createTuple(elements: [Value]) -> TupleInst` * `protocol Context`: - `func getTupleType(elements: [AST.Type]) -> AST.Type` - `func getTupleTypeWithLabels(elements: [AST.Type], labels: [swift.Identifier]) -> AST.Type` * `class Function`: - `var sourceFile: BridgedNullableSourceFile` - `func mapTypeIntoContext(_ type: Type) -> Type` * `class PartialApplyInst`: - `var substitutionMap: SubstitutionMap` * `class SwitchEnumInst`: - `var numCases: Int` - `public func getSuccessorForDefault() -> BasicBlock?` * `Type`: - `var category: ValueCategory` - `func getEnumCasePayload(caseIdx: Int, function: Function) -> Type` - `func mapTypeOutOfContext() -> Type` - `static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type` * `struct EnumCase`: - `let enumElementDecl: EnumElementDecl` * `struct TupleElementArray`: - `func label(at index: Int) -> swift.Identifier` * Define `enum ValueCategory` with `address` and `object` elements
1 parent 876fd51 commit b3d31c7

21 files changed

+561
-11
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ public class Decl: CustomStringConvertible, Hashable {
3939
public class ValueDecl: Decl {
4040
final public var nameLoc: SourceLoc? { SourceLoc(bridged: bridged.Value_getNameLoc()) }
4141
final public var userFacingName: StringRef { StringRef(bridged: bridged.Value_getUserFacingName()) }
42+
final public var baseIdentifier: swift.Identifier { bridged.Value_getBaseIdentifier() }
4243
final public var isObjC: Bool { bridged.Value_isObjC() }
4344
}
4445

@@ -56,6 +57,10 @@ public class NominalTypeDecl: GenericTypeDecl {
5657
final public var valueTypeDestructor: DestructorDecl? {
5758
bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self)
5859
}
60+
61+
public var declaredInterfaceType : AST.`Type` {
62+
AST.`Type`(bridged: bridged.NominalType_getDeclaredInterfaceType())
63+
}
5964
}
6065

6166
final public class EnumDecl: NominalTypeDecl {
@@ -118,6 +123,8 @@ final public class MacroDecl: ValueDecl {}
118123

119124
final public class EnumElementDecl: ValueDecl {
120125
public var hasAssociatedValues: Bool { bridged.EnumElementDecl_hasAssociatedValues() }
126+
public var parameterList: BridgedParameterList { bridged.EnumElementDecl_getParameterList() }
127+
public var nameStr: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) }
121128
}
122129

123130
final public class ExtensionDecl: Decl {}

SwiftCompilerSources/Sources/AST/GenericSignature.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,4 +35,18 @@ public struct GenericSignature: CustomStringConvertible, NoReflectionChildren {
3535
}
3636

3737
public var isEmpty: Bool { bridged.impl == nil }
38+
39+
public var canonicalSignature: CanGenericSignature { CanGenericSignature(bridged: bridged.getCanonicalSignature()) }
40+
}
41+
42+
public struct CanGenericSignature {
43+
public let bridged: BridgedCanGenericSignature
44+
45+
public init(bridged: BridgedCanGenericSignature) {
46+
self.bridged = bridged
47+
}
48+
49+
public var isEmpty: Bool { bridged.impl == nil }
50+
51+
public var genericSignature: GenericSignature { GenericSignature(bridged: bridged.getGenericSignature()) }
3852
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,30 @@ public struct Type: TypeProperties, CustomStringConvertible, NoReflectionChildre
6666
public func subst(with substitutionMap: SubstitutionMap) -> Type {
6767
return Type(bridged: bridged.subst(substitutionMap.bridged))
6868
}
69+
70+
public func mapTypeOutOfContext() -> Type {
71+
return Type(bridged: bridged.mapTypeOutOfContext())
72+
}
73+
74+
public func getReducedType(sig: GenericSignature) -> CanonicalType {
75+
CanonicalType(bridged: bridged.getReducedType(sig.bridged))
76+
}
77+
78+
public func GenericTypeParam_getName() -> swift.Identifier {
79+
return bridged.GenericTypeParam_getName()
80+
}
81+
82+
public func GenericTypeParam_getDepth() -> UInt {
83+
return bridged.GenericTypeParam_getDepth()
84+
}
85+
86+
public func GenericTypeParam_getIndex() -> UInt {
87+
return bridged.GenericTypeParam_getIndex()
88+
}
89+
90+
public func GenericTypeParam_getParamKind() -> swift.GenericTypeParamKind {
91+
return bridged.GenericTypeParam_getParamKind()
92+
}
6993
}
7094

7195
/// A Type that is statically known to be canonical.
@@ -86,6 +110,10 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti
86110
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
87111
return rawType.subst(with: substitutionMap).canonical
88112
}
113+
114+
public func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature {
115+
CanGenericSignature(bridged: bridged.SILFunctionType_getSubstGenericSignature())
116+
}
89117
}
90118

91119
/// Implements the common members of `AST.Type`, `AST.CanonicalType` and `SIL.Type`.

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,10 @@ extension CanonicalType {
3636
precondition(isBox)
3737
return BoxFieldsArray(boxType: self, function: function)
3838
}
39+
40+
public func loweredType(in function: Function) -> Type {
41+
function.bridged.getLoweredType(bridged).type
42+
}
3943
}
4044

4145
extension Decl {

SwiftCompilerSources/Sources/SIL/Argument.swift

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ public class Argument : Value, Hashable {
5050

5151
public var sourceLoc: SourceLoc? { findVarDecl()?.nameLoc }
5252

53+
public func replaceAllUsesWith(newArg: Argument) {
54+
bridged.replaceAllUsesWith(newArg.bridged)
55+
}
56+
5357
public static func ==(lhs: Argument, rhs: Argument) -> Bool {
5458
lhs === rhs
5559
}

SwiftCompilerSources/Sources/SIL/BasicBlock.swift

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,11 @@ final public class BasicBlock : CustomStringConvertible, HasShortDescription, Ha
6868
(decl as Decl?).bridged).argument as! FunctionArgument
6969
}
7070

71+
public func insertPhiArgument(atPosition: Int, type: Type, ownership: Ownership, _ context: some MutatingContext) -> Argument {
72+
context.notifyInstructionsChanged()
73+
return bridged.insertPhiArgument(atPosition, type.bridged, ownership._bridged).argument
74+
}
75+
7176
public func eraseArgument(at index: Int, _ context: some MutatingContext) {
7277
context.notifyInstructionsChanged()
7378
bridged.eraseArgument(index)

SwiftCompilerSources/Sources/SIL/Builder.swift

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,13 @@ public struct Builder {
662662
return notifyNew(tuple.getAs(TupleInst.self))
663663
}
664664

665+
public func createTuple(elements: [Value]) -> TupleInst {
666+
let tuple = elements.withBridgedValues { valuesRef in
667+
return bridged.createTuple(valuesRef)
668+
}
669+
return notifyNew(tuple.getAs(TupleInst.self))
670+
}
671+
665672
public func createTupleExtract(tuple: Value, elementIndex: Int) -> TupleExtractInst {
666673
return notifyNew(bridged.createTupleExtract(tuple.bridged, elementIndex).getAs(TupleExtractInst.self))
667674
}

SwiftCompilerSources/Sources/SIL/Context.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,20 @@ extension Context {
5555
}
5656
}
5757

58+
public func getTupleType(elements: [AST.`Type`]) -> AST.`Type` {
59+
let bridgedElements = elements.map { $0.bridged }
60+
return bridgedElements.withBridgedArrayRef {
61+
AST.`Type`(bridged: _bridged.getTupleType($0))
62+
}
63+
}
64+
65+
public func getTupleTypeWithLabels(elements: [AST.`Type`], labels: [swift.Identifier]) -> AST.`Type` {
66+
assert(elements.count == labels.count)
67+
return elements.withBridgedArrayRef{
68+
eltArr in labels.withBridgedArrayRef{labelsArr in
69+
AST.`Type`(bridged: _bridged.getTupleTypeWithLabels(eltArr, labelsArr))}}
70+
}
71+
5872
public var swiftArrayDecl: NominalTypeDecl {
5973
_bridged.getSwiftArrayDecl().getAs(NominalTypeDecl.self)
6074
}

SwiftCompilerSources/Sources/SIL/Function.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
2626
return Location(bridged: bridged.getLocation())
2727
}
2828

29+
public var sourceFile: BridgedNullableSourceFile {
30+
return bridged.getSourceFile()
31+
}
32+
2933
final public var description: String {
3034
return String(taking: bridged.getDebugDescription())
3135
}
@@ -88,6 +92,10 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
8892
return AST.`Type`(bridged: bridged.mapTypeIntoContext(type.bridged))
8993
}
9094

95+
public func mapTypeIntoContext(_ type: Type) -> Type {
96+
return Type(bridged: bridged.mapTypeIntoContext(type.bridged))
97+
}
98+
9199
/// Returns true if the function is a definition and not only an external declaration.
92100
///
93101
/// This is the case if the function contains a body, i.e. some basic blocks.

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1351,6 +1351,7 @@ final public class PartialApplyInst : SingleValueInstruction, ApplySite {
13511351
public var hasUnknownResultIsolation: Bool { bridged.PartialApplyInst_hasUnknownResultIsolation() }
13521352
public var unappliedArgumentCount: Int { bridged.PartialApply_getCalleeArgIndexOfFirstAppliedArg() }
13531353
public var calleeConvention: ArgumentConvention { type.bridged.getCalleeConvention().convention }
1354+
public var substitutionMap: SubstitutionMap { SubstitutionMap(bridged: bridged.PartialApplyInst_getSubstitutionMap()) }
13541355
}
13551356

13561357
final public class ApplyInst : SingleValueInstruction, FullApplySite {
@@ -1929,6 +1930,7 @@ final public class SwitchValueInst : TermInst {
19291930
final public class SwitchEnumInst : TermInst {
19301931

19311932
public var enumOp: Value { operands[0].value }
1933+
public var numCases: Int { bridged.SwitchEnumInst_getNumCases() }
19321934

19331935
public struct CaseIndexArray : RandomAccessCollection {
19341936
fileprivate let switchEnum: SwitchEnumInst
@@ -1953,6 +1955,10 @@ final public class SwitchEnumInst : TermInst {
19531955
cases.first(where: { $0.0 == forCaseIndex })?.1
19541956
}
19551957

1958+
public func getSuccessorForDefault() -> BasicBlock? {
1959+
return self.bridged.SwitchEnumInst_getSuccessorForDefault().block
1960+
}
1961+
19561962
// This does not handle the special case where the default covers exactly
19571963
// the "missing" case.
19581964
public func getUniqueCase(forSuccessor: BasicBlock) -> Int? {

0 commit comments

Comments
 (0)