Skip to content

Commit 4ad199f

Browse files
authored
Merge pull request #65311 from slavapestov/open-pack-element-serialization
Fix serialization of open_pack_element and tweak variadic generic function mangling
2 parents ab12c4d + 394e305 commit 4ad199f

12 files changed

+62
-25
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3156,17 +3156,17 @@ void ASTMangler::appendGenericSignatureParts(
31563156
ArrayRef<CanGenericTypeParamType> params,
31573157
unsigned initialParamDepth,
31583158
ArrayRef<Requirement> requirements) {
3159-
// Mangle the requirements.
3160-
for (const Requirement &reqt : requirements) {
3161-
appendRequirement(reqt, sig);
3162-
}
3163-
31643159
// Mangle which generic parameters are pack parameters.
31653160
for (auto param : params) {
31663161
if (param->isParameterPack())
31673162
appendOpWithGenericParamIndex("Rv", param);
31683163
}
31693164

3165+
// Mangle the requirements.
3166+
for (const Requirement &reqt : requirements) {
3167+
appendRequirement(reqt, sig);
3168+
}
3169+
31703170
if (params.size() == 1 && params[0]->getDepth() == initialParamDepth)
31713171
return appendOperator("l");
31723172

lib/Serialization/Deserialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -871,8 +871,8 @@ ModuleFile::getConformanceChecked(ProtocolConformanceID conformanceID) {
871871

872872
case SerializedProtocolConformanceKind::Pack: {
873873
auto conformanceIndex = (conformanceID >> SerializedProtocolConformanceKind::Shift) - 1;
874-
assert(conformanceIndex < Conformances.size() && "invalid pack conformance ID");
875-
auto &conformanceOrOffset = Conformances[conformanceIndex];
874+
assert(conformanceIndex < PackConformances.size() && "invalid pack conformance ID");
875+
auto &conformanceOrOffset = PackConformances[conformanceIndex];
876876
if (!conformanceOrOffset.isComplete()) {
877877
BCOffsetRAII restoreOffset(DeclTypeCursor);
878878
if (auto error = diagnoseFatalIfNotSuccess(

lib/Serialization/DeserializeSIL.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1284,6 +1284,11 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
12841284
SILInstHasSymbolLayout::readRecord(scratch, ValID, ListOfValues);
12851285
RawOpCode = (unsigned)SILInstructionKind::HasSymbolInst;
12861286
break;
1287+
case SIL_OPEN_PACK_ELEMENT:
1288+
SILOpenPackElementLayout::readRecord(scratch, Attr,
1289+
TyID, TyCategory, ValID);
1290+
RawOpCode = (unsigned)SILInstructionKind::OpenPackElementInst;
1291+
break;
12871292
case SIL_PACK_ELEMENT_GET:
12881293
SILPackElementGetLayout::readRecord(scratch, RawOpCode,
12891294
TyID, TyCategory,
@@ -1424,7 +1429,7 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
14241429
break;
14251430
}
14261431
case SILInstructionKind::OpenPackElementInst: {
1427-
assert(RecordKind == SIL_ONE_OPERAND && "Layout should be OneOperand");
1432+
assert(RecordKind == SIL_OPEN_PACK_ELEMENT && "Layout should be OpenPackElement");
14281433
auto index = getLocalValue(ValID,
14291434
getSILType(MF->getType(TyID), (SILValueCategory) TyCategory, Fn));
14301435
auto env = MF->getGenericEnvironmentChecked(Attr);

lib/Serialization/ModuleFile.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ ModuleFile::ModuleFile(std::shared_ptr<const ModuleFileSharedCore> core)
110110
allocateBuffer(Decls, core->Decls);
111111
allocateBuffer(LocalDeclContexts, core->LocalDeclContexts);
112112
allocateBuffer(Conformances, core->Conformances);
113+
allocateBuffer(PackConformances, core->PackConformances);
113114
allocateBuffer(SILLayouts, core->SILLayouts);
114115
allocateBuffer(Types, core->Types);
115116
allocateBuffer(ClangTypes, core->ClangTypes);

lib/Serialization/SILFormat.h

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ namespace sil_block {
159159
SIL_INST_INCREMENT_PROFILER_COUNTER,
160160
SIL_MOVEONLY_DEINIT,
161161
SIL_INST_HAS_SYMBOL,
162+
SIL_OPEN_PACK_ELEMENT,
162163
SIL_PACK_ELEMENT_GET,
163164
SIL_PACK_ELEMENT_SET,
164165
};
@@ -468,6 +469,15 @@ namespace sil_block {
468469
ValueIDField
469470
>;
470471

472+
// The pack_element_get instruction.
473+
using SILOpenPackElementLayout = BCRecordLayout<
474+
SIL_OPEN_PACK_ELEMENT,
475+
GenericEnvironmentIDField,
476+
TypeIDField,
477+
SILTypeCategoryField,
478+
ValueIDField
479+
>;
480+
471481
// The pack_element_get instruction.
472482
using SILPackElementGetLayout = BCRecordLayout<
473483
SIL_PACK_ELEMENT_GET,

lib/Serialization/Serialization.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -926,6 +926,9 @@ void Serializer::writeBlockInfoBlock() {
926926
BLOCK_RECORD(sil_block, SIL_ONE_OPERAND_EXTRA_ATTR);
927927
BLOCK_RECORD(sil_block, SIL_ONE_TYPE_ONE_OPERAND_EXTRA_ATTR);
928928
BLOCK_RECORD(sil_block, SIL_TWO_OPERANDS_EXTRA_ATTR);
929+
BLOCK_RECORD(sil_block, SIL_OPEN_PACK_ELEMENT);
930+
BLOCK_RECORD(sil_block, SIL_PACK_ELEMENT_GET);
931+
BLOCK_RECORD(sil_block, SIL_PACK_ELEMENT_SET);
929932

930933
BLOCK(SIL_INDEX_BLOCK);
931934
BLOCK_RECORD(sil_index_block, SIL_FUNC_NAMES);

lib/Serialization/SerializeSIL.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1869,7 +1869,16 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
18691869
auto &opei = cast<OpenPackElementInst>(SI);
18701870
auto envRef =
18711871
S.addGenericEnvironmentRef(opei.getOpenedGenericEnvironment());
1872-
writeOneOperandLayout(SI.getKind(), envRef, opei.getIndexOperand());
1872+
auto operand = opei.getIndexOperand();
1873+
auto operandRef = addValueRef(operand);
1874+
auto operandType = operand->getType();
1875+
auto operandTypeRef = S.addTypeRef(operandType.getASTType());
1876+
1877+
SILOpenPackElementLayout::emitRecord(Out, ScratchRecord,
1878+
SILAbbrCodes[SILOpenPackElementLayout::Code],
1879+
envRef,
1880+
operandTypeRef, unsigned(operandType.getCategory()),
1881+
operandRef);
18731882
break;
18741883
}
18751884
case SILInstructionKind::GetAsyncContinuationAddrInst: {
@@ -3059,6 +3068,7 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
30593068
registerSILAbbr<SILInstLinearFunctionExtractLayout>();
30603069
registerSILAbbr<SILInstIncrementProfilerCounterLayout>();
30613070
registerSILAbbr<SILInstHasSymbolLayout>();
3071+
registerSILAbbr<SILOpenPackElementLayout>();
30623072
registerSILAbbr<SILPackElementGetLayout>();
30633073
registerSILAbbr<SILPackElementSetLayout>();
30643074

test/DebugInfo/variadic-generics-count.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public func f3<each T>(ts: repeat each T, more_ts: repeat each T) {
2525
}
2626

2727
public func f4<each U, each V>(us: repeat (each U, each V)) {
28-
// CHECK: define {{.*}} @"$s1a2f42usyx_q_txQp_tq_RhzRvzRv_r0_lF"(%swift.opaque** {{.*}}, i{{32|64}} [[COUNT4_1:.*]], %swift.type** {{.*}}, %swift.type** {{.*}})
28+
// CHECK: define {{.*}} @"$s1a2f42usyx_q_txQp_tRvzRv_q_Rhzr0_lF"(%swift.opaque** {{.*}}, i{{32|64}} [[COUNT4_1:.*]], %swift.type** {{.*}}, %swift.type** {{.*}})
2929
// CHECK-DAG: store i{{32|64}} [[COUNT4_1]], i{{32|64}}* %[[COUNT4_1_A:.*]], align
3030
// CHECK-DAG: call void @llvm.dbg.declare({{.*}}[[COUNT4_1_A]], metadata ![[COUNT4_1_VAR:[0-9]+]], metadata !DIExpression())
3131
// CHECK-LABEL: ret void

test/IRGen/variadic_generic_functions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ func f1<each T>(t: repeat each T) {}
88
// CHECK-LABEL: define hidden swiftcc void @"$s26variadic_generic_functions2f21t1uyxxQp_q_q_QptRvzRv_r0_lF"(%swift.opaque** noalias nocapture %0, %swift.opaque** noalias nocapture %1, i64 %2, i64 %3, %swift.type** %"each T", %swift.type** %"each U")
99
func f2<each T, each U>(t: repeat each T, u: repeat each U) {}
1010

11-
// CHECK-LABEL: define hidden swiftcc void @"$s26variadic_generic_functions2f31t1uyxxQp_q_xQptq_RhzRvzRv_r0_lF"(%swift.opaque** noalias nocapture %0, %swift.opaque** noalias nocapture %1, i64 %2, %swift.type** %"each T", %swift.type** %"each U")
11+
// CHECK-LABEL: define hidden swiftcc void @"$s26variadic_generic_functions2f31t1uyxxQp_q_xQptRvzRv_q_Rhzr0_lF"(%swift.opaque** noalias nocapture %0, %swift.opaque** noalias nocapture %1, i64 %2, %swift.type** %"each T", %swift.type** %"each U")
1212
func f3<each T, each U>(t: repeat each T, u: repeat each U) where (repeat (each T, each U)): Any {}
1313

1414
protocol P {}
Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,41 @@
11
// RUN: %empty-directory(%t)
22
// 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
3+
// RUN: %target-swift-frontend -emit-ir %s -I %t -DAPP -module-name main -enable-experimental-feature VariadicGenerics
44

55
// Because of -enable-experimental-feature VariadicGenerics
66
// REQUIRES: asserts
77

88
#if LIB
99

10-
public func callee<each T>(_: repeat each T) {}
10+
public func calleeWithPack<each T>(_: repeat each T) {}
1111

12-
@_transparent public func caller<each T>(_ t: repeat each T) {
13-
callee(repeat [each t])
12+
public func calleeGeneric<T>(_: T) {}
13+
14+
@_transparent public func transparentCaller<each T>(_ t: repeat each T) {
15+
calleeWithPack(repeat [each t])
16+
calleeGeneric((repeat [each t]))
1417
}
1518

16-
public func calleer() {
17-
caller(1, "hi", false)
19+
@_alwaysEmitIntoClient public func serializedCaller<each T>(_ t: repeat each T) {
20+
calleeWithPack(repeat [each t])
21+
calleeGeneric((repeat [each t]))
1822
}
1923

20-
public func foo<each T: Equatable>(_: repeat each T) {}
24+
public func transparentCaller2() {
25+
transparentCaller(1, "hi", false)
26+
}
2127

22-
@_transparent public func bar(x: Int, y: String) {
23-
foo(x, y)
28+
@_alwaysEmitIntoClient
29+
public func calleeWithRequirement<each T: Equatable>(_ t: repeat each T) {
30+
repeat ((each t) == (each t))
2431
}
2532

2633
#elseif APP
2734

2835
import pack_expansion_type
2936

30-
caller(1, "hi", false)
31-
bar(x: 1, y: "hi")
37+
transparentCaller(1, "hi", false)
38+
serializedCaller(1, "hi", false)
39+
calleeWithRequirement(1, "hi")
3240

3341
#endif

0 commit comments

Comments
 (0)