Commit Graph

1326 Commits

Author SHA1 Message Date
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
John McCall
059ed4cd10 Propagate alignment arguments around through the
slow allocation/deallocation APIs.

Swift SVN r18160
2014-05-16 01:40:36 +00:00
Joe Groff
972ddc10d9 IRGen: Implement type metadata lookup for dynamic Self.
Fixes <rdar://problem/16925631>.

Swift SVN r18125
2014-05-15 20:44:59 +00:00
Joe Groff
202ee91549 IRGen/Runtime: Use objc_readClassPair to officially register generic classes with the ObjC runtime.
At long last, our generic classes are legal ObjC citizens. Note that you'll need to be either on OSX 10.10, or on OSX 10.9 with a recent enough Xcode 6.0 to have a not-broken arclite (6A207 worksforme) and building against a OSX 10.10 SDK, for this to work going forward.

Swift SVN r17916
2014-05-12 02:37:42 +00:00
Joe Groff
43cc4a28f2 IRGen: Instantiate ObjC metaclasses and rodata with generic classes.
If we officially register our classes with the ObjC runtime, we can't get away with generic class instances sharing a runtime name or a metaclass anymore, so pack the metaclass and rodata templates into the generic metadata template and add codegen to the fill function to wire up the references at instantiation time. Since we don't have a runtime mangler yet, create a stupid unique name for classes by tacking on the pointer value.

Swift SVN r17882
2014-05-11 19:42:38 +00:00
Ted Kremenek
3d3491b4c6 Initialize 'vwtableValue' to null (-Wconditional-uninitialized).
Swift SVN r17614
2014-05-07 07:34:49 +00:00
John McCall
bafeb84a56 Generate unique type metadata for foreign classes.
Swift SVN r17430
2014-05-05 06:45:42 +00:00
John McCall
b6831b0195 Extract a function to create a global string
constant for a type mangling.

Swift SVN r17429
2014-05-05 06:45:41 +00:00
John McCall
2b969c41a2 Track whether a class is "foreign" in the AST.
This basically just means "it's a CF class" for now,
but you could imagine applying this to all sorts of
class-like types from peer runtimes that we can't
support all possible language features for.

There are quite a few language features that require
fairly deep object-model integration to implement,
like subclassing and adding polymorphic methods.
Some of those features, like final classes, are useful
to generally support as attributes, but most of
them aren't.  At least in the short term, it makes
sense to have a big hammer we can hit things with.

Swift SVN r17428
2014-05-05 06:45:40 +00:00
Chris Lattner
a7012f23ef Remove swift::ArrayType. It is dead and vestigial code for supporting fixed size
arrays, which never got baked.  Remove it until we have time to do things right.


Swift SVN r16995
2014-04-28 21:18:27 +00:00
John McCall
63072df530 Add class size and address point fields to class metadata.
I've put these fields on the class object for now, just
so we can at least theoretically update them.  A superclass
that grew left rather than right could maybe even be made
to work with this schema, but probably not.

rdar://16705821

Swift SVN r16880
2014-04-26 10:57:50 +00:00
John McCall
f3d4513721 Turn some 64-bit metadata fields into 32-bit fields.
We really don't need to support individual objects
this large, much less more than 4 billion fields in
a single type.

Also rearrange the fields to bring the instance
size/alignment fields closer to the class header,
just for a minor locality win.

Swift SVN r16879
2014-04-26 09:43:39 +00:00
John McCall
cd8c2a47d4 Refactor metadata layout to allow non-pointer-sized
fields and reduce boilerplate.  NFC.

Swift SVN r16877
2014-04-26 07:00:55 +00:00
John McCall
8681963bcb A couple of long-overdue renames.
Builtin.ObjectPointer -> Builtin.NativeObject
Builtin.ObjCPointer -> Builtin.UnknownObject

Swift SVN r16634
2014-04-22 00:17:08 +00:00
Joe Groff
0cb9bb50c1 IRGen: Get 'isa' pointers via object_getClass for ObjC-heritage classes.
We can't know whether a mixed-heritage class has a non-pointer isa, so use object_getClass to get its isa pointer. Fixes <rdar://problem/16656489>.

Swift SVN r16549
2014-04-18 21:28:41 +00:00
Joe Groff
f7cf592777 Add a SIL-only '@block_storage T' type to represent on-stack storage for blocks.
Blocks need to be born on the stack, so we need a way to represent that on-stack storage. @block_storage T will represent the layout of a block that contains storage for a capture of type T.

Swift SVN r16355
2014-04-15 03:56:09 +00:00
John McCall
8a85750c4c Plumb a lot of querying for different kinds of existential
type.

Swift SVN r16232
2014-04-11 22:30:14 +00:00