Skip to content

Commit 35ad79e

Browse files
Merge pull request #5528 from swiftwasm/main
[pull] swiftwasm from main
2 parents 113bf4e + fde2cf5 commit 35ad79e

26 files changed

+219
-84
lines changed

cmake/modules/SwiftConfigureSDK.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ macro(configure_sdk_unix name architectures)
378378
message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}")
379379
endif()
380380
elseif("${prefix}" STREQUAL "FREEBSD")
381-
if(NOT arch STREQUAL x86_64)
381+
if(NOT arch MATCHES "(arm64|x86_64)")
382382
message(FATAL_ERROR "unsupported arch for FreeBSD: ${arch}")
383383
endif()
384384

@@ -389,7 +389,7 @@ macro(configure_sdk_unix name architectures)
389389
string(REGEX REPLACE "[-].*" "" freebsd_system_version ${CMAKE_SYSTEM_VERSION})
390390
message(STATUS "FreeBSD Version: ${freebsd_system_version}")
391391

392-
set(SWIFT_SDK_FREEBSD_ARCH_x86_64_TRIPLE "x86_64-unknown-freebsd${freebsd_system_version}")
392+
set(SWIFT_SDK_FREEBSD_ARCH_${arch}_TRIPLE "${arch}-unknown-freebsd${freebsd_system_version}")
393393
elseif("${prefix}" STREQUAL "OPENBSD")
394394
if(NOT arch STREQUAL amd64)
395395
message(FATAL_ERROR "unsupported arch for OpenBSD: ${arch}")

include/swift/AST/PrintOptions.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,12 @@ struct PrintOptions {
553553
/// Whether to always desugar optional types from `base_type?` to `Optional<base_type>`
554554
bool AlwaysDesugarOptionalTypes = false;
555555

556+
/// Whether to always print explicit `Pack{...}` around pack
557+
/// types.
558+
///
559+
/// This is set to \c false for diagnostic arguments.
560+
bool PrintExplicitPackTypes = true;
561+
556562
/// \see ShouldQualifyNestedDeclarations
557563
enum class QualifyNestedDeclarations {
558564
Never,
@@ -606,6 +612,7 @@ struct PrintOptions {
606612
/// The print options used for formatting diagnostic arguments.
607613
static PrintOptions forDiagnosticArguments() {
608614
PrintOptions result;
615+
result.PrintExplicitPackTypes = false;
609616
return result;
610617
}
611618

include/swift/Runtime/Enum.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,11 @@ void swift_initEnumMetadataSingleCase(EnumMetadata *enumType,
4545
EnumLayoutFlags flags,
4646
const TypeLayout *payload);
4747

48+
SWIFT_RUNTIME_EXPORT
49+
void swift_initEnumMetadataSingleCaseWithLayoutString(
50+
EnumMetadata *self, EnumLayoutFlags layoutFlags,
51+
const Metadata *payloadType);
52+
4853
/// Initialize the type metadata for a single-payload enum type.
4954
///
5055
/// \param enumType - pointer to the instantiated but uninitialized metadata

include/swift/Runtime/RuntimeFunctions.def

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1300,6 +1300,17 @@ FUNCTION(InitEnumMetadataSingleCase,
13001300
ATTRS(NoUnwind, WillReturn),
13011301
EFFECT(MetaData))
13021302

1303+
// void swift_initEnumMetadataSingleCaseWithLayoutString(Metadata *enumType,
1304+
// EnumLayoutFlags flags,
1305+
// Metadata *payload);
1306+
FUNCTION(InitEnumMetadataSingleCaseWithLayoutString,
1307+
swift_initEnumMetadataSingleCaseWithLayoutString,
1308+
C_CC, AlwaysAvailable,
1309+
RETURNS(VoidTy),
1310+
ARGS(TypeMetadataPtrTy, SizeTy, TypeMetadataPtrTy),
1311+
ATTRS(NoUnwind, WillReturn),
1312+
EFFECT(MetaData))
1313+
13031314
// void swift_initEnumMetadataSinglePayload(Metadata *enumType,
13041315
// EnumLayoutFlags flags,
13051316
// TypeLayout *payload,

lib/AST/ASTPrinter.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6051,7 +6051,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60516051
}
60526052

60536053
void visitPackType(PackType *T) {
6054-
Printer << "Pack{";
6054+
if (Options.PrintExplicitPackTypes)
6055+
Printer << "Pack{";
60556056

60566057
auto Fields = T->getElementTypes();
60576058
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
@@ -6060,7 +6061,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
60606061
Type EltType = Fields[i];
60616062
visit(EltType);
60626063
}
6063-
Printer << "}";
6064+
6065+
if (Options.PrintExplicitPackTypes)
6066+
Printer << "}";
60646067
}
60656068

60666069
void visitSILPackType(SILPackType *T) {

lib/IRGen/GenEnum.cpp

Lines changed: 48 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -663,13 +663,42 @@ namespace {
663663
metadata);
664664
}
665665

666-
void initializeMetadataWithLayoutString(IRGenFunction &IGF,
667-
llvm::Value *metadata,
668-
bool isVWTMutable,
669-
SILType T,
670-
MetadataDependencyCollector *collector) const override {
671-
// Not yet supported on this type, so forward to regular method
672-
initializeMetadata(IGF, metadata, isVWTMutable, T, collector);
666+
void initializeMetadataWithLayoutString(
667+
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
668+
MetadataDependencyCollector *collector) const override {
669+
if (TIK >= Fixed)
670+
return;
671+
672+
assert(ElementsWithPayload.size() == 1 &&
673+
"empty singleton enum should not be dynamic!");
674+
675+
auto payloadTy =
676+
T.getEnumElementType(ElementsWithPayload[0].decl, IGM.getSILModule(),
677+
IGM.getMaximalTypeExpansionContext());
678+
679+
auto request = DynamicMetadataRequest::getNonBlocking(
680+
MetadataState::LayoutComplete, collector);
681+
auto payloadMetadata =
682+
IGF.emitTypeMetadataRefForLayout(payloadTy, request);
683+
684+
auto flags = emitEnumLayoutFlags(IGF.IGM, isVWTMutable);
685+
IGF.Builder.CreateCall(
686+
IGF.IGM
687+
.getInitEnumMetadataSingleCaseWithLayoutStringFunctionPointer(),
688+
{metadata, flags, payloadMetadata});
689+
690+
// Pre swift-5.1 runtimes were missing the initialization of the
691+
// the extraInhabitantCount field. Do it here instead.
692+
auto payloadLayout = emitTypeLayoutRef(IGF, payloadTy, collector);
693+
auto payloadRef = IGF.Builder.CreateBitOrPointerCast(
694+
payloadLayout, IGF.IGM.TypeLayoutTy->getPointerTo());
695+
auto payloadExtraInhabitantCount =
696+
IGF.Builder.CreateLoad(IGF.Builder.CreateStructGEP(
697+
Address(payloadRef, IGF.IGM.TypeLayoutTy, Alignment(1)), 3,
698+
Size(IGF.IGM.DataLayout.getTypeAllocSize(IGF.IGM.SizeTy) * 2 +
699+
IGF.IGM.DataLayout.getTypeAllocSize(IGF.IGM.Int32Ty))));
700+
emitStoreOfExtraInhabitantCount(IGF, payloadExtraInhabitantCount,
701+
metadata);
673702
}
674703

675704
bool mayHaveExtraInhabitants(IRGenModule &IGM) const override {
@@ -973,11 +1002,9 @@ namespace {
9731002
// witness table initialization.
9741003
}
9751004

976-
void initializeMetadataWithLayoutString(IRGenFunction &IGF,
977-
llvm::Value *metadata,
978-
bool isVWTMutable,
979-
SILType T,
980-
MetadataDependencyCollector *collector) const override {
1005+
void initializeMetadataWithLayoutString(
1006+
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
1007+
MetadataDependencyCollector *collector) const override {
9811008
// No-payload enums are always fixed-size so never need dynamic value
9821009
// witness table initialization.
9831010
}
@@ -3207,11 +3234,9 @@ namespace {
32073234
{metadata, flags, payloadLayout, emptyCasesVal});
32083235
}
32093236

3210-
void initializeMetadataWithLayoutString(IRGenFunction &IGF,
3211-
llvm::Value *metadata,
3212-
bool isVWTMutable,
3213-
SILType T,
3214-
MetadataDependencyCollector *collector) const override {
3237+
void initializeMetadataWithLayoutString(
3238+
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
3239+
MetadataDependencyCollector *collector) const override {
32153240
// Not yet supported on this type, so forward to regular method
32163241
initializeMetadata(IGF, metadata, isVWTMutable, T, collector);
32173242
}
@@ -5335,11 +5360,9 @@ namespace {
53355360
{metadata, flags, numPayloadsVal, payloadLayoutArray});
53365361
}
53375362

5338-
void initializeMetadataWithLayoutString(IRGenFunction &IGF,
5339-
llvm::Value *metadata,
5340-
bool isVWTMutable,
5341-
SILType T,
5342-
MetadataDependencyCollector *collector) const override {
5363+
void initializeMetadataWithLayoutString(
5364+
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
5365+
MetadataDependencyCollector *collector) const override {
53435366
// Fixed-size enums don't need dynamic metadata initialization.
53445367
if (TIK >= Fixed) return;
53455368

@@ -6035,11 +6058,9 @@ namespace {
60356058
llvm_unreachable("resilient enums cannot be defined");
60366059
}
60376060

6038-
void initializeMetadataWithLayoutString(IRGenFunction &IGF,
6039-
llvm::Value *metadata,
6040-
bool isVWTMutable,
6041-
SILType T,
6042-
MetadataDependencyCollector *collector) const override {
6061+
void initializeMetadataWithLayoutString(
6062+
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
6063+
MetadataDependencyCollector *collector) const override {
60436064
llvm_unreachable("resilient enums cannot be defined");
60446065
}
60456066

lib/IRGen/GenMeta.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5737,14 +5737,17 @@ namespace {
57375737
}
57385738

57395739
auto &strategy = getEnumImplStrategy(IGM, getLoweredType());
5740+
bool isSupportedCase = strategy.getElementsWithPayload().size() > 1 ||
5741+
(strategy.getElementsWithPayload().size() == 1 &&
5742+
strategy.getElementsWithNoPayload().empty());
57405743

57415744
return !!getLayoutString() ||
57425745
(IGM.Context.LangOpts.hasFeature(
5743-
Feature::LayoutStringValueWitnessesInstantiation) &&
5746+
Feature::LayoutStringValueWitnessesInstantiation) &&
57445747
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
5745-
(HasDependentVWT || HasDependentMetadata) &&
5746-
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())) &&
5747-
strategy.getElementsWithPayload().size() > 1);
5748+
(HasDependentVWT || HasDependentMetadata) &&
5749+
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())) &&
5750+
isSupportedCase);
57485751
}
57495752

57505753
llvm::Constant *emitNominalTypeDescriptor() {

lib/IRGen/GenValueWitness.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -892,7 +892,7 @@ bool isRuntimeInstatiatedLayoutString(IRGenModule &IGM,
892892
Feature::LayoutStringValueWitnessesInstantiation) &&
893893
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
894894
if (auto *enumEntry = typeLayoutEntry->getAsEnum()) {
895-
return enumEntry->isMultiPayloadEnum();
895+
return enumEntry->isMultiPayloadEnum() || enumEntry->isSingleton();
896896
}
897897
return (typeLayoutEntry->isAlignedGroup() &&
898898
!typeLayoutEntry->isFixedSize(IGM));

lib/Sema/CSDiagnostics.cpp

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -120,14 +120,6 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
120120
return env->mapElementTypeIntoPackContext(type);
121121
}
122122

123-
if (auto *packType = type->getAs<PackType>()) {
124-
if (packType->getNumElements() == 1) {
125-
auto eltType = resolveType(packType->getElementType(0));
126-
if (auto expansion = eltType->getAs<PackExpansionType>())
127-
return expansion->getPatternType();
128-
}
129-
}
130-
131123
return type->isPlaceholder() ? Type(type->getASTContext().TheUnresolvedType)
132124
: type;
133125
});

lib/Sema/TypeCheckGeneric.cpp

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -838,6 +838,7 @@ static std::string gatherGenericParamBindingsText(
838838

839839
SmallString<128> result;
840840
llvm::raw_svector_ostream OS(result);
841+
auto options = PrintOptions::forDiagnosticArguments();
841842

842843
for (auto gp : genericParams) {
843844
auto canonGP = gp->getCanonicalType()->castTo<GenericTypeParamType>();
@@ -859,19 +860,7 @@ static std::string gatherGenericParamBindingsText(
859860
if (!type)
860861
return "";
861862

862-
if (auto *packType = type->getAs<PackType>()) {
863-
bool first = true;
864-
for (auto eltType : packType->getElementTypes()) {
865-
if (first)
866-
first = false;
867-
else
868-
OS << ", ";
869-
870-
OS << eltType;
871-
}
872-
} else {
873-
OS << type.getString();
874-
}
863+
type->print(OS, options);
875864
}
876865

877866
OS << "]";

stdlib/public/runtime/Enum.cpp

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,61 @@ swift::swift_initEnumMetadataSingleCase(EnumMetadata *self,
6565
vwtable->publishLayout(layout);
6666
}
6767

68+
void swift::swift_initEnumMetadataSingleCaseWithLayoutString(
69+
EnumMetadata *self, EnumLayoutFlags layoutFlags,
70+
const Metadata *payloadType) {
71+
assert(self->hasLayoutString());
72+
73+
auto payloadLayout = payloadType->getTypeLayout();
74+
auto vwtable = getMutableVWTableForInit(self, layoutFlags);
75+
76+
TypeLayout layout;
77+
layout.size = payloadLayout->size;
78+
layout.stride = payloadLayout->stride;
79+
layout.flags = payloadLayout->flags.withEnumWitnesses(true);
80+
layout.extraInhabitantCount = payloadLayout->getNumExtraInhabitants();
81+
82+
auto refCountBytes = _swift_refCountBytesForMetatype(payloadType);
83+
const size_t fixedLayoutStringSize =
84+
layoutStringHeaderSize + sizeof(uint64_t) * 2;
85+
86+
uint8_t *layoutStr =
87+
(uint8_t *)MetadataAllocator(LayoutStringTag)
88+
.Allocate(fixedLayoutStringSize + refCountBytes, alignof(uint8_t));
89+
90+
size_t layoutStrOffset = sizeof(uint64_t);
91+
writeBytes(layoutStr, layoutStrOffset, refCountBytes);
92+
size_t fullOffset = 0;
93+
size_t previousFieldOffset = 0;
94+
LayoutStringFlags flags = LayoutStringFlags::Empty;
95+
96+
_swift_addRefCountStringForMetatype(layoutStr, layoutStrOffset, flags,
97+
payloadType, fullOffset,
98+
previousFieldOffset);
99+
100+
writeBytes(layoutStr, layoutStrOffset, (uint64_t)previousFieldOffset);
101+
writeBytes(layoutStr, layoutStrOffset, (uint64_t)0);
102+
103+
// we mask out HasRelativePointers, because at this point they have all been
104+
// resolved to metadata pointers
105+
layoutStrOffset = 0;
106+
writeBytes(layoutStr, layoutStrOffset,
107+
((uint64_t)flags) &
108+
~((uint64_t)LayoutStringFlags::HasRelativePointers));
109+
110+
vwtable->destroy = swift_generic_destroy;
111+
vwtable->initializeWithCopy = swift_generic_initWithCopy;
112+
vwtable->initializeWithTake = swift_generic_initWithTake;
113+
vwtable->assignWithCopy = swift_generic_assignWithCopy;
114+
vwtable->assignWithTake = swift_generic_assignWithTake;
115+
116+
installCommonValueWitnesses(layout, vwtable);
117+
118+
self->setLayoutString(layoutStr);
119+
120+
vwtable->publishLayout(layout);
121+
}
122+
68123
void
69124
swift::swift_initEnumMetadataSinglePayload(EnumMetadata *self,
70125
EnumLayoutFlags layoutFlags,
@@ -221,6 +276,8 @@ void swift::swift_initEnumMetadataMultiPayloadWithLayoutString(
221276
EnumLayoutFlags layoutFlags,
222277
unsigned numPayloads,
223278
const Metadata * const *payloadLayouts) {
279+
assert(enumType->hasLayoutString());
280+
224281
// Accumulate the layout requirements of the payloads.
225282
size_t payloadSize = 0, alignMask = 0;
226283
bool isPOD = true, isBT = true;

stdlib/public/runtime/Metadata.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2750,8 +2750,8 @@ void swift::swift_initStructMetadataWithLayoutString(
27502750
previousFieldOffset);
27512751
}
27522752

2753-
writeBytes(layoutStr, layoutStrOffset, previousFieldOffset);
2754-
writeBytes(layoutStr, layoutStrOffset, 0);
2753+
writeBytes(layoutStr, layoutStrOffset, (uint64_t)previousFieldOffset);
2754+
writeBytes(layoutStr, layoutStrOffset, (uint64_t)0);
27552755

27562756
// we mask out HasRelativePointers, because at this point they have all been
27572757
// resolved to metadata pointers

test/Constraints/pack-expansion-expressions.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ func tupleExpansion<each T, each U>(
138138
_ = zip(repeat each tuple1, with: repeat each tuple1.element) // legacy syntax
139139

140140
_ = zip(repeat each tuple1, with: repeat each tuple2)
141-
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'each T' and 'each U' have the same shape}}
141+
// expected-error@-1 {{global function 'zip(_:with:)' requires the type packs 'repeat each T' and 'repeat each U' have the same shape}}
142142

143143
_ = forward(repeat each tuple3)
144144
}

test/Constraints/pack_expansion_types.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -240,9 +240,9 @@ func patternInstantiationConcreteValid() {
240240

241241
func patternInstantiationConcreteInvalid() {
242242
let _: Set<Int> = patternInstantiationTupleTest1()
243-
// expected-error@-1 {{cannot convert value of type '(repeat Array<Pack{_}>)' to specified type 'Set<Int>'}}
243+
// expected-error@-1 {{cannot convert value of type '(repeat Array<_>)' to specified type 'Set<Int>'}}
244244

245-
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Pack{Int, _}>)' is not convertible to '(Array<Int>, Set<String>)', tuples have a different number of elements}}
245+
let _: (Array<Int>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Int, _>)' is not convertible to '(Array<Int>, Set<String>)', tuples have a different number of elements}}
246246
}
247247

248248
func patternInstantiationGenericValid<each T, each U>(t: repeat each T, u: repeat each U)
@@ -272,7 +272,7 @@ func patternInstantiationGenericInvalid<each T: Hashable>(t: repeat each T) {
272272
let _: (repeat Set<each T>) = patternInstantiationTupleTest1() // expected-error {{cannot convert value of type '(repeat Array<each T>)' to specified type '(repeat Set<each T>)}}
273273
// expected-error@-1 {{generic parameter 'each T' could not be inferred}}
274274

275-
let _: (repeat Array<each T>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<Pack{repeat each T, _}>)' is not convertible to '(repeat Array<each T>, Set<String>)', tuples have a different number of elements}}
275+
let _: (repeat Array<each T>, Set<String>) = patternInstantiationTupleTest1() // expected-error {{'(repeat Array<repeat each T, _>)' is not convertible to '(repeat Array<each T>, Set<String>)', tuples have a different number of elements}}
276276
}
277277

278278
// rdar://107996926 - Vanishing metatype of tuple not supported

0 commit comments

Comments
 (0)