Commit Graph

1232 Commits

Author SHA1 Message Date
Arnold Schwaighofer
c322b3592d Add a data dependence between opened existential values and method_inst that 'use' them.
Before this patch there was no dependence visible to the optimizer between a
open_existential and the witness_method allowing the optimizer to reorder the
two instruction. The dependence was implicit in the opened archetype but this
is not a concept model by the SIL optimizer.

  %2 = open_existential %0 : $*FooProto to $*@opened("...") FooProto
  %3 = witness_method $@opened("...") FooProto,
                      #FooProto.bar!1 : $@cc(...)
  %4 = apply %3<...>(%2)

This patch changes the SIL representation such that witness_methods on opened
archetypes take the open_existential (or the producer of the opened existential)
as an operand preventing the optimizer from reordering them.

  %2 = open_existential %0 : $*FooProto to $*@opened("...") FooProto
  %3 = witness_method $@opened("...") FooProto,
                      #FooProto.bar!1,
                      %2 : $*@opened("...") FooProto : $@cc(...)
  %4 = apply %3<...>(%2)

rdar://18984526

Swift SVN r23438
2014-11-19 17:22:22 +00:00
Joe Groff
055e8671a0 SILGen: Fix memory leak when casting from address-only to loadable trivial type.
The result does not carry a meaningful cleanup when trivial. Fixes rdar://problem/18936388.

Swift SVN r23284
2014-11-12 21:03:15 +00:00
Joe Groff
b73b22a179 Remove meaningless code.
Swift SVN r23278
2014-11-12 18:13:40 +00:00
Joe Groff
aa3f35c28d Don't consider DynamicTypeExpr or MetatypeConversionExpr to produce a statically derived metatype.
Though the value may be statically known in some cases, that isn't good enough to do what we try to do with this information. In particular, if we invoke a class method on a MetatypeConversion, we want to dispatch to the method of the original metatype, not statically call the method of the converted type, which is what is evident in the AST. Fixes rdar://problem/18877135.

Swift SVN r23277
2014-11-12 18:10:31 +00:00
Joe Groff
976d1db2f8 SILGen: Don't leak BridgeObjects when we cast their bit patterns.
Fixes rdar://problem/18925836.

Swift SVN r23240
2014-11-11 19:16:14 +00:00
Joe Groff
db4ff15380 SILGen: Implement partial application of generic methods.
Most of the parts were already here. We mishandled a few edge cases in RValueEmitter because of MemberRefExpr/ApplyExpr confusion at the Sema level, and we artifically asserted that we didn't support this. Removing the assertion and wiring up the existing thunking infrastructure made this just fall out. Fixes rdar://problem/18763738.

Swift SVN r22944
2014-10-26 02:34:26 +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
Joe Groff
e3f9a2035c SIL: Move SILGen and passes over to use "builtin" instead of "apply (builtin_function_ref)".
Swift SVN r22785
2014-10-15 23:37:22 +00:00
Chris Lattner
558439b507 simplify the implementation of emitMetatypeOfValue a bit, NFC.
Swift SVN r22733
2014-10-14 21:06:15 +00:00
Joe Groff
c459a57222 SILGen: Allow castFromNativeObject to AnyObject.
And in IRGen, fix up pointer cast codegen to work with types that explode to single scalars while being stored in named structs. Fixes rdar://problem/18604262

Swift SVN r22671
2014-10-10 18:55:21 +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
782833f054 SIL: Remove the project_existential* instructions.
Swift SVN r22457
2014-10-02 04:06:10 +00:00
Joe Groff
79e39cd342 SILGen: Replace the remaining uses of project_existential with open_existential.
Swift SVN r22454
2014-10-02 01:02:01 +00:00
Joe Groff
d90a3d6586 SILGen: Remove dead code for special-case lowering of protocol methods.
These now share the archetype code path.

Swift SVN r22444
2014-10-01 21:44:11 +00:00
Joe Groff
23f91d918a SILGen: Open existential bases of property accesses.
Use open_existential/witness_method instead of project_existential/protocol_method for property accessor lookups to, eliminating the remaining uses of protocol_method.

Swift SVN r22443
2014-10-01 21:38:24 +00:00
Joe Groff
a9f0cc0ae7 SILGen: Open existentials when invoking their methods.
This simplifies the code generation path for existential methods by allowing it to shared more code with the generic case, (It'll be even simpler when Sema opens the existentials for SILGen...) turning protocol_method lookups into open_existential + witness_method sequences. In this patch, we handle normal generic method lookups, but property accesses still go through protocol_method.

Swift SVN r22437
2014-10-01 20:15:58 +00:00
Joe Groff
560f99918a SILGen: Claim orig and subst parameter clauses in sync when emitting calls.
This fixes bugs when we call the result of generic methods that return functions; we were advancing through orig types of the signature but not formal ones, causing us to fall out of sync when we exhausted curry levels of the original function and started calling function-typed results. Fixes rdar://problem/18143223 and rdar://problem/18495979, maybe others too.

Swift SVN r22418
2014-10-01 02:02:21 +00:00
Joe Groff
c098645f34 SIL: Conformances relate to formal types, so change witness_method's lookup type to a CanType.
Should be NFC in practice, since only nominal types can currently conform to protocols anyway, but improves modeling of the system.

Swift SVN r22360
2014-09-29 20:44:00 +00:00
Joe Groff
e7f7033d31 SILGen: Fix up handling of 'self' parameter when opaque protocol requirements are accessed through class-constrained type.
"self" needs to be materialized in these cases. We were handling methods correctly, but not property accesses. Fixes rdar://problem/18454204.

Swift SVN r22309
2014-09-27 02:11:48 +00:00
John McCall
f20225a34b Access properties and subscripts in the most efficient
semantically valid way.

Previously, this decision algorithm was repeated in a
bunch of different places, and it was usually expressed
in terms of whether the decl declared any accessor
functions.  There are, however, multiple reasons why a
decl might provide accessor functions that don't require
it to be accessed through them; for example, we
generate trivial accessors for a stored property that
satisfies a protocol requirement, but non-protocol
uses of the property do not need to use them.

As part of this, and in preparation for allowing
get/mutableAddressor combinations, I've gone ahead and
made l-value emission use-sensitive.  This happens to
also optimize loads from observing properties backed
by storage.

rdar://18465527

Swift SVN r22298
2014-09-26 06:35:51 +00:00
John McCall
6923101341 Rename "AccessKind" to "AccessSemantics". NFC.
There are a lot of different ways to interpret the
"kind" of an access.  This enum specifically dictates
the semantic rules for an access:  direct-to-storage
and direct-to-accessor accesses may be semantically
different from ordinary accesses, e.g. if there are
observers or overrides.

Swift SVN r22290
2014-09-25 23:12:39 +00:00
Joe Groff
9eb4f5b512 SILGen: Use Array._allocateUninitialized to form array literals.
This avoids a pointless copy every time an array literal is written, and will let us retire the horrible "alloc_array" instruction and globs of broken IRGen code. Implements rdar://problem/16386862, and probably fixes a bunch of bugs related to alloc_array brokenness.

Swift SVN r22289
2014-09-25 22:26:20 +00:00
John McCall
defaf07046 SILGen support for addressors.
Addressors now appear to pass a simple smoke-test; in
order to allow some parallel developement, I'll be
committing actual SILGen tests for this later and
separately.

Swift SVN r22243
2014-09-23 23:46:52 +00:00
Joe Groff
6cd5123712 SILGen: Use the formal type when taking dynamicTypes of values.
The metatype is associated with the formal AST type of the value, not whatever lowered SIL type we happen to have lying around. Adjust the SIL verifier to check that value_metatype instructions produce a metatype for which the instance is a potential lowering rather than by exact type match. This lets us take the metatype of metatypes (and incidentally, of functions, and of tuples thereof), fixing rdar://problem/17242770.

Swift SVN r22202
2014-09-23 02:56:48 +00:00
Joe Groff
f518ff5003 Remove the dead Builtin.typeof function.
We have syntax for this now.

Swift SVN r22201
2014-09-23 02:56:41 +00:00
John McCall
2fc49df319 Teach SILGen to take advantage of materializeForSet
accessors on non-dynamic storage.

This allows us to take advantage of dynamic knowledge
that a property is implemented as stored in order to
gain direct access to it instead of copying into a
temporary.  When a class or protocol-member property
is expensive to copy, and particularly when it's of
copy-on-write type, such direct accesses can massively
improve the performance of a program.

We are currently only able to apply this when the
property is implemented purely as stored.  A willSet
observer inherently blocks the optimization, but we
should be able to make it work even for properties
with didSet observers.  This could be done by returning
an arbitrary callback from materializeForSet instead
of returning a flag indicating whether to call the
setter.

rdar://17416120

Swift SVN r22122
2014-09-19 05:25:22 +00:00
John McCall
8cae5ba1d0 Generalize 'isDirectPropertyAccess' to allow for
direct (i.e. non-polymorphic) access to accessor
functions, and use this in materializeForSet for
computed properties.

Swift SVN r22059
2014-09-18 05:51:32 +00:00
Joe Groff
3aa75d0c38 SILGen: Fix broken value semantics in previous commit.
When we take advantage of AllowPlusZero, we shouldn't put a cleanup on the materialized temporary.

Swift SVN r21982
2014-09-16 21:44:49 +00:00
Joe Groff
d3702950af SILGen: Materialize class-constrained "self" parameters when passed to non-class-constrained protocol methods.
Fixes rdar://problem/17480006, rdar://problem/17557409, rdar://problem/17929884 and crash suite #019.

Swift SVN r21981
2014-09-16 21:12:40 +00:00
Doug Gregor
37ec99601a Don't try to copy-initialize an aggregate whose members have
Recent versions of Clang correctly reject this rdar://problem/18291554.

Swift SVN r21848
2014-09-10 19:14:25 +00:00
Doug Gregor
769ba83b28 Clean up "self" properly when delegating to a factory method.
Fixes the leak reported in rdar://problem/18141590.


Swift SVN r21739
2014-09-05 04:57:58 +00:00
Joe Groff
53bbefbe42 SILGen: Thread direct access through all uses of emitRValueForDecl.
Swift SVN r21715
2014-09-04 18:11:04 +00:00
Chris Lattner
44a1eb7715 Rework SILGen of optional methods to work with non-@objc situations:
- Split getSelfTypeForDynamicLookup into two pieces, and generalize it
    to work on non-loadable protocols.
  - Change dynamic_method_branch to take its argument as a protocol of any
    protocol type, instead of as something of UnownObject type.
  - Teach emitForcedDynamicMemberRef to only do its peephole optimization for
    @objc cases, since it is special behavior of objc_msgSend.
  - enhance emitDynamicPartialApply & emitDynamicMemberRefExpr to emit the
    proper project_existential instruction (not a _ref) when dealing with a
    non-classbound protocol.

Change the verifier to allow DynamicMethodBranchInst to work on non-@objc
protocol members.

This eliminates a bunch of pointless unchecked_ref_cast's in the generated
SIL for existing code, but this got squashed at IRGen time anyway, so no
real change for anything that sema permits.



Swift SVN r21519
2014-08-28 07:37:18 +00:00
Chris Lattner
dc7c565af8 further generalize dynamic_method and dynamic_method_br: instead of having them force
the subject to "Builtin.UnknownObject", just traffic in the protocol type directly.


Swift SVN r21516
2014-08-28 05:41:35 +00:00
Joe Groff
4b6556c987 Remove stale comment.
Swift SVN r21490
2014-08-27 20:19:08 +00:00
Doug Gregor
7cae0cfc8e Remove -enable-dynamic and its language option; it's always on anyway.
Swift SVN r21354
2014-08-21 15:15:12 +00:00
Doug Gregor
67d0efd162 Use SILGenFunction::getMethodDispatch() to decide on delegating-initializer dispatch.
Fixes an attempt to perform vtable dispatch to an initializer that was
defined in an extension and is not @objc, which means it had no vtable
entry <rdar://problem/17909833>.

Swift SVN r21352
2014-08-21 14:05:16 +00:00
Arnold Schwaighofer
16e41ada77 Make Builtin.canBeClass return a tri-state
Replace the true/maybe state that Builtin.canBeClass was returning by a
tri-state (yes, no, maybe) allowing the optimizer to use the definite no
answer.  This removes the need of the sizeof check that we had in
isClassOrObjCExistential. It also removes the need to CSE this function since
in most cases we will be able to instantiate canBeClass to yes or no (vs maybe)
at compile time.

benchmark``````````````,``baserun0``,``optrun2``,``delta,``speedup
ClassArrayGetter```````,``988.00````,``337.00```,``644.00``,````````191.7%
DeltaBlue``````````````,``2429.00```,``1927.00``,``460.00``,````````23.9%
Dictionary`````````````,``1374.00```,``1231.00``,``129.00``,````````10.9%
Havlak`````````````````,``1079.00```,``911.00```,``124.00``,````````13.7%
Rectangles`````````````,``924.00````,``541.00```,``379.00``,````````70.1%

radar://16823238

Swift SVN r21331
2014-08-21 00:55:40 +00:00
Joe Groff
6d2a3bb0fa SILGen: Load 'self' at +1 for class-constrained generic methods.
"self" for class-constrained witnesses is still passed at +1, so we can't load it using AllowPlusZero. Fixes <rdar://problem/17852846>.

Swift SVN r20868
2014-08-01 00:31:20 +00:00
Chris Lattner
2a9ba67b04 refactor some call emission logic w.r.t. inout arguments to expose the LValue for the inout
argument to SILGenApply.  NFC.


Swift SVN r20729
2014-07-30 00:37:09 +00:00
Chris Lattner
6d04cc4255 revert r20720: getting RValueSource's for self arguments is futile: we need
a concrete SILValue when emitting the Callee object so we can look things up
in method tables etc.  Without reengineering all of Callee, that isn't going 
to work.


Swift SVN r20727
2014-07-30 00:17:29 +00:00
Chris Lattner
b89476336f push RValueSource down into the 'self' parameter emission logic in applications a bit farther, NFC.
Swift SVN r20720
2014-07-29 23:29:40 +00:00
Chris Lattner
0db920b1c4 Reject obvious inout argument aliases with a good diagnostic, for example:
t2.swift:4:11: error: inout arguments are not allowed to alias each other
 swap(&x, &x)
          ^~
t2.swift:4:7: note: previous aliasing argument
 swap(&x, &x)
      ^~

This can (and arguably should) also be handled with a later diagnostic pass, but
handling it in SILGen gives us full range emission capability.



Swift SVN r20469
2014-07-24 05:11:28 +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
Joe Groff
9b63a6d241 SILGen: Don't check requiresObjCDispatch for dynamic_method lookups.
They by definition always require ObjC dispatch. Will be tested when 'dynamic' is enabled.

Swift SVN r20284
2014-07-22 00:23:35 +00:00
Joe Groff
36b2fd2d49 SILGen: Only dynamically dispatch 'dynamic' methods in extensions.
When 'dynamic' is honored, extension methods can only be dynamically dispatched if they'll be objc-dispatched, since they don't have native vtable entries.

Swift SVN r19999
2014-07-16 01:43:20 +00:00
Joe Groff
45eec9a2e9 Remove 'interface' from the method names of SILFunctionType.
SILFunctionTypes are always interface types now. NFC.

Swift SVN r19952
2014-07-14 22:03:46 +00:00
Joe Groff
eaf84f5f0e SILGen: Use the 'dynamic' attribute to decide whether to emit ObjC dispatches.
When -enable-dynamic is passed, only require ObjC dispatch for 'dynamic' methods and accessors instead of for all @objc entities.

Swift SVN r19839
2014-07-11 00:12:25 +00:00
Chris Lattner
5b49d59c57 Remove the @ from @final and @lazy, the last major piece of
rdar://17168115.

Also, reinstate the ARM driver change and testcase that I removed
in my last patch.


Swift SVN r19790
2014-07-10 06:23:27 +00:00
Chris Lattner
02999cac51 Reinstate the @ on the @objc attribute. This is largely a revert of r19555 with a few tweaks.
Swift SVN r19706
2014-07-08 21:50:34 +00:00