Skip to content

Commit a9a1a15

Browse files
Merge pull request #39076 from varungandhi-apple/vg-concurrency-mangling
[IRGen] Support back-deployment of concurrency-related function types.
2 parents d9110de + b519489 commit a9a1a15

File tree

3 files changed

+88
-2
lines changed

3 files changed

+88
-2
lines changed

lib/IRGen/GenReflection.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -191,6 +191,17 @@ swift::irgen::getRuntimeVersionThatSupportsDemanglingType(CanType type) {
191191
// guards, so we don't need to limit availability of mangled names
192192
// involving them.
193193
}
194+
195+
bool needsConcurrency = type.findIf([](CanType t) -> bool {
196+
if (auto fn = dyn_cast<AnyFunctionType>(t)) {
197+
return fn->isAsync() || fn->isSendable() || fn->hasGlobalActor();
198+
}
199+
return false;
200+
});
201+
if (needsConcurrency) {
202+
return llvm::VersionTuple(5, 5);
203+
}
204+
194205
return None;
195206
}
196207

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-swift-frontend %s -target x86_64-apple-macosx12.0 -module-name main -emit-ir -o %t/new.ir
3+
// RUN: %FileCheck %s --check-prefix=NEW < %t/new.ir
4+
// RUN: %target-swift-frontend %s -target x86_64-apple-macosx10.15 -module-name main -emit-ir -o %t/old.ir -disable-availability-checking
5+
// RUN: %FileCheck %s --check-prefix=OLD < %t/old.ir
6+
7+
// Check that we add extra type metadata accessors for new kinds of functions
8+
// when back-deploying. These are used instead of using demangling cache
9+
// variables since old runtimes cannot synthesize type metadata based on the
10+
// new mangling.
11+
12+
// RUN: %target-build-swift -target x86_64-apple-macosx10.15 %s -o %t/test_mangling -Xfrontend -disable-availability-checking
13+
// RUN: %target-run %t/test_mangling
14+
15+
// REQUIRES: OS=macosx
16+
// REQUIRES: executable_test
17+
18+
protocol MyProtocol {
19+
associatedtype AssocSendable
20+
associatedtype AssocAsync
21+
associatedtype AssocGlobalActor
22+
}
23+
24+
typealias SendableFn = @Sendable () -> Void
25+
typealias AsyncFn = () async -> Void
26+
typealias GlobalActorFn = @MainActor () -> Void
27+
28+
struct MyStruct: MyProtocol {
29+
typealias AssocSendable = SendableFn
30+
typealias AssocAsync = AsyncFn
31+
typealias AssocGlobalActor = GlobalActorFn
32+
}
33+
34+
func assocSendable<T: MyProtocol>(_: T.Type) -> Any.Type { return T.AssocSendable.self }
35+
func assocAsync<T: MyProtocol>(_: T.Type) -> Any.Type { return T.AssocAsync.self }
36+
func assocGlobalActor<T: MyProtocol>(_: T.Type) -> Any.Type { return T.AssocGlobalActor.self }
37+
38+
assert(assocSendable(MyStruct.self) == SendableFn.self)
39+
assert(assocAsync(MyStruct.self) == AsyncFn.self)
40+
assert(assocGlobalActor(MyStruct.self) == GlobalActorFn.self)
41+
42+
// type metadata accessor for @Sendable () -> ()
43+
// OLD: define linkonce_odr hidden swiftcc %swift.metadata_response @"$syyYbcMa"
44+
// NEW-NOT: define linkonce_odr hidden swiftcc %swift.metadata_response @"$syyYbcMa"
45+
46+
// type metadata accessor for () async -> ()
47+
// OLD: define linkonce_odr hidden swiftcc %swift.metadata_response @"$syyYacMa"
48+
// NEW-NOT: define linkonce_odr hidden swiftcc %swift.metadata_response @"$syyYacMa"
49+
50+
// type metadata accessor for @MainActor () -> ()
51+
// OLD: define linkonce_odr hidden swiftcc %swift.metadata_response @"$syyScMYccMa"
52+
// NEW-NOT: define linkonce_odr hidden swiftcc %swift.metadata_response @"$syyScMYccMa"
53+
54+
// OLD: call swiftcc %swift.metadata_response @"$syyYbcMa"
55+
// OLD-NOT: call %swift.type* @__swift_instantiateConcreteTypeFromMangledName({ i32, i32 }* @"$syyYbcMD")
56+
57+
// NEW-NOT: call swiftcc %swift.metadata_response @"$syyYbcMa"
58+
// NEW: call %swift.type* @__swift_instantiateConcreteTypeFromMangledName({ i32, i32 }* @"$syyYbcMD")
59+
60+
// OLD: call swiftcc %swift.metadata_response @"$syyYacMa"
61+
// OLD-NOT: %swift.type* @__swift_instantiateConcreteTypeFromMangledName({ i32, i32 }* @"$syyYacMD")
62+
63+
// NEW-NOT: call swiftcc %swift.metadata_response @"$syyYacMa"
64+
// NEW: call %swift.type* @__swift_instantiateConcreteTypeFromMangledName({ i32, i32 }* @"$syyYacMD")
65+
66+
// OLD: call swiftcc %swift.metadata_response @"$syyScMYccMa"
67+
// OLD-NOT: call %swift.type* @__swift_instantiateConcreteTypeFromMangledName({ i32, i32 }* @"$syyScMYccMD")
68+
69+
// NEW-NOT: call swiftcc %swift.metadata_response @"$syyScMYccMa"
70+
// NEW: call %swift.type* @__swift_instantiateConcreteTypeFromMangledName({ i32, i32 }* @"$syyScMYccMD")

test/Reflection/typeref_decoding_concurrency.swift

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,19 @@
33
// REQUIRES: concurrency
44
// REQUIRES: libdispatch
55

6+
// REQUIRES: OS=macosx
7+
68
// rdar://76038845
79
// UNSUPPORTED: use_os_stdlib
810
// UNSUPPORTED: back_deployment_runtime
911

1012
// RUN: %empty-directory(%t)
1113

12-
// RUN: %target-build-swift -Xfrontend -enable-anonymous-context-mangled-names %S/Inputs/ConcurrencyTypes.swift -parse-as-library -emit-module -emit-library -module-name TypesToReflect -o %t/%target-library-name(TypesToReflect)
13-
// RUN: %target-build-swift -Xfrontend -enable-anonymous-context-mangled-names %S/Inputs/ConcurrencyTypes.swift %S/Inputs/main.swift -emit-module -emit-executable -module-name TypesToReflect -o %t/TypesToReflect
14+
// RUN: %target-build-swift -Xfrontend -enable-anonymous-context-mangled-names %S/Inputs/ConcurrencyTypes.swift -parse-as-library -emit-module -emit-library -module-name TypesToReflect -o %t/%target-library-name(TypesToReflect) -target x86_64-apple-macosx12.0
15+
// RUN: %target-build-swift -Xfrontend -enable-anonymous-context-mangled-names %S/Inputs/ConcurrencyTypes.swift %S/Inputs/main.swift -emit-module -emit-executable -module-name TypesToReflect -o %t/TypesToReflect -target x86_64-apple-macosx12.0
16+
17+
// For macOS versions before 12.0, the mangling for concurrency-related
18+
// types cannot be used to create type metadata.
1419

1520
// RUN: %target-swift-reflection-dump -binary-filename %t/%target-library-name(TypesToReflect) | %FileCheck %s
1621
// RUN: %target-swift-reflection-dump -binary-filename %t/TypesToReflect | %FileCheck %s

0 commit comments

Comments
 (0)