Skip to content

SIL serialization fixes for variadic generics #65155

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 3 commits into from
Apr 18, 2023
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
5 changes: 3 additions & 2 deletions lib/SILOptimizer/Transforms/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -523,7 +523,8 @@ class HashVisitor : public SILInstructionVisitor<HashVisitor, llvm::hash_code> {

hash_code visitDynamicPackIndexInst(DynamicPackIndexInst *X) {
return llvm::hash_combine(
X->getKind(), X->getIndexedPackType(), &X->getOperandRef());
X->getKind(), X->getIndexedPackType(),
tryLookThroughOwnershipInsts(&X->getOperandRef()));
}

hash_code visitTuplePackElementAddrInst(TuplePackElementAddrInst *X) {
Expand Down Expand Up @@ -585,7 +586,7 @@ bool llvm::DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS,
};
bool isEqual =
LHSI->getKind() == RHSI->getKind() && LHSI->isIdenticalTo(RHSI, opCmp);
#ifdef NDEBUG
#ifndef NDEBUG
if (isEqual && getHashValue(LHS) != getHashValue(RHS)) {
llvm::dbgs() << "LHS: ";
LHSI->dump();
Expand Down
1 change: 0 additions & 1 deletion lib/Serialization/DeclTypeRecordNodes.def
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,6 @@ TYPE(NAME_ALIAS)

TYPE(PACK_EXPANSION)
TYPE(PACK)
TRAILING_INFO(PACK_TYPE_ELT)
TYPE(SIL_PACK)

TYPE(ERROR)
Expand Down
31 changes: 9 additions & 22 deletions lib/Serialization/Deserialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6876,31 +6876,18 @@ Expected<Type> DESERIALIZE_TYPE(PACK_EXPANSION_TYPE)(

Expected<Type> DESERIALIZE_TYPE(PACK_TYPE)(
ModuleFile &MF, SmallVectorImpl<uint64_t> &scratch, StringRef blobData) {
// The pack record itself is empty. Read all trailing elements.
SmallVector<Type, 8> elements;
while (true) {
llvm::BitstreamEntry entry =
MF.fatalIfUnexpected(MF.DeclTypeCursor.advance(AF_DontPopBlockAtEnd));
if (entry.Kind != llvm::BitstreamEntry::Record)
break;

scratch.clear();
unsigned recordID = MF.fatalIfUnexpected(
MF.DeclTypeCursor.readRecord(entry.ID, scratch, &blobData));
if (recordID != decls_block::PACK_TYPE_ELT)
break;

TypeID typeID;
decls_block::PackTypeEltLayout::readRecord(scratch, typeID);

auto elementTy = MF.getTypeChecked(typeID);
if (!elementTy)
return elementTy.takeError();
ArrayRef<uint64_t> elementTypeIDs;
decls_block::PackTypeLayout::readRecord(scratch, elementTypeIDs);

elements.push_back(elementTy.get());
SmallVector<Type, 8> elementTypes;
for (auto elementTypeID : elementTypeIDs) {
auto elementType = MF.getTypeChecked(elementTypeID);
if (!elementType)
return elementType.takeError();
elementTypes.push_back(elementType.get());
}

return PackType::get(MF.getContext(), elements);
return PackType::get(MF.getContext(), elementTypes);
}

Expected<Type> DESERIALIZE_TYPE(SIL_PACK_TYPE)(
Expand Down
5 changes: 2 additions & 3 deletions lib/Serialization/DeserializeSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1285,11 +1285,10 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
RawOpCode = (unsigned)SILInstructionKind::HasSymbolInst;
break;
case SIL_PACK_ELEMENT_GET:
SILPackElementGetLayout::readRecord(scratch,
SILPackElementGetLayout::readRecord(scratch, RawOpCode,
TyID, TyCategory,
TyID2, TyCategory2, ValID2,
ValID3);
RawOpCode = (unsigned)SILInstructionKind::PackElementGetInst;
break;
case SIL_PACK_ELEMENT_SET:
SILPackElementSetLayout::readRecord(scratch,
Expand Down Expand Up @@ -1332,7 +1331,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
}
case SILInstructionKind::AllocPackInst: {
assert(RecordKind == SIL_ONE_TYPE && "Layout should be OneType.");
ResultInst = Builder.createAllocStack(
ResultInst = Builder.createAllocPack(
Loc, getSILType(MF->getType(TyID), (SILValueCategory)TyCategory, Fn));
break;
}
Expand Down
10 changes: 3 additions & 7 deletions lib/Serialization/ModuleFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
/// 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.
/// Don't worry about adhering to the 80-column limit for this line.
const uint16_t SWIFTMODULE_VERSION_MINOR = 758; // drop_deinit instruction
const uint16_t SWIFTMODULE_VERSION_MINOR = 759; // change PackType

/// A standard hash seed used for all string hashes in a serialized module.
///
Expand Down Expand Up @@ -1327,14 +1327,10 @@ namespace decls_block {
);

TYPE_LAYOUT(PackTypeLayout,
PACK_TYPE
PACK_TYPE,
BCArray<TypeIDField> // component types
);

using PackTypeEltLayout = BCRecordLayout<
PACK_TYPE_ELT,
TypeIDField // type
>;

TYPE_LAYOUT(SILPackTypeLayout,
SIL_PACK_TYPE,
BCFixed<1>, // is address
Expand Down
1 change: 1 addition & 0 deletions lib/Serialization/SILFormat.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,6 +471,7 @@ namespace sil_block {
// The pack_element_get instruction.
using SILPackElementGetLayout = BCRecordLayout<
SIL_PACK_ELEMENT_GET,
SILInstOpCodeField,
TypeIDField, // element type
SILTypeCategoryField, // element type category
TypeIDField, // pack type
Expand Down
15 changes: 8 additions & 7 deletions lib/Serialization/Serialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5004,14 +5004,14 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
void visitPackType(const PackType *packTy) {
using namespace decls_block;
unsigned abbrCode = S.DeclTypeAbbrCodes[PackTypeLayout::Code];
PackTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode);

abbrCode = S.DeclTypeAbbrCodes[PackTypeEltLayout::Code];
for (auto elt : packTy->getElementTypes()) {
PackTypeEltLayout::emitRecord(
S.Out, S.ScratchRecord, abbrCode,
S.addTypeRef(elt));
SmallVector<TypeID, 8> variableData;
for (auto elementType : packTy->getElementTypes()) {
variableData.push_back(S.addTypeRef(elementType));
}

PackTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
variableData);
}

void visitSILPackType(const SILPackType *packTy) {
Expand Down Expand Up @@ -5549,6 +5549,7 @@ void Serializer::writeAllDeclsAndTypes() {
registerDeclTypeAbbr<ExistentialMetatypeTypeLayout>();
registerDeclTypeAbbr<PrimaryArchetypeTypeLayout>();
registerDeclTypeAbbr<OpenedArchetypeTypeLayout>();
registerDeclTypeAbbr<ElementArchetypeTypeLayout>();
registerDeclTypeAbbr<OpaqueArchetypeTypeLayout>();
registerDeclTypeAbbr<PackArchetypeTypeLayout>();
registerDeclTypeAbbr<ProtocolCompositionTypeLayout>();
Expand All @@ -5568,7 +5569,7 @@ void Serializer::writeAllDeclsAndTypes() {
registerDeclTypeAbbr<DynamicSelfTypeLayout>();
registerDeclTypeAbbr<PackExpansionTypeLayout>();
registerDeclTypeAbbr<PackTypeLayout>();
registerDeclTypeAbbr<PackTypeEltLayout>();
registerDeclTypeAbbr<SILPackTypeLayout>();

registerDeclTypeAbbr<ErrorFlagLayout>();
registerDeclTypeAbbr<ErrorTypeLayout>();
Expand Down
4 changes: 4 additions & 0 deletions lib/Serialization/SerializeSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1680,6 +1680,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
auto indexRef = addValueRef(PEGI->getIndex());
SILPackElementGetLayout::emitRecord(Out, ScratchRecord,
SILAbbrCodes[SILPackElementGetLayout::Code],
(unsigned)SI.getKind(),
elementTypeRef,
(unsigned) elementType.getCategory(),
packTypeRef,
Expand Down Expand Up @@ -1721,6 +1722,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
auto indexRef = addValueRef(TPEAI->getIndex());
SILPackElementGetLayout::emitRecord(Out, ScratchRecord,
SILAbbrCodes[SILPackElementGetLayout::Code],
(unsigned)SI.getKind(),
elementTypeRef,
(unsigned) elementType.getCategory(),
tupleTypeRef,
Expand Down Expand Up @@ -3057,6 +3059,8 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
registerSILAbbr<SILInstLinearFunctionExtractLayout>();
registerSILAbbr<SILInstIncrementProfilerCounterLayout>();
registerSILAbbr<SILInstHasSymbolLayout>();
registerSILAbbr<SILPackElementGetLayout>();
registerSILAbbr<SILPackElementSetLayout>();

registerSILAbbr<VTableLayout>();
registerSILAbbr<VTableEntryLayout>();
Expand Down
2 changes: 0 additions & 2 deletions test/IRGen/run_variadic_generics.sil
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

// REQUIRES: executable_test

// REQUIRES: rdar108045677

import Builtin
import Swift
import PrintShims
Expand Down
26 changes: 26 additions & 0 deletions test/SIL/Serialization/pack_expansion_type.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module %s -emit-module-path %t/pack_expansion_type.swiftmodule -DLIB -enable-experimental-feature VariadicGenerics
// RUN: %target-swift-frontend -emit-sil %s -I %t -DAPP -module-name main -enable-experimental-feature VariadicGenerics

// Because of -enable-experimental-feature VariadicGenerics
// REQUIRES: asserts

#if LIB

public func callee<each T>(_: repeat each T) {}

@_transparent public func caller<each T>(_ t: repeat each T) {
callee(repeat [each t])
}

public func calleer() {
caller(1, "hi", false)
}

#elseif APP

import pack_expansion_type

caller(1, "hi", false)

#endif