Commit Graph

306 Commits

Author SHA1 Message Date
Erik Eckstein
1b312a85bd Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-16 10:39:20 +01:00
Slava Pestov
61872fa948 IRGen: Don't use the demangler to realize imported Objective-C class metadata for now
Recently we started using the runtime demangler to realize imported
Objective-C class metadata, to save on code size since no metadata
accessor function needs to be emitted in this case.

This introduced a regression where OBJC_CLASSREF symbols were no
longer being emitted in some cases, which breaks autolinking.
The fix for this was tracked by rdar://56136123, but unfortunately
had to be reverted because it caused other problems.

Until the original fix can be re-applied, let's put in a temporary
workaround where we avoid going through the runtime demangler for
imported Objective-C classes. This regresses code size but unblocks
everyone involved, until the fix for the demangling code path
(8247525471) can be re-applied.

Fixes <rdar://56621277>.
2020-01-14 00:44:30 -05:00
swift-ci
5dcb80b599 Merge remote-tracking branch 'origin/master' into master-rebranch 2020-01-10 15:24:32 -08:00
Nate Chandler
9e2e090623 [IRGen] Emit metadata accessors last.
Metadata accessors are dependent on prespecializations of the metadata
of generic, in-module types.  Those prespecializations are themselves
dependent on usages of the types in functions.  Consequently, the
accessors must be emitted after all the functions are emitted.
2020-01-09 17:25:34 -08:00
Nate Chandler
c4d13e4b4b [IRGen] Directly reference prespecializations.
When possible, directly reference metadata prespecializations.  Doing so
is possible when the type is defined in the same module, because in
those cases the metadata accessor can be modified to ensure that the
prespecialized metadata is canonical.

rdar://problem/56994171
2020-01-09 17:25:33 -08:00
Nate Chandler
840ded4923 [IRGen] Accessor returns prespecializations.
For every prespecialization of generic metadata that exists in the
module where the generic type is defined, the metadata accessor gains
code with the following effect

  switch arguments {
  case prespecialization1.genericArguments:
    return prespecialization1
  case prespecialization2.genericArguments:
    return prespecialization2
  ...
  default:
    return swift_getGenericMetadata(...)
  }

rdar://problem/56961700
2020-01-09 17:25:33 -08:00
Nate Chandler
9ea71d1114 [IRGen] Prepare to refer to prespecializations.
When emitting a reference to the metadata for a generic type, prepare
to, rather than always inserting calls to the type metadata access
function, emit direct references to static specializations when possible
and emit calls to the forthcoming swift_getCanonicalSpecializedMetadata
when not possible.

For now, the metadata access function is always called.
2020-01-09 17:25:32 -08:00
Joe Groff
fb34044408 Merge remote-tracking branch 'origin/master' into master-next 2019-12-10 12:46:41 -08:00
Arnold Schwaighofer
0d324d223f Add swift_getTypeByMangledNameInContextInMetadataState such that we can
use getTypeByMangledName when abstract metadata state is requested

This can significantly reduce the code size of apps constructing deeply
nested types with conditional conformances.

Requires a new runtime.

rdar://57157619
2019-11-18 14:41:35 -08:00
swift-ci
aa57f0c92b Merge remote-tracking branch 'origin/master' into master-next 2019-11-12 12:09:57 -08:00
Arnold Schwaighofer
4cba76309f IRGen: Add TypeExpansionContext to IRGen 2019-11-11 14:21:52 -08:00
swift-ci
2689ff9343 Merge remote-tracking branch 'origin/master' into master-next 2019-10-30 07:49:45 -07:00
Robert Widmann
b849e51768 Use operator bool to claw back some readability 2019-10-29 16:56:21 -07:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Xi Ge
64c0680686 Merge remote-tracking branch 'apple/master' into master-next 2019-10-28 10:19:36 -07:00
Arnold Schwaighofer
b75691cdde ReplaceOpaqueTypesWithUnderlyingTypes: We can't look through opaque archetypes if the underlying type contains a type not accessible from the current context
We can't access types in the same module if they are private: they could
be in a different TU.
We can't access types from different modules if they are private or
internal.

Some more information can be gleaned from the resilience expansion: A
minimally resilient function can't reference private or internal types.

rdar://56093964
2019-10-14 10:54:21 -07:00
Harlan Haskins
d4247942c4 Merge remote-tracking branch 'upstream/master' into master-next 2019-09-26 20:19:25 -07:00
Harlan Haskins
e349b7b123 Merge branch 'master' into master-rebranch 2019-09-26 20:16:05 -07:00
Jordan Rose
a6dd630ca3 Eliminate Builtin.UnknownObject as an AST type (#27378)
This removes it from the AST and largely replaces it with AnyObject
at the SIL and IRGen layers. Some notes:

- Reflection still uses the notion of "unknown object" to mean an
  object with unknown refcounting. There's no real reason to make
  this different from AnyObject (an existential containing a
  single object with unknown refcounting), but this way nothing
  changes for clients of Reflection, and it's consistent with how
  native objects are represented.

- The value witness table and reflection descriptor for AnyObject
  use the mangling "BO" instead of "yXl".

- The demangler and remangler continue to support "BO" because it's
  still in use as a type encoding, even if it's not an AST-level
  Type anymore.

- Type-based alias analysis for Builtin.UnknownObject was incorrect,
  so it's a good thing we weren't using it.

- Same with enum layout. (This one assumed UnknownObject never
  referred to an Objective-C tagged pointer. That certainly wasn't how
  we were using it!)
2019-09-26 17:48:04 -07:00
Jordan Rose
eefb0d0644 Merge remote-tracking branch 'origin/master' into master-next 2019-09-04 14:01:43 -07:00
swift-ci
a6f4a4650b Merge remote-tracking branch 'origin/master' into master-rebranch 2019-09-04 12:43:45 -07:00
Joe Groff
0ae86c9c9d IRGen: Backward-deploy fix using open-coded accessors.
If we mangled an opaque associated type while targeting an older OS, we can use a \9
accessor reference string to instantiate the associated type.
2019-09-04 10:22:17 -07:00
swift-ci
c51c36c558 Merge remote-tracking branch 'origin/master' into master-rebranch 2019-08-26 10:04:38 -07:00
swift-ci
270a611d47 Merge remote-tracking branch 'origin/master' into master-next 2019-08-26 09:49:56 -07:00
Arnold Schwaighofer
781582b205 IRGen: Add addLoweredTypeRef API and use it for capture and
box descriptors

We want to substitute opaque result types in addTypeRef but when we pass
SILFunctionTypes this would fail because AST type substitution does not
support lowered SIL types.

Instead add addLoweredTypeRef which substitutes based on SILTypes.

rdar://54529445
2019-08-22 13:40:38 -07:00
Arnold Schwaighofer
8d26dc5fe5 Revert to use of getOrInsertFunction api.
We really want to get the function if it already exists not create new
ones.
2019-08-15 14:14:06 -07:00
Shoaib Meenai
8ba5dbd59b [IRGen] Adjust for LLVM r352827
getOrInsertFunction returns a FunctonCallee after LLVM r352827, so we
need to call getCallee to get the actual Value.
2019-08-10 19:18:04 -07:00
Slava Pestov
6248628961 Merge remote-tracking branch 'origin/master' into master-next 2019-08-09 15:51:48 -04:00
swift-ci
299ef46699 Merge remote-tracking branch 'origin/master' into master-rebranch 2019-08-08 20:03:45 -07:00
swift-ci
740cb36b76 Merge remote-tracking branch 'origin/master' into master-rebranch 2019-08-08 18:04:29 -07:00
Joe Groff
b9e6ae07e4 IRGen: Avoid unnecessary swift_getObjectType calls.
In too many places, we were calling into `emitDynamicTypeOfOpaqueHeapObject` even when we had
more specific type information about the heap object we were querying. Replace all calls with
`emitDynamicTypeOfHeapObject`, which uses the best available access path and completely avoids
runtime calls for pure Swift classes and heap objects. When targeting non-ObjC-interop platforms,
we also know we never need to call `swift_getObjectType`, so avoid doing so altogether.
2019-08-08 18:00:10 -07:00
Brent Royal-Gordon
639aac719b Update for new getOrInsertFunction() return type 2019-08-08 17:56:37 -07:00
Joe Groff
77b012af1d SIL: Add an [exact_self_class] function attribute.
This indicates that the "self" argument to the current function is always dynamically of the exact
static base class type, allowing metadata accesses in IRGen to use the local self metadata to answer
metadata requests for the class type. Set this attribute on allocating entry points of designated
inits, which is one of the most common places where we emit redundant metadata accesses.
2019-08-08 14:03:07 -07:00
Joe Groff
fc269690b7 IRGen: Answer class metadata requests with the self argument in methods of final classes.
Class methods always have a "self" argument that can be used to get the metadata of the dynamic
Self type, which in final classes is always the same as the statically-known base class. Use this
to avoid reconstructing the static base class type.
2019-08-08 14:03:06 -07:00
Joe Groff
44af3a9398 Merge pull request #26561 from jckarter/opaque-assoc-type-demangle-workaround
IRGen: Work around runtime bug demangling associated types of opaque types.
2019-08-08 13:06:40 -07:00
Joe Groff
e6bc3e7adf IRGen: Work around runtime bug demangling associated types of opaque types.
The Swift 5.1 runtime (and master, for the time being) do not successfully demangle this mangling,
so for the time being, fall back to open-coded metadata access in this case. Workaround for
rdar://problem/54084733.
2019-08-08 10:31:48 -07:00
Joe Groff
9bd97ea0c1 Merge pull request #26542 from jckarter/opaque-type-metadata-peephole
IRGen: Peephole metadata requests for opaque types.
2019-08-08 09:36:17 -07:00
Joe Groff
8bd2319530 IRGen: Peephole metadata requests for opaque types.
If we're allowed to know at IRGen time what the underlying type of an opaque type is, we can
satisfy references to the opaque type's metadata or protocol witness tables by directly referencing
the underlying type instead.
2019-08-07 19:57:04 -07:00
Joe Groff
3e2965f452 IRGen: Omit frame pointers from metadata accessors. 2019-08-07 14:16:41 -07:00
Joe Groff
afceb4e332 IRGen: Factor out calling convention shuffling in metadata accessors.
Metadata accessors that take <=3 arguments have to put the arguments in a temporary buffer and
move the registers around in order to call swift_getGenericMetadata. We can factor this into a
common shim function.
2019-08-07 13:25:11 -07:00
Jonas Devlieghere
c01a9b180a Update master-next for upstream changes 2019-08-05 13:20:00 -07:00
Joe Groff
5d0580919a Do not inline metadata accessors. 2019-08-02 14:28:58 -07:00
Joe Groff
f0e5e1911d IRGen: Access concrete type metadata by mangled name.
When we generate code that asks for complete metadata for a fully concrete specific type that
doesn't have trivial metadata access, like `(Int, String)` or `[String: [Any]]`,
generate a cache variable that points to a mangled name, and use a common accessor function
that turns that cache variable into a pointer to the instantiated metadata. This saves a bunch
of code size, and should have minimal runtime impact, since the demangling of any string only
has to happen once.

This mostly just works, though it exposed a couple of issues:

- Mangling a type ref including objc protocols didn't cause the objc protocol record to get
  instantiated. Fixed as part of this patch.
- The runtime type demangler doesn't correctly handle retroactive conformances. If there are
  multiple retroactive conformances in a process at runtime, then even though the mangled string
  refers to a specific conformance, the runtime still just picks one without listening to the
  mangler. This is left to fix later, rdar://problem/53828345.

There is some more follow-up work that we can do to further improve the gains:

- We could improve the runtime-provided entry points, adding versions that don't require size
  to be cached, and which can handle arbitrary metadata requests. This would allow for mangled
  names to also be used for incomplete metadata accesses and improve code size of some generic
  type accessors. However, we'd only be able to take advantage of the new entry points in
  OSes that ship a new runtime.
- We could choose to always symbolic reference all type references, which would generally reduce
  the size of mangled strings, as well as make runtime demangling more efficient, since it wouldn't
  need to hit the runtime caches. This would however require that we be able to handle symbolic
  references across files in the MetadataReader in order to avoid regressing remote mirror
  functionality.
2019-08-02 14:28:53 -07:00
Joe Groff
f9e5de9371 IRGen: Reenable caching opaque type metadata accessors.
Dynamic replacement can only really get away with replacing opaque return types if a function's
opaque type has never been instantiated in the first place. Meanwhile, repeatedly instantiating
the metadata is too slow for many clients to tolerate. Fixes rdar://problem/53213600.
2019-07-17 20:15:09 -07:00
Arnold Schwaighofer
a9fe7f18d8 Remove code I added for a previous implementation 2019-04-22 07:42:49 -07:00
Arnold Schwaighofer
84c7b77d02 Make opaque type descriptors dynamically replaceable
This is to support dynamic function replacement of functions with opaque
result type.

This approach requires that all state is thrown away (that could contain the
old returned type for an opaque type) between replacements.

rdar://48887938
2019-04-22 07:31:07 -07:00
Slava Pestov
d6868acf8a IRGen: Categories on class stubs should go in a separate list and section 2019-04-19 16:50:29 -04:00
Slava Pestov
2846fa66a9 IRGen: Fix for categories on resilient classes from other modules
If a class has resilient metadata from our point of view, it might
still not have a class stub, if its entire inheritance chain is
defined in a single module.

Note that inserting a superclass is still a resilient operation;
the only way to change a class from having static metadata to having
a class stub is to change it's root class, which is not something
we can do resiliently.
2019-04-19 16:50:29 -04:00
Joe Groff
95c43f4e18 Decode opaque types in the runtime demangler. 2019-04-17 14:44:40 -07:00
Slava Pestov
dd80f588dd IRGen: Emit foreign type metadata using the lazy metadata mechanism
Instead of a wholly separate lazyness mechanism for foreign metadata where
the first call to getAddrOfForeignTypeMetadataCandidate() would emit the
metadata, emit it using the lazy metadata mechanism.

This eliminates some code duplication. It also ensures that foreign
metadata is only emitted once per SIL module, and not once per LLVM
module, avoiding duplicate copies that must be ODR'd away in multi-threaded
mode.

This fixes the test case from <rdar://problem/49710077>.
2019-04-12 01:46:23 -04:00