Skip to content

Commit 4c3f044

Browse files
authored
Merge pull request #27185 from jckarter/fast-path-short-mangling-lookups
Runtime: Fast path lookup for short manglings.
2 parents 8aa5cae + 78ed70c commit 4c3f044

File tree

3 files changed

+58
-9
lines changed

3 files changed

+58
-9
lines changed

include/swift/Demangling/StandardTypesMangling.def

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,18 @@
1313
/// STANDARD_TYPE(KIND, MANGLING, TYPENAME)
1414
/// The 1-character MANGLING for a known TYPENAME of KIND.
1515

16-
STANDARD_TYPE(Structure, A, AutoreleasingUnsafeMutablePointer)
16+
/// OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
17+
/// The 1-character MANGLING for a known TYPENAME of KIND, for a type that's
18+
/// only available with ObjC interop enabled.
19+
20+
#ifndef OBJC_INTEROP_STANDARD_TYPE
21+
#define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME) \
22+
STANDARD_TYPE(KIND, MANGLING, TYPENAME)
23+
#endif
24+
25+
OBJC_INTEROP_STANDARD_TYPE(Structure, A, AutoreleasingUnsafeMutablePointer)
1726
STANDARD_TYPE(Structure, a, Array)
1827
STANDARD_TYPE(Structure, b, Bool)
19-
STANDARD_TYPE(Structure, c, UnicodeScalar)
2028
STANDARD_TYPE(Structure, D, Dictionary)
2129
STANDARD_TYPE(Structure, d, Double)
2230
STANDARD_TYPE(Structure, f, Float)
@@ -66,3 +74,4 @@ STANDARD_TYPE(Protocol, Z, SignedInteger)
6674
STANDARD_TYPE(Protocol, z, BinaryInteger)
6775

6876
#undef STANDARD_TYPE
77+
#undef OBJC_INTEROP_STANDARD_TYPE

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -718,21 +718,61 @@ _findContextDescriptorInCache(TypeMetadataPrivateState &T,
718718
return iter->getSecond();
719719
}
720720

721+
#define DESCRIPTOR_MANGLING_SUFFIX_Structure Mn
722+
#define DESCRIPTOR_MANGLING_SUFFIX_Enum Mn
723+
#define DESCRIPTOR_MANGLING_SUFFIX_Protocol Mp
724+
725+
#define DESCRIPTOR_MANGLING_SUFFIX_(X) X
726+
#define DESCRIPTOR_MANGLING_SUFFIX(KIND) \
727+
DESCRIPTOR_MANGLING_SUFFIX_(DESCRIPTOR_MANGLING_SUFFIX_ ## KIND)
728+
729+
#define DESCRIPTOR_MANGLING_(CHAR, SUFFIX) \
730+
$sS ## CHAR ## SUFFIX
731+
#define DESCRIPTOR_MANGLING(CHAR, SUFFIX) DESCRIPTOR_MANGLING_(CHAR, SUFFIX)
732+
733+
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME) \
734+
extern "C" const ContextDescriptor DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND));
735+
736+
#if !SWIFT_OBJC_INTEROP
737+
# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
738+
#endif
739+
740+
#include "swift/Demangling/StandardTypesMangling.def"
741+
721742
static const ContextDescriptor *
722743
_findContextDescriptor(Demangle::NodePointer node,
723-
Demangle::Demangler &Dem) {
724-
const ContextDescriptor *foundContext = nullptr;
725-
auto &T = TypeMetadataRecords.get();
726-
727-
// If we have a symbolic reference to a context, resolve it immediately.
744+
Demangle::Demangler &Dem) {
728745
NodePointer symbolicNode = node;
729746
if (symbolicNode->getKind() == Node::Kind::Type)
730747
symbolicNode = symbolicNode->getChild(0);
748+
749+
// If we have a symbolic reference to a context, resolve it immediately.
731750
if (symbolicNode->getKind() == Node::Kind::TypeSymbolicReference) {
732751
return cast<TypeContextDescriptor>(
733752
(const ContextDescriptor *)symbolicNode->getIndex());
734753
}
735754

755+
// Fast-path lookup for standard library type references with short manglings.
756+
if (symbolicNode->getNumChildren() >= 2
757+
&& symbolicNode->getChild(0)->getKind() == Node::Kind::Module
758+
&& symbolicNode->getChild(0)->getText().equals("Swift")
759+
&& symbolicNode->getChild(1)->getKind() == Node::Kind::Identifier) {
760+
auto name = symbolicNode->getChild(1)->getText();
761+
762+
#define STANDARD_TYPE(KIND, MANGLING, TYPENAME) \
763+
if (name.equals(#TYPENAME)) { \
764+
return &DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND)); \
765+
}
766+
#if !SWIFT_OBJC_INTEROP
767+
# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME)
768+
#endif
769+
770+
#include "swift/Demangling/StandardTypesMangling.def"
771+
}
772+
773+
const ContextDescriptor *foundContext = nullptr;
774+
auto &T = TypeMetadataRecords.get();
775+
736776
// Nothing to resolve if have a generic parameter.
737777
if (symbolicNode->getKind() == Node::Kind::DependentGenericParamType)
738778
return nullptr;

test/SILGen/arguments.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ func arg_tuple(x: Int, y: Float) {}
2525
arg_tuple(x: i, y: f)
2626

2727
func arg_deep_tuples(x: Int, y: (Float, UnicodeScalar)) {}
28-
// CHECK-LABEL: sil hidden [ossa] @$ss15arg_deep_tuples1x1yySi_Sf_ScttF
28+
// CHECK-LABEL: sil hidden [ossa] @$ss15arg_deep_tuples1x1yySi_Sf_s13UnicodeScalarVttF
2929
// CHECK: bb0([[X:%[0-9]+]] : $Int, [[Y_0:%[0-9]+]] : $Float, [[Y_1:%[0-9]+]] : $UnicodeScalar):
3030

3131
arg_deep_tuples(x:i, y:(f, c))
@@ -37,7 +37,7 @@ var named_subtuple = (x:f, y:c)
3737
arg_deep_tuples(x:i, y: named_subtuple)
3838

3939
func arg_deep_tuples_2(x: Int, _: (y: Float, z: UnicodeScalar)) {}
40-
// CHECK-LABEL: sil hidden [ossa] @$ss17arg_deep_tuples_21x_ySi_Sf1y_Sc1zttF
40+
// CHECK-LABEL: sil hidden [ossa] @$ss17arg_deep_tuples_21x_ySi_Sf1y_s13UnicodeScalarV1zttF
4141
// CHECK: bb0([[X:%[0-9]+]] : $Int, [[Y:%[0-9]+]] : $Float, [[Z:%[0-9]+]] : $UnicodeScalar):
4242

4343
arg_deep_tuples_2(x: i, (f, c))

0 commit comments

Comments
 (0)