Skip to content

Commit 41fd71c

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 668e446 commit 41fd71c

21 files changed

+576
-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: 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/Builder.swift

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -662,6 +662,22 @@ 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+
672+
public func createPayloadTupleForBranchTracingEnum(elements: [Value], tupleWithLabels: Type)
673+
-> TupleInst
674+
{
675+
let tuple = elements.withBridgedValues { valuesRef in
676+
return bridged.createPayloadTupleForBranchTracingEnum(valuesRef, tupleWithLabels.bridged)
677+
}
678+
return notifyNew(tuple.getAs(TupleInst.self))
679+
}
680+
665681
public func createTupleExtract(tuple: Value, elementIndex: Int) -> TupleExtractInst {
666682
return notifyNew(bridged.createTupleExtract(tuple.bridged, elementIndex).getAs(TupleExtractInst.self))
667683
}

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)