Skip to content

Commit 0e31d41

Browse files
committed
AST: Move GenericSignature::typeErased() to SIL
This is used by the specializer and belongs at the SIL level.
1 parent a34ec19 commit 0e31d41

File tree

5 files changed

+63
-59
lines changed

5 files changed

+63
-59
lines changed

include/swift/AST/GenericSignature.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,9 +225,6 @@ class GenericSignature {
225225
/// requirement signature against the protocol generic signature
226226
/// <Self where Self : P>.
227227
void verify(ArrayRef<Requirement> reqts) const;
228-
229-
/// Returns a new signature with the given parameters erased
230-
GenericSignature typeErased(ArrayRef<Type> typeErasedParams) const;
231228
};
232229

233230
/// A reference to a canonical generic signature.

include/swift/SIL/SILFunction.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,9 @@ class SILSpecializeAttr final {
9999
Partial
100100
};
101101

102+
static GenericSignature buildTypeErasedSignature(
103+
GenericSignature sig, ArrayRef<Type> typeErasedParams);
104+
102105
static SILSpecializeAttr *create(SILModule &M,
103106
GenericSignature specializedSignature,
104107
ArrayRef<Type> typeErasedParams,

lib/AST/GenericSignature.cpp

Lines changed: 0 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -500,60 +500,6 @@ CanType GenericSignature::getReducedType(Type type) const {
500500
return getPointer()->getReducedType(type);
501501
}
502502

503-
GenericSignature GenericSignature::typeErased(ArrayRef<Type> typeErasedParams) const {
504-
bool changedSignature = false;
505-
llvm::SmallVector<Requirement, 2> requirementsErased;
506-
auto &C = Ptr->getASTContext();
507-
508-
for (auto req : getRequirements()) {
509-
bool found = std::any_of(typeErasedParams.begin(),
510-
typeErasedParams.end(),
511-
[&](Type t) {
512-
auto other = req.getFirstType();
513-
return t->isEqual(other);
514-
});
515-
if (found && req.getKind() == RequirementKind::Layout) {
516-
auto layout = req.getLayoutConstraint();
517-
if (layout->isClass()) {
518-
requirementsErased.push_back(Requirement(RequirementKind::SameType,
519-
req.getFirstType(),
520-
C.getAnyObjectType()));
521-
} else if (layout->isBridgeObject()) {
522-
requirementsErased.push_back(Requirement(RequirementKind::SameType,
523-
req.getFirstType(),
524-
C.TheBridgeObjectType));
525-
} else if (layout->isFixedSizeTrivial()) {
526-
unsigned bitWidth = layout->getTrivialSizeInBits();
527-
requirementsErased.push_back(
528-
Requirement(RequirementKind::SameType, req.getFirstType(),
529-
CanType(BuiltinIntegerType::get(bitWidth, C))));
530-
} else if (layout->isTrivialStride()) {
531-
requirementsErased.push_back(
532-
Requirement(RequirementKind::SameType, req.getFirstType(),
533-
CanType(BuiltinVectorType::get(
534-
Ptr->getASTContext(),
535-
BuiltinIntegerType::get(8, Ptr->getASTContext()),
536-
layout->getTrivialStride()))));
537-
} else {
538-
requirementsErased.push_back(req);
539-
}
540-
} else {
541-
requirementsErased.push_back(req);
542-
}
543-
changedSignature |= found;
544-
}
545-
546-
if (changedSignature) {
547-
return buildGenericSignature(
548-
Ptr->getASTContext(), GenericSignature(),
549-
SmallVector<GenericTypeParamType *>(getGenericParams()),
550-
requirementsErased,
551-
/*allowInverses=*/false);
552-
}
553-
554-
return *this;
555-
}
556-
557503
CanType GenericSignatureImpl::getReducedType(Type type) const {
558504
type = type->getCanonicalType();
559505

lib/SIL/IR/SILFunction.cpp

Lines changed: 58 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,61 @@
3939
using namespace swift;
4040
using namespace Lowering;
4141

42+
GenericSignature SILSpecializeAttr::buildTypeErasedSignature(
43+
GenericSignature sig, ArrayRef<Type> typeErasedParams) {
44+
bool changedSignature = false;
45+
llvm::SmallVector<Requirement, 2> requirementsErased;
46+
auto &C = sig->getASTContext();
47+
48+
for (auto req : sig.getRequirements()) {
49+
bool found = std::any_of(typeErasedParams.begin(),
50+
typeErasedParams.end(),
51+
[&](Type t) {
52+
auto other = req.getFirstType();
53+
return t->isEqual(other);
54+
});
55+
if (found && req.getKind() == RequirementKind::Layout) {
56+
auto layout = req.getLayoutConstraint();
57+
if (layout->isClass()) {
58+
requirementsErased.push_back(Requirement(RequirementKind::SameType,
59+
req.getFirstType(),
60+
C.getAnyObjectType()));
61+
} else if (layout->isBridgeObject()) {
62+
requirementsErased.push_back(Requirement(RequirementKind::SameType,
63+
req.getFirstType(),
64+
C.TheBridgeObjectType));
65+
} else if (layout->isFixedSizeTrivial()) {
66+
unsigned bitWidth = layout->getTrivialSizeInBits();
67+
requirementsErased.push_back(
68+
Requirement(RequirementKind::SameType, req.getFirstType(),
69+
CanType(BuiltinIntegerType::get(bitWidth, C))));
70+
} else if (layout->isTrivialStride()) {
71+
requirementsErased.push_back(
72+
Requirement(RequirementKind::SameType, req.getFirstType(),
73+
CanType(BuiltinVectorType::get(
74+
C,
75+
BuiltinIntegerType::get(8, C),
76+
layout->getTrivialStride()))));
77+
} else {
78+
requirementsErased.push_back(req);
79+
}
80+
} else {
81+
requirementsErased.push_back(req);
82+
}
83+
changedSignature |= found;
84+
}
85+
86+
if (changedSignature) {
87+
return buildGenericSignature(
88+
C, GenericSignature(),
89+
SmallVector<GenericTypeParamType *>(sig.getGenericParams()),
90+
requirementsErased,
91+
/*allowInverses=*/false);
92+
}
93+
94+
return sig;
95+
}
96+
4297
SILSpecializeAttr::SILSpecializeAttr(bool exported, SpecializationKind kind,
4398
GenericSignature specializedSig,
4499
GenericSignature unerasedSpecializedSig,
@@ -62,7 +117,9 @@ SILSpecializeAttr::create(SILModule &M, GenericSignature specializedSig,
62117
SILFunction *target, Identifier spiGroup,
63118
const ModuleDecl *spiModule,
64119
AvailabilityContext availability) {
65-
auto erasedSpecializedSig = specializedSig.typeErased(typeErasedParams);
120+
auto erasedSpecializedSig =
121+
SILSpecializeAttr::buildTypeErasedSignature(specializedSig,
122+
typeErasedParams);
66123

67124
void *buf = M.allocate(sizeof(SILSpecializeAttr), alignof(SILSpecializeAttr));
68125

lib/SIL/IR/SILSymbolVisitor.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -465,7 +465,8 @@ class SILSymbolVisitorImpl : public ASTVisitor<SILSymbolVisitorImpl> {
465465

466466
auto specializedSignature = attr->getSpecializedSignature(AFD);
467467
auto erasedSignature =
468-
specializedSignature.typeErased(attr->getTypeErasedParams());
468+
SILSpecializeAttr::buildTypeErasedSignature(specializedSignature,
469+
attr->getTypeErasedParams());
469470

470471
if (auto *targetFun = attr->getTargetFunctionDecl(AFD)) {
471472
addFunction(SILDeclRef(targetFun, erasedSignature),

0 commit comments

Comments
 (0)