Commit Graph

340 Commits

Author SHA1 Message Date
Erik Eckstein
f860a9b73f Demangler: support protocols as parents of bound generic types to avoid crash in the indexer for invalid swift code.
rdar://problem/57533567
2019-12-06 15:16:46 +01:00
Doug Gregor
ab5d161c05 [SILGen] Separate the initialization of a wrapped property from a wrapped value
Teach SILGen to emit a separate SIL function to capture the
initialization of the backing storage type for a wrapped property
based on the wrapped value. This eliminates manual code expansion at
every use site.
2019-09-24 09:11:53 -07:00
Joe Groff
6817c53031 Mangle associated types of opaque types like dependent member types.
The archetype mangling does not have enough information to accurately recover the associated type
at runtime. This fixes rdar://problem/54084733.

Although this changes the mangling in both runtime and symbols, this should not affect ABI, because
there is no way for associated types of opaque types to be surfaced in the types of public
declarations today.
2019-09-04 10:20:10 -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
John McCall
7095953f84 Fix the demangling of dependent protocol conformance refs.
When mangling a dependent protocol conformance ref, the mangler currently uses `0_` to mean an unknown index and `N_` to mean the index `N - 1`. Unfortunately, this is somewhat confused: `0_` is actually the mangling for index 1, and index 0 is supposed to be mangled as just `_`, so true indexes are actually offset by 2. So the first thing to do here is to clarify what's going on throughout the mangler, demangler, and ABI documentation.

Also, the demangler attempts to produce a `DependentProtocolConformance*` node with the appropriate child nodes and an optional index payload. Unfortunately, demangle nodes cannot have both children and a value payload, so whenever it creates a node with an index payload, the demangler will assert.  It does this whenever the mangled index is not 0; since (per above) the mangler always produces a non-zero mangled index in this production, the demangler will always assert when processing these. So clearly this is well-tested code, since +asserts builds will always trigger the demangler when mangling a name in the first place.  To fix this, we need to make the index a child of the mangling node instead of its payload; at the same time, we can make it store the semantically correct index value and just introduce a new `UnknownIndex` node to handle the `0_` case.  This is easy because all current clients ignore this information.

Finally, due to an apparent copy-and-paste error, the demangler attempts to produce a `DependentProtocolConformanceRoot` node for associated protocol conformances; this is easily resolved.

This fixes the crash in SR-10926 (rdar://51710424).  The obscurity of this crash --- which originally made us think it might be related to Error self-conformance --- is because it is only triggered when a function signature takes advantage of a concrete-but-dependent retroactive conformance, which (to be both concrete and dependent) must furthermore be conditional.  Testing the other cases besides a root conformance requires an even more obscure testcase.
2019-07-09 17:25:44 -04:00
Slava Pestov
198e8d1a1f AST: Introduce a new debug-only mangling for @escaping @convention(block) functions
Our mangling did not encode if an Objective-C block was escaping or
not. This is not a huge problem in practice, but for debug info we
want type reconstruction to round-trip exactly. There was a previous
workaround to paper over this specific problem.

Remove the workaround, and add a new 'XL' mangling for escaping
blocks. Since we don't actually want to break ABI compatibility,
only use the new mangling in DWARF debug info.
2019-06-12 19:10:19 -07:00
Erik Eckstein
5daf48b0d9 Demangler: fix substitutions for OpaqueType in the remangler.
rdar://problem/50132625
2019-05-02 10:22:46 -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
Joe Groff
95c43f4e18 Decode opaque types in the runtime demangler. 2019-04-17 14:44:40 -07:00
Joe Groff
05dfec0cda Push the conformance accessor hack down into getTypeByMangledName. 2019-04-17 14:44:40 -07:00
Joe Groff
42e1824a30 Mangle opaque result types. 2019-04-17 14:43:32 -07:00
Slava Pestov
42e74f28c0 Demangle: Add node for reabstraction thunk capturing DynamicSelfType
Also, NodePrinter was printing the 'from' and 'to' type backwards,
so fix that.
2019-04-14 18:06:16 -04:00
Slava Pestov
4207738e17 Add mangling and IRGen LinkEntity for Objective-C resilient class stubs 2019-03-26 18:44:44 -04:00
Erik Eckstein
c6abbfa29d Remangler: don't construct a std::string for hashing and comparing identifiers
This avoids memory allocations.
2019-03-13 11:49:58 -07:00
Erik Eckstein
abccbd8c8c runtime: make the old remangler allocation free.
Extract common code from the old and new remangler into a common base class.
This lets the old remangler benefit from the changes I did recently in the new remangler.
2019-03-13 11:49:58 -07:00
Erik Eckstein
d7fd45d74a Remangler: Use a bump-pointer allocated string instead of std::string
Done by replacing DemanglerPrinter with a bump-pointer allocated CharVector buffer.
This avoids malloc calls.

SR-10028
rdar://problem/48575729
2019-03-06 14:37:03 -08:00
Erik Eckstein
9050be62de Remangler: Implement the hash map for substitutions in the spirit of llvm's SmallPtrSet.
This avoids malloc calls in the common case. Only if there are more than 16 substitutions in the fixed-sized array, the overflow goes into a std::unordered_map.

SR-10028
rdar://problem/48575729
2019-03-06 14:37:03 -08:00
Erik Eckstein
de51f8cac6 Remangler: use the bump pointer allocator instead of std::vector for internal substitution data structures
This avoids mallocs in the runtime.

SR-10028
rdar://problem/48575729
2019-03-06 14:37:03 -08:00
Erik Eckstein
8404c73dba Demangler: support stack allocation for the Demangler's allocator
The demangler can be initialized with a preallocated memory on the stack. Only in case of an overflow, the bump pointer allocator mallocs new memory.
Also, support that a new instance of a demangler can "borrow" the free memory from an existing demangler. This is useful because in the runtime the demangler is invoked recursively. With this feature, all the nested demanglers can share a single stack allocated space.
2019-02-15 09:29:49 -08:00
Erik Eckstein
58f2d373d4 Demangler: Reduce sizeof(Node) from 48 bytes to 24 bytes
This is done by disallowing nodes with children to also have index or text payloads.
In some cases those payloads were not needed anyway, because the information can be derived later.
In other cases the fix was to insert an additional child node with the index/text payload.

Also, implement single or double children as "inline" children, which avoids needing a separate node vector for children.

All this reduces the needed size for node trees by over 2x.
2019-02-15 09:29:49 -08:00
Slava Pestov
2c015164cf Stop passing NodePointer by reference 2019-02-13 21:51:28 -05:00
Slava Pestov
d0c7b1547e ASTMangler: Mangle sugared types for the debugger
Fixes <rdar://problem/48004306>.
2019-02-13 19:17:25 -05:00
Slava Pestov
535f5bdedf Remangler: Fix for correct round-tripping 2019-01-29 02:15:56 -05:00
Slava Pestov
9761e5ca42 Demangler: Print BuiltinFloatType as "FPIEEE<size>" not "Float<size>"
The type checker calls these types Builtin.FPIEEE<size>; the demangler
should too.

This is just cosmetic at the moment, but it was causing problems when
I added support for builtin types to the TypeDecoder.
2019-01-24 11:12:54 -05:00
Jordan Rose
0ca4d557f4 [Mangling] Split up protocol-conformance-ref further
New(er) grammar:

    // same module as conforming type, or non-unique
    protocol-conformance-ref ::= protocol 'HP'
    // same module as protocol
    protocol-conformance-ref ::= protocol 'Hp'
    // retroactive
    protocol-conformance-ref ::= protocol module

We don't make use of this distinction anywhere yet, but we could in
the future.
2018-12-20 14:00:06 -08:00
Jordan Rose
0c9ab0048a [Mangling] Disambiguate protocol-conformance-ref better
Fix to 510b64fcd5. The mangling operator "HP" has to distinguish
between "protocol" and "protocol module", not between the presence
or absence of protocol-conformance-ref. New grammar:

    protocol-conformance-ref ::= protocol
    protocol-conformance-ref ::= protocol module 'HP'

rdar://problem/46735592, again
2018-12-19 16:17:31 -08:00
Doug Gregor
510b64fcd5 [Mangling] Give protocol-conformance-ref an operator.
Due to some unfortunate refactoring, protocol-conformance-ref is a
nonterminal in the mangling grammar that doesn't have its own
operator:

```
protocol-conformance-ref ::= protocol module?
```

Both "module" and "protocol" can be an "identifier", which introduces
a mangling collision. Address the mangling collision by using the
operator "HP".

Fixes rdar://problem/46735592.
2018-12-17 18:11:47 -08:00
Doug Gregor
735a83bb8d [Mangling] Separate mangling for associated/base conformance accessors.
It’s simpler to use separate manglings for these two cases.
2018-12-04 00:34:05 -08:00
Doug Gregor
e6620b055d [Mangling] Separate out base conformance descriptors.
Separate the mangling of base conformance descriptors from that of
associated conformance descriptors, and simplify it.
2018-12-04 00:13:54 -08:00
Doug Gregor
22b20ccd8e [Mangling] Generalize the mangling of associated witness table accessors.
Use a general ‘type’ production for the conforming type of an associated
witness table accessor mangling, so that we can mangle base protocol
witness table accessors. These entities are always internal symbols, so the
mangling itself doesn’t affect the ABI.
2018-11-29 17:05:05 -08:00
Slava Pestov
bc119d0d6d SILGen: Emit inlinable keypath thunks from inlinable contexts
It's still not resilient because we emit direct references to stored
properties, but its progress.
2018-11-16 23:18:30 -05:00
John McCall
1065f99c71 Assorted fixes for the self-conformance infrastructure 2018-11-15 22:41:58 -05:00
John McCall
5553224fd4 Support the explicit representation of self-conformances.
Big, but actually NFC because we're never actually creating them.
2018-11-15 16:42:03 -05:00
Arnold Schwaighofer
152e8db8bb IRGen and runtime implementation for dynamic replacements 2018-11-06 09:58:36 -08:00
Arnold Schwaighofer
ebbe3aed1c IRGen: Add implementation for dynamically replaceable functions
A dynamically replaceable function calls through a global variable that
holds the function pointer.

struct ChainEntry {
   void *(funPtr)();
   struct ChainEntry *next;
}

ChainEntry dynamicallyReplaceableVar;

void dynamicallyReplaceableFunction() {
  dynamicallyReplaceableVar.funPtr()
}

dynamic replacements will be chainable so the global variable also
functions as the root entry in the chain of replacements.

A dynamic replacement functions can call the previous implementation by
going through its chain entry.

ChainEntry chainEntryOf_dynamic_replacement_for_foo;

void dynamic_replacement_for_foo() {
   // call the previous (original) implementation.
   chainEntryOf_dynamic_replacement_for_foo.funPtr();
}
2018-11-06 09:53:21 -08:00
Doug Gregor
5e232de4b8 [Mangling] Mangle retroactive conformances using the new conformance mangling
Change the retroactive conformance mangling to use the new
any-protocol-conformance mangling, which maintains more information about
concrete conformances. Specifically, it maintains conformance information
for conditional requirements. It also uses the protocol-conformance-ref
production that will eventually allow symbolic references to protocol
conformance descriptors.

While here, extend the “is retroactive” check during mangling to look for
retroactive conformances in the conditional requirements of a conformance.
The immediate conformance might not be retroactive, but its specialization
might depend on a retroactive conformance. Mangle these as “retroactive”, so
we can correctly reconstruct the exact type.
2018-11-02 22:35:34 -07:00
Doug Gregor
1efe2bca21 [Mangling] Introduce mangling for protocol conformances.
Introduce complete mangling for references to protocol conformances:

* Mangle requirements of conditional conformances when present.
* Mangle conformance access paths for generic environment-dependent
  conformances.
* Abstract protocol conformance references so we can introduce
  symbolic references for them.
2018-11-02 22:35:34 -07:00
John McCall
cf511445e2 Basic support for Builtin.IntegerLiteral. 2018-10-31 18:42:34 -04:00
Slava Pestov
4cecc268dc IRGen: Emit Objective-C metadata update callbacks 2018-10-26 16:54:23 -04:00
Doug Gregor
5b41ac16db [ABI] Introduce indirect symbolic references to context descriptors.
Extending the mangling of symbolic references to also include indirect
symbolic references. This allows mangled names to refer to context
descriptors (both type and protocol) not in the current source file.

For now, only permit indirect symbolic references within the current module,
because remote mirrors (among other things) is unable to handle relocations.

Co-authored-by: Joe Groff <jgroff@apple.com>
2018-10-23 16:06:42 -07:00
Doug Gregor
b71bef1e42 [ABI] Mangling for default associated conformance accessors.
Default associated conformance accessors will be used in default
witness tables to fill in associated conformances for defaulted
associated types. Add (de|re|)mangling support for them and make them
linking entities in IRGen.
2018-09-17 22:45:15 -07:00
Doug Gregor
4549fcd673 [ABI] Add associated conformance descriptors.
Associated conformance descriptors are aliases that refer to associated
conformance requirements within a protocol descriptor’s list of
requirements. They will be used to provide protocol resilience against
the addition of new associated conformance requirements (which only makes 
sense for newly-introduced, defaulted associated types).
2018-09-17 16:32:29 -07:00
Doug Gregor
2ef9363bd1 [ABI] Add default associated type witnesses to resilient protocols.
When an associated type witness has a default, record that as part of
the protocol and emit a default associated type metadata accessor into the
default witness table. This allows a defaulted associated type to be
added to a protocol resiliently.

This is another part of rdar://problem/44167982, but it’s still very
limiting because the new associated type cannot have any conformances.
2018-09-15 22:04:46 -07:00
Doug Gregor
bbe56b284a [ABI] Add protocol requirements base descriptor.
Introduce an alias that refers one element prior to the start of a
protocol descriptor’s protocol requirements. This can be subtracted from
an associated type descriptor address to determine the offset of the
associated type accessor within a corresponding witness table. The code
generation for the latter is not yet implemented.
2018-09-14 20:59:03 -07:00
Doug Gregor
9873d52814 [ABI] Emit associated type descriptors referencing each requirement.
Emit associated type descriptors (as aliases) to reference each associated
type requirement within a resilient protocol.
2018-09-14 20:59:03 -07:00
Erik Eckstein
882f844ff0 demangler: Support mangling typealiases in functions in generic nominals.
For example:

public struct Mystruct<T> {
  func testit<U>(x: T, u: U) {
    typealias Myalias = AnyObject
  }
}

In this case the Myalias has a generic function as context.
2018-09-11 08:33:16 -07:00
Slava Pestov
2990271686 IRGen: Add mangling for method lookup functions 2018-09-07 21:50:58 -07:00
Slava Pestov
dad44cdd4a IRGen: Introduce 'method descriptor' mangling 2018-08-31 00:20:38 -06:00
Slava Pestov
8be09fef74 IRGen/Runtime: Rename "InPlaceMetadata" to "SingletonMetadata"
It's not actually "in-place" for resilient classes, which have a
pattern with an allocation function.
2018-08-24 00:52:36 -07:00
swift-ci
72304d7b4a Merge remote-tracking branch 'origin/master' into master-next 2018-08-01 07:29:57 -07:00