Skip to content

Commit 69583c7

Browse files
committed
SIL: Sort associated types in witness table layouts
This allows them to be re-ordered resiliently.
1 parent 75db43b commit 69583c7

File tree

3 files changed

+13
-4
lines changed

3 files changed

+13
-4
lines changed

include/swift/SIL/SILWitnessVisitor.h

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,12 +58,21 @@ template <class T> class SILWitnessVisitor : public ASTVisitor<T> {
5858
if (haveAddedAssociatedTypes) return;
5959
haveAddedAssociatedTypes = true;
6060

61+
SmallVector<AssociatedTypeDecl *, 2> associatedTypes;
6162
for (Decl *member : protocol->getMembers()) {
6263
if (auto associatedType = dyn_cast<AssociatedTypeDecl>(member)) {
63-
// TODO: only add associated types when they're new?
64-
asDerived().addAssociatedType(AssociatedType(associatedType));
64+
associatedTypes.push_back(associatedType);
6565
}
6666
}
67+
68+
// Sort associated types by name, for resilience.
69+
llvm::array_pod_sort(associatedTypes.begin(), associatedTypes.end(),
70+
TypeDecl::compare);
71+
72+
for (auto *associatedType : associatedTypes) {
73+
// TODO: only add associated types when they're new?
74+
asDerived().addAssociatedType(AssociatedType(associatedType));
75+
}
6776
};
6877

6978
for (const auto &reqt : protocol->getRequirementSignature()) {

test/IRGen/associated_type_witness.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ protocol Assocked {
1212

1313
struct Universal : P, Q {}
1414

15-
// CHECK: [[ASSOC_TYPE_NAMES:@.*]] = private constant [29 x i8] c"OneAssoc TwoAssoc ThreeAssoc\00"
15+
// CHECK: [[ASSOC_TYPE_NAMES:@.*]] = private constant [29 x i8] c"OneAssoc ThreeAssoc TwoAssoc\00"
1616
// CHECK: @"$S23associated_type_witness18HasThreeAssocTypesMp" =
1717
// CHECK-SAME: [[ASSOC_TYPE_NAMES]] to i64
1818

test/IRGen/associated_types.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ func testFastRuncible<T: Runcible, U: FastRuncible>(_ t: T, u: U)
7575
// 1. Get the type metadata for U.RuncerType.Runcee.
7676
// 1a. Get the type metadata for U.RuncerType.
7777
// Note that we actually look things up in T, which is going to prove unfortunate.
78-
// CHECK: [[T0_GEP:%.*]] = getelementptr inbounds i8*, i8** %T.Runcible, i32 1
78+
// CHECK: [[T0_GEP:%.*]] = getelementptr inbounds i8*, i8** %T.Runcible, i32 2
7979
// CHECK: [[T0:%.*]] = load i8*, i8** [[T0_GEP]]
8080
// CHECK-NEXT: [[T1:%.*]] = bitcast i8* [[T0]] to %swift.metadata_response ([[INT]], %swift.type*, i8**)*
8181
// CHECK-NEXT: [[T2:%.*]] = call swiftcc %swift.metadata_response [[T1]]([[INT]] 0, %swift.type* %T, i8** %T.Runcible)

0 commit comments

Comments
 (0)