Skip to content

[IRGen][WMO] type descriptors generated for an imported Clang types a… #70977

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
Jan 19, 2024
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
3 changes: 3 additions & 0 deletions include/swift/IRGen/Linking.h
Original file line number Diff line number Diff line change
Expand Up @@ -1597,6 +1597,9 @@ class LinkEntity {
getKind() == Kind::DispatchThunkAllocator ||
getKind() == Kind::DispatchThunkDerivative;
}
bool isNominalTypeDescriptor() const {
return getKind() == Kind::NominalTypeDescriptor;
}

/// Determine whether this entity will be weak-imported.
bool isWeakImported(ModuleDecl *module) const;
Expand Down
11 changes: 11 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2403,6 +2403,17 @@ LinkInfo LinkInfo::get(const UniversalLinkageInfo &linkInfo,
if (const auto *DC = entity.getDeclContextForEmission()) {
if (const auto *MD = DC->getParentModule())
isKnownLocal = MD == swiftModule || MD->isStaticLibrary();
if (!isKnownLocal && !isDefinition) {
bool isClangImportedEntity =
isa<ClangModuleUnit>(DC->getModuleScopeContext());
// Nominal type descriptor for a type imported from a Clang module
// is always a local declaration as it's generated on demand. When WMO is
// off, it's emitted into the current file's object file. When WMO is on,
// it's emitted into one of the object files in the current module, and
// thus it's never imported from outside of the module.
if (isClangImportedEntity && entity.isNominalTypeDescriptor())
isKnownLocal = true;
}
} else if (entity.hasSILFunction()) {
// SIL serialized entities (functions, witness tables, vtables) do not have
// an associated DeclContext and are serialized into the current module. As
Expand Down
53 changes: 53 additions & 0 deletions test/IRGen/imported_clang_type_metadata_is_local.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend -c -num-threads 1 -emit-ir -wmo -O %t/useA.swift %t/useB.swift -I %t/Inputs -o %t/useA.swift.ir -o %t/useB.swift.ir
// RUN: cat %t/useB.swift.ir | %FileCheck %s

//--- Inputs/module.modulemap
module ClangModule {
header "header.h"
}

//--- Inputs/header.h

struct ImportedClangType {
int x;
};

//--- useA.swift

import ClangModule

func testA<T>(_ x: T) {
print(x)
}

public func testARun() {
testA(ImportedClangType(x: 0))
}

public struct TestUseFieldA {
let x: ImportedClangType = ImportedClangType()
}

//--- useB.swift

import ClangModule

func testB<T>(_ x: T) {
print(x)
}

public func testBRun() {
testB(ImportedClangType(x: 0))
}

public struct TestUseFieldB {
let x: ImportedClangType = ImportedClangType()
}

// The type metadata emitted for TU B, refers to the metadata from TU A,
// but it doesn't need to dllimport it across the module boundary, as it's
// emitted in the same module.
// CHECK: @"$sSo17ImportedClangTypeVMn" = external global %swift.type_descriptor