Skip to content

[Serialization] Serialize SILBoxType substitutions directly. #6573

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions include/swift/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
/// in source control, you should also update the comment to briefly
/// describe what change you made. The content of this comment isn't important;
/// it just ensures a conflict if two people change the module format.
const uint16_t VERSION_MINOR = 301; // Last change: Deterministic conformances
const uint16_t VERSION_MINOR = 302; // Last change: SIL box type substitutions

using DeclID = PointerEmbeddedInt<unsigned, 31>;
using DeclIDField = BCFixed<31>;
Expand Down Expand Up @@ -716,8 +716,8 @@ namespace decls_block {

using SILBoxTypeLayout = BCRecordLayout<
SIL_BOX_TYPE,
SILLayoutIDField, // layout
BCArray<TypeIDField> // generic arguments
SILLayoutIDField // layout
// trailing substitutions
>;

template <unsigned Code>
Expand Down
56 changes: 13 additions & 43 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3977,9 +3977,8 @@ Type ModuleFile::getType(TypeID TID) {

case decls_block::SIL_BOX_TYPE: {
SILLayoutID layoutID;
ArrayRef<uint64_t> args;

decls_block::SILBoxTypeLayout::readRecord(scratch, layoutID, args);
decls_block::SILBoxTypeLayout::readRecord(scratch, layoutID);

// Get the layout.
auto getLayout = [&]() -> SILLayout * {
Expand Down Expand Up @@ -4008,49 +4007,20 @@ Type ModuleFile::getType(TypeID TID) {

SmallVector<Substitution, 4> genericArgs;
if (auto sig = layout->getGenericSignature()) {
if (args.size() != sig->getAllDependentTypes().size()) {
error();
return nullptr;
}
TypeSubstitutionMap mappings;

for (unsigned i : indices(sig->getGenericParams())) {
mappings[sig->getGenericParams()[i]] =
getType(args[i])->getCanonicalType();
}

bool ok = true;
sig->getSubstitutions(mappings,
[&](CanType depTy, Type replacementTy, ProtocolType *proto)
-> ProtocolConformanceRef {
if (replacementTy->is<SubstitutableType>()
|| replacementTy->is<DependentMemberType>())
return ProtocolConformanceRef(proto->getDecl());

auto conformance = getAssociatedModule()
->lookupConformance(replacementTy, proto->getDecl(), nullptr);
if (!conformance) {
error();
ok = false;
return ProtocolConformanceRef(proto->getDecl());
}
return *conformance;
},
genericArgs);
if (!ok)
return nullptr;

for (auto &arg : genericArgs) {
arg = Substitution(arg.getReplacement()->getCanonicalType(),
arg.getConformances());
}
} else {
if (args.size() != 0) {
error();
return nullptr;
for (unsigned i : range(sig->getAllDependentTypes().size())) {
(void)i;
auto sub = maybeReadSubstitution(DeclTypeCursor);
if (!sub) {
error();
return nullptr;
}

genericArgs.push_back(
Substitution(sub->getReplacement()->getCanonicalType(),
sub->getConformances()));
}
}

typeOrOffset = SILBoxType::get(getContext(), layout, genericArgs);
break;
}
Expand Down
19 changes: 12 additions & 7 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3185,13 +3185,18 @@ void Serializer::writeType(Type ty) {

unsigned abbrCode = DeclTypeAbbrCodes[SILBoxTypeLayout::Code];
SILLayoutID layoutRef = addSILLayoutRef(boxTy->getLayout());

SmallVector<TypeID, 4> genericArgs;
for (auto &arg : boxTy->getGenericArgs())
genericArgs.push_back(addTypeRef(arg.getReplacement()));

SILBoxTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,
layoutRef, genericArgs);

#ifndef NDEBUG
if (auto sig = boxTy->getLayout()->getGenericSignature()) {
assert(sig->getAllDependentTypes().size()
== boxTy->getGenericArgs().size());
}
#endif

SILBoxTypeLayout::emitRecord(Out, ScratchRecord, abbrCode, layoutRef);

// Write the set of substitutions.
writeSubstitutions(boxTy->getGenericArgs(), DeclTypeAbbrCodes);
break;
}

Expand Down
2 changes: 1 addition & 1 deletion test/Serialization/sil_box_types.sil
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ sil @boxes_extra_reqs : $@convention(thin) <T where T : Q, T.AT : P> (@owned <τ
bb0(%0 : $<τ_0_0 where τ_0_0 : Q, τ_0_0.AT : P> { var τ_0_0 } <T, T.AT>):
%1 = project_box %0 : $<τ_0_0 where τ_0_0 : Q, τ_0_0.AT : P> { var τ_0_0 } <T, T.AT>, 0
return undef : $()
}
}