Merge pull request #85914 from aschwaighofer/embedded_foreign_type_metadata

[embedded] Add support for some foreign metadata
This commit is contained in:
Arnold Schwaighofer
2025-12-10 06:58:55 -08:00
committed by GitHub
4 changed files with 166 additions and 15 deletions

View File

@@ -5308,7 +5308,8 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(
return cast<llvm::GlobalValue>(addr);
}
bool hasEmbeddedExistentials =
Context.LangOpts.hasFeature(Feature::EmbeddedExistentials);
auto entity =
(isPrespecialized &&
!irgen::isCanonicalInitializableTypeMetadataStaticallyAddressable(
@@ -5324,7 +5325,7 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(
if (Context.LangOpts.hasFeature(Feature::Embedded)) {
entity = LinkEntity::forTypeMetadata(concreteType,
TypeMetadataAddress::AddressPoint);
if (Context.LangOpts.hasFeature(Feature::EmbeddedExistentials))
if (hasEmbeddedExistentials)
entity = LinkEntity::forTypeMetadata(concreteType,
TypeMetadataAddress::FullMetadata);
}
@@ -5350,7 +5351,7 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(
markGlobalAsUsedBasedOnLinkage(*this, link, var);
if (Context.LangOpts.hasFeature(Feature::Embedded) &&
!Context.LangOpts.hasFeature(Feature::EmbeddedExistentials)) {
!hasEmbeddedExistentials) {
return var;
}
@@ -5361,14 +5362,13 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(
if (auto nominal = concreteType->getAnyNominal()) {
// Keep type metadata around for all types (except @_objcImplementation,
// since we're using ObjC metadata for that).
if (!isObjCImpl &&
!Context.LangOpts.hasFeature(Feature::EmbeddedExistentials))
if (!isObjCImpl && !hasEmbeddedExistentials)
addRuntimeResolvableType(nominal);
// Don't define the alias for foreign type metadata, prespecialized
// generic metadata, or @_objcImplementation classes, since they're not ABI.
if (requiresForeignTypeMetadata(nominal) ||
(isPrespecialized && !Context.LangOpts.hasFeature(Feature::EmbeddedExistentials)) ||
if ((requiresForeignTypeMetadata(nominal) && !hasEmbeddedExistentials) ||
(isPrespecialized && !hasEmbeddedExistentials) ||
isObjCImpl)
return var;
@@ -5382,7 +5382,7 @@ llvm::GlobalValue *IRGenModule::defineTypeMetadata(
}
}
if (Context.LangOpts.hasFeature(Feature::EmbeddedExistentials)) {
if (hasEmbeddedExistentials) {
adjustmentIndex = MetadataAdjustmentIndex::EmbeddedWithExistentials;
}

View File

@@ -7202,17 +7202,23 @@ void irgen::emitForeignTypeMetadata(IRGenModule &IGM, NominalTypeDecl *decl) {
auto init = builder.beginStruct();
init.setPacked(true);
auto isEmbedded =
IGM.Context.LangOpts.hasFeature(Feature::EmbeddedExistentials);
if (auto classDecl = dyn_cast<ClassDecl>(decl)) {
if (classDecl->isForeignReferenceType()) {
assert(!isEmbedded && "emitting foregin reference type not supported");
ForeignReferenceTypeMetadataBuilder builder(IGM, classDecl, init);
builder.layout();
IGM.defineTypeMetadata(type, /*isPattern=*/false,
builder.canBeConstant(),
init.finishAndCreateFuture());
builder.createMetadataAccessFunction();
if (!isEmbedded)
builder.createMetadataAccessFunction();
} else {
assert(classDecl->getForeignClassKind() == ClassDecl::ForeignKind::CFType);
assert(!isEmbedded && "emitting foregin cf class type not supported");
ForeignClassMetadataBuilder builder(IGM, classDecl, init);
builder.layout();
@@ -7220,28 +7226,37 @@ void irgen::emitForeignTypeMetadata(IRGenModule &IGM, NominalTypeDecl *decl) {
IGM.defineTypeMetadata(type, /*isPattern=*/false,
builder.canBeConstant(),
init.finishAndCreateFuture());
builder.createMetadataAccessFunction();
if (!isEmbedded)
builder.createMetadataAccessFunction();
}
} else if (auto structDecl = dyn_cast<StructDecl>(decl)) {
assert(isa<ClangModuleUnit>(structDecl->getModuleScopeContext()));
ForeignStructMetadataBuilder builder(IGM, structDecl, init);
builder.layout();
if (isEmbedded)
builder.embeddedLayout();
else
builder.layout();
IGM.defineTypeMetadata(type, /*isPattern=*/false,
builder.canBeConstant(),
init.finishAndCreateFuture());
builder.createMetadataAccessFunction();
if (!isEmbedded)
builder.createMetadataAccessFunction();
} else if (auto enumDecl = dyn_cast<EnumDecl>(decl)) {
assert(enumDecl->hasClangNode());
ForeignEnumMetadataBuilder builder(IGM, enumDecl, init);
builder.layout();
if (isEmbedded)
builder.embeddedLayout();
else
builder.layout();
IGM.defineTypeMetadata(type, /*isPattern=*/false,
builder.canBeConstant(),
init.finishAndCreateFuture());
builder.createMetadataAccessFunction();
if (!isEmbedded)
builder.createMetadataAccessFunction();
} else {
llvm_unreachable("foreign metadata for unexpected type?!");
}