Commit Graph

284 Commits

Author SHA1 Message Date
Roman Levenstein
de3b850ce8 Use more descriptive names for calling conventions.
Rename RuntimeCC into DefaultCC
Rename RuntimeCC1 into RegisterPreservingCC
Remove RuntimeCC0 because it was identical to DefaultCC.
2016-02-25 05:31:00 -08:00
Roman Levenstein
ec04b22145 Properly propagate the calling convention into LLVM's call instructions.
Each runtime function definition in RuntimeFunctions.def states which calling convention
should be used for this runtime function.  But IRGen and LLVMPasses were not always
properly propagating this declared calling convention all the way down to llvm's Call instructions.
In many cases, the standard C convention was set for the call irrespective of the actual calling
convention defined for a given runtime function. As a result, incorrect code was generated.

This commit tries to fix all those places, where such a mismatch was found. In many cases this is
achieved by defining a helper function CreateCall in such a way that makes sure that the call instruction
gets the same calling convention as the one used by its callee operand.
2016-02-25 05:30:59 -08:00
Slava Pestov
33ed1e0ab6 IRGen: Add ResilienceExpansion parameter to TypeInfo::isKnownEmpty(), NFC
In a few places, we have to be careful about the distinction between
"empty in this resilience domain" versus "empty in all resilience
domains". Make callers think about this by adding a parameter instead
of relying on them to check isFixedSize() as necessary first.

While making this change I noticed that the code for checking if
types are empty when computing extra inhabitants of structs and enums
might be slightly wrong in the face of resilience; I will revisit
this later.
2016-01-08 19:56:01 -08:00
Slava Pestov
7faf90a787 IRGen: Replace ResilienceScope enum with AST's ResilienceExpansion, NFC 2016-01-07 08:29:23 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Joe Groff
cf87b9d571 IRGen: Generate swift_fixLifetime marker as a private stub.
This lets us remove `swift_fixLifetime` as a real runtime entry point. Also, avoid generating the marker at all if the LLVM ARC optimizer won't be run, as in -Onone or -disable-llvm-arc-optimizer mode.
2015-12-21 18:06:18 -08:00
John McCall
e389ba080b Unowned references under ObjC interop only have one extra inhabitant.
A single extra inhabitant is good enough for the most important case,
that being a single level of optionality.  Otherwise, we want to
reserve maximal flexibility for the implementation.

This commit also fixes a bug where I was not correctly defining
the extra-inhabitant rules for all of the existential cases.
2015-12-09 19:28:38 -08:00
John McCall
bdb4b896a2 Move ObjC-compatible unowned references to the new runtime
functions.

Take the code for the old, broken reference-counting
implementation and delete it with prejudice.
2015-12-08 16:20:32 -08:00
John McCall
4d1b6e2eb6 Reform the runtime interface for unowned reference-counting.
This is a bit of a hodge-podge of related changes that I decided
weren't quite worth teasing apart:

First, rename the weak{Retain,Release} entrypoints to
unowned{Retain,Release} to better reflect their actual use
from generated code.

Second, standardize the names of the rest of the entrypoints around
unowned{operation}.

Third, standardize IRGen's internal naming scheme and API for
reference-counting so that (1) there are generic functions for
emitting operations using a given reference-counting style and
(2) all operations explicitly call out the kind and style of
reference counting.

Finally, implement a number of new entrypoints for unknown unowned
reference-counting.  These entrypoints use a completely different
and incompatible scheme for working with ObjC references.  The
primary difference is that the new scheme abandons the flawed idea
(which I take responsibility for) that we can simulate an unowned
reference count for ObjC references, and instead moves towards an
address-only scheme when the reference might store an ObjC reference.
(The current implementation is still trivially takable, but that is
not something we should be relying on.)  These will be tested in a
follow-up commit.  For now, we still rely on the bad assumption of
reference-countability.
2015-12-04 13:18:14 -08:00
Michael Gottesman
9fb54bf4bf Fix for upstream ilist changes. 2015-11-11 16:07:41 -08:00
Slava Pestov
3d19691bab IRGen: Always use Swift reference counting when Obj-C interop is not available
The swift_unknown* entry points are not available on the Linux port.
Previously we would still attempt to use them in a couple of cases:

1) Foreign classes
2) Existentials and archetypes
3) Optionals of boxed existentials

Note that this patch changes IRGen to never emit the
swift_errorRelease/Retain entry points on Linux. We would like to
use them in the future if we ever adopt a tagged-pointer representation
for small errors. In this case, they can be brought back, and the
TypeInfo for optionals will need to be generalized to propagate the
reference counting of the payload type, instead of defaulting to
unknown if the payload type is not natively reference counted.
A similar change will need to be made to support blocks, if we ever
want to use the blocks runtime on Linux.

Fixes <rdar://problem/23335318>, <rdar://problem/23335537>,
<rdar://problem/23335453>.
2015-11-03 13:03:50 -08:00
Slava Pestov
76f1168adf Remove dealloc_ref's [constructor] form since its no longer needed
Swift SVN r32688
2015-10-14 22:50:09 +00:00
Slava Pestov
7cc59e01d7 SIL: Add DeallocPartialRefInst
This instruction destroys instance variables in a partially-initialized
object, then frees it.

Swift SVN r32661
2015-10-13 19:56:20 +00:00
Xin Tong
dca508bfbb This is part of a series of commits to remove reference forwarding for
some of the ARC entry points. rdar://22724641. After this commit,
swift_retain_noresult will be completely replaced by swift_retain.
LLVMARCOpts pass is modified NOT to rewrite swift_retain to
swift_retain_noresult which forward no reference.

Swift SVN r32082
2015-09-18 21:56:50 +00:00
Michael Gottesman
121ef3ef9f Revert the series of commits for removing the return value from swift_retain_noresult.
I asked that the patches were split up so I could do post commit review.

This reverts commit r32059.
This reverts commit r32058.
This reverts commit r32056.
This reverts commit r32055.

Swift SVN r32060
2015-09-18 02:31:24 +00:00
Xin Tong
4e46dacc3d Completely replace swift_retain_noresult with swift_retain. this is part of a series of commits
to remove reference forwarding for some of the ARC entry points. rdar://22724641. After this
commit, swift_retain_noresult will be completely replaced by swift_retain and LLVMARCOpts.cpp
will no longer canonicalize swift_retain to swift_retain_noresult as now swift_retain returns no
reference.

Swift SVN r32058
2015-09-18 01:51:17 +00:00
Slava Pestov
1628bfc3e2 SIL: Split dealloc_ref into [constructor] and [destructor] kinds
dealloc_ref [destructor] is the existing behavior. It expects the
reference count to have reached zero and the isDeallocating bit to
be set.

The new [constructor] variant first drops the initial strong
reference.

This allows DI to properly free uninitialized instances in
constructors. Previously this would fail with an assertion if the
runtime was built with debugging enabled.

Progress on <rdar://problem/21991742>.

Swift SVN r31142
2015-08-11 18:38:00 +00:00
Joe Groff
d7d3ff2b10 IRGen/Runtime: Keep the payload offset inside both compiler- and runtime-generated box metadata.
A missing link in our polymorphic box projection story, and likely a perf improvement for the generic case as well.

Swift SVN r29835
2015-07-01 04:51:56 +00:00
Joe Groff
480a55c544 IRGen: Fix the address type of empty boxes.
Should be %swift.opaque*, or whatever the empty type claims as its storage type, not a pointer to the box type itself. Fixes rdar://problem/21597855.

Swift SVN r29795
2015-06-29 22:25:30 +00:00
Joe Groff
2630dba793 Runtime: Rename Box2 functions to Box.
Swift SVN r29763
2015-06-27 18:10:17 +00:00
Dmitri Hrybenko
2fc1cbe8c1 Adjust to the new IRBuilder APIs
Swift SVN r29692
2015-06-25 22:01:24 +00:00
Joe Groff
02b1b1c4ba IRGen: Save the private heap metadata we emit for a HeapLayout.
Instead of slothfully re-emitting it every time we emit an allocation using it.

Swift SVN r29678
2015-06-25 18:29:44 +00:00
Joe Groff
3c2ee54d43 IRGen: Implementation of typed box operations.
When producing TypeInfo for a box, try to reuse instantiations for common type structures:

- any POD type with the same stride and alignment can share a fixed HeapLayout;
- any single-refcounted-pointer type can share a fixed HeapLayout with types that have the same ReferenceCounting;
- dynamically-sized types can share a runtime-based box implementation.

For the runtime implementation, use new to-be-implemented variants of allocBox/deallocBox that will produce polymorphically-projectable boxes using instantiated metadata.

Swift SVN r29612
2015-06-24 19:43:24 +00:00
John McCall
1f80de3d71 Restore the original intent of our IRBuilder to not provide
functions to create load/store instructions without alignment.

Fix a couple of places that were unnecessarily using this.
This includes patching up some very suspicious code for generating
"shadow copies" of explosions for debug info that's not using
the existing TypeInfo-based load/store facilities for some
reason; I left the existing pattern in place for now, but it's
probably bogus.

Swift SVN r29459
2015-06-17 21:34:03 +00:00
Joe Groff
79dc6db4bb IRGen: Redesign enum codegen to break payloads into word-sized chunks.
Using LLVM large integers to represent enum payloads has been causing compiler performance and code size problems with large types, and has also exposed a long tail of backend bugs. Replace them with an "EnumPayload" abstraction that manages breaking a large opaque binary value into chunks, along with masking, testing, and extracting typed data from the binary blob. For now, use a word-sized chunking schema always, though the architecture here is set up to eventually allow the use of an arbitrary explosion schema, which would benefit single-payload enums by allowing the payload to follow the explosion schema of the contained value.

This time, adjust the assertion in emitCompare not to perform a check before we've established that the payload is empty, since APInt doesn't have a 0-bit state and the default-constructed form is nondeterminisitic. (We should probably use a more-tailored representation for enum payload bit patterns than APInt or ClusteredBitVector.)

Swift SVN r28985
2015-05-24 16:20:21 +00:00
Ted Kremenek
bbf61217cd Revert "IRGen: Redesign enum codegen to break payloads into word-sized chunks."
This is asserting on the bots:

Assertion failed: ((~mask & value) == 0 && "value has masked out bits set?!"), function emitCompare

Swift SVN r28983
2015-05-24 05:38:53 +00:00
Joe Groff
3691991374 IRGen: Redesign enum codegen to break payloads into word-sized chunks.
Using LLVM large integers to represent enum payloads has been causing compiler performance and code size problems with large types, and has also exposed a long tail of backend bugs. Replace them with an "EnumPayload" abstraction that manages breaking a large opaque binary value into chunks, along with masking, testing, and extracting typed data from the binary blob. For now, use a word-sized chunking schema always, though the architecture here is set up to eventually allow the use of an arbitrary explosion schema, which would benefit single-payload enums by allowing the payload to follow the explosion schema of the contained value.

Swift SVN r28982
2015-05-24 05:23:23 +00:00
Manman Ren
e94aae06da [Function Attribute] add target-cpu and target-features sets if they're non-null.
All llvm::Functions created during IRGen will have target-cpu and target-features
attributes if they are non-null.

Update testing cases to expect the attribute in function definition.
Add testing case function-target-features.swift to verify target-cpu and
target-features.

rdar://20772331


Swift SVN r28186
2015-05-05 23:19:48 +00:00
Andrew Trick
a174aa4dfe Add AST and SILGen support for Builtin.isUnique.
Preparation to fix <rdar://problem/18151694> Add Builtin.checkUnique
to avoid lost Array copies.

This adds the following new builtins:

    isUnique : <T> (inout T[?]) -> Int1
    isUniqueOrPinned : <T> (inout T[?]) -> Int1

These builtins take an inout object reference and return a
boolean. Passing the reference inout forces the optimizer to preserve
a retain distinct from what’s required to maintain lifetime for any of
the reference's source-level copies, because the called function is
allowed to replace the reference, thereby releasing the referent.

Before this change, the API entry points for uniqueness checking
already took an inout reference. However, after full inlining, it was
possible for two source-level variables that reference the same object
to appear to be the same variable from the optimizer's perspective
because an address to the variable was longer taken at the point of
checking uniqueness. Consequently the optimizer could remove
"redundant" copies which were actually needed to implement
copy-on-write semantics. With a builtin, the variable whose reference
is being checked for uniqueness appears mutable at the level of an
individual SIL instruction.

The kind of reference count checking that Builtin.isUnique performs
depends on the argument type:

    - Native object types are directly checked by reading the
      strong reference count:
      (Builtin.NativeObject, known native class reference)

    - Objective-C object types require an additional check that the
      dynamic object type uses native swift reference counting:
      (Builtin.UnknownObject, unknown class reference, class existential)

    - Bridged object types allow the dymanic object type check to be
      bypassed based on the pointer encoding:
      (Builtin.BridgeObject)

Any of the above types may also be wrapped in an optional.  If the
static argument type is optional, then a null check is also performed.

Thus, isUnique only returns true for non-null, native swift object
references with a strong reference count of one.

isUniqueOrPinned has the same semantics as isUnique except that it
also returns true if the object is marked pinned regardless of the
reference count. This allows for simultaneous non-structural
modification of multiple subobjects.

In some cases, the standard library can dynamically determine that it
has a native reference even though the static type is a bridge or
unknown object. Unsafe variants of the builtin are available to allow
the additional pointer bit mask and dynamic class lookup to be
bypassed in these cases:

    isUnique_native : <T> (inout T[?]) -> Int1
    isUniqueOrPinned_native : <T> (inout T[?]) -> Int1

These builtins perform an implicit cast to NativeObject before
checking uniqueness. There’s no way at SIL level to cast the address
of a reference, so we need to encapsulate this operation as part of
the builtin.

Swift SVN r27887
2015-04-28 22:54:24 +00:00
Joe Groff
e4e0f35aed IRGen: Implement an @_alignment attribute.
This is an internal-only affordance for the numerics team to be able to work on SIMD-compatible types. For now, it can only increase alignment of fixed-layout structs and enums; dynamic layout, classes, and other obvious extensions are left to another day when we can design a proper layout control design.

Swift SVN r27323
2015-04-15 17:23:30 +00:00
Joe Groff
1e3f5c5b10 IRGen: Fix miscalculation in non-fixed partial_apply offset calculation.
Offset by the current element size to get the base for the next element, not the previous. Oops. Fixes rdar://problem/20475584.

Swift SVN r27238
2015-04-12 17:33:29 +00:00
Joe Groff
768420d33b IRGen/Runtime: Give ErrorType its own retain/release entry points.
Some future-proofing to let us change ErrorType's reference counting in the future, or to use various tagged pointer optimizations in its representation.

Swift SVN r27213
2015-04-10 18:54:16 +00:00
Chris Lattner
79ed57f9f2 standardize naming of tuples and tuple patterns on "elements".
Previously some parts of the compiler referred to them as "fields",
and most referred to them as "elements".  Use the more generic 'elements'
nomenclature because that's what we refer to other things in the compiler
(e.g. the elements of a bracestmt).

At the same time, make the API better by providing "getElement" consistently
and using it, instead of getElements()[i].

NFC.



Swift SVN r26894
2015-04-02 20:23:49 +00:00
Joe Groff
58b43b8973 IRGen: Fix the extra inhabitants of unowned references.
They ought to be the same as strong references, for now. Fixes rdar://problem/18326077.

Swift SVN r25019
2015-02-05 23:51:10 +00:00
Joe Groff
3f2bb26c04 Reinstate r24526, r24548, r24549, and r24550.
OpaqueStorageTypeInfo will be adjusted to use [N x i8] storage types in the following commit.

Swift SVN r24569
2015-01-20 21:49:46 +00:00
Joe Groff
344514a39f Revert r24526, r24548, r24549, and r24550.
OpaqueStorageTypeInfo uses iNNN types that don't always have the correct alloc size for an expected
size at the LLVM level. This needs to be fixed before my partial apply closure fixes can hold.

Swift SVN r24551
2015-01-20 06:14:20 +00:00
Joe Groff
934661c000 IRGen: Wire up NonFixedOffsets for partial apply objects.
This gets generic curries working correctly. Add an interpreter test to verify.

Swift SVN r24548
2015-01-20 04:42:26 +00:00
Joe Groff
444ce77b63 IRGen: Use NecessaryBindings to save captured generic context into partial_apply closures.
In order to deal with generic indirect value captures, we need to be able to bind their type metadata in the partial apply forwarder and heap object destructor to have access to the value operations for that type. NecessaryBindings gives us a way to do that and clean up our ad-hoc polymorphic argument forwarding we had before. N(intended)FC yet, aside from some harmless reordering of operations, since we also need to implement NonFixedOffsets for heap objects to be fully operational.

Swift SVN r24526
2015-01-19 22:52:14 +00:00
John McCall
1326105c30 Fix an embarrasing IRGen bug with strong_pin:
use the call result as the pin handle.

Swift SVN r24418
2015-01-14 19:14:02 +00:00
John McCall
275ef489d4 Switch IRGen to use ClusteredBitVector.
IRGen uses a typedef, SpareBitVector, for its principal
purpose of tracking spare bits.  Other uses should not
use this typedef, and I've tried to follow that, but I
did this rewrite mostly with sed and may have missed
some fixups.

This should be almost completely NFC.  There may be
some subtle changes in spare bits for witness tables
and other off-beat pointer types.  I also fixed a bug
where IRGen thought that thin functions were two
pointers wide, but this wouldn't have affected anything
because we never store thin functions anyway, since
they're not a valid AST type.

This commit repplies r24305 with two fixes:

  - It fixes the computation of spare bits for unusual
    integer types to use the already-agreed-upon type
    size instead of recomputing it.  This fixes the
    i386 stdlib build.  Joe and I agreed that we should
    also change the size to use the LLVM alloc size
    instead of the next power of 2, but this patch
    does not do that yet.

  - It changes the spare bits in function types back
    to the empty set.  I'll be changing this in a
    follow-up, but it needs to be tied to runtime
    changes.  This fixes the regression test failures.

Swift SVN r24324
2015-01-09 21:06:37 +00:00
Dmitri Hrybenko
4caca5d098 Revert "Switch IRGen to use ClusteredBitVector."
This reverts commit r24305.  It broke the standard library build.

Swift SVN r24318
2015-01-09 18:25:21 +00:00
John McCall
6e41b3c742 Switch IRGen to use ClusteredBitVector.
IRGen uses a typedef, SpareBitVector, for its principal
purpose of tracking spare bits.  Other uses should not
use this typedef, and I've tried to follow that, but I
did this rewrite mostly with sed and may have missed
some fixups.

This should be almost completely NFC.  There may be
some subtle changes in spare bits for witness tables
and other off-beat pointer types.  I also fixed a bug
where IRGen thought that thin functions were two
pointers wide, but this wouldn't have affected anything
because we never store thin functions anyway, since
they're not a valid AST type.

Swift SVN r24305
2015-01-09 10:05:51 +00:00
John McCall
3b4e0d307e Intrinsic support for pinning.
Using the intrinsics is obnoxious because I needed them
to return Builtin.NativeObject?, but there's no reasonable
way to safely generate optional types from Builtins.cpp.
Ugh.

Dave and I also decided that there's no need for
swift_tryPin to allow a null object.

Swift SVN r23824
2014-12-10 00:52:48 +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
0885c5617e Runtime: Invoke objc_destructInstance before deallocating class instances.
This is necessary to clean up associated objects and ObjC weak references to the object. rdar://problem/18637774

Swift SVN r23068
2014-11-02 21:03:18 +00:00
Joe Groff
fc369ce734 Give Builtin.BridgeObject nil as an extra inhabitant.
Reserve the other low pointer values for potential internal use.

Swift SVN r22881
2014-10-23 00:49:14 +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
Jordan Rose
042569a3be Optional: Replace uses of Nothing with None.
llvm::Optional (like Swift.Optional!) uses None as its placeholder value,
not Nothing.

Swift SVN r22476
2014-10-02 18:51:42 +00:00
Joe Groff
b95fa01331 IRGen: Remove dead heap array support code.
Swift SVN r22293
2014-09-26 02:51:31 +00:00