Skip to content

Commit 1545e01

Browse files
committed
SIL: Let SubstitutionMap.replacementTypes return AST types rather than optional SIL types.
This is what the C++ SubstitutionMap does. One has to use `Type.loweredType` to get from the AST type to the SIL type.
1 parent 1f4332a commit 1545e01

File tree

10 files changed

+62
-61
lines changed

10 files changed

+62
-61
lines changed

SwiftCompilerSources/Sources/AST/SubstitutionMap.swift

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,14 @@ public struct SubstitutionMap: CustomStringConvertible {
5656
return Conformance(bridged: bridgedSubs.getConformance(index))
5757
}
5858
}
59+
60+
public var replacementTypes: TypeArray {
61+
TypeArray(bridged: bridged.getReplacementTypes())
62+
}
63+
64+
/// The single replacement type if it's guarnateed that the substitution map has a single replacement type.
65+
public var replacementType: Type {
66+
assert(replacementTypes.count == 1)
67+
return replacementTypes[0]
68+
}
5969
}

SwiftCompilerSources/Sources/AST/Type.swift

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,3 +64,23 @@ public struct CanonicalType: CustomStringConvertible, NoReflectionChildren {
6464
return type.subst(with: substitutionMap).canonical
6565
}
6666
}
67+
68+
public struct TypeArray : RandomAccessCollection, CustomReflectable {
69+
public let bridged: BridgedASTTypeArray
70+
71+
public var startIndex: Int { return 0 }
72+
public var endIndex: Int { return bridged.getCount() }
73+
74+
public init(bridged: BridgedASTTypeArray) {
75+
self.bridged = bridged
76+
}
77+
78+
public subscript(_ index: Int) -> Type {
79+
Type(bridged: bridged.getAt(index))
80+
}
81+
82+
public var customMirror: Mirror {
83+
let c: [Mirror.Child] = map { (label: nil, value: $0) }
84+
return Mirror(self, children: c)
85+
}
86+
}

SwiftCompilerSources/Sources/Optimizer/FunctionPasses/AllocVectorLowering.swift

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -224,9 +224,10 @@ private func createOutlinedGlobal(
224224
return
225225
}
226226

227-
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0]!
227+
let function = allocVectorBuiltin.parentFunction
228+
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0].loweredType(in: function)
228229
let outlinedGlobal = context.createGlobalVariable(
229-
name: context.mangleOutlinedVariable(from: allocVectorBuiltin.parentFunction),
230+
name: context.mangleOutlinedVariable(from: function),
230231
type: elementType, linkage: .private, isLet: false)
231232

232233
let globalBuilder = Builder(staticInitializerOf: outlinedGlobal, context)
@@ -259,7 +260,8 @@ private func createStackAllocatedVector(
259260
_ context: FunctionPassContext
260261
) {
261262
let builder = Builder(before: allocVectorBuiltin, context)
262-
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0]!
263+
let function = allocVectorBuiltin.parentFunction
264+
let elementType = allocVectorBuiltin.substitutionMap.replacementTypes[0].loweredType(in: function)
263265
let allocVec = builder.createAllocVector(capacity: allocVectorBuiltin.operands[1].value, elementType: elementType)
264266
let rawVectorPointer = builder.createAddressToPointer(address: allocVec, pointerType: allocVectorBuiltin.type,
265267
needStackProtection: true)

SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyBuiltin.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ extension BuiltinInst : OnoneSimplifyable {
3333
.Alignof:
3434
optimizeTargetTypeConst(context)
3535
case .DestroyArray:
36-
if let elementType = substitutionMap.replacementTypes[0],
36+
if let elementType = substitutionMap.replacementTypes[0].canonical.silType,
3737
elementType.isTrivial(in: parentFunction)
3838
{
3939
context.erase(instruction: self)
@@ -129,7 +129,7 @@ private extension BuiltinInst {
129129
}
130130

131131
func optimizeCanBeClass(_ context: SimplifyContext) {
132-
guard let ty = substitutionMap.replacementTypes[0] else {
132+
guard let ty = substitutionMap.replacementTypes[0].canonical.silType else {
133133
return
134134
}
135135
let literal: IntegerLiteralInst
@@ -167,7 +167,7 @@ private extension BuiltinInst {
167167
}
168168

169169
func optimizeTargetTypeConst(_ context: SimplifyContext) {
170-
guard let ty = substitutionMap.replacementTypes[0] else {
170+
guard let ty = substitutionMap.replacementTypes[0].canonical.silType else {
171171
return
172172
}
173173

SwiftCompilerSources/Sources/SIL/ASTExtensions.swift

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -58,9 +58,4 @@ extension SubstitutionMap {
5858
public func getMethodSubstitutions(for method: Function) -> SubstitutionMap {
5959
return SubstitutionMap(bridged: method.bridged.getMethodSubstitutions(bridged))
6060
}
61-
62-
public var replacementTypes: OptionalTypeArray {
63-
let types = BridgedTypeArray.fromReplacementTypes(bridged)
64-
return OptionalTypeArray(bridged: types)
65-
}
6661
}

SwiftCompilerSources/Sources/SIL/Type.swift

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -243,26 +243,6 @@ public struct TypeArray : RandomAccessCollection, CustomReflectable {
243243
}
244244
}
245245

246-
public struct OptionalTypeArray : RandomAccessCollection, CustomReflectable {
247-
private let bridged: BridgedTypeArray
248-
249-
public var startIndex: Int { return 0 }
250-
public var endIndex: Int { return bridged.getCount() }
251-
252-
public init(bridged: BridgedTypeArray) {
253-
self.bridged = bridged
254-
}
255-
256-
public subscript(_ index: Int) -> Type? {
257-
bridged.getAt(index).typeOrNil
258-
}
259-
260-
public var customMirror: Mirror {
261-
let c: [Mirror.Child] = map { (label: nil, value: $0 ?? "<invalid>") }
262-
return Mirror(self, children: c)
263-
}
264-
}
265-
266246
public struct NominalFieldsArray : RandomAccessCollection, FormattedLikeArray {
267247
fileprivate let type: Type
268248
fileprivate let function: Function

include/swift/AST/ASTBridging.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2293,6 +2293,15 @@ class BridgedCanType {
22932293
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTType getType() const;
22942294
};
22952295

2296+
struct BridgedASTTypeArray {
2297+
BridgedArrayRef typeArray;
2298+
2299+
SwiftInt getCount() const { return SwiftInt(typeArray.Length); }
2300+
2301+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
2302+
BridgedASTType getAt(SwiftInt index) const;
2303+
};
2304+
22962305
struct BridgedConformance {
22972306
void * _Nullable opaqueValue;
22982307

@@ -2330,6 +2339,7 @@ struct BridgedSubstitutionMap {
23302339
BRIDGED_INLINE bool hasAnySubstitutableParams() const;
23312340
BRIDGED_INLINE SwiftInt getNumConformances() const;
23322341
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedConformance getConformance(SwiftInt index) const;
2342+
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE BridgedASTTypeArray getReplacementTypes() const;
23332343
};
23342344

23352345
struct BridgedFingerprint {

include/swift/AST/ASTBridgingImpl.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,15 @@ BridgedASTType BridgedCanType::getType() const {
338338
return {type};
339339
}
340340

341+
//===----------------------------------------------------------------------===//
342+
// MARK: BridgedASTTypeArray
343+
//===----------------------------------------------------------------------===//
344+
345+
BridgedASTType BridgedASTTypeArray::getAt(SwiftInt index) const {
346+
return {typeArray.unbridged<swift::Type>()[index].getPointer()};
347+
}
348+
349+
341350
//===----------------------------------------------------------------------===//
342351
// MARK: BridgedConformance
343352
//===----------------------------------------------------------------------===//
@@ -462,6 +471,10 @@ BridgedConformance BridgedSubstitutionMap::getConformance(SwiftInt index) const
462471
return unbridged().getConformances()[index];
463472
}
464473

474+
BridgedASTTypeArray BridgedSubstitutionMap::getReplacementTypes() const {
475+
return {unbridged().getReplacementTypes()};
476+
}
477+
465478
//===----------------------------------------------------------------------===//
466479
// MARK: BridgedFingerprint
467480
//===----------------------------------------------------------------------===//

include/swift/SIL/SILBridging.h

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -614,18 +614,6 @@ struct BridgedMultiValueResult {
614614
BRIDGED_INLINE SwiftInt getIndex() const;
615615
};
616616

617-
struct BridgedTypeArray {
618-
BridgedArrayRef typeArray;
619-
620-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
621-
static BridgedTypeArray fromReplacementTypes(BridgedSubstitutionMap substMap);
622-
623-
SwiftInt getCount() const { return SwiftInt(typeArray.Length); }
624-
625-
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE
626-
BridgedType getAt(SwiftInt index) const;
627-
};
628-
629617
struct BridgedSILTypeArray {
630618
BridgedArrayRef typeArray;
631619

include/swift/SIL/SILBridgingImpl.h

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -988,24 +988,7 @@ SwiftInt BridgedMultiValueResult::getIndex() const {
988988
}
989989

990990
//===----------------------------------------------------------------------===//
991-
// BridgedTypeArray
992-
//===----------------------------------------------------------------------===//
993-
994-
BridgedTypeArray
995-
BridgedTypeArray::fromReplacementTypes(BridgedSubstitutionMap substMap) {
996-
return {substMap.unbridged().getReplacementTypes()};
997-
}
998-
999-
BridgedType BridgedTypeArray::getAt(SwiftInt index) const {
1000-
swift::Type origTy = typeArray.unbridged<swift::Type>()[index];
1001-
auto ty = origTy->getCanonicalType();
1002-
if (ty->isLegalSILType())
1003-
return swift::SILType::getPrimitiveObjectType(ty);
1004-
return swift::SILType();
1005-
}
1006-
1007-
//===----------------------------------------------------------------------===//
1008-
// BridgedTypeArray
991+
// BridgedSILTypeArray
1009992
//===----------------------------------------------------------------------===//
1010993

1011994
BridgedType BridgedSILTypeArray::getAt(SwiftInt index) const {

0 commit comments

Comments
 (0)