Commit Graph

1292 Commits

Author SHA1 Message Date
Joe Groff
c0722c21d1 IRGen: Defer emitting field type generators for nominal types.
The field type generator may end up producing recursive static references to metadata while we're generating metadata. Fixes rdar://problem/19838839.

Swift SVN r25534
2015-02-25 21:20:50 +00:00
Argyrios Kyrtzidis
426fa4cbc3 Fix -Wunused-local-typedef warning.
Swift SVN r25482
2015-02-23 00:24:07 +00:00
Erik Eckstein
9544893104 IRGen: Use a different cache for layout-metadata.
Metadata used for layout is (or may be) different than the "regular" metadata.
Therefore we need a separate cache for it.



Swift SVN r25238
2015-02-12 13:44:23 +00:00
Erik Eckstein
a5b8e05ea6 Reuse type metadata in the most obvious cases.
This avoids redundant calls of swift_get*Metadata() if the metadata is reused in the same basic block
or if it is already availabe in the function entry block.
There is still room for improvement, e.g. by not limiting the reuse to a single block.

This change reduces the dylib codesize by about 11%, mostly in protocoll witnesses (-34%).

It also results in some improvements for the -Onone performance:
RangeAssignment: +31%
StdlibSort: +20%




Swift SVN r25123
2015-02-10 14:42:39 +00:00
Joe Groff
36c7177ecc IRGen: Follow SIL's logic for deciding when overrides reuse vtable slots.
IRGen miscalculated that the allocating constructor for a required init that overrides a non-required init introduces a new vtable entry; the non-required super allocator is statically dispatched. Fix this and head off further divergences by reusing the logic in the new SILDeclRef::getOverriddenVTableEntry method. Fixes rdar://problem/19514920.

Swift SVN r25068
2015-02-07 05:57:57 +00:00
Joe Groff
1d9d153136 IRGen: Consistently mangle protocol names in protocol descriptors with the _Tt prefix.
Since protocol descriptors look like ObjC Protocol objects, we want to mangle the type prefix into their names to make them obviously Swift mangled names, but we were only doing this for @objc protocols, not native protocols. For consistency, do the same thing with native descriptors, and update the runtime type display code to match. Fixes rdar://problem/19691574.

Swift SVN r25014
2015-02-05 20:04:20 +00:00
Joe Groff
e318a8c2e8 IRGen: Populate vtables using the SILVTable instead of reinventing AST lookup.
We still preserve IRGen's current ordering of vtable slots, but use SIL's record of which SILFunction implements which method instead of walking overrides independently. Another step on the way to rdar://problem/19321484; if SILGen determines that a thunk is needed to interface an override with its base method, IRGen will now pick it up. (Thunk generation is still busted in certain inheritance cases I need to fix before declaring victory though.)

Swift SVN r24732
2015-01-26 19:58:54 +00:00
Greg Parker
8adb66f949 Work around a miscompile in NominalTypeDescriptor.FieldNames on arm64.
If the FieldNames data is exactly 4 or 8 or 16 bytes long then on arm64 
it is compiled as a 4/8/16 byte literal instead of as ordinary data. 
This confuses something in the toolchain so the final linked executable 
gets an invalid FieldNames pointer. 
Workaround: pad the FieldNames data to avoid those sizes.

rdar://19537198


Swift SVN r24587
2015-01-21 04:01:33 +00:00
John McCall
f3dc58667d Improve the typing of materializeForSet callbacks to
use a thin function type.

We still need thin-function-to-RawPointer conversions
for generic code, but that's fixable with some sort of
partial_apply_thin_recoverable instruction.

Swift SVN r24364
2015-01-11 21:13:35 +00:00
John McCall
6a91f7a172 Various improvements to the function-type ABI.
Teach IRGen and the runtime about the extra inhabitants
of function pointers, and take advantage of that in
thin and thick function types.

Also add runtime entrypoints for thin function type
metadata.

Swift SVN r24346
2015-01-10 01:45:37 +00:00
John McCall
169e4fe319 Add Builtin.UnsafeValueBuffer, which provides opaque
storage for arbitrary values.

A buffer doesn't provide any way to identify the type of
value it stores, and so it cannot be copied, moved, or
destroyed independently; thus it's not available as a
first-class type in Swift, which is why I've labelled
it Unsafe.  But it does allow an efficient means of
opaquely preserving information between two cooperating
functions.  This will be useful for the adjustments I
need to make to materializeForSet to support safe
addressors.

I considered making this a SIL type category instead,
like $@value_buffer T.  This is an attractive idea because
it's generally better-typed.  The disadvantages are that:
- it would need its own address_to_pointer equivalents and
- alloc_stack doesn't know what type will be stored in
  any particular buffer, so there still needs to be
  something opaque.

This representation is a bit gross, but it'll do.

Swift SVN r23903
2014-12-13 01:27:12 +00:00
Graham Batty
06c75f5f76 Revert "Appropriately disable some more objective-c codegen on non-objc."
This reverts commit f62a356cd3689a1325c921315cedfd56eda7cb00.

Swift SVN r23578
2014-11-24 17:42:18 +00:00
Graham Batty
b0d7b07e05 Appropriately disable some more objective-c codegen on non-objc.
Swift SVN r23574
2014-11-24 17:40:42 +00:00
David Farler
3c4428dd78 Emit unique metadata for functions with inout
References to functions that take inout parameters crash the compiler
because InOutType isn't a "real" type in itself and has no special type
metadata to emit. It merely further qualifies the function's input
types.

For example, we would like to have a unique entry in the cache for:

var f: (T, T) -> ()

and

var f2: (inout T, T) -> ()

For each argument type metadata pointer in the function's input, take
advantage of pointer alignment and mark the lowest bit if it is inout.
Since the metadata cache uses pointers to create the key, this creates a
unique entry while still being able to extract the actual pointer.

This fixes <rdar://problem/17655125>, and a couple of other similar
crashes.

Swift SVN r23557
2014-11-22 22:01:23 +00:00
Joe Groff
b5b860ad39 IRGen: Properly create ObjC protocols using the runtime in JIT mode.
Just injecting a new protocol descriptor into an already-running ObjC runtime isn't a good idea, since the runtime might have already canonized the protocol somewhere else, and it won't recognize that classes conform to protocols it doesn't know about.

Swift SVN r23313
2014-11-14 01:01:02 +00:00
Michael Gottesman
92021c0a52 [irgen] Call getObjectClass with CallingConv::C instead of RuntimeCC.
Swift SVN r23309
2014-11-13 21:14:14 +00:00
Michael Gottesman
2e37bef050 [irgen] Use object_getClass instead of object_getType() when lowering objc existential_metatype instructions.
Previously, we were not respecting the representation of the existential
metatype and were treating all existential metatypes as if the metatype
was a thick metatype. Instead now we properly grab the instance of the
class from the existential and then query the runtime for the
objc_class. This is done via the new entrypoint
emitHeapMetadataRefForUnknownHeapObject.

I also modified emitHeapMetadataRefForHeapObject to use
emitHeapMetadataRefForUnknownHeapObject instead of
emitLoadOfObjCHeapMetadataRef since the latter does not properly handle
tagged pointers. This bug was found on inspection when Joe and I were
talking about this change.

rdar://18841292

Swift SVN r23308
2014-11-13 19:36:00 +00:00
Nadav Rotem
5e9e68c568 Change the linkage type of internally used functions from Internal to Private, to remove them from the dylib symbol table.
This reduces the dylib size by ~3%.

Swift SVN r23212
2014-11-10 22:29:16 +00:00
Joe Groff
8d2b27134c IRGen: Emit ObjC class metadata references for class-constrained archetypes.
Swift SVN r23199
2014-11-09 20:54:26 +00:00
Graham Batty
63a429f16e Changes to code generation to support non-objc targets
Swift SVN r23118
2014-11-05 18:05:55 +00:00
Joe Groff
af0121f8e6 IRGen: Don't nonlazily realize classes (unless they ask).
We lazily realize classes when we access their metadata now, so there's no need to force the ObjC runtime to do this greedily anymore, except for classes that the runtime statically references. For those cases, add an @objc_non_lazy_realization class attribute that will put that class reference in the nlclslist section.

Swift SVN r23105
2014-11-05 00:19:37 +00:00
Joe Groff
5e6f4ffd55 IRGen: Mark calls to getForeignTypeMetadata as readnone.
Swift SVN r23100
2014-11-04 17:43:28 +00:00
Joe Groff
e773d9a1aa IRGen: Unique the metadata for imported structs and enums at runtime.
Move the uniquing information for ForeignTypeMetadata behind the address point so we can share the layout between foreign classes and the existing layout for struct and enum metadata. Emit metadata records for imported structs and enums as foreign metadata candidates, and dynamically unique references to the metadata by calling swift_getForeignTypeMetadata.

Swift SVN r23081
2014-11-03 06:12:13 +00:00
Erik Eckstein
6b8502b982 Store a fatal-error function in slots of eliminated vtable and witness table methods.
If for some reason an eliminated dead method is called (e.g. because of a compiler bug),
then the application aborts with a readable error message.



Swift SVN r22990
2014-10-28 16:06:37 +00:00
Joe Groff
1a24b1b56b IRGen/Runtime: Expand the private data area in metadata caches.
And move the new size into an ABI constant so we can change it in one place in the future if need be.

Swift SVN r22899
2014-10-23 22:16:32 +00:00
Joe Groff
f761fc03e6 IRGen/Runtime: Put a reference to the metadata pattern in the nominal type descriptor.
We want to use the reserved space in the metadata pattern for protocol conformance caching, and this link lets us find the metadata pattern from an instance of the generic type.

Swift SVN r22898
2014-10-23 21:33:38 +00:00
Joe Groff
5a2f48e3be Add a Builtin.BridgeObject type.
This is a type that has ownership of a reference while allowing access to the
spare bits inside the pointer, but which can also safely hold an ObjC tagged pointer
reference (with no spare bits of course). It additionally blesses one
Foundation-coordinated bit with the meaning of "has swift refcounting" in order
to get a faster short-circuit to native refcounting. It supports the following
builtin operations:

- Builtin.castToBridgeObject<T>(ref: T, bits: Builtin.Word) ->
  Builtin.BridgeObject

  Creates a BridgeObject that contains the bitwise-OR of the bit patterns of
  "ref" and "bits". It is the user's responsibility to ensure "bits" doesn't
  interfere with the reference identity of the resulting value. In other words,
  it is undefined behavior unless:

    castReferenceFromBridgeObject(castToBridgeObject(ref, bits)) === ref

  This means "bits" must be zero if "ref" is a tagged pointer. If "ref" is a real
  object pointer, "bits" must not have any non-spare bits set (unless they're
  already set in the pointer value). The native discriminator bit may only be set
  if the object is Swift-refcounted.

- Builtin.castReferenceFromBridgeObject<T>(bo: Builtin.BridgeObject) -> T

  Extracts the reference from a BridgeObject.

- Builtin.castBitPatternFromBridgeObject(bo: Builtin.BridgeObject) -> Builtin.Word

  Presents the bit pattern of a BridgeObject as a Word.

BridgeObject's bits are set up as follows on the various platforms:

i386, armv7:

  No ObjC tagged pointers
  Swift native refcounting flag bit: 0x0000_0001
  Other available spare bits:        0x0000_0002

x86_64:

  Reserved for ObjC tagged pointers: 0x8000_0000_0000_0001
  Swift native refcounting flag bit: 0x0000_0000_0000_0002
  Other available spare bits:        0x7F00_0000_0000_0004

arm64:

  Reserved for ObjC tagged pointers: 0x8000_0000_0000_0000
  Swift native refcounting flag bit: 0x4000_0000_0000_0000
  Other available spare bits:        0x3F00_0000_0000_0007

TODO: BridgeObject doesn't present any extra inhabitants. It ought to at least provide null as an extra inhabitant for Optional.

Swift SVN r22880
2014-10-23 00:09:23 +00:00
John McCall
89e60f31aa Add protocol witness tables to existential metatype
layouts.  Introduce new SIL instructions to initialize
and open existential metatype values.

Don't actually, y'know, lift any of the restriction on
existential metatypes; just pointlessly burn extra
memory storing them.

Swift SVN r22592
2014-10-08 01:20:13 +00:00
Erik Eckstein
43f68b6974 Enable dead function removal for internal function in whole-module compilation.
This is controlled by a new isWholeModule() attribute in SILModule.

It gives about 9% code size reduction on the benchmark executables.
For test-suite reasons it is currently not done for the stdlib.



Swift SVN r22491
2014-10-03 14:14:23 +00:00
Joe Groff
1c4e08a9c8 Fix up a tangle of issues related to reabstraction.
- A spot fix in SILGen for reabstracting the result of a downcast, which fixes checked casts to function types.
- Associate the layout information in type metadata records with the most abstract representation of the type. This is the correct thing to do in cases where we need the metadata as a tag for an opaque value--if we store a value in an Any, or pass it as an unconstrained generic parameter, we must maximally reabstract it. This fixes the value semantics of existentials containing trivial metatypes.
- To ensure that we get runtime layout of structs and enums correct when they contain reabstractable types, introduce a "metadata for layout" concept, which doesn't need to describe the canonical metadata for the type, but only needs to describe a type with equivalent layout and value semantics. This is a correctness fix that allows us to correctly lay out generic types containing dependent tuples and functions, and although we don't really take advantage of it here, it's also a potential runtime performance win down the road, because we could potentially produce direct metadata for a primitive type that's layout-equivalent with a runtime-instantiated type. To aid in type safety here, push SILType deeper into IRGen in places where we potentially care about specific representations of types.
- Finally, fix an inconsistency between the runtime and IRGen's concept of what spare bits unmanaged references and thick metatypes have.

Together, these fixes address rdar://problem/16406907, rdar://problem/17822208, rdar://problem/18189508, and likely many other related issues, and also fixes crash suite cases 012 and 024.

Swift SVN r21963
2014-09-16 01:44:34 +00:00
Joe Groff
3cf9a13d68 IRGen: Fix some synchronization issues when skipping @objc protocols for witness emission.
We were stopping too early when binding witness tables for local archetypes, causing us to miss binding witness tables for native protocols if the number of total protocols was greater than the number of protocols with witness tables, and we failed to increment the local type data key to match the Archetype::getConformsTo() ordinal when binding polymorphic parameters. Together these fix rdar://problem/18232916 (though adding any methods to a class still crashes because of rdar://problem/17480006).

Swift SVN r21796
2014-09-09 03:22:49 +00:00
John McCall
590c25479d Use isa-masking to read the class object pointer from
instances of Swift subclasses of ObjC classes.

We were already doing this in the runtime.  This patch
unhides the runtime's mask word (swift_isaMask) and makes
IR-gen take advantage of it when it can.

Swift SVN r21592
2014-08-29 21:36:53 +00:00
John McCall
0ddc7ee5b6 Resilience expansion is not an IR-generation concept.
If a type has to be passed or returned resiliently, it
will necessarily be passed indirectly, which is already
represented in SILFunctionType.  There is no need to
represent this as a separate channel of information.

NFC. Also fixes a problem where the signature cache
for ExtraData::Block was writing past the end of an
array (but into the storage for an adjacent array
which was fortunately never used).

ExtraData should also disappear as a concept, but we're
still relying on that for existential protocol witnesses.

Swift SVN r21548
2014-08-28 23:07:50 +00:00
John McCall
c1b03784df Instead of passing type metadata to swift_initClassMetadata,
pass the size and alignment of each field.  Take advantage
of this to pass a constant size and alignment when
possible.

This avoids the need to recursively find type metadata for
every field type, allowing generic recursively-structured
classes to be built.  There are a number of more complicated
cases that this approach isn't good enough for, but this
is good enough for now to fix rdar://18067671.

Also make an effort to properly support generic subclasses
of Objective-C classes.

Swift SVN r21506
2014-08-28 01:27:53 +00:00
John McCall
77ea35ab46 Introduce a flag to mark classes with Swift-native
refcounting and take advantage of it.

Also, set the Swift1 flag in classes we generate.

Also, initialize a global cache of the non-pointer-isa
mask and use that instead of object_getClass, at least
within the runtime.

Also, centralize the runtime on a _swift_getSuperclass
function and make that use a direct access while we
await word from Greg on the desired ABI requirements.

Swift SVN r21077
2014-08-06 21:51:45 +00:00
Greg Parker
48ef4efb39 IRGen: Don't read an ObjC isa field directly when building a generic subclass
of a generic superclass.

A new generic class gets its metaclass's superclass pointer from the 
superclass's metaclass. On arm64 the superclass's metaclass may not be 
a pointer and must be fetched using object_getClass().

Three existing tests caught this bug but only when running on an iOS 8 
device. On iOS 7, the arclite implementation of objc_readClassPair() 
happened to rewrite the field before the runtime tripped over it.


Swift SVN r20811
2014-07-31 05:13:17 +00:00
John McCall
6165da998c Rename some of the emitTypeMetadataFor... routines to make
it clear that they're requesting a dynamic type.

Some conceptual functional change; practical functionality
change should be limited.

Swift SVN r20733
2014-07-30 04:40:51 +00:00
Joe Groff
48851bc8fc IRGen: Remove inaccurate assertion about extension methods.
@objc extension methods can override non-Clang-imported @objc methods in their superclass. Fixes <rdar://problem/17720365>.

Swift SVN r20687
2014-07-29 20:21:14 +00:00
John McCall
64aa0ea950 When instantiating a generic class, compensate for
unexpected forematter from the superclass.

This requires a pretty substantial shift in the
generic-metadata allocation/initialization dance
because (1) we can't allocate class metadata without
knowing what the superclass is and (2) the offset
from the metadata cache entry to the address point is
no longer determined solely by the metadata pattern.

While I'm making invasive changes to metadata, fix
two race conditions in metadata creation.  The first
is that we need to ensure that only one thread succeeds
at lazily creating a generic-metadata cache.  The second
is that we need to ensure that only one thread actually
attempts to create a particular metadata; any others
should block until the metadata is successfully built.

This commit finishes rdar://17776354.  LLDB will
need to adjust to the runtime-private metadata layout
changes.

Swift SVN r20537
2014-07-25 10:08:51 +00:00
Joe Groff
623aba1786 Encapsulate Substitution's state.
Expose Substitution's archetype, replacement, and conformances only through getters so we can actually assert invariants about them. To start, require  replacement types to be materializable in order to catch cases where the type-checker tries to bind type variables to lvalue or inout types, and require the conformance array to match the number of protocol conformances required by the archetype. This exposes some latent bugs in the test suite I've marked as failures for now:

- test/Constraints/overload.swift was quietly suffering from <rdar://problem/17507421>, but we didn't notice because we never tried to codegen it.
- test/SIL/Parser/array_roundtrip.swift doesn't correctly roundtrip substitutions, which I filed as <rdar://problem/17781140>.

Swift SVN r20418
2014-07-23 18:00:38 +00:00
John McCall
807619b3c8 Force classes to be realized before forming a metadata
reference to them.

Fixes rdar://17776354.

Swift SVN r20384
2014-07-23 09:28:34 +00:00
John McCall
1ae1f750d0 Move most type metadata lookups into their own readnone
functions, and make those functions memoize the result.

This memoization can be both threadsafe and extremely
fast because of the memory ordering rules of the platforms
we're targeting: x86 is very permissive, and ARM has a
very convenient address-dependence rule which happens to
exactly match the semantics we need.

Swift SVN r20381
2014-07-23 07:38:26 +00:00
John McCall
ae0dfd4676 Change the layout of the extended class-object header: add
a flags field, add an instance address point field, and reserve
some additional space.

This change must be coordinated with a corresponding change
to ObjC runtime bits in libarclite; without this, dynamic
subclassing features like KVO will break.

The actual contents of the new fields can change without
bothering the ObjC runtime.

Swift SVN r20183
2014-07-18 21:39:43 +00:00
Joe Groff
03c9f871d2 IRGen/Runtime: Add a "primary generic parameters" count to generic parameter descriptors.
lldb needs this to be able to tell how many generic parameters are actually needed to instantiate a generic type. Fixes <rdar://problem/17425286>.

Swift SVN r19573
2014-07-04 19:34:11 +00:00
Joe Groff
a9a4601f22 IRGen: Update codegen for changes to cmpxchg LLVM instruction.
It now returns success as a separate i1 in the return struct.

Swift SVN r18883
2014-06-13 23:40:51 +00:00
Bob Wilson
fe63f7d35c GlobalAlias has been changing a fair amount lately upstream, but as of
r210062 they can point at arbitrary ConstantExprs again, and as of
r210734 our use of this can actually pass the IR verifier again.

Patch by Justin Bogner.

Swift SVN r18834
2014-06-12 19:48:41 +00:00
Chris Lattner
218859f7c2 Fix class metadata emission to properly strip ownership modifiers in a case
that it missed, fixing rdar://17220646.


Swift SVN r18738
2014-06-08 21:21:30 +00:00
Chris Lattner
2dc1df52d1 Implement <rdar://problem/16954464> Cannot make a strong IBOutlet
This adds support for the "strong" ownership modifier, and specifically 
make it override the "implicit weak" behavior of @IBOutlet.



Swift SVN r18737
2014-06-08 20:49:21 +00:00
Joe Groff
8c60c4af1f IRGen/Runtime: Emit distinct type metadata for block types.
Blocks need their own type metadata with value witnesses appropriate to the block representation. Fixes <rdar://problem/16918740> and <rdar://problem/16981126>.

Swift SVN r18508
2014-05-21 20:24:11 +00:00
Jordan Rose
99ae560530 [IRGen] Update for LLVM API change in r208983.
If you have not pulled this commit yet, it is safe to revert this locally.

Swift SVN r18207
2014-05-16 19:54:10 +00:00