Skip to content

Commit 9f9013b

Browse files
authored
Merge pull request #65155 from slavapestov/variadic-generic-sil-fixes
SIL serialization fixes for variadic generics
2 parents 1ddc793 + 5681a39 commit 9f9013b

File tree

10 files changed

+56
-44
lines changed

10 files changed

+56
-44
lines changed

lib/SILOptimizer/Transforms/CSE.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -523,7 +523,8 @@ class HashVisitor : public SILInstructionVisitor<HashVisitor, llvm::hash_code> {
523523

524524
hash_code visitDynamicPackIndexInst(DynamicPackIndexInst *X) {
525525
return llvm::hash_combine(
526-
X->getKind(), X->getIndexedPackType(), &X->getOperandRef());
526+
X->getKind(), X->getIndexedPackType(),
527+
tryLookThroughOwnershipInsts(&X->getOperandRef()));
527528
}
528529

529530
hash_code visitTuplePackElementAddrInst(TuplePackElementAddrInst *X) {
@@ -585,7 +586,7 @@ bool llvm::DenseMapInfo<SimpleValue>::isEqual(SimpleValue LHS,
585586
};
586587
bool isEqual =
587588
LHSI->getKind() == RHSI->getKind() && LHSI->isIdenticalTo(RHSI, opCmp);
588-
#ifdef NDEBUG
589+
#ifndef NDEBUG
589590
if (isEqual && getHashValue(LHS) != getHashValue(RHS)) {
590591
llvm::dbgs() << "LHS: ";
591592
LHSI->dump();

lib/Serialization/DeclTypeRecordNodes.def

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,6 @@ TYPE(NAME_ALIAS)
115115

116116
TYPE(PACK_EXPANSION)
117117
TYPE(PACK)
118-
TRAILING_INFO(PACK_TYPE_ELT)
119118
TYPE(SIL_PACK)
120119

121120
TYPE(ERROR)

lib/Serialization/Deserialization.cpp

Lines changed: 9 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -6876,31 +6876,18 @@ Expected<Type> DESERIALIZE_TYPE(PACK_EXPANSION_TYPE)(
68766876

68776877
Expected<Type> DESERIALIZE_TYPE(PACK_TYPE)(
68786878
ModuleFile &MF, SmallVectorImpl<uint64_t> &scratch, StringRef blobData) {
6879-
// The pack record itself is empty. Read all trailing elements.
6880-
SmallVector<Type, 8> elements;
6881-
while (true) {
6882-
llvm::BitstreamEntry entry =
6883-
MF.fatalIfUnexpected(MF.DeclTypeCursor.advance(AF_DontPopBlockAtEnd));
6884-
if (entry.Kind != llvm::BitstreamEntry::Record)
6885-
break;
6886-
6887-
scratch.clear();
6888-
unsigned recordID = MF.fatalIfUnexpected(
6889-
MF.DeclTypeCursor.readRecord(entry.ID, scratch, &blobData));
6890-
if (recordID != decls_block::PACK_TYPE_ELT)
6891-
break;
6892-
6893-
TypeID typeID;
6894-
decls_block::PackTypeEltLayout::readRecord(scratch, typeID);
6895-
6896-
auto elementTy = MF.getTypeChecked(typeID);
6897-
if (!elementTy)
6898-
return elementTy.takeError();
6879+
ArrayRef<uint64_t> elementTypeIDs;
6880+
decls_block::PackTypeLayout::readRecord(scratch, elementTypeIDs);
68996881

6900-
elements.push_back(elementTy.get());
6882+
SmallVector<Type, 8> elementTypes;
6883+
for (auto elementTypeID : elementTypeIDs) {
6884+
auto elementType = MF.getTypeChecked(elementTypeID);
6885+
if (!elementType)
6886+
return elementType.takeError();
6887+
elementTypes.push_back(elementType.get());
69016888
}
69026889

6903-
return PackType::get(MF.getContext(), elements);
6890+
return PackType::get(MF.getContext(), elementTypes);
69046891
}
69056892

69066893
Expected<Type> DESERIALIZE_TYPE(SIL_PACK_TYPE)(

lib/Serialization/DeserializeSIL.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1285,11 +1285,10 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
12851285
RawOpCode = (unsigned)SILInstructionKind::HasSymbolInst;
12861286
break;
12871287
case SIL_PACK_ELEMENT_GET:
1288-
SILPackElementGetLayout::readRecord(scratch,
1288+
SILPackElementGetLayout::readRecord(scratch, RawOpCode,
12891289
TyID, TyCategory,
12901290
TyID2, TyCategory2, ValID2,
12911291
ValID3);
1292-
RawOpCode = (unsigned)SILInstructionKind::PackElementGetInst;
12931292
break;
12941293
case SIL_PACK_ELEMENT_SET:
12951294
SILPackElementSetLayout::readRecord(scratch,
@@ -1332,7 +1331,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
13321331
}
13331332
case SILInstructionKind::AllocPackInst: {
13341333
assert(RecordKind == SIL_ONE_TYPE && "Layout should be OneType.");
1335-
ResultInst = Builder.createAllocStack(
1334+
ResultInst = Builder.createAllocPack(
13361335
Loc, getSILType(MF->getType(TyID), (SILValueCategory)TyCategory, Fn));
13371336
break;
13381337
}

lib/Serialization/ModuleFormat.h

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5858
/// describe what change you made. The content of this comment isn't important;
5959
/// it just ensures a conflict if two people change the module format.
6060
/// Don't worry about adhering to the 80-column limit for this line.
61-
const uint16_t SWIFTMODULE_VERSION_MINOR = 758; // drop_deinit instruction
61+
const uint16_t SWIFTMODULE_VERSION_MINOR = 759; // change PackType
6262

6363
/// A standard hash seed used for all string hashes in a serialized module.
6464
///
@@ -1327,14 +1327,10 @@ namespace decls_block {
13271327
);
13281328

13291329
TYPE_LAYOUT(PackTypeLayout,
1330-
PACK_TYPE
1330+
PACK_TYPE,
1331+
BCArray<TypeIDField> // component types
13311332
);
13321333

1333-
using PackTypeEltLayout = BCRecordLayout<
1334-
PACK_TYPE_ELT,
1335-
TypeIDField // type
1336-
>;
1337-
13381334
TYPE_LAYOUT(SILPackTypeLayout,
13391335
SIL_PACK_TYPE,
13401336
BCFixed<1>, // is address

lib/Serialization/SILFormat.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -471,6 +471,7 @@ namespace sil_block {
471471
// The pack_element_get instruction.
472472
using SILPackElementGetLayout = BCRecordLayout<
473473
SIL_PACK_ELEMENT_GET,
474+
SILInstOpCodeField,
474475
TypeIDField, // element type
475476
SILTypeCategoryField, // element type category
476477
TypeIDField, // pack type

lib/Serialization/Serialization.cpp

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5004,14 +5004,14 @@ class Serializer::TypeSerializer : public TypeVisitor<TypeSerializer> {
50045004
void visitPackType(const PackType *packTy) {
50055005
using namespace decls_block;
50065006
unsigned abbrCode = S.DeclTypeAbbrCodes[PackTypeLayout::Code];
5007-
PackTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode);
50085007

5009-
abbrCode = S.DeclTypeAbbrCodes[PackTypeEltLayout::Code];
5010-
for (auto elt : packTy->getElementTypes()) {
5011-
PackTypeEltLayout::emitRecord(
5012-
S.Out, S.ScratchRecord, abbrCode,
5013-
S.addTypeRef(elt));
5008+
SmallVector<TypeID, 8> variableData;
5009+
for (auto elementType : packTy->getElementTypes()) {
5010+
variableData.push_back(S.addTypeRef(elementType));
50145011
}
5012+
5013+
PackTypeLayout::emitRecord(S.Out, S.ScratchRecord, abbrCode,
5014+
variableData);
50155015
}
50165016

50175017
void visitSILPackType(const SILPackType *packTy) {
@@ -5549,6 +5549,7 @@ void Serializer::writeAllDeclsAndTypes() {
55495549
registerDeclTypeAbbr<ExistentialMetatypeTypeLayout>();
55505550
registerDeclTypeAbbr<PrimaryArchetypeTypeLayout>();
55515551
registerDeclTypeAbbr<OpenedArchetypeTypeLayout>();
5552+
registerDeclTypeAbbr<ElementArchetypeTypeLayout>();
55525553
registerDeclTypeAbbr<OpaqueArchetypeTypeLayout>();
55535554
registerDeclTypeAbbr<PackArchetypeTypeLayout>();
55545555
registerDeclTypeAbbr<ProtocolCompositionTypeLayout>();
@@ -5568,7 +5569,7 @@ void Serializer::writeAllDeclsAndTypes() {
55685569
registerDeclTypeAbbr<DynamicSelfTypeLayout>();
55695570
registerDeclTypeAbbr<PackExpansionTypeLayout>();
55705571
registerDeclTypeAbbr<PackTypeLayout>();
5571-
registerDeclTypeAbbr<PackTypeEltLayout>();
5572+
registerDeclTypeAbbr<SILPackTypeLayout>();
55725573

55735574
registerDeclTypeAbbr<ErrorFlagLayout>();
55745575
registerDeclTypeAbbr<ErrorTypeLayout>();

lib/Serialization/SerializeSIL.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,6 +1680,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
16801680
auto indexRef = addValueRef(PEGI->getIndex());
16811681
SILPackElementGetLayout::emitRecord(Out, ScratchRecord,
16821682
SILAbbrCodes[SILPackElementGetLayout::Code],
1683+
(unsigned)SI.getKind(),
16831684
elementTypeRef,
16841685
(unsigned) elementType.getCategory(),
16851686
packTypeRef,
@@ -1721,6 +1722,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
17211722
auto indexRef = addValueRef(TPEAI->getIndex());
17221723
SILPackElementGetLayout::emitRecord(Out, ScratchRecord,
17231724
SILAbbrCodes[SILPackElementGetLayout::Code],
1725+
(unsigned)SI.getKind(),
17241726
elementTypeRef,
17251727
(unsigned) elementType.getCategory(),
17261728
tupleTypeRef,
@@ -3057,6 +3059,8 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
30573059
registerSILAbbr<SILInstLinearFunctionExtractLayout>();
30583060
registerSILAbbr<SILInstIncrementProfilerCounterLayout>();
30593061
registerSILAbbr<SILInstHasSymbolLayout>();
3062+
registerSILAbbr<SILPackElementGetLayout>();
3063+
registerSILAbbr<SILPackElementSetLayout>();
30603064

30613065
registerSILAbbr<VTableLayout>();
30623066
registerSILAbbr<VTableEntryLayout>();

test/IRGen/run_variadic_generics.sil

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
// REQUIRES: executable_test
1010

11-
// REQUIRES: rdar108045677
12-
1311
import Builtin
1412
import Swift
1513
import PrintShims
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend -emit-module %s -emit-module-path %t/pack_expansion_type.swiftmodule -DLIB -enable-experimental-feature VariadicGenerics
3+
// RUN: %target-swift-frontend -emit-sil %s -I %t -DAPP -module-name main -enable-experimental-feature VariadicGenerics
4+
5+
// Because of -enable-experimental-feature VariadicGenerics
6+
// REQUIRES: asserts
7+
8+
#if LIB
9+
10+
public func callee<each T>(_: repeat each T) {}
11+
12+
@_transparent public func caller<each T>(_ t: repeat each T) {
13+
callee(repeat [each t])
14+
}
15+
16+
public func calleer() {
17+
caller(1, "hi", false)
18+
}
19+
20+
#elseif APP
21+
22+
import pack_expansion_type
23+
24+
caller(1, "hi", false)
25+
26+
#endif

0 commit comments

Comments
 (0)