On some Harvard architectures like WebAssembly that allow sliding code
and data address space offsets independently, it's impossible to make
direct relative reference to code from data because the relative offset
between them is not representable.
Use absolute function references instead of relative ones on such targets.
Since `TargetGenericEnvironmentDescriptor` uses trailing objects to
store parameters, requirements and other information. IR Emission
should pad the structure to be 4 bytes aligned before adding generic
requirements, because that's the alignment expected by
`TargetGenericRequirementDescriptor`.
Previously, the trailing flags field of runtime instantiated generic
metadata for types which had prespecialization enabled were not zeroed.
Consequently, the field always contained garbage. Often, metadata was
instantiated on new (and so, zeroed) pages, so the garbage happened to
be zero as is appropriate. However, when the metadata was instantiated
on pages which had previously been dirtied, the garbage value would
sometimes indicate that the metadata was canonical statically
specialized. When that occurred, swift_checkMetadataState would
incorrectly return a metadata state of complete, despite the fact that
the metadata might not in fact be complete. As a result, the runtime
was trafficking in incomplete metadata as if it were complete, resulting
in various crashes that would arise from for example missing a witness
table or a value witness table.
Here the problem is corrected. The trailing flags field of structs and
enums that have the field is set to 0.
For structs, this is accomplished by modifying the extra data pattern to
exist not only when there is fixed type info for the type but also when
the type is being prespecialized. Specifically, the extra data for a
struct is, rather than an i32 array of field offsets, a struct
consisting of none, one, or both of the following: (1) the array of
field offsets, (2) the trailing flags.
Similarly, enums now have an extra data pattern which consists of none,
one, or both of the following: (1) the payload size, (2) the trailing
flags. Enum metadata extra data setting was previously achieved by
customizing the metadata allocation function; that customization is now
eliminated, being replaced with the shared runtime code for copying
extra data into place, a modest code size savings.
rdar://problem/61465515
The struct and enum metadata scanners were both adding a pointer for the
trailing flags field. That was only correct for 64-bit platforms.
Instead, add an Int64.
Use relative references instead of pointers so that the pattern can be true-const. Instead of trying
to instantiate a constant key path literal in-place, point to a cache variable that we can store
a pointer to the shared instance into when instantiated. Now that the pattern format has diverged
significantly from the instance format, simplify and refactor the instantiation code using a walker
for the pattern format instead of trying to reuse the code for working with instantiated instances.
rdar://problem/42674576
Type of elements contained by field offsets vector can be adjusted
to 32-bit integers (from being pointer sized) to safe space in the
binary since segment size is limited to 4 GB.
Resolves: rdar://problem/36560486
We have RelativeDirectPointer and RelativeIndirectablePointer. The latter
cannot be used with function pointers because it assumes the payload is
2-byte aligned and uses the least significant bit to dynamically
distinguish a direct pointer from an indirect pointer.
For resilient conformances, we want to use protocol dispatch thunks as
keys in the witness table template. They're not 2 byte aligned, and
they're always defined in a different image, so we need an unconditional
indirect relative pointer template.
Alter the value metadata layout to use an absolute pointer for the
nominal type descriptor rather than a relative offset relative to the
complete type metadata. Although this is slightly less efficient in
terms of load times, this is more portable across different
environments. For example, PE/COFF does not provide a cross-section
relative offset relocation. Other platform ports are unable to provide
a 64-bit relative offset encoding.
Given that the value witness table reference in the value metadata is
currently an absolute pointer, this page is most likely going to be
dirtied by the loader.
To get the full benefit of dyld3 on Darwin platforms, pointer relocations need to be pointer-aligned, which unfortunately requires growing some key path data structures a little bit. This does tidy up some code that had to hack around our lack of unaligned load/store operations on UnsafeRawPointer, at least. While we're here, we can also simplify the identification strategy for reabstracted stored properties; we only need the property index to identify, not the absolute offset. rdar://problem/32318829
This is NFC in intent, but I had to restructure the code to emit more
of the lists "inline", which means I inevitably altered some IRGen
emission patterns in ways that are visible to tests:
- GenClass emits property/ivar/whatever descriptors in a somewhat
different order.
- An ext method type list is now emitted as just an array, not a struct
containing only that array.
- Protocol descriptors are no longer emitted as packed structs.
I was sorely tempted to stop using packed structs for all the metadata
emission, but didn't really want to update that many tests in one go.
initialization in-place on demand. Initialize parent metadata
references correctly on struct and enum metadata.
Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.
Includes a fix since the earlier commit to make enum metadata
writable if they have an unfilled payload size. This didn't show
up on Darwin because "constant" is currently unenforced there in
global data containing relocations.
This patch requires an associated LLDB change which is being
submitted in parallel.
initialization in-place on demand. Initialize parent metadata
references correctly on struct and enum metadata.
Also includes several minor improvements related to relative
pointers that I was using before deciding to simply switch the
parent reference to an absolute reference to get better access
patterns.
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.
- 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