Skip to content

Commit c564bd4

Browse files
committed
Fix a pair of IRGen bugs around materializing function type metadata
with sending results: - The sending result mangling was added in the 6.0 runtime, so demangling cannot be used to produce this metadata when targeting an earlier runtime. - The combination of a sending result with isolation requires the 6.1 runtime to successfully demangle, due to a bug in the 6.0 demangler.
1 parent c051edc commit c564bd4

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

lib/IRGen/GenReflection.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -182,9 +182,10 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
182182
Swift_5_2,
183183
Swift_5_5,
184184
Swift_6_0,
185+
Swift_6_1,
185186

186187
// Short-circuit if we find this requirement.
187-
Latest = Swift_6_0
188+
Latest = Swift_6_1
188189
};
189190

190191
VersionRequirement latestRequirement = None;
@@ -198,16 +199,29 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
198199

199200
(void) type.findIf([&](CanType t) -> bool {
200201
if (auto fn = dyn_cast<AnyFunctionType>(t)) {
202+
auto isolation = fn->getIsolation();
203+
auto sendingResult = fn->hasSendingResult();
204+
205+
// The Swift 6.1 runtime fixes a bug preventing successful demangling
206+
// when @isolated(any) or global actor isolation is combined with a
207+
// sending result.
208+
if (sendingResult &&
209+
(isolation.isErased() || isolation.isGlobalActor()))
210+
return addRequirement(Swift_6_1);
211+
201212
// The Swift 6.0 runtime is the first version able to demangle types
202-
// that involve typed throws or @isolated(any), or for that matter
203-
// represent them at all at runtime.
204-
if (!fn.getThrownError().isNull() || fn->getIsolation().isErased())
213+
// that involve typed throws, @isolated(any), or a sending result, or
214+
// for that matter to represent them at all at runtime.
215+
if (!fn.getThrownError().isNull() ||
216+
isolation.isErased() ||
217+
sendingResult)
205218
return addRequirement(Swift_6_0);
206219

207220
// The Swift 5.5 runtime is the first version able to demangle types
208221
// related to concurrency.
209-
if (fn->isAsync() || fn->isSendable() ||
210-
!fn->getIsolation().isNonIsolated())
222+
if (fn->isAsync() ||
223+
fn->isSendable() ||
224+
!isolation.isNonIsolated())
211225
return addRequirement(Swift_5_5);
212226

213227
return false;
@@ -255,6 +269,7 @@ getRuntimeVersionThatSupportsDemanglingType(CanType type) {
255269
});
256270

257271
switch (latestRequirement) {
272+
case Swift_6_1: return llvm::VersionTuple(6, 1);
258273
case Swift_6_0: return llvm::VersionTuple(6, 0);
259274
case Swift_5_5: return llvm::VersionTuple(5, 5);
260275
case Swift_5_2: return llvm::VersionTuple(5, 2);

lib/IRGen/MetadataRequest.cpp

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1674,12 +1674,6 @@ static MetadataResponse emitFunctionTypeMetadataRef(IRGenFunction &IGF,
16741674
}
16751675

16761676
default:
1677-
assert((!params.empty() || type->isDifferentiable() ||
1678-
!type->getIsolation().isNonIsolated() ||
1679-
type->getThrownError()) &&
1680-
"0 parameter case should be specialized unless it is a "
1681-
"differentiable function or has a global actor");
1682-
16831677
llvm::SmallVector<llvm::Value *, 8> arguments;
16841678

16851679
arguments.push_back(flagsVal);

test/IRGen/isolated_any_metadata.sil

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
1-
// RUN: %swift -emit-ir %s -target x86_64-apple-macosx10.10 -disable-legacy-type-info -parse-stdlib | %FileCheck -DINT=i64 %s -check-prefixes=CHECK,CHECK-ACCESSOR
2-
// RUN: %swift -emit-ir %s -target x86_64-unknown-linux-gnu -disable-legacy-type-info -parse-stdlib | %FileCheck -DINT=i64 %s -check-prefixes=CHECK,CHECK-DEMANGLE
1+
// RUN: %swift -emit-ir %s -target x86_64-apple-macosx10.10 -disable-legacy-type-info -parse-stdlib | %FileCheck -DINT=i64 %s -check-prefixes=CHECK,CHECK-ACCESSOR,CHECK-COMBO-ACCESSOR
2+
// RUN: %swift -emit-ir %s -target x86_64-apple-macos15.0 -disable-legacy-type-info -parse-stdlib | %FileCheck -DINT=i64 %s -check-prefixes=CHECK,CHECK-DEMANGLE,CHECK-COMBO-ACCESSOR
3+
// RUN: %swift -emit-ir %s -target x86_64-unknown-linux-gnu -disable-legacy-type-info -parse-stdlib | %FileCheck -DINT=i64 %s -check-prefixes=CHECK,CHECK-DEMANGLE,CHECK-COMBO-DEMANGLE
34

45
// REQUIRES: concurrency
56

67
sil_stage canonical
78

9+
struct NonSendable {}
10+
811
// CHECK-LABEL: define{{.*}} swiftcc ptr @get_metadata
912
// CHECK: entry:
1013
// CHECK-ACCESSOR-NEXT: [[T0:%.*]] = call swiftcc %swift.metadata_response @"$syyYbYAcMa"([[INT]] 0)
@@ -22,3 +25,20 @@ entry:
2225
// 3288334336 == 0xc4000000 == (ExtendedFlags | Sendable | Escaping)
2326
// 2 == IsolatedAny
2427
// CHECK-ACCESSOR: call ptr @swift_getExtendedFunctionTypeMetadata([[INT]] 3288334336, [[INT]] 0, ptr null, ptr null, ptr getelementptr inbounds {{.*}} @"$sytN"{{.*}}), ptr null, i32 2, ptr null)
28+
29+
// CHECK-LABEL: define{{.*}} swiftcc ptr @get_combo_metadata
30+
// CHECK: entry:
31+
// CHECK-COMBO-ACCESSOR-NEXT: [[T0:%.*]] = call swiftcc %swift.metadata_response @"$s21isolated_any_metadata11NonSendableVyYbYAYTcMa"([[INT]] 0)
32+
// CHECK-COMBO-ACCESSOR-NEXT: [[METADATA:%.]] = extractvalue %swift.metadata_response [[T0]], 0
33+
// CHECK-COMBO-DEMANGLE-NEXT: [[METADATA:%.*]] = call ptr @__swift_instantiateConcreteTypeFromMangledName(ptr @"$s21isolated_any_metadata11NonSendableVyYbYAYTcMD")
34+
// CHECK-NEXT: ret ptr [[METADATA]]
35+
sil @get_combo_metadata : $() -> @thick Any.Type {
36+
entry:
37+
%type = metatype $@thick (@isolated(any) @Sendable () -> sending NonSendable).Type
38+
%result = init_existential_metatype %type : $@thick (@isolated(any) @Sendable () -> sending NonSendable).Type, $@thick Any.Type
39+
return %result : $@thick Any.Type
40+
}
41+
42+
// CHECK-COMBO-ACCESSOR-LABEL: define{{.*}} swiftcc %swift.metadata_response @"$s21isolated_any_metadata11NonSendableVyYbYAYTcMa"
43+
// 18 == 0x12 == IsolatedAny | HasSendingResult
44+
// CHECK-COMBO-ACCESSOR: call ptr @swift_getExtendedFunctionTypeMetadata([[INT]] 3288334336, [[INT]] 0, ptr null, ptr null, ptr getelementptr inbounds {{.*}} @"$s21isolated_any_metadata11NonSendableVMf"{{.*}}), ptr null, i32 18, ptr null)

0 commit comments

Comments
 (0)