From 9e7dfad38428f1d453131994d7eb0af5db7bcb3a Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 22 Aug 2019 12:42:25 -0700 Subject: [PATCH 1/2] Remove unused short mangling for UnicodeScalar, which no longer uses that name --- include/swift/Demangling/StandardTypesMangling.def | 1 - test/SILGen/arguments.swift | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/include/swift/Demangling/StandardTypesMangling.def b/include/swift/Demangling/StandardTypesMangling.def index 051e945cd2c3a..e47adc0e50d4a 100644 --- a/include/swift/Demangling/StandardTypesMangling.def +++ b/include/swift/Demangling/StandardTypesMangling.def @@ -16,7 +16,6 @@ STANDARD_TYPE(Structure, A, AutoreleasingUnsafeMutablePointer) STANDARD_TYPE(Structure, a, Array) STANDARD_TYPE(Structure, b, Bool) -STANDARD_TYPE(Structure, c, UnicodeScalar) STANDARD_TYPE(Structure, D, Dictionary) STANDARD_TYPE(Structure, d, Double) STANDARD_TYPE(Structure, f, Float) diff --git a/test/SILGen/arguments.swift b/test/SILGen/arguments.swift index d010489a9fb7a..30a8fc708164d 100644 --- a/test/SILGen/arguments.swift +++ b/test/SILGen/arguments.swift @@ -25,7 +25,7 @@ func arg_tuple(x: Int, y: Float) {} arg_tuple(x: i, y: f) func arg_deep_tuples(x: Int, y: (Float, UnicodeScalar)) {} -// CHECK-LABEL: sil hidden [ossa] @$ss15arg_deep_tuples1x1yySi_Sf_ScttF +// CHECK-LABEL: sil hidden [ossa] @$ss15arg_deep_tuples1x1yySi_Sf_s13UnicodeScalarVttF // CHECK: bb0([[X:%[0-9]+]] : $Int, [[Y_0:%[0-9]+]] : $Float, [[Y_1:%[0-9]+]] : $UnicodeScalar): arg_deep_tuples(x:i, y:(f, c)) @@ -37,7 +37,7 @@ var named_subtuple = (x:f, y:c) arg_deep_tuples(x:i, y: named_subtuple) func arg_deep_tuples_2(x: Int, _: (y: Float, z: UnicodeScalar)) {} -// CHECK-LABEL: sil hidden [ossa] @$ss17arg_deep_tuples_21x_ySi_Sf1y_Sc1zttF +// CHECK-LABEL: sil hidden [ossa] @$ss17arg_deep_tuples_21x_ySi_Sf1y_s13UnicodeScalarV1zttF // CHECK: bb0([[X:%[0-9]+]] : $Int, [[Y:%[0-9]+]] : $Float, [[Z:%[0-9]+]] : $UnicodeScalar): arg_deep_tuples_2(x: i, (f, c)) From 78ed70cbb8e0dfaaf24e2c053f237f766d11c5be Mon Sep 17 00:00:00 2001 From: Joe Groff Date: Thu, 22 Aug 2019 12:43:14 -0700 Subject: [PATCH 2/2] Runtime: Fast path lookup for short manglings. Mangling these common types takes only two bytes, which is shorter than a symbolic reference. We know where their metadata is in the standard library, too, so we don't need to search the lookup tables for them. --- .../Demangling/StandardTypesMangling.def | 12 ++++- stdlib/public/runtime/MetadataLookup.cpp | 50 +++++++++++++++++-- 2 files changed, 56 insertions(+), 6 deletions(-) diff --git a/include/swift/Demangling/StandardTypesMangling.def b/include/swift/Demangling/StandardTypesMangling.def index e47adc0e50d4a..336ef32ca32c0 100644 --- a/include/swift/Demangling/StandardTypesMangling.def +++ b/include/swift/Demangling/StandardTypesMangling.def @@ -13,7 +13,16 @@ /// STANDARD_TYPE(KIND, MANGLING, TYPENAME) /// The 1-character MANGLING for a known TYPENAME of KIND. -STANDARD_TYPE(Structure, A, AutoreleasingUnsafeMutablePointer) +/// OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME) +/// The 1-character MANGLING for a known TYPENAME of KIND, for a type that's +/// only available with ObjC interop enabled. + +#ifndef OBJC_INTEROP_STANDARD_TYPE +#define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME) \ + STANDARD_TYPE(KIND, MANGLING, TYPENAME) +#endif + +OBJC_INTEROP_STANDARD_TYPE(Structure, A, AutoreleasingUnsafeMutablePointer) STANDARD_TYPE(Structure, a, Array) STANDARD_TYPE(Structure, b, Bool) STANDARD_TYPE(Structure, D, Dictionary) @@ -65,3 +74,4 @@ STANDARD_TYPE(Protocol, Z, SignedInteger) STANDARD_TYPE(Protocol, z, BinaryInteger) #undef STANDARD_TYPE +#undef OBJC_INTEROP_STANDARD_TYPE diff --git a/stdlib/public/runtime/MetadataLookup.cpp b/stdlib/public/runtime/MetadataLookup.cpp index d0e51b34e228e..17d776b6acab8 100644 --- a/stdlib/public/runtime/MetadataLookup.cpp +++ b/stdlib/public/runtime/MetadataLookup.cpp @@ -708,21 +708,61 @@ _findContextDescriptorInCache(TypeMetadataPrivateState &T, return iter->getSecond(); } +#define DESCRIPTOR_MANGLING_SUFFIX_Structure Mn +#define DESCRIPTOR_MANGLING_SUFFIX_Enum Mn +#define DESCRIPTOR_MANGLING_SUFFIX_Protocol Mp + +#define DESCRIPTOR_MANGLING_SUFFIX_(X) X +#define DESCRIPTOR_MANGLING_SUFFIX(KIND) \ + DESCRIPTOR_MANGLING_SUFFIX_(DESCRIPTOR_MANGLING_SUFFIX_ ## KIND) + +#define DESCRIPTOR_MANGLING_(CHAR, SUFFIX) \ + $sS ## CHAR ## SUFFIX +#define DESCRIPTOR_MANGLING(CHAR, SUFFIX) DESCRIPTOR_MANGLING_(CHAR, SUFFIX) + +#define STANDARD_TYPE(KIND, MANGLING, TYPENAME) \ + extern "C" const ContextDescriptor DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND)); + +#if !SWIFT_OBJC_INTEROP +# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME) +#endif + +#include "swift/Demangling/StandardTypesMangling.def" + static const ContextDescriptor * _findContextDescriptor(Demangle::NodePointer node, - Demangle::Demangler &Dem) { - const ContextDescriptor *foundContext = nullptr; - auto &T = TypeMetadataRecords.get(); - - // If we have a symbolic reference to a context, resolve it immediately. + Demangle::Demangler &Dem) { NodePointer symbolicNode = node; if (symbolicNode->getKind() == Node::Kind::Type) symbolicNode = symbolicNode->getChild(0); + + // If we have a symbolic reference to a context, resolve it immediately. if (symbolicNode->getKind() == Node::Kind::TypeSymbolicReference) { return cast( (const ContextDescriptor *)symbolicNode->getIndex()); } + // Fast-path lookup for standard library type references with short manglings. + if (symbolicNode->getNumChildren() >= 2 + && symbolicNode->getChild(0)->getKind() == Node::Kind::Module + && symbolicNode->getChild(0)->getText().equals("Swift") + && symbolicNode->getChild(1)->getKind() == Node::Kind::Identifier) { + auto name = symbolicNode->getChild(1)->getText(); + +#define STANDARD_TYPE(KIND, MANGLING, TYPENAME) \ + if (name.equals(#TYPENAME)) { \ + return &DESCRIPTOR_MANGLING(MANGLING, DESCRIPTOR_MANGLING_SUFFIX(KIND)); \ + } +#if !SWIFT_OBJC_INTEROP +# define OBJC_INTEROP_STANDARD_TYPE(KIND, MANGLING, TYPENAME) +#endif + +#include "swift/Demangling/StandardTypesMangling.def" + } + + const ContextDescriptor *foundContext = nullptr; + auto &T = TypeMetadataRecords.get(); + // Nothing to resolve if have a generic parameter. if (symbolicNode->getKind() == Node::Kind::DependentGenericParamType) return nullptr;