Commit Graph

9332 Commits

Author SHA1 Message Date
Michael Gottesman
9f5d430e93 [sil-aa] Add predicates to SILType that will be used in TBAA so that TBAA does not need to touch the AST.
We should never touch the AST from SIL if we don't need to. The specific
added methods are:

1. SILType::isBuiltinNativeObjectType().
2. SILType::isBuiltinRawPointerType().
3. SILType::isBuiltinType().
4. SILType::isBuiltinIntegerType().
5. SILType::isBuiltinFloatType().
6. SILType::isGenericType(). This supersedes the hasUnboundGenerics
function in Local.h. After WWDC, we should remove that method but I
don't want to update random parts of the SILPasses.
7. SILType::aggregateContainsRecord. Enables easy queries to determine
if a SILType T1 contains a separate SILType T2 as a member.

rdar://16651852

Swift SVN r18147
2014-05-16 00:21:41 +00:00
Joe Groff
793fe06a34 SILGen: Emit an artificial toplevel for files with @UIApplicationMain classes.
If a source file contains the main class for its module, then implicitly emit a top_level_code that invokes UIApplicationMain with the name of the marked class.

Swift SVN r18088
2014-05-15 00:53:38 +00:00
John McCall
a3fb501114 Recognize the objc_bridge and objc_bridge_mutable
attributes and create implicit conversions for them.

Also, when generating Clang modules for import, set the
appropriate macro to make CoreFoundation actually apply
those attributes to its typedefs.

Also, give shared linkage to imported class method
definitions.

The net effect is that CFString now automatically
converts to NSString and vice-versa.

Depends on Clang r208756 in order to preserve attributes
applied to tag types in multiple typedef declarations.

Swift SVN r18069
2014-05-14 08:19:39 +00:00
Michael Gottesman
2c373a8cf4 [sil-module] Change DEBUG_TYPE to "sil-module" from "libsil"
I don't know why someone chose that name, but it makes no sense.

Swift SVN r17978
2014-05-12 23:59:50 +00:00
Michael Gottesman
e5191519e6 [linker] Link in closures references via function_refs during mandatory inlining.
Pattern matching on whether or not the function was applied is fragile
in the face of various possible compositions in between the function_ref
and the apply (i.e. thin_to_thick_function as an example).

Lets be conservative and just always deserialize references to closures
during mandatory inlining when referenced via a function_ref so we avoid
this problem if additional instructions like this are added.

Swift SVN r17977
2014-05-12 23:59:48 +00:00
Joe Pamer
1e5b9116d4 More array casting work:
- Continue adding support for checked downcasts of array types (rdar://problem/16535104)
- Fix non-bridged array conversions post-r17868
- Fix rdar://problem/16773693
- Add tests for NSArray coercions to and from Array<T>

Swift SVN r17957
2014-05-12 20:49:42 +00:00
Chris Lattner
2c28ec9925 teach silbuilder that stores of weak and unowned pointers don't affect
strong refcounts, allowing more retains and releases to be avoided when
manipulating unowned and weak properties.


Swift SVN r17933
2014-05-12 15:41:55 +00:00
Doug Gregor
5159201aed Bridge T[] to NSArray when T is bridged to Objective-C.
Generalize the bridging of AnyObject[] to NSArray into bridging T[] to
NSArray, following what we already have for dictionaries. Use some new
Foundation-level entry points for the bridging, which could use an
audit from someone who understands Array better than I do.

Part of <rdar://problem/16535097>.


Swift SVN r17869
2014-05-11 06:12:34 +00:00
Chris Lattner
5db1372c64 rename couldReduceRefcount -> couldReduceStrongRefcount and teach it about some
new instructions that don't mutate the strong refcount.  This allows us to
avoid retains and releases around weak and unowned pointer manipulations, for
example for this:

func unowned_local() -> C {
  let c = C()
  unowned let uc = c
  return uc
}

before:

sil @_TF1w13unowned_localFT_CS_1C : $@thin () -> @owned C {
bb0:
  // function_ref w.C.__allocating_init (w.C.Type)() -> w.C
  %0 = function_ref @_TFC1w1CCfMS0_FT_S0_ : $@thin (@thick C.Type) -> @owned C // user: %2
  %1 = metatype $@thick C.Type                    // user: %2
  %2 = apply %0(%1) : $@thin (@thick C.Type) -> @owned C // users: %3, %5, %6, %9, %14
  debug_value %2 : $C  // let c                   // id: %3
  %4 = alloc_box $@sil_unowned C  // let uc       // users: %8, %10, %13
  strong_retain %2 : $C                           // id: %5
  %6 = ref_to_unowned %2 : $C to $@sil_unowned C  // users: %7, %8
  unowned_retain %6 : $@sil_unowned C             // id: %7
  store %6 to %4#1 : $*@sil_unowned C             // id: %8
  strong_release %2 : $C                          // id: %9
  %10 = load %4#1 : $*@sil_unowned C              // users: %11, %12
  strong_retain_unowned %10 : $@sil_unowned C     // id: %11
  %12 = unowned_to_ref %10 : $@sil_unowned C to $C // user: %15
  strong_release %4#0 : $Builtin.NativeObject     // id: %13
  strong_release %2 : $C                          // id: %14
  return %12 : $C                                 // id: %15
}

after:

sil @_TF1w13unowned_localFT_CS_1C : $@thin () -> @owned C {
bb0:
  // function_ref w.C.__allocating_init (w.C.Type)() -> w.C
  %0 = function_ref @_TFC1w1CCfMS0_FT_S0_ : $@thin (@thick C.Type) -> @owned C // user: %2
  %1 = metatype $@thick C.Type                    // user: %2
  %2 = apply %0(%1) : $@thin (@thick C.Type) -> @owned C // users: %3, %5, %12
  debug_value %2 : $C  // let c                   // id: %3
  %4 = alloc_box $@sil_unowned C  // let uc       // users: %7, %8, %11
  %5 = ref_to_unowned %2 : $C to $@sil_unowned C  // users: %6, %7
  unowned_retain %5 : $@sil_unowned C             // id: %6
  store %5 to %4#1 : $*@sil_unowned C             // id: %7
  %8 = load %4#1 : $*@sil_unowned C               // users: %9, %10
  strong_retain_unowned %8 : $@sil_unowned C      // id: %9
  %10 = unowned_to_ref %8 : $@sil_unowned C to $C // user: %13
  strong_release %4#0 : $Builtin.NativeObject     // id: %11
  strong_release %2 : $C                          // id: %12
  return %10 : $C                                 // id: %13
}



Swift SVN r17844
2014-05-10 21:08:33 +00:00
Joe Groff
3dda4c9cf5 SILGen: Reference ObjC initializers and factories through foreign-to-native thunks.
Eliminate the duplicate half-broken bridging logic in emitClassConstructorAllocator by referencing foreign initializers through their foreign-to-native thunks, which SILGen knows how to emit already. Do the same thing for factory initializers by suppressing their normal allocating initializer codegen and just referencing the usual foreign-to-native thunk for them. This fixes <rdar://problem/16853205> because we get the ownership thunking right now.

Swift SVN r17840
2014-05-10 20:18:53 +00:00
Doug Gregor
104cf17d5d SILGen: Bridge Dictionary<K, V> <-> NSDictionary in Objective-C thunks.
Implements the majority of Dictionary bridging in SILGen
<rdar://problem/16870626>.

Swift SVN r17838
2014-05-10 19:51:00 +00:00
Joe Groff
d6957d0328 Make existential-to-concrete metatype casts work.
This mostly falls out from the metatype cast infrastructure, but we need to generalize some Sema and SILGen code to accept AnyMetatypeType. Concrete-to-existential metatypes will need more runtime checking that isn't implemented, so raise a 'not implemented' error on those for now.

Swift SVN r17798
2014-05-09 20:57:55 +00:00
Joe Groff
1dce36edd2 Make 'T.self is U.Type' work.
Fix up all of type-checking, SILGen, IRGen, and the runtime to support checked casts of metatypes. <rdar://problem/16847453>

Swift SVN r17719
2014-05-08 22:55:14 +00:00
Michael Gottesman
fd0a78b301 [sil-combine] Canonicalize index_raw_addr byte indexing operations => index_addr object size indexing operations.
This should eliminate pointless operations that get added to our inline
cost itinerary.

rdar://15567647
rdar://16762768
rdar://16832529

Swift SVN r17644
2014-05-07 22:01:30 +00:00
John McCall
a83e5740de Import CF types as managed pointers when they are:
- the type of a const global variable
  - the type of a parameter, always
  - the return type of a function that has been audited
    or has an explicit retained/not-retained attribute
  - the return type of an ObjC method that has an explicit
    retain/not-retained/inner-pointer attribute

Additionally, choose the correct conventions for all
these cases during SIL type lowering.

All this importing logic is still only enabled under
-Xfrontend -import-cf-types.

Swift SVN r17543
2014-05-06 08:29:44 +00:00
Michael Gottesman
b56c31dc5f [sil-combine] convert_function simplification.
convert_function works essentially as a bitcast on functions that
enables you to change address/ref type parameters. This commit enforces
that invariant in the verifier and teaches SILCombine how to simplify it
so we can use it in the devirtualizer.

Swift SVN r17516
2014-05-06 01:16:14 +00:00
Joe Groff
bf858cc8a1 SILGen: Handle inner pointer returns through currying and dynamic lookup.
When we partially apply an inner-pointer method or property, the thunk or partial_apply that applies "self" needs to be the one that handles lifetime-extending "self". Verify that a partial_apply-ed inner pointer method is not inner pointer and implement lifetime extension in the partial apply thunk. Fixes <rdar://problem/16803701>.

Swift SVN r17321
2014-05-03 18:37:42 +00:00
Michael Gottesman
4ca16e6e05 [deserialization] Put in assert in SILWitnessTable::create to make sure we don't ever get passed in a null conformance.
Swift SVN r17284
2014-05-02 22:35:34 +00:00
Michael Gottesman
a7bb61265c [deserialization] Tighten up deserialization of witness tables.
We now enforce via an assert that each witness table is unique and that
every protocol conformance that is referenceable from SIL must have a
witness table matched to it.

Also, I taught the linker that it should deserialize witness tables for
InitExistentialRefInst instructions, something that was missed before.

Swift SVN r17283
2014-05-02 22:28:13 +00:00
Michael Gottesman
0f8733867b [deserialization] Change the return value of findFuncInWitnessTable to match the
ordering of SILWitnessTable, ArrayRef<Substitution> in lookUpWitnessTable.

Swift SVN r17266
2014-05-02 19:15:41 +00:00
Ted Kremenek
050fd53af7 Rename UncheckedOptional to ImplicitlyUnwrappedOptional.
Swift SVN r17232
2014-05-02 06:13:57 +00:00
Michael Gottesman
5dbfb3ab7f [devirtualization] Return the witness table when looking up SILFunctions from a witness table.
This is important since it enables one to analyze the type of the
conformance that the witness table implements which may be different
than the original type. This follows the precedent where we return the
Substitutions from the protocol conformance tree traversal.

Swift SVN r17220
2014-05-02 01:43:31 +00:00
Michael Gottesman
4ff2e7b7f1 Re-enable the verifier always. XFAIL remaining problem tests so we get verifier converage.
Swift SVN r17213
2014-05-02 00:53:58 +00:00
Michael Gottesman
e1385e7c9c Re-enable the verifier when processing SILGen and the gauranteed diagnostic passes.
Swift SVN r17212
2014-05-02 00:39:08 +00:00
Joe Groff
a3a54545e5 SIL: Fix up ownership and bridging when emitting dynamic method calls.
There were a bunch of things broken here--it's amazing this ever appeared to work.

- Retain 'self' before partial_applying it to the method, so we don't overrelease it.
- Correctly lower the ownership conventions of the dynamic method against the SILDeclRef, so we don't overrelease arguments or over-over-release self, and we handle ObjC methods with weird conventions correctly.
- Thunk when there are bridging type differences between the partially-applied ObjC method and a Swift method, so we don't crash if the method takes NSStrings or other bridged types.

Add verifier checks that the result of 'dynamic_method' and BB arg of 'dynamic_method_br' actually match the method they're dispatching.

Swift SVN r17198
2014-05-01 22:46:11 +00:00
Doug Gregor
3d3ff6811a Add a pile of missing #includes exposed by pruning includes in top-of-tree LLVM.
Swift SVN r17157
2014-05-01 14:26:34 +00:00
Michael Gottesman
c9baa6d739 [devirtualization] Extract the method SILModule::lookUpSILFunctionFromVTable from optimizeClassMethodInst. NFC.
Swift SVN r17133
2014-05-01 02:08:57 +00:00
Michael Gottesman
5449d6f673 [devirtualizer] Refactor findFuncInWitnessTable -> SILModule::findFuncInWitnessTable.
Swift SVN r17130
2014-05-01 01:06:06 +00:00
Joe Groff
51bde1485d Remove stray assertion.
The verifier will eventually check this (again).

Swift SVN r17128
2014-05-01 00:08:41 +00:00
Joe Groff
aeb607ad5b Fix latent SIL verification errors.
r15824 overrode SILVerifier::visitSILBasicBlock without calling up to the super definition, causing us to never actually verify any instructions for the past three weeks. Awesome. Patch up the latent bugs that have crept in, except for three devirtualizer tests that fail. This doesn't reenable the verifier because I don't want to cause crashes until all the regressions have been cleared up.

Swift SVN r17121
2014-04-30 22:33:46 +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
Ted Kremenek
6caf910d32 Implement new syntactic sugar for UncheckedOptional<T>.
This leaves in the existing syntax for @unchecked T?.  That will
be addressed in later patches.

There's still a mysterious case where some of the SIL output
includes UncheckedOptional<T> and some places T!.

Moreover, this doesn't handle SourceKit's behavior for printing
for overrides.  This just handles parsing the 'T!' syntax.

Swift SVN r16945
2014-04-27 21:59:29 +00:00
Michael Gottesman
2d026d7bd1 [devirtualization] Expose TypeBase::gatherAllSubstitutions as
SILType::gatherAllSubstitutions wrapped to be SIL level friendly.

Swift SVN r16908
2014-04-27 02:47:46 +00:00
Michael Gottesman
e2cde3c7f4 [devirtualization] Properly substitute in types for upcast when handling
a specialized inherited conformance.

Swift SVN r16900
2014-04-27 00:06:46 +00:00
Michael Gottesman
888f72cbbf [specializer] Teach the specializer how to specialize partial apply insts.
<rdar://problem/16696421>

Swift SVN r16871
2014-04-26 04:39:39 +00:00
Michael Gottesman
46d5660768 [devirtualization] Add SILType::substInterfaceGenericArgs a SIL level
wrapper for SILFunctionType::substInterfaceGenericArgs.

By exposing this we reduce the need for SIL level passes to touch the
AST when they need to specialize with just a subst list.

Swift SVN r16765
2014-04-24 19:41:18 +00:00
Joe Groff
7d274e28a7 SIL: Add an unchecked_addr_cast instruction.
Cast an address to another address type without the silly address_to_pointer/pointer_to_address round trip.

Swift SVN r16743
2014-04-24 04:14:48 +00:00
Michael Gottesman
14e0f3fef2 [deserialization] When deserializing init_existential_inst pull in all the relevant witness tables.
<rdar://problem/16646818>

Swift SVN r16742
2014-04-24 03:37:34 +00:00
Michael Gottesman
afa464f980 [deserialization] Do not allow SILModule to attempt to deserialize a witness table lazily while we are already deserializing.
When deserializing, the serialized sil loader attempts to first lookup
from its SILModule the witness table to attempt to grab a witness table
declaration. Before this patch if the SILModule could not find it, it
would attempt to deserialize it. In certain edge cases this would cause
us to attempt to deserialize a function definition which violates the
invariant that that should never happen.

This patch adds an argument to SILModule::lookUpWitnessTable that
enables you to turn off the lazy deserialization behavior. The default
argument gives the current behavior which should be used everywhere
except for the deserializer.

Swift SVN r16740
2014-04-24 03:37:33 +00:00
Joe Groff
68f942117a SIL: Squash 'ref_to_native_object' and 'native_object_to_ref' into one 'unchecked_ref_cast' insn.
An unsafe cast from a base to a derived class isn't really all that different from one from Builtin.NativeObject to an arbitrary class, so relax this pair of instructions to allow an arbitrary bitcast. This only combines the instructions; it doesn't attempt to simplify any codegen that was emitting round-trip casts before yet.

Swift SVN r16736
2014-04-24 03:10:53 +00:00
Joe Groff
285ec1e2dd SIL: Add an unchecked_enum_data instruction.
This allows the payload for a loadable enum to be unsafely projected without branching, enabling more enum optimizations when switch branches can be culled or when indirect enum code can be promoted.

Swift SVN r16729
2014-04-24 00:41:08 +00:00
Joe Groff
55f6b925de SIL: Rename 'take_enum_data_addr' to 'unchecked_take_enum_data_addr'.
In preparation for adding an 'unchecked_enum_data' equivalent for loadable enums.

Swift SVN r16728
2014-04-24 00:40:47 +00:00
John McCall
b38a63950d Implement @unowned(unsafe).
This was part of the original weak design that
there was never any particular reason to rush the
implementation for.  It's convenient to do this now
so that we can use it to implement Unmanaged<T> for
importing CF types.

Swift SVN r16693
2014-04-23 08:51:23 +00:00
Andrew Trick
f58ebbc251 Add a global_init attribute to SILFunction.
The implied semantics are:
- side-effects can occur any time before the first invocation.
- all calls to the same global_init function have the same side-effects.
- any operation that may observe the initializer's side-effects must be
  preceded by a call to the initializer.

This is currently true if the function is an addressor that was lazily
generated from a global variable access. Note that the initialization
function itself does not need this attribute. It is private and only
called within the addressor.

Swift SVN r16683
2014-04-23 01:09:47 +00:00
Chris Lattner
a3916614ef respond to mainline API changes in the Debug.h header, by defining the
DEBUG_TYPE macro in all .cpp files that use the DEBUG macro.  Hopefully
this will unbreak the build.


Swift SVN r16638
2014-04-22 02:54:55 +00:00
Joe Groff
d90f9d46ae SILGen: Lifetime-extend the "self" parameters of inner-pointer methods.
Do this the lazy way, just autoreleasing "self" after the call. A future optimization pass may be able to eliminate this autorelease when it recognizes the lifetime of the derived value, but that's not immediately necessary.

Swift SVN r16635
2014-04-22 00:25:17 +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
dcad7fb248 SIL: Add an autorelease_value instruction.
Give us a way to formally represent autoreleases in SIL separate from autoreleased returns, allowing us to lifetime-extend inner pointer parameters the lazy way and hopefully clean up some asmname hacks in the stdlib implementation too.

Swift SVN r16632
2014-04-21 23:11:38 +00:00
Mark Lacey
924e30ea61 Rewrite BB args whose only use is in struct/tuple extract.
If we have BB args that are only used in a struct/tuple extract, and
that are generated in each predecessor with a struct/tuple instruction,
retype the BB arg and replace the argument with what would have been the
extracted value.

This provides more opportunties for jump threading to kick in.

Swift SVN r16509
2014-04-18 07:19:33 +00:00
Joe Groff
eaa6088102 SIL: Remove the now-obsolete bridge_to_block instruction.
Swift SVN r16487
2014-04-18 02:26:10 +00:00