Skip to content

Commit

Permalink
Merge pull request #5528 from swiftwasm/main
Browse files Browse the repository at this point in the history
[pull] swiftwasm from main
  • Loading branch information
kateinoigakukun authored Jun 15, 2023
2 parents 113bf4e + fde2cf5 commit 35ad79e
Show file tree
Hide file tree
Showing 26 changed files with 219 additions and 84 deletions.
4 changes: 2 additions & 2 deletions cmake/modules/SwiftConfigureSDK.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -378,7 +378,7 @@ macro(configure_sdk_unix name architectures)
message(FATAL_ERROR "unknown arch for ${prefix}: ${arch}")
endif()
elseif("${prefix}" STREQUAL "FREEBSD")
if(NOT arch STREQUAL x86_64)
if(NOT arch MATCHES "(arm64|x86_64)")
message(FATAL_ERROR "unsupported arch for FreeBSD: ${arch}")
endif()

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

set(SWIFT_SDK_FREEBSD_ARCH_x86_64_TRIPLE "x86_64-unknown-freebsd${freebsd_system_version}")
set(SWIFT_SDK_FREEBSD_ARCH_${arch}_TRIPLE "${arch}-unknown-freebsd${freebsd_system_version}")
elseif("${prefix}" STREQUAL "OPENBSD")
if(NOT arch STREQUAL amd64)
message(FATAL_ERROR "unsupported arch for OpenBSD: ${arch}")
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/PrintOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -553,6 +553,12 @@ struct PrintOptions {
/// Whether to always desugar optional types from `base_type?` to `Optional<base_type>`
bool AlwaysDesugarOptionalTypes = false;

/// Whether to always print explicit `Pack{...}` around pack
/// types.
///
/// This is set to \c false for diagnostic arguments.
bool PrintExplicitPackTypes = true;

/// \see ShouldQualifyNestedDeclarations
enum class QualifyNestedDeclarations {
Never,
Expand Down Expand Up @@ -606,6 +612,7 @@ struct PrintOptions {
/// The print options used for formatting diagnostic arguments.
static PrintOptions forDiagnosticArguments() {
PrintOptions result;
result.PrintExplicitPackTypes = false;
return result;
}

Expand Down
5 changes: 5 additions & 0 deletions include/swift/Runtime/Enum.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@ void swift_initEnumMetadataSingleCase(EnumMetadata *enumType,
EnumLayoutFlags flags,
const TypeLayout *payload);

SWIFT_RUNTIME_EXPORT
void swift_initEnumMetadataSingleCaseWithLayoutString(
EnumMetadata *self, EnumLayoutFlags layoutFlags,
const Metadata *payloadType);

/// Initialize the type metadata for a single-payload enum type.
///
/// \param enumType - pointer to the instantiated but uninitialized metadata
Expand Down
11 changes: 11 additions & 0 deletions include/swift/Runtime/RuntimeFunctions.def
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,17 @@ FUNCTION(InitEnumMetadataSingleCase,
ATTRS(NoUnwind, WillReturn),
EFFECT(MetaData))

// void swift_initEnumMetadataSingleCaseWithLayoutString(Metadata *enumType,
// EnumLayoutFlags flags,
// Metadata *payload);
FUNCTION(InitEnumMetadataSingleCaseWithLayoutString,
swift_initEnumMetadataSingleCaseWithLayoutString,
C_CC, AlwaysAvailable,
RETURNS(VoidTy),
ARGS(TypeMetadataPtrTy, SizeTy, TypeMetadataPtrTy),
ATTRS(NoUnwind, WillReturn),
EFFECT(MetaData))

// void swift_initEnumMetadataSinglePayload(Metadata *enumType,
// EnumLayoutFlags flags,
// TypeLayout *payload,
Expand Down
7 changes: 5 additions & 2 deletions lib/AST/ASTPrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6051,7 +6051,8 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
}

void visitPackType(PackType *T) {
Printer << "Pack{";
if (Options.PrintExplicitPackTypes)
Printer << "Pack{";

auto Fields = T->getElementTypes();
for (unsigned i = 0, e = Fields.size(); i != e; ++i) {
Expand All @@ -6060,7 +6061,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
Type EltType = Fields[i];
visit(EltType);
}
Printer << "}";

if (Options.PrintExplicitPackTypes)
Printer << "}";
}

void visitSILPackType(SILPackType *T) {
Expand Down
75 changes: 48 additions & 27 deletions lib/IRGen/GenEnum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -663,13 +663,42 @@ namespace {
metadata);
}

void initializeMetadataWithLayoutString(IRGenFunction &IGF,
llvm::Value *metadata,
bool isVWTMutable,
SILType T,
MetadataDependencyCollector *collector) const override {
// Not yet supported on this type, so forward to regular method
initializeMetadata(IGF, metadata, isVWTMutable, T, collector);
void initializeMetadataWithLayoutString(
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
MetadataDependencyCollector *collector) const override {
if (TIK >= Fixed)
return;

assert(ElementsWithPayload.size() == 1 &&
"empty singleton enum should not be dynamic!");

auto payloadTy =
T.getEnumElementType(ElementsWithPayload[0].decl, IGM.getSILModule(),
IGM.getMaximalTypeExpansionContext());

auto request = DynamicMetadataRequest::getNonBlocking(
MetadataState::LayoutComplete, collector);
auto payloadMetadata =
IGF.emitTypeMetadataRefForLayout(payloadTy, request);

auto flags = emitEnumLayoutFlags(IGF.IGM, isVWTMutable);
IGF.Builder.CreateCall(
IGF.IGM
.getInitEnumMetadataSingleCaseWithLayoutStringFunctionPointer(),
{metadata, flags, payloadMetadata});

// Pre swift-5.1 runtimes were missing the initialization of the
// the extraInhabitantCount field. Do it here instead.
auto payloadLayout = emitTypeLayoutRef(IGF, payloadTy, collector);
auto payloadRef = IGF.Builder.CreateBitOrPointerCast(
payloadLayout, IGF.IGM.TypeLayoutTy->getPointerTo());
auto payloadExtraInhabitantCount =
IGF.Builder.CreateLoad(IGF.Builder.CreateStructGEP(
Address(payloadRef, IGF.IGM.TypeLayoutTy, Alignment(1)), 3,
Size(IGF.IGM.DataLayout.getTypeAllocSize(IGF.IGM.SizeTy) * 2 +
IGF.IGM.DataLayout.getTypeAllocSize(IGF.IGM.Int32Ty))));
emitStoreOfExtraInhabitantCount(IGF, payloadExtraInhabitantCount,
metadata);
}

bool mayHaveExtraInhabitants(IRGenModule &IGM) const override {
Expand Down Expand Up @@ -973,11 +1002,9 @@ namespace {
// witness table initialization.
}

void initializeMetadataWithLayoutString(IRGenFunction &IGF,
llvm::Value *metadata,
bool isVWTMutable,
SILType T,
MetadataDependencyCollector *collector) const override {
void initializeMetadataWithLayoutString(
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
MetadataDependencyCollector *collector) const override {
// No-payload enums are always fixed-size so never need dynamic value
// witness table initialization.
}
Expand Down Expand Up @@ -3207,11 +3234,9 @@ namespace {
{metadata, flags, payloadLayout, emptyCasesVal});
}

void initializeMetadataWithLayoutString(IRGenFunction &IGF,
llvm::Value *metadata,
bool isVWTMutable,
SILType T,
MetadataDependencyCollector *collector) const override {
void initializeMetadataWithLayoutString(
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
MetadataDependencyCollector *collector) const override {
// Not yet supported on this type, so forward to regular method
initializeMetadata(IGF, metadata, isVWTMutable, T, collector);
}
Expand Down Expand Up @@ -5335,11 +5360,9 @@ namespace {
{metadata, flags, numPayloadsVal, payloadLayoutArray});
}

void initializeMetadataWithLayoutString(IRGenFunction &IGF,
llvm::Value *metadata,
bool isVWTMutable,
SILType T,
MetadataDependencyCollector *collector) const override {
void initializeMetadataWithLayoutString(
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
MetadataDependencyCollector *collector) const override {
// Fixed-size enums don't need dynamic metadata initialization.
if (TIK >= Fixed) return;

Expand Down Expand Up @@ -6035,11 +6058,9 @@ namespace {
llvm_unreachable("resilient enums cannot be defined");
}

void initializeMetadataWithLayoutString(IRGenFunction &IGF,
llvm::Value *metadata,
bool isVWTMutable,
SILType T,
MetadataDependencyCollector *collector) const override {
void initializeMetadataWithLayoutString(
IRGenFunction &IGF, llvm::Value *metadata, bool isVWTMutable, SILType T,
MetadataDependencyCollector *collector) const override {
llvm_unreachable("resilient enums cannot be defined");
}

Expand Down
11 changes: 7 additions & 4 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5737,14 +5737,17 @@ namespace {
}

auto &strategy = getEnumImplStrategy(IGM, getLoweredType());
bool isSupportedCase = strategy.getElementsWithPayload().size() > 1 ||
(strategy.getElementsWithPayload().size() == 1 &&
strategy.getElementsWithNoPayload().empty());

return !!getLayoutString() ||
(IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation &&
(HasDependentVWT || HasDependentMetadata) &&
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())) &&
strategy.getElementsWithPayload().size() > 1);
(HasDependentVWT || HasDependentMetadata) &&
!isa<FixedTypeInfo>(IGM.getTypeInfo(getLoweredType())) &&
isSupportedCase);
}

llvm::Constant *emitNominalTypeDescriptor() {
Expand Down
2 changes: 1 addition & 1 deletion lib/IRGen/GenValueWitness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -892,7 +892,7 @@ bool isRuntimeInstatiatedLayoutString(IRGenModule &IGM,
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
if (auto *enumEntry = typeLayoutEntry->getAsEnum()) {
return enumEntry->isMultiPayloadEnum();
return enumEntry->isMultiPayloadEnum() || enumEntry->isSingleton();
}
return (typeLayoutEntry->isAlignedGroup() &&
!typeLayoutEntry->isFixedSize(IGM));
Expand Down
8 changes: 0 additions & 8 deletions lib/Sema/CSDiagnostics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,6 @@ Type FailureDiagnostic::resolveType(Type rawType, bool reconstituteSugar,
return env->mapElementTypeIntoPackContext(type);
}

if (auto *packType = type->getAs<PackType>()) {
if (packType->getNumElements() == 1) {
auto eltType = resolveType(packType->getElementType(0));
if (auto expansion = eltType->getAs<PackExpansionType>())
return expansion->getPatternType();
}
}

return type->isPlaceholder() ? Type(type->getASTContext().TheUnresolvedType)
: type;
});
Expand Down
15 changes: 2 additions & 13 deletions lib/Sema/TypeCheckGeneric.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -838,6 +838,7 @@ static std::string gatherGenericParamBindingsText(

SmallString<128> result;
llvm::raw_svector_ostream OS(result);
auto options = PrintOptions::forDiagnosticArguments();

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

if (auto *packType = type->getAs<PackType>()) {
bool first = true;
for (auto eltType : packType->getElementTypes()) {
if (first)
first = false;
else
OS << ", ";

OS << eltType;
}
} else {
OS << type.getString();
}
type->print(OS, options);
}

OS << "]";
Expand Down
57 changes: 57 additions & 0 deletions stdlib/public/runtime/Enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,61 @@ swift::swift_initEnumMetadataSingleCase(EnumMetadata *self,
vwtable->publishLayout(layout);
}

void swift::swift_initEnumMetadataSingleCaseWithLayoutString(
EnumMetadata *self, EnumLayoutFlags layoutFlags,
const Metadata *payloadType) {
assert(self->hasLayoutString());

auto payloadLayout = payloadType->getTypeLayout();
auto vwtable = getMutableVWTableForInit(self, layoutFlags);

TypeLayout layout;
layout.size = payloadLayout->size;
layout.stride = payloadLayout->stride;
layout.flags = payloadLayout->flags.withEnumWitnesses(true);
layout.extraInhabitantCount = payloadLayout->getNumExtraInhabitants();

auto refCountBytes = _swift_refCountBytesForMetatype(payloadType);
const size_t fixedLayoutStringSize =
layoutStringHeaderSize + sizeof(uint64_t) * 2;

uint8_t *layoutStr =
(uint8_t *)MetadataAllocator(LayoutStringTag)
.Allocate(fixedLayoutStringSize + refCountBytes, alignof(uint8_t));

size_t layoutStrOffset = sizeof(uint64_t);
writeBytes(layoutStr, layoutStrOffset, refCountBytes);
size_t fullOffset = 0;
size_t previousFieldOffset = 0;
LayoutStringFlags flags = LayoutStringFlags::Empty;

_swift_addRefCountStringForMetatype(layoutStr, layoutStrOffset, flags,
payloadType, fullOffset,
previousFieldOffset);

writeBytes(layoutStr, layoutStrOffset, (uint64_t)previousFieldOffset);
writeBytes(layoutStr, layoutStrOffset, (uint64_t)0);

// we mask out HasRelativePointers, because at this point they have all been
// resolved to metadata pointers
layoutStrOffset = 0;
writeBytes(layoutStr, layoutStrOffset,
((uint64_t)flags) &
~((uint64_t)LayoutStringFlags::HasRelativePointers));

vwtable->destroy = swift_generic_destroy;
vwtable->initializeWithCopy = swift_generic_initWithCopy;
vwtable->initializeWithTake = swift_generic_initWithTake;
vwtable->assignWithCopy = swift_generic_assignWithCopy;
vwtable->assignWithTake = swift_generic_assignWithTake;

installCommonValueWitnesses(layout, vwtable);

self->setLayoutString(layoutStr);

vwtable->publishLayout(layout);
}

void
swift::swift_initEnumMetadataSinglePayload(EnumMetadata *self,
EnumLayoutFlags layoutFlags,
Expand Down Expand Up @@ -221,6 +276,8 @@ void swift::swift_initEnumMetadataMultiPayloadWithLayoutString(
EnumLayoutFlags layoutFlags,
unsigned numPayloads,
const Metadata * const *payloadLayouts) {
assert(enumType->hasLayoutString());

// Accumulate the layout requirements of the payloads.
size_t payloadSize = 0, alignMask = 0;
bool isPOD = true, isBT = true;
Expand Down
4 changes: 2 additions & 2 deletions stdlib/public/runtime/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2750,8 +2750,8 @@ void swift::swift_initStructMetadataWithLayoutString(
previousFieldOffset);
}

writeBytes(layoutStr, layoutStrOffset, previousFieldOffset);
writeBytes(layoutStr, layoutStrOffset, 0);
writeBytes(layoutStr, layoutStrOffset, (uint64_t)previousFieldOffset);
writeBytes(layoutStr, layoutStrOffset, (uint64_t)0);

// we mask out HasRelativePointers, because at this point they have all been
// resolved to metadata pointers
Expand Down
2 changes: 1 addition & 1 deletion test/Constraints/pack-expansion-expressions.swift
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func tupleExpansion<each T, each U>(
_ = zip(repeat each tuple1, with: repeat each tuple1.element) // legacy syntax

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

_ = forward(repeat each tuple3)
}
Expand Down
6 changes: 3 additions & 3 deletions test/Constraints/pack_expansion_types.swift
Original file line number Diff line number Diff line change
Expand Up @@ -240,9 +240,9 @@ func patternInstantiationConcreteValid() {

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

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}}
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}}
}

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

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}}
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}}
}

// rdar://107996926 - Vanishing metatype of tuple not supported
Expand Down
Loading

0 comments on commit 35ad79e

Please sign in to comment.