Skip to content

Commit 5a987d7

Browse files
committed
Bridging: Implement bridges required for ongoing AutoDiff changes
In swiftlang#83926, part of the changes resolving swiftlang#68944 is submitted. The AutoDiff closure specialization optimizer pass relies on several AST-level bridges not implemented yet. This patch introduces these missing bridges.
1 parent a2d92d2 commit 5a987d7

File tree

19 files changed

+473
-11
lines changed

19 files changed

+473
-11
lines changed

SwiftCompilerSources/Sources/AST/Declarations.swift

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,10 @@ public class NominalTypeDecl: GenericTypeDecl {
5656
final public var valueTypeDestructor: DestructorDecl? {
5757
bridged.NominalType_getValueTypeDestructor().getAs(DestructorDecl.self)
5858
}
59+
60+
public var declaredInterfaceType : AST.`Type` {
61+
AST.`Type`(bridged: bridged.NominalType_getDeclaredInterfaceType())
62+
}
5963
}
6064

6165
final public class EnumDecl: NominalTypeDecl {
@@ -118,6 +122,8 @@ final public class MacroDecl: ValueDecl {}
118122

119123
final public class EnumElementDecl: ValueDecl {
120124
public var hasAssociatedValues: Bool { bridged.EnumElementDecl_hasAssociatedValues() }
125+
public var parameterList: BridgedParameterList { bridged.EnumElementDecl_getParameterList() }
126+
public var name: StringRef { StringRef(bridged: bridged.EnumElementDecl_getNameStr()) }
121127
}
122128

123129
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: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,14 @@ 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+
}
6977
}
7078

7179
/// A Type that is statically known to be canonical.
@@ -86,6 +94,10 @@ public struct CanonicalType: TypeProperties, CustomStringConvertible, NoReflecti
8694
public func subst(with substitutionMap: SubstitutionMap) -> CanonicalType {
8795
return rawType.subst(with: substitutionMap).canonical
8896
}
97+
98+
public func SILFunctionType_getSubstGenericSignature() -> CanGenericSignature {
99+
CanGenericSignature(bridged: bridged.SILFunctionType_getSubstGenericSignature())
100+
}
89101
}
90102

91103
/// 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/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: 2 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

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
2727

2828
public var isAddress: Bool { bridged.isAddress() }
2929
public var isObject: Bool { !isAddress }
30+
public var category: ValueCategory { ValueCategory(bridged: bridged.getCategory()) }
3031

3132
public var addressType: Type { bridged.getAddressType().type }
3233
public var objectType: Type { bridged.getObjectType().type }
@@ -214,6 +215,16 @@ public struct Type : TypeProperties, CustomStringConvertible, NoReflectionChildr
214215
}
215216
return false
216217
}
218+
219+
public func getEnumCasePayload(caseIdx: Int, function: Function) -> Type {
220+
bridged.getEnumCasePayload(caseIdx, function.bridged).type
221+
}
222+
223+
public func mapTypeOutOfContext() -> Type { bridged.mapTypeOutOfContext().type }
224+
225+
public static func getPrimitiveType(canType: CanonicalType, silValueCategory: ValueCategory) -> Type {
226+
BridgedType.getPrimitiveType(canType: canType.bridged, silValueCategory: silValueCategory._bridged).type
227+
}
217228
}
218229

219230
extension Type: Equatable {
@@ -266,6 +277,7 @@ public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
266277
}
267278

268279
public struct EnumCase {
280+
public let enumElementDecl : EnumElementDecl
269281
public let payload: Type?
270282
public let index: Int
271283
}
@@ -288,7 +300,8 @@ public struct EnumCases : CollectionLikeSequence, IteratorProtocol {
288300
caseIterator = caseIterator.getNext()
289301
caseIndex += 1
290302
}
291-
return EnumCase(payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
303+
return EnumCase(enumElementDecl: enumType.bridged.getEnumElementDecl(caseIterator).getAs(EnumElementDecl.self),
304+
payload: enumType.bridged.getEnumCasePayload(caseIterator, function.bridged).typeOrNil,
292305
index: caseIndex)
293306
}
294307
return nil
@@ -304,6 +317,10 @@ public struct TupleElementArray : RandomAccessCollection, FormattedLikeArray {
304317
public subscript(_ index: Int) -> Type {
305318
type.bridged.getTupleElementType(index).type
306319
}
320+
321+
public func label(at index: Int) -> swift.Identifier {
322+
type.bridged.getTupleElementLabel(index)
323+
}
307324
}
308325

309326
public struct BoxFieldsArray : RandomAccessCollection, FormattedLikeArray {

0 commit comments

Comments
 (0)