Commit Graph

287 Commits

Author SHA1 Message Date
David Farler
fe2872fae1 SwiftRemoteMirror: Lower types with imported / Objective-C types
- Lower Objective-C class typerefs as strong references with unknown
reference counting.
- Lower other imported C types as builtin blobs of their known
size, alignment, etc.

In the future, it might be beneficial to track which stored properties
of imported types are pointers, for better conservative scanning of
outgoing pointers to the heap.

rdar://problem/26240258
rdar://problem/26240394
2016-05-12 20:49:32 -07:00
Slava Pestov
c056b25f44 IRGen: Fix capture descriptor emission 2016-05-09 13:40:58 -07:00
Slava Pestov
d6c04816d6 Reflection: We need to distinguish metatype from reference sources
Getting metadata from a metatype source just means loading from an offset;
with a reference source we have to dereference the isa pointer first.

Thanks to @bitjammer for pointing out this was broken.
2016-05-05 14:58:33 -07:00
Slava Pestov
bb8d1d3416 IRGen: Fixes for capture descriptor emission
- Fix caller/callee confusion, and use the right SIL function type
  for obtaining the generic signature.
- Correctly interpret the NecessaryBindings structure and the
  substitutions therein.
- Fix alignment for capture and builtin type descriptors
- Put capture descriptor typerefs in the correct section

Add new SIL-level tests to precisely trigger various scenarios.
2016-05-05 13:47:54 -07:00
Slava Pestov
c22ac4d0e3 IRGen: Didn't mean to remove this part 2016-04-30 15:38:30 -07:00
Slava Pestov
96218acf66 IRGen: Only emit capture descriptors if -enable-reflection-metadata frontend flag is passed in
Also, simplify the IRGen/reflection_metadata.swift test a bit; there's no
reason to split it up into two files.
2016-04-30 15:36:00 -07:00
David Farler
a1ff1e6a7b Eagerly emit reflection metadata as decls are emitted
Rather than collection nominal type and extension decls and emit
reflection metadata records in one go, we can emit them as they
are encountered and instead collection builtin types referenced
by those at the end.
2016-04-29 17:07:55 -07:00
Slava Pestov
be870fdec4 IRGen: Emit capture descriptors in their own section
Now that reflection metadata is in TEXT, we can have relative references
to capture descriptors from closure context metadata.
2016-04-29 16:00:09 -07:00
Slava Pestov
51ae5a89a3 Reflection: Place reflection metadata in TEXT segment 2016-04-29 15:47:27 -07:00
David Farler
5594fc5339 Collect builtins referenced by captures
If there are any builtin types referenced by closure captures
[in the standard library], make sure to collect them so we emit
reflection metadata for them in the builtin section.
2016-04-29 14:28:32 -07:00
John McCall
6c92c324f6 Rename IRGenModuleDispatcher to just IRGenerator and transfer
ownership of some of the basic structures to it.
2016-04-27 09:42:03 -07:00
Slava Pestov
00e0c89b30 IRGen: Emit reflection info for protocols
For now, just enough for lowering.

Perhaps we should have a way to always just get this information
from metadata instead, since protocol metadata is always static
and not instantiated.

This would require the static "object file" interface used by
swift-reflection-dump to take a callback for symbol lookup.
2016-04-25 20:08:48 -07:00
David Farler
b6985b5cfe Build fix: React to API changes in ReflectionMetadataBuilder
NFC.
2016-04-23 00:08:28 -07:00
David Farler
f24f445997 Merge pull request #2274 from apple/closure-metadata
Start emitting capture descriptors for closure metadata
2016-04-22 23:43:00 -07:00
Slava Pestov
0d34bc21ef IRGen: Emit reflection metadata for certain builtin types when -enable-reflection-builtins flag is passed in
These types are not directly referenced as fields of aggregate types,
but are needed for reflection type lowering.

Also, use a SetVector to collect referenced builtin types, instead of
a SmallPtrSet, to ensure compiler output is deterministic.
2016-04-22 22:36:05 -07:00
David Farler
c360ba85b7 Emit typerefs and metadata source strings as relative offsets to globals
MCJIT doesn't like offset relocations into the data sections, so the
capture descriptors and their typerefs/metadata source encoded strings
will need to be emitted as globals and not go into the reflection data
sections.
2016-04-22 19:09:06 -07:00
David Farler
c05e48b116 Add CaptureDescriptor struct definition
For convenience when reading looking at heap closure metadata.

Also move the capture typerefs above the metadata sources, since
they're more likely to be accessed than generic metadata sources.
2016-04-22 19:09:06 -07:00
David Farler
6a51ae84b2 Add Self- and SelfWitnessTable metadata sources to capture descriptors
These can show up in partial applications of functions with the
witness_method calling convention.
2016-04-22 19:09:06 -07:00
David Farler
09d0cfee8d Hang closure capture descriptors from their heap metadata
Now we can discern the types of values in heap boxes at runtime!
Closure reference captures are a common way of creating reference
cycles, so this provides some basic infrastructure for detecting those
someday.

A closure capture descriptor has the following:

- The number of captures.
- The number of sources of metadata reachable from the closure.
  This is important for substituting generics at runtime since we
  can't know precisely what will get captured until we observe a
  closure.
- The number of types in the NecessaryBindings structure.
  This is a holding tank in a closure for sources of metadata that
  can't be gotten from the captured values themselves.
- The metadata source map, a list of pairs, for each
  source of metadata for every generic argument needed to perform
  substitution at runtime.
  Key: The typeref for the generic parameter visible from the closure
  in the Swift source.
  Value: The metadata source, which describes how to crawl the heap from
  the closure to get to the metadata for that generic argument.
- A list of typerefs for the captured values themselves.

Follow-up: IRGen tests for various capture scenarios, which will include
MetadataSource encoding tests.

rdar://problem/24989531
2016-04-22 19:09:06 -07:00
Slava Pestov
98abbdb332 Emit reflection metadata, but not reflection names, by default
This allows the reflection type lowering test to pass with the
default build configuration.
2016-04-21 15:15:30 -07:00
Slava Pestov
c76b1f1cec Reflection: Record the nominal type kind in the field descriptor
Tested by upcoming type lowering patch, for now NFC.
2016-04-18 21:17:41 -07:00
David Farler
ff67f7f6af [GenReflection] Emit associated type reflection metadata via extensions
Don't leave behind conformances gotten through extensions when
emitting associated type reflection metadata.
2016-04-15 17:48:27 -07:00
Slava Pestov
824bd7544d IRGen: Emit typerefs for all builtin types referenced from reflection metadata sections
In order to perform layout, the remote mirrors library needs to know
about the size, alignment and extra inhabitants of builtin types.

Ideally we would emit a reflection info section in libswiftRuntime.o,
but in the meantime just duplicate builtin type metadata for all
builtin types referenced from the current module instead.

In practice only the stdlib and a handful of overlays like the SIMD
overlay use builtin types, and only a few at a time.

Tested manually by running swift-reflection-tool on the standard
library -- I'll add automated tests by using -parse-stdlib to
reference Builtin types in a subsequent patch that adds more layout
logic.

NFC if -enable-reflection-metadata is off.
2016-04-15 00:12:11 -07:00
Slava Pestov
f236383865 Reflection: Fix metadata for empty cases
Also change std::endl to '\n' in the dumper, since std::endl flushes
the output stream.
2016-04-14 23:44:45 -07:00
Slava Pestov
25f7632f37 IRGen: Record enum payload type, not the constructor type
If we have a 'case Foo(T)' inside 'enum E', we only want to keep
'T' around, not 'E.Type -> T -> E'.
2016-04-14 17:02:41 -07:00
Slava Pestov
55b2b3f278 IRGen: Use 4 byte alignment for reflection metadata section
Otherwise, we will insert padding if the field metadata section size
is not a multiple of the word size, which will cause a crash when
we later try to read it.
2016-04-14 15:31:14 -07:00
David Farler
7e8b8f83e8 Emit reflection metadata by default
This turns on the remote reflection metadata by default for swift
invocations and the standard library. The size delta for all of the
sections is:

Section __swift3_typeref: 20176
Section __swift3_reflstr: 2556
Section __swift3_fieldmd: 8172
Section __swift3_assocty: 18112

Recognizing that this is a nontrivial increase in binary size, we can
reduce this with a few methods:

- Trie for strings (typerefs and field names are both strings) but would
  need an implementation
- Compress the entire sections: this would need an implementation
  available on all platforms where we support the functionality.
- Don't use the AST mangler but a custom mangling, which may be slightly
  more compact because it can specialize by need and maybe not include
  some mangle nodes that typerefs don't need.
2016-03-18 17:50:32 -07:00
David Farler
be34129c43 Include protocols in DependentMemberTypeRef
This is necessary for proper uniquing when looking up associated
types.
2016-03-17 19:02:01 -07:00
David Farler
4fc146a14a [GenReflection] Use declared type for associated type record keys
Similiar to the change to field reflection metadata, don't use the
interface for the key of associated type lookup, because the nominal
type descriptors don't include generics in their mangled name strings.
2016-03-07 17:43:13 -08:00
David Farler
b8345e2ffd [GenReflection] Use pretty stack trace when emitting metadata 2016-03-07 17:43:13 -08:00
David Farler
96386b03be Don't use interface type as field descriptor key
Nominal type descriptors use declared types for their mangled names,
so we need to use them when scanning the fieldmd section for a
matching record. This is fine because the descriptor can tell us
about the type's generics. Individual field records continue to use
the interface type.
2016-03-07 17:43:12 -08:00
David Farler
cd6d05e23f Move paltry linker section name length assert into Mach-O case
This is where it really matters - the section name has to be 16
characters or less.
2016-03-07 17:43:12 -08:00
David Farler
161a56ce8f Remove unnecessary if auto check - NFC 2016-03-07 17:43:12 -08:00
David Farler
744806a742 [Reflection] Add Foreign, ObjC, and Opaque type references
These likely don't have Swift type metadata but might be useful to
record or instantiate based on what kind of metadata is encountered for
the sake of memory tools.
2016-03-02 21:25:04 -08:00
David Farler
69bb6235fa [Reflection] Serialize associated types for nominal decls
This closes the loop on being able to resolve dependent member types
during remote reflection.
2016-02-12 16:34:28 -08:00
David Farler
086000a198 Start the swiftReflection library
- Nearly done: TypeRefs and the mangled name decoder.

- Add the swift-reflection-test tool.
  The field reflection pipeline is roughly:
  - Decode type references
  - Substitute generic parameters
  - Calculate sizes and offsets

  There is currently only one action in the tool, which will test the
  *Decode* part of the pipeline: `dump-reflection-section`. This reads
  the *swift3_reflect section from an object file and dumps the decoded
  type references for all of the stored properties and enum cases in the
  file.
  - TODO: Write tests with various type arrangements to exercise the
    decoder - there are likely some holes in the decoder still since the
    AST mangler is quite rich in its kinds.

  TODO: The next test mode, `dump-field-types`, will do the following:
  1. Launch a swift executable with a canned stopping point
  2. Get the address of a heap object instance of interest
  3. Dump the fully substituted typerefs of all of the stored properties
  or enum case payloads.

  That test mode will be more involved since it will attach to another
  process and need to read from its address space but will test the
  entire out-of-process reflection pipeline in a controlled environment.
  We can maybe take this test a step further, with an option or a new
  test mode, that prints the entire heap reference graph rooted at that
  object of interest, in order to test the ability to detect reference
  cycles, for example.
2016-02-04 18:10:49 -08:00
David Farler
a6a5ece206 IRGen: Emit type references for remote reflection
- Implement emission of type references for nominal type field
  reflection, using a small custom encoder resulting in packed
  structs, not strings. This will let us embed 7-bit encoded
  32-bit relative offsets directly in the structure (not yet
  hooked in).
- Use the AST Mangler for encoding type references
  Archetypes and internal references were complicating this before, so we
  can take the opportunity to reuse this machinery and avoid unique code
  and new ABI.

Next up: Tests for reading the reflection sections and converting the
demangle tree into a tree of type references.

Todo: For concrete types, serialize the types for associated types of
their conformances to bootstrap the typeref substitution process.

rdar://problem/15617914
2016-02-03 13:52:26 -08:00