Commit Graph

66 Commits

Author SHA1 Message Date
Adrian Prantl
8e1d6c013b Make SILDebugLocation a value member of SILInstruction again.
The overhead of uniquing the locations in a Densemap isn't worth any of
the potential memory savings: While this adds an extra pointer and
unsigned to each SILInstruction, any extra memory is completely lost in
the noise (measured on a release -emit-ir build of the x86_64 stdlib).
This is not too surpising as the ratio between SILInstructions and unique
SILLocations is not very high and the DenseMap also needs space.

<rdar://problem/22706994>
2016-02-19 13:41:54 -08:00
John McCall
e249fd680e Destructure result types in SIL function types.
Similarly to how we've always handled parameter types, we
now recursively expand tuples in result types and separately
determine a result convention for each result.

The most important code-generation change here is that
indirect results are now returned separately from each
other and from any direct results.  It is generally far
better, when receiving an indirect result, to receive it
as an independent result; the caller is much more likely
to be able to directly receive the result in the address
they want to initialize, rather than having to receive it
in temporary memory and then copy parts of it into the
target.

The most important conceptual change here that clients and
producers of SIL must be aware of is the new distinction
between a SILFunctionType's *parameters* and its *argument
list*.  The former is just the formal parameters, derived
purely from the parameter types of the original function;
indirect results are no longer in this list.  The latter
includes the indirect result arguments; as always, all
the indirect results strictly precede the parameters.
Apply instructions and entry block arguments follow the
argument list, not the parameter list.

A relatively minor change is that there can now be multiple
direct results, each with its own result convention.
This is a minor change because I've chosen to leave
return instructions as taking a single operand and
apply instructions as producing a single result; when
the type describes multiple results, they are implicitly
bound up in a tuple.  It might make sense to split these
up and allow e.g. return instructions to take a list
of operands; however, it's not clear what to do on the
caller side, and this would be a major change that can
be separated out from this already over-large patch.

Unsurprisingly, the most invasive changes here are in
SILGen; this requires substantial reworking of both call
emission and reabstraction.  It also proved important
to switch several SILGen operations over to work with
RValue instead of ManagedValue, since otherwise they
would be forced to spuriously "implode" buffers.
2016-02-18 01:26:28 -08:00
Erik Eckstein
506ab9809f SIL: remove getTyp() from SILValue 2016-01-25 15:00:49 -08:00
practicalswift
50baf2e53b Use consistent formatting in top of file headers. 2016-01-04 02:17:48 +01:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Adrian Prantl
7e97baa463 Assert that no SILInstruction is created without a SILDebugScope. 2015-11-19 10:54:42 -08:00
Adrian Prantl
d756a6953b Remove redundant assertions used while debugging. 2015-11-19 10:54:42 -08:00
Adrian Prantl
8ab1e2dd50 Unify debug scope and location handling in SILInstruction and SILBuilder.
The drivers for this change are providing a simpler API to SIL pass
authors, having a more efficient of the in-memory representation,
and ruling out an entire class of common bugs that usually result
in hard-to-debug backend crashes.

Summary
-------

SILInstruction

Old                   New
+---------------+     +------------------+    +-----------------+
|SILInstruction |     |SILInstruction    |    |SILDebugLocation |
+---------------+     +------------------+    +-----------------+
| ...           |     | ...              |    | ...             |
|SILLocation    |     |SILDebugLocation *| -> |SILLocation      |
|SILDebugScope *|     +------------------+    |SILDebugScope *  |
+---------------+                             +-----------------+

We’re introducing a new class SILDebugLocation which represents the
combination of a SILLocation and a SILDebugScope.
Instead of storing an inline SILLocation and a SILDebugScope pointer,
SILInstruction now only has one SILDebugLocation pointer. The APIs of
SILBuilder and SILDebugLocation guarantees that every SILInstruction
has a nonempty SILDebugScope.

Developer-visible changes include:

SILBuilder
----------

In the old design SILBuilder populated the InsertedInstrs list to
allow setting the debug scopes of all built instructions in bulk
at the very end (as the responsibility of the user). In the new design,
SILBuilder now carries a "current debug scope" state and immediately
sets the debug scope when an instruction is inserted.
This fixes a use-after-free issue with with SIL passes that delete
instructions before destroying the SILBuilder that created them.

Because of this, SILBuilderWithScopes no longer needs to be a template,
which simplifies its call sites.

SILInstruction
--------------

It is neither possible or necessary to manually call setDebugScope()
on a SILInstruction any more. The function still exists as a private
method, but is only used when splicing instructions from one function
to another.

Efficiency
----------

In addition to dropping 20 bytes from each SILInstruction,
SILDebugLocations are now allocated in the SILModule's bump pointer
allocator and are uniqued by SILBuilder. Unfortunately repeat compiles
of the standard library already vary by about 5% so I couldn’t yet
produce reliable numbers for how much this saves overall.

rdar://problem/22017421
2015-11-19 09:31:26 -08:00
Andrew Trick
57a450b28f Generate unchecked_ref_cast, not unchecked_ref_bit_cast.
This improves support for promoting to and generating
unchecked_ref_cast so we no longer need unchecked_ref_bit_cast, which
will just go away in the next commit.

Swift SVN r32597
2015-10-10 05:42:53 +00:00
Andrew Trick
778012c479 Handle SIL unchecked_addr_cast conservatively when promoting to a value cast.
Unless we can prove that the input and output are layout identical we must use either trivial_bit_cast or bitwise_cast.

This changes the lowering to be more conservative for:

- Builtin.reinterpretCast
- address_to_pointer -> pointer_to_address

This also makes LoadStoreOpts more conservative in:

- forwardAddrToUncheckedCastToLd

In order to commit this without massive performance regressions, I added SILType::canBitCastAsSingleRef. When this is true for both sides
of a bitwise case, we can convert it to a RefBitCast, which has RC identity.

Swift SVN r30172
2015-07-13 22:46:17 +00:00
Michael Gottesman
96634b4be6 Add a SILBuilder::emitDestroyAddr API that returns a pointer union and use it to implement SILBuilder::emitDestroyAddrAndFold.
Now on SILBuilder, destroy_addr has an API that matches the APIs for
strong_release and release_value.

Done at Chris's request.

Swift SVN r28170
2015-05-05 17:23:11 +00:00
Michael Gottesman
9006fca42b Rename SILBuilder::emitDestroyAddr => SILBuilder::emitDestroyAddrAndFold.
Now it matches SILBuilder::emit{StrongRelease,ReleaseValue}AndFold which perform
the same operation but on object types.

Swift SVN r27806
2015-04-27 17:29:50 +00:00
Michael Gottesman
a3aa89d4c4 Remove Callback from SILBuilder and instead rename
emit{StrongRelease,ReleaseValue} => emit{StrongRelease,ReleaseValue}AndFold.
Then introduce a new method emit{StrongRelease,ReleaseValue} that returns a
PointerUnion containing the increment to be deleted if it exists. This obviates
the need for the callback.

Swift SVN r27804
2015-04-27 07:29:13 +00:00
Michael Gottesman
583d7aa30a Add a delete callback to SILBuilder so that if we delete retains and releases in emitStrongRelease, emitReleaseValue, passes (like SILCombine) can update their state. Also teach the closure deletion code how to detect such a case and not send a notification message if no new instruction is created.
Swift SVN r27803
2015-04-27 05:37:09 +00:00
Joe Groff
ad0d20c07a Fold "AbstractCC" into SILFunctionType::Representation.
These aren't really orthogonal concerns--you'll never have a @thick @cc(objc_method), or an @objc_block @cc(witness_method)--and we have gross decision trees all over the codebase that try to hopscotch between the subset of combinations that make sense. Stop the madness by eliminating AbstractCC and folding its states into SILFunctionTypeRepresentation. This cleans up a ton of code across the compiler.

I couldn't quite eliminate AbstractCC's information from AST function types, since SIL type lowering transiently created AnyFunctionTypes with AbstractCCs set, even though these never occur at the source level. To accommodate type lowering, allow AnyFunctionType::ExtInfo to carry a SILFunctionTypeRepresentation, and arrange for the overlapping representations to share raw values.

In order to avoid disturbing test output, AST and SILFunctionTypes are still printed and parsed using the existing @thin/@thick/@objc_block and @cc() attributes, which is kind of gross, but lets me stage in the real source-breaking change separately.

Swift SVN r27095
2015-04-07 21:59:39 +00:00
Joe Groff
56de5fb95e SIL: Drop unnecessary bits from SILFunctionType::ExtInfo.
"Autoclosure" is uninteresting to SIL. "noescape" isn't currently used by SIL and we shouldn't have it until it has a meaningful effect on SIL. "throws" should be adequately represented by a SIL function type having an error result.

Swift SVN r27023
2015-04-05 17:35:28 +00:00
Joe Pamer
eee40fc53f Add basic parsing, sema and mangling support for throwing function types. Next up, metadata and serialization support, as well as more tests.
Swift SVN r26767
2015-03-31 18:55:19 +00:00
John McCall
35b7db3ae1 Parsing support for error results from SILFunctionType.
Swift SVN r26566
2015-03-26 00:01:32 +00:00
John McCall
ee4aa14703 Stop reordering blocks in SILBuilder::emitBlock.
This change permits SILGen to make smarter decisions about
block placement by keeping related blocks together instead
of always inserting to the end to the function.  The
flipside is that SILGen needs to be somewhat careful to
create blocks in the right order.  Counter-intuitively,
that order is the reverse of the order in which the blocks
should be laid out, since blocks created later will be
inserted before blocks created earlier.  Note, however,
that this produces the right results for recursive
emission.

To that end, adjust a couple of places in SILGen to
create blocks in properly nested order.

All of the block-order differences in the tests seem
to be desirable; several of them even had confused
comments wondering how on earth a block got injected
where it did.

Also, fix the implementation of SILBuilder::moveBlockTo,
and fix a latent bug in epilogue emission where epilogBB
was erased from its parent (deleting it) and then
queried multiple times (!).

Swift SVN r26428
2015-03-23 06:38:20 +00:00
Chris Lattner
7faecf021e minor cleanups along the way, NFC.
Swift SVN r24347
2015-01-10 01:58:25 +00:00
Chris Lattner
21a7a6c89a Doug twisted my arm and convinced me that noescape was a better match for the semantics we’ll have here. NoCapture would be confusable with "this closure doesn’t have any captures, thus should be compatible with thin function types"
The attribute itself remains __'ized.



Swift SVN r24113
2014-12-23 17:52:10 +00:00
Chris Lattner
e349ee3a60 Start the implementation of a "nocapture" attribute, which is only valid on paramdecls.
This is part of rdar://16323038.  Because this hasn't been fully design reviewed and
implemented, I'm naming it as __nocapture for now.  It is blocking finishing off the
"improved let model" work.




Swift SVN r24079
2014-12-22 21:34:30 +00:00
Adrian Prantl
25d1d5e65a Move the check for required debug scopes into the SIL verifier.
Swift SVN r23041
2014-10-31 22:32:20 +00:00
Adrian Prantl
c41b30299f Audit all SILPasses to ensure that new instructions are never created
without a valid SILDebugScope. An assertion in IRGenSIL prevents future
optimizations from regressing in this regard.
Introducing SILBuilderWithScope and SILBuilderwithPostprocess to ease the
transition.

This patch is large, but mostly mechanical.
<rdar://problem/18494573> Swift: Debugger is not stopping at the set breakpoint

Swift SVN r22978
2014-10-28 01:49:11 +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
John McCall
cd6fc21900 Allow SILBasicBlocks to be constructed after a specific
block, and do so when splitting a block.

Swift SVN r19076
2014-06-22 05:01:06 +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
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
8adaab0233 Fold ExtInfo::isThin and ::isBlock into a "Representation" enum.
These bits are orthogonal to each other, so combine them into one, and diagnose attempts to produce a type that's both. Spot-fix a bunch of places this revealed by inspection that we would have crashed in SILGen or IRGen if blocks were be handled.

Swift SVN r16088
2014-04-09 00:37:26 +00:00
Chris Lattner
f6e87dec54 Rework SILGen's emission of release_value to have it auto-merge into
retain_value instructions, just like we do for strong_retain/release.

This wraps up rdar://15889208, which is admittedly more of a moral 
victory than a practical one.


Swift SVN r15804
2014-04-02 16:39:53 +00:00
Chris Lattner
6540423613 rename CopyValueInst -> RetainValueInst. The .sil syntax
isn't changed yet.


Swift SVN r15775
2014-04-02 05:11:31 +00:00
Doug Gregor
50a7d66112 SILBuilder: peephole 'metatype' instruction used only by a thick/ObjC conversion instruction.
The 'metatype' instruction can produce either a thick or Objective-C
metatype value for a class. Sometimes SILGen picks the "wrong" one,
and ends up converting the value directly, which is sub-optimal. Clean
this up in SILBuilder.

I didn't add a corresponding SILCombine optimization, nor did I add
this for class_metatype, because it seems unlikely that those will
occur in practice.


Swift SVN r14183
2014-02-20 23:39:44 +00:00
Joe Groff
b19cfb27ea Drop the context generic params from SILFunctionType.
SILFunctionType is now 100% context free!

Swift SVN r13775
2014-02-11 04:21:20 +00:00
Joe Groff
481fbb7b91 Drop the non-interface types from SILFunctionType.
There are some straggling references to the context generic param list, but nothing uses the non-interface param or result types anymore!

Swift SVN r13725
2014-02-09 22:39:01 +00:00
Joe Groff
0776c4b6b8 SIL: Reorient function type lowering toward interface types.
Lower types for SILDeclRefs from the interface types of their referents, dragging the old type along for the ride so we can still offer the context to clients that haven't been weaned off of it. Make SILFunctionType's interface types and generic signature independent arguments of its  Derive the context types of SILFunctionType from the interface types, instead of the other way around. Do a bunch of annoying inseparable work in the AST and IRGen to accommodate the switchover.

Swift SVN r12536
2014-01-18 19:42:02 +00:00
Joe Groff
b2f0b90ba2 SIL: Switch to SILFunctionType interface types in easy-to-reach places.
In nongeneric contexts, or contexts where we only care about the indirectness of parameters or have already substituted the generic parameters for a function, the interface types are interchangeable, so just switch over.

Swift SVN r12044
2014-01-08 04:48:29 +00:00
Chris Lattner
b689013ad3 fix comment header.
Swift SVN r11931
2014-01-06 18:00:30 +00:00
Chris Lattner
a30e8730f4 teach SILBuilder::emitStrongRelease that copyaddr and assign instructions
cannot decrement a retain count when they are initializations or of trivial
types.  This allows us to zap some retain/releases being emitted by silgen,
particularly when accessing self ivars.



Swift SVN r11019
2013-12-09 15:20:57 +00:00
Chris Lattner
510ed4ccfa Introduce a new use classifier: "Initialization". This allows us to
tag things known from SILGen to be initializers (e.g., copyaddrs with
the bit set) and when we analyze the DI properties for an assign, we
can move it to this classification.  This allows stuff that wants to
reason about the difference (e.g. the conditional destroy logic) to
do so precisely.


Swift SVN r10658
2013-11-22 06:17:32 +00:00
Chris Lattner
0e6bec9fb0 Enhance emitDestroyAddr to return the instruction it produces, if it does.
Swift SVN r10628
2013-11-21 07:18:31 +00:00
John McCall
20e58dcf93 Change the type of function values in SIL to SILFunctionType.
Perform major abstraction remappings in SILGen.  Introduce
thunking functions as necessary to map between abstraction
patterns.

Swift SVN r10562
2013-11-19 22:55:09 +00:00
Chris Lattner
4968cc208b A couple of related changes:
- Enhance SILBuilder::emitStrongRelease to be smarter.
- Start using emitStrongRelease in type lowering, SILGen,
  CapturePromotion (replacing its implementation of the
  same logic), and MandatoryInlining (one more place)
- Rename the primitive createStrongRetain/ReleaseInst
  instructions to lose their suffix.
- Now that createStrongRetain/ReleaseInst are not special
  cases from the naming perspective, remove some special cases
  from DeserializeSIL and ParseSIL.
  


Swift SVN r10449
2013-11-14 02:21:27 +00:00
Chris Lattner
b2ac1db9f6 introduce a new helper to SILBuilder that emits a StrongRelease operation,
scanning up the local block to see if it immediately cancels a retain 
operation.

Use this in mandatory inlining to zap more retains and release.  Before
this patch, the result LogicValue allocation blocked this optimization,
preventing the partial_apply from being deleted from the case in 
rdar://15328833.




Swift SVN r10447
2013-11-14 01:50:28 +00:00
John McCall
2ed33e4ffa Make convenient accessors for getting a lowered SILType for
a struct/class field or a tuple element.

Make DefiniteInitialization traffic in SILTypes more.

Swift SVN r10055
2013-11-08 22:04:04 +00:00
Joe Groff
61360ba7a4 SIL: Remove ConvertCCInst.
Swift SVN r9577
2013-10-22 03:16:27 +00:00
Chris Lattner
a6cde9cd8d Add a convenience function to map a CanType+StructDecl to the mapped
CanType.  This doesn't really fit on SILBuilder, but until StructType 
and BoundGenericStructType are merged, this will do.


Swift SVN r9336
2013-10-14 23:42:41 +00:00
Joe Groff
8f68e1b68b SIL: Add create(Struct,Tuple)ElementAddr helpers that derive the element type.
To be consistent with create(Struct,Tuple)Extract.

Swift SVN r9138
2013-10-10 15:34:17 +00:00
Joe Groff
5650130518 SIL: Substitute struct field types when creating struct ops in passes.
The AST type of the VarDecl is incorrect for bound generic structs and needs to be substituted.

Swift SVN r9130
2013-10-10 04:35:57 +00:00
John McCall
b880e60100 Remove SILFunctionTypeInfo in favor of SILFunctionType.
We still don't actually use this as a type, however.

Swift SVN r9091
2013-10-09 20:55:55 +00:00
Chris Lattner
c6d7c2941e make the scan for copy_addr slightly stronger when emitting a destroy_addr.
Swift SVN r8969
2013-10-07 18:02:56 +00:00