Skip to content

Commit fe00d20

Browse files
Merge pull request #78017 from aschwaighofer/anon_context_desc
IRGen: Fix anonymous context descriptors of alwaysEmitIntoClient functions
2 parents 18669a0 + d31e4a5 commit fe00d20

File tree

2 files changed

+85
-0
lines changed

2 files changed

+85
-0
lines changed

lib/IRGen/GenDecl.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -809,6 +809,12 @@ IRGenModule::getAddrOfContextDescriptorForParent(DeclContext *parent,
809809

810810
case DeclContextKind::GenericTypeDecl:
811811
if (auto nomTy = dyn_cast<NominalTypeDecl>(parent)) {
812+
if (nomTy->getDeclContext()->getParentModule() != getSwiftModule() &&
813+
fromAnonymousContext) {
814+
// Can't emit a direct reference.
815+
auto entity = LinkEntity::forNominalTypeDescriptor(nomTy);
816+
return getAddrOfLLVMVariableOrGOTEquivalent(entity);
817+
}
812818
return {getAddrOfTypeContextDescriptor(nomTy, DontRequireMetadata),
813819
ConstantReference::Direct};
814820
}
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: split-file %s %t
3+
4+
// RUN: %target-swift-frontend \
5+
// RUN: %t/C.swift \
6+
// RUN: -emit-module \
7+
// RUN: -enable-library-evolution \
8+
// RUN: -module-name C \
9+
// RUN: -emit-module-path %t/C.swiftmodule
10+
11+
// RUN: %target-swift-frontend \
12+
// RUN: %t/A.swift \
13+
// RUN: -enable-anonymous-context-mangled-names \
14+
// RUN: -I %t -emit-ir | %FileCheck %s
15+
16+
// REQUIRES: PTRSIZE=64
17+
// REQUIRES: OS=macosx || OS=ios
18+
// UNSUPPORTED: CPU=arm64e
19+
20+
//--- A.swift
21+
import C
22+
public func callStuff() {
23+
var x: any P = Empty()
24+
if #available(iOS 17.3, macOS 14.3, *) {
25+
x = MyBuilder.f(Empty())
26+
x = MyBuilder.h(Empty())
27+
}
28+
print(x)
29+
}
30+
31+
// Make sure that when we generate an anoynmous context descriptor its parent
32+
// relative reference to another module uses an indirect relative reference.
33+
34+
// CHECK: @"$s1C9MyBuilderVMn" = external global %swift.type_descriptor
35+
// CHECK: @"got.$s1C9MyBuilderVMn" = private unnamed_addr constant ptr @"$s1C9MyBuilderVMn"
36+
// CHECK: @"$s1C9MyBuilderV1fyQrAA1P_pFZMXX" = linkonce_odr hidden constant <{ i32, i32, i32 }> <{ i32 {{[0-9]+}}, i32 add (i32 trunc (i64 sub (i64 ptrtoint (ptr @"got.$s1C9MyBuilderVMn"
37+
// CHECK: @"$s1C9MyBuilderV1hyQrAA1P_pFZMXX" = linkonce_odr hidden constant <{ i32, i32, i32 }> <{ i32 {{[0-9]+}}, i32 add (i32 trunc (i64 sub (i64 ptrtoint (ptr @"got.$s1C9MyBuilderVMn"
38+
39+
//--- C.swift
40+
public protocol P {
41+
func g()
42+
}
43+
44+
public struct Empty : P {
45+
public init() {}
46+
public func g() {}
47+
}
48+
49+
@available(iOS 17.3, macOS 14.3, *)
50+
public struct S : P {
51+
public init<T: P>(_ c: T) {}
52+
53+
public func g() {
54+
print("g")
55+
}
56+
}
57+
58+
public struct MyBuilder {
59+
@_alwaysEmitIntoClient
60+
@available(iOS 17.3, macOS 14.3, *)
61+
public static func f(_ content: any P) -> some P {
62+
if #unavailable(iOS 17.3, macOS 14.3) {
63+
return Empty()
64+
}
65+
return S(content)
66+
}
67+
}
68+
69+
70+
extension MyBuilder {
71+
@_alwaysEmitIntoClient
72+
@available(iOS 17.3, macOS 14.3, *)
73+
public static func h(_ content: any P) -> some P {
74+
if #unavailable(iOS 17.3, macOS 14.3) {
75+
return Empty()
76+
}
77+
return S(content)
78+
}
79+
}

0 commit comments

Comments
 (0)