Skip to content

Commit 80e23d3

Browse files
authored
Merge pull request #74916 from rjmccall/strip-isolated-any-reflection-metadata
Suppress `@isolated(any)` in reflective metadata strings on old targets
2 parents c20abe5 + 2c6e0ef commit 80e23d3

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

Diff for: include/swift/AST/ASTMangler.h

+8
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,14 @@ class ASTMangler : public Mangler {
7575
/// If enabled, inverses will not be mangled into generic signatures.
7676
bool AllowInverses = true;
7777

78+
/// If enabled, @isolated(any) can be encoded in the mangled name.
79+
/// Suppressing type attributes this way is generally questionable ---
80+
/// for example, it does not interact properly with substitutions ---
81+
/// and should only be done in situations where it is just going to be
82+
/// interpreted as a type and the exact string value does not play
83+
/// a critical role.
84+
bool AllowIsolatedAny = true;
85+
7886
/// If enabled, declarations annotated with @_originallyDefinedIn are mangled
7987
/// as if they're part of their original module. Disabled for debug mangling,
8088
/// because lldb wants to find declarations in the modules they're currently

Diff for: lib/AST/ASTMangler.cpp

+4-2
Original file line numberDiff line numberDiff line change
@@ -2063,7 +2063,8 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
20632063
case SILFunctionTypeIsolation::Unknown:
20642064
break;
20652065
case SILFunctionTypeIsolation::Erased:
2066-
OpArgs.push_back('A');
2066+
if (AllowIsolatedAny)
2067+
OpArgs.push_back('A');
20672068
break;
20682069
}
20692070

@@ -3097,7 +3098,8 @@ void ASTMangler::appendFunctionSignature(AnyFunctionType *fn,
30973098
appendOperator("Yc");
30983099
break;
30993100
case FunctionTypeIsolation::Kind::Erased:
3100-
appendOperator("YA");
3101+
if (AllowIsolatedAny)
3102+
appendOperator("YA");
31013103
break;
31023104
}
31033105

Diff for: lib/IRGen/IRGenMangler.cpp

+9
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,19 @@ IRGenMangler::mangleTypeForReflection(IRGenModule &IGM,
161161
ASTContext &ctx = Ty->getASTContext();
162162
llvm::SaveAndRestore<bool> savedConcurrencyStandardSubstitutions(
163163
AllowConcurrencyStandardSubstitutions);
164+
llvm::SaveAndRestore<bool> savedIsolatedAny(AllowIsolatedAny);
164165
if (auto runtimeCompatVersion = getSwiftRuntimeCompatibilityVersionForTarget(
165166
ctx.LangOpts.Target)) {
166167
if (*runtimeCompatVersion < llvm::VersionTuple(5, 5))
167168
AllowConcurrencyStandardSubstitutions = false;
169+
170+
// Suppress @isolated(any) if we're mangling for pre-6.0 runtimes.
171+
// This is unprincipled but, because of the restrictions in e.g.
172+
// mangledNameIsUnknownToDeployTarget, should only happen when
173+
// mangling for certain reflective uses where we have to hope that
174+
// the exact type identity is generally unimportant.
175+
if (*runtimeCompatVersion < llvm::VersionTuple(6, 0))
176+
AllowIsolatedAny = false;
168177
}
169178

170179
llvm::SaveAndRestore<bool> savedAllowStandardSubstitutions(

Diff for: test/IRGen/reflection_metadata_isolated_any.swift

+17-5
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,6 @@
44
// REQUIRES: OS=macosx
55
// UNSUPPORTED: CPU=arm64e
66

7-
public struct MyStruct {
8-
let fn: @isolated(any) () -> ()
9-
}
10-
117
// Make sure that we only emit a demangling-based type description
128
// for @isolated(any) types when deploying to runtimes that support it.
139
// If we don't, we fall back on using a type metadata accessor, which
@@ -18,6 +14,22 @@ public struct MyStruct {
1814
// ordinary function type.
1915
// rdar://129861211
2016

17+
// Closure capture metadata:
18+
19+
// CHECK-LABEL: @"\01l__swift5_reflection_descriptor" = private constant
20+
// CHECK-PRESENT-SAME: ptr @"symbolic SiIeAgd_"
21+
// CHECK-SUPPRESSED-SAME: ptr @"symbolic SiIegd_"
22+
// CHECK-LABEL: @metadata = private constant %swift.full_boxmetadata { {{.*}}, ptr @"\01l__swift5_reflection_descriptor" }, align
23+
func makeClosure(fn: @escaping @isolated(any) () -> Int) -> (() async -> Int) {
24+
return { await fn() + 1 }
25+
}
26+
27+
// Struct field metadata:
28+
29+
public struct MyStruct {
30+
let fn: @isolated(any) () -> ()
31+
}
32+
2133
// CHECK-LABEL: @"$s32reflection_metadata_isolated_any8MyStructVMF" = internal constant
2234
// CHECK-PRESENT-SAME: ptr @"symbolic yyYAc"
23-
// CHECK-SUPPRESSED-SAME: ptr @"get_type_metadata yyYAc.1"
35+
// CHECK-SUPPRESSED-SAME: ptr @"get_type_metadata yyYAc.{{[0-9]+}}"

0 commit comments

Comments
 (0)