Skip to content

IRGen: Fix anonymous context descriptors of alwaysEmitIntoClient functions #78017

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
Dec 10, 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
6 changes: 6 additions & 0 deletions lib/IRGen/GenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -809,6 +809,12 @@ IRGenModule::getAddrOfContextDescriptorForParent(DeclContext *parent,

case DeclContextKind::GenericTypeDecl:
if (auto nomTy = dyn_cast<NominalTypeDecl>(parent)) {
if (nomTy->getDeclContext()->getParentModule() != getSwiftModule() &&
fromAnonymousContext) {
// Can't emit a direct reference.
auto entity = LinkEntity::forNominalTypeDescriptor(nomTy);
return getAddrOfLLVMVariableOrGOTEquivalent(entity);
}
return {getAddrOfTypeContextDescriptor(nomTy, DontRequireMetadata),
ConstantReference::Direct};
}
Expand Down
79 changes: 79 additions & 0 deletions test/IRGen/anonymous_context_descriptors.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// RUN: %empty-directory(%t)
// RUN: split-file %s %t

// RUN: %target-swift-frontend \
// RUN: %t/C.swift \
// RUN: -emit-module \
// RUN: -enable-library-evolution \
// RUN: -module-name C \
// RUN: -emit-module-path %t/C.swiftmodule

// RUN: %target-swift-frontend \
// RUN: %t/A.swift \
// RUN: -enable-anonymous-context-mangled-names \
// RUN: -I %t -emit-ir | %FileCheck %s

// REQUIRES: PTRSIZE=64
// REQUIRES: OS=macosx || OS=ios
// UNSUPPORTED: CPU=arm64e

//--- A.swift
import C
public func callStuff() {
var x: any P = Empty()
if #available(iOS 17.3, macOS 14.3, *) {
x = MyBuilder.f(Empty())
x = MyBuilder.h(Empty())
}
print(x)
}

// Make sure that when we generate an anoynmous context descriptor its parent
// relative reference to another module uses an indirect relative reference.

// CHECK: @"$s1C9MyBuilderVMn" = external global %swift.type_descriptor
// CHECK: @"got.$s1C9MyBuilderVMn" = private unnamed_addr constant ptr @"$s1C9MyBuilderVMn"
// CHECK: @"$s1C9MyBuilderV1fyQrAA1P_pFZMXX" = linkonce_odr hidden constant <{ i32, i32, i32 }> <{ i32 {{[0-9]+}}, i32 add (i32 trunc (i64 sub (i64 ptrtoint (ptr @"got.$s1C9MyBuilderVMn"
// CHECK: @"$s1C9MyBuilderV1hyQrAA1P_pFZMXX" = linkonce_odr hidden constant <{ i32, i32, i32 }> <{ i32 {{[0-9]+}}, i32 add (i32 trunc (i64 sub (i64 ptrtoint (ptr @"got.$s1C9MyBuilderVMn"

//--- C.swift
public protocol P {
func g()
}

public struct Empty : P {
public init() {}
public func g() {}
}

@available(iOS 17.3, macOS 14.3, *)
public struct S : P {
public init<T: P>(_ c: T) {}

public func g() {
print("g")
}
}

public struct MyBuilder {
@_alwaysEmitIntoClient
@available(iOS 17.3, macOS 14.3, *)
public static func f(_ content: any P) -> some P {
if #unavailable(iOS 17.3, macOS 14.3) {
return Empty()
}
return S(content)
}
}


extension MyBuilder {
@_alwaysEmitIntoClient
@available(iOS 17.3, macOS 14.3, *)
public static func h(_ content: any P) -> some P {
if #unavailable(iOS 17.3, macOS 14.3) {
return Empty()
}
return S(content)
}
}