Skip to content

[IRGen] Fix enum metadata instantiation for resilient payload #67349

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 1 commit into from
Jul 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
36 changes: 29 additions & 7 deletions lib/IRGen/GenMeta.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2581,16 +2581,18 @@ void irgen::emitLazyTypeContextDescriptor(IRGenModule &IGM,
auto genericSig =
lowered.getNominalOrBoundGenericNominal()->getGenericSignature();
hasLayoutString = !!typeLayoutEntry->layoutString(IGM, genericSig);
}

if (auto sd = dyn_cast<StructDecl>(type)) {
if (IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnessesInstantiation) &&
if (!hasLayoutString &&
IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
hasLayoutString |= requiresForeignTypeMetadata(type) ||
needsSingletonMetadataInitialization(IGM, type) ||
(type->isGenericContext() && !isa<FixedTypeInfo>(ti));
needsSingletonMetadataInitialization(IGM, type) ||
(type->isGenericContext() && !isa<FixedTypeInfo>(ti));
}
}

if (auto sd = dyn_cast<StructDecl>(type)) {
StructContextDescriptorBuilder(IGM, sd, requireMetadata,
hasLayoutString).emit();
} else if (auto ed = dyn_cast<EnumDecl>(type)) {
Expand Down Expand Up @@ -5487,6 +5489,26 @@ namespace {
return emitValueWitnessTable(relativeReference);
}

bool hasInstantiatedLayoutString() {
if (IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnessesInstantiation) &&
IGM.getOptions().EnableLayoutStringValueWitnessesInstantiation) {
return needsSingletonMetadataInitialization(IGM, Target);
}

return false;
}

bool hasLayoutString() {
if (!IGM.Context.LangOpts.hasFeature(
Feature::LayoutStringValueWitnesses) ||
!IGM.getOptions().EnableLayoutStringValueWitnesses) {
return false;
}

return hasInstantiatedLayoutString() || !!getLayoutString();
}

llvm::Constant *emitLayoutString() {
if (!IGM.Context.LangOpts.hasFeature(Feature::LayoutStringValueWitnesses) ||
!IGM.getOptions().EnableLayoutStringValueWitnesses)
Expand Down Expand Up @@ -5521,7 +5543,7 @@ namespace {

llvm::Constant *emitNominalTypeDescriptor() {
auto descriptor = EnumContextDescriptorBuilder(
IGM, Target, RequireMetadata, !!getLayoutString())
IGM, Target, RequireMetadata, hasLayoutString())
.emit();
return descriptor;
}
Expand Down Expand Up @@ -5570,7 +5592,7 @@ namespace {
}

bool canBeConstant() {
return !HasUnfilledPayloadSize;
return !HasUnfilledPayloadSize && !hasInstantiatedLayoutString();
}
};

Expand Down
2 changes: 2 additions & 0 deletions stdlib/public/runtime/Enum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,8 @@ XIElement findXIElement(const Metadata *type) {
void swift::swift_initEnumMetadataSinglePayloadWithLayoutString(
EnumMetadata *self, EnumLayoutFlags layoutFlags,
const Metadata *payloadType, unsigned emptyCases) {
assert(self->hasLayoutString());

auto *payloadLayout = payloadType->getTypeLayout();
size_t payloadSize = payloadLayout->size;
unsigned payloadNumExtraInhabitants = payloadLayout->getNumExtraInhabitants();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,3 +78,9 @@ public func getResilientSinglePayloadEnumSimpleEmpty0() -> ResilientSinglePayloa
public func getResilientSingletonEnumNonEmpty(_ x: AnyObject) -> ResilientSingletonEnum {
return .nonEmpty(x)
}

public enum ResilientSinglePayloadEnum {
case empty0
case empty1
case nonEmpty(AnyObject, Int)
}
16 changes: 16 additions & 0 deletions test/Interpreter/layout_string_witnesses_dynamic.swift
Original file line number Diff line number Diff line change
Expand Up @@ -686,6 +686,22 @@ func testResilientSingletonEnumGenericInjectTag() {

testResilientSingletonEnumGenericInjectTag()

enum ResilientPayloadSinglePayloadEnum {
case empty0
case empty1
case empty2
case nonEmpty(ResilientSinglePayloadEnum, Int)
}

func testResilientPayloadSinglePayloadEnum() {
let xxx = ResilientPayloadSinglePayloadEnum.nonEmpty(.empty0, 1)

// CHECK: nonEmpty(layout_string_witnesses_types_resilient.ResilientSinglePayloadEnum.empty0, 1)
print(xxx)
}

testResilientPayloadSinglePayloadEnum()

#if os(macOS)

import Foundation
Expand Down