Commit Graph

258 Commits

Author SHA1 Message Date
Adrian Prantl
f40d28f402 Debug Info: Don't reset the debug scope after leaving the outermost scope,
because the debugger is not expecting the function epilogue to
be in a different scope.

rdar://problem/23621232
2015-11-19 17:41:51 -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
Roman Levenstein
f8029c7eb5 A temporary workaround for a use-after-free issue related to debug scopes.
This fix is just to unblock the subsequent commit. Adrian is working on a proper solution for this issue.

rdar://22017421 and rdar://23306385
2015-11-17 17:20:45 -08:00
Michael Gottesman
9fb54bf4bf Fix for upstream ilist changes. 2015-11-11 16:07:41 -08:00
Slava Pestov
1b55becf76 SILGen: Fix issues with one-element tuples in thunks
Take apart exploded one-element tuples and be more careful with
passing around tuple abstraction patterns.

Also, now we can  remove the inputSubstType parameter from
emitOrigToSubstValue() and emitSubstToOrigValue(), making the
signatures of these functions nice and simple once again.

Fixes <rdar://problem/19506347> and <rdar://problem/22502450>.
2015-11-06 13:51:15 -08:00
Slava Pestov
91cf4c11da SIL: Move description of abstraction patterns to AbstractionPattern.h and write new comment in SILGenPoly describing function thunks, NFC
Probably SILGenPoly.cpp should be named SILGenThunk.cpp, but I'm saving
that for if I ever extract the duplication between bridging thunks and
re-abstraction thunks.
2015-11-05 21:49:23 -08:00
Slava Pestov
4bedf4562b Use the 'self consumed' analysis to eliminate releases of consumed self
Previously, SILGen would store a null pointer into the self box upon
encountering a constructor delegation that consumes self. This was a
constant source of bugs. Now, use the new analysis to make this use
DI information instead, emitting an extra bit at runtime if necessary.

Also re-organize the DI tests for initializers, and add CHECK: lines
instead of just asserting we don't crash or diagnose.

Swift SVN r32604
2015-10-11 02:11:15 +00:00
Slava Pestov
23d79a46ec SILGen: Fix miscompile if self accessed multiple times while forming delegating init call
When emitting a RebindSelfInConstructorExpr, the first access of
'self' would take the self value out of the box, since the call
consumes it. This caused a miscompile if we have to load instance
variables to form the call, since then we would try to load from
the box again, dereferencing a null pointer. Now, store the taken
self value and borrow it on subsequent access.

This is a regression from r31141.

Fixes <rdar://problem/22939666>.

Swift SVN r32407
2015-10-02 21:26:12 +00:00
Slava Pestov
533f42dd2f Closures and local functions only capture generic parameters if necessary
The CaptureInfo computed by Sema now records if the body of the
function uses any generic parameters from the outer context.
SIL type lowering only adds a generic signature if this is the
case, instead of unconditionally.

This might yield a marginal performance improvement in some cases,
but more interestingly will allow @convention(c) conversions from
generic context.

Swift SVN r32161
2015-09-22 21:08:28 +00:00
Slava Pestov
0af82e6345 SILGen: Move optional and existential conversions to SILGenConvert.cpp, NFC
This is a better place for code shared between expression emission
and thunks.

Swift SVN r31544
2015-08-27 21:53:49 +00:00
Slava Pestov
4a09d72420 SILGen: Refactor existentials a bit more for use in thunks, NFC
Swift SVN r31526
2015-08-27 08:35:25 +00:00
Slava Pestov
e1f0f02480 SILGen: Three-parameter re-abstraction thunks
Right now, re-abstraction thunks are set up to convert values
as follows, where L is type lowering:

- OrigToSubst: L(origType, substType) -> L(substType)
- SubstToOrig: L(substType) -> L(origType, substType)

This assumes there's no AST-level type conversion, because
when we visit a type in contravariant position, we flip the
direction of the transform but we're still converting *to*
substType -- which will now equal to the type of the input,
not the type of the expected result!

This caused several problems:

- VTable thunk generation had a bunch of special logic to
  compute a substOverrideType, and wrap the thunk result
  in an optional, duplicating work done in the transform

- Witness thunk generation similarly had to handle the case
  of upcasting to a base class to call the witness, and
  casting the result of materializeForSet back to the right
  type for properties defined on the base.

  Now the materializeForSet cast sequence is a bit longer,
  we unpack the returned tuple and do a convert_function
  on the function, then pack it again -- before we would
  unchecked_ref_cast the tuple, which is technically
  incorrect since the tuple is not a ref, but IRGen didn't
  seem to care...

To handle the conversions correctly, we add a third AST type
parameter to a transform, named inputType. Now, transforms
perform these conversions:

- OrigToSubst: L(origType, inputType) -> L(substType)
- SubstToOrig: L(inputType) -> L(origType, substType)

When we flip the direction of the transform while visiting
types in contravariant position, we also swap substType with
inputType.

Note that this is similar to how bridging thunks work, for
the same reason -- bridging thunks convert between AST types.

This is mostly just a nice cleanup that fixes some obscure
corner cases for now, but this functionality will be used
in a subsequent patch.

Swift SVN r31486
2015-08-26 09:15:29 +00:00
Slava Pestov
689be69345 SILGen: Split off cleanup of transforms code from bigger patch
- Generalize::transformFunction() had a couple of little optimizations
  for emitting convert_function or thin_to_thick_function instead of
  a thunk, if possible -- move this into code shared by all
  transforms

- Nuke Generalize since it doesn't do anything special anymore

Swift SVN r31423
2015-08-24 00:23:57 +00:00
Slava Pestov
e416bfbc4e SILGen: Refactor code for existentials
We need to be able to introduce and eliminate existentials inside
reabstraction thunks, so make this logic independent of RValue
and Expr emission.

NFC for now.

Swift SVN r31375
2015-08-21 02:26:57 +00:00
Joe Groff
88b83fd725 SILGen: Don't clobber variables from other pattern bindings in a PatternBindingDecl's initialization.
The PBD entry index got dropped on the way from SILGenModule to SILGenFunction. Oops. Fixes rdar://problem/22207407.

Swift SVN r31316
2015-08-18 23:05:28 +00:00
Slava Pestov
9b74a4b0d6 SILGen: Fix memory leak when sub-expression of ErasureExpr throws
If we didn't initialize the existential, we have to emit a cleanup
because we may have allocated a buffer on the heap to store the value.

Factor out the TakeExistentialCleanup that appears in a few places,
rename it to DeinitExistentialCleanup and add support for deallocating
boxed existentials.

Then, use a special Initialization subclass to keep track of the
state of the memory in emit{AddressOnly,Boxed}Erasure().

Swift SVN r31259
2015-08-16 16:45:44 +00:00
John McCall
807f7f5434 Reimplement materializeForSet emission in SILGen instead of
the type-checker.  The strategy for now is to just use this
for protocol witness thunk emission, where it is required
when generating a materializeForSet for storage that is
either implemented in a protocol extension or requires
reabstraction to the requirement's pattern.

Eventually, this should be generalized to the point that
we can use it for all materializeForSet emission, which
I don't think will take much.  However, that's not really
the sort of instability we want to embrace for the current
release.

WIP towards rdar://21836671; currently disabled, so NFC.

Swift SVN r31072
2015-08-07 09:22:27 +00:00
Jordan Rose
5b98703f36 [SILGen] Simplify ForceTryExpr generation a bit.
No functionality change.

Swift SVN r31031
2015-08-05 22:17:37 +00:00
Slava Pestov
48fe3e1c9b SILGen: Fix static computed properties in protocol extensions
We need to keep the AST formal type of the base around when building up
lvalues.

When the getter or setter involves an accessor call, we would use the
lowered type of the self argument to form the call. However, it might be
at the wrong level of abstraction, causing a @thin -vs- @thick metatype
mismatch. Using the formal type instead allows SILGenApply logic to emit
a thin to thick metatype conversion if necessary.

Fixes <rdar://problem/21358641>.

Swift SVN r30913
2015-08-01 07:21:25 +00:00
Andrew Trick
bd1c3ba0fc Revert "ManagedValue forwardInto and assignInto should not always "take" their value."
This reverts commit r30272.

There's a way to fix Builtin.reinterpretCast that preserves SILGen
RValue invariants better according to JohnM. So although these changes
are valid, there will be no way for me to test them.

After reverting this, I will roll back in the change to
emitImplicitValueConstructor because I think the old code was
violating SILGen invariants.

Swift SVN r30306
2015-07-17 06:52:04 +00:00
Andrew Trick
fda0971390 ManagedValue forwardInto and assignInto should not always "take" their value.
This is required in order to fix Builtin.reinterpretCast for
address-only types. That upcoming fix will include a test case that
exposes this SILGen path.

Swift SVN r30272
2015-07-16 21:24:04 +00:00
John McCall
ed0681004c Remove the almost-pointless Materialize type from SILGen,
and stop forcing optionals to memory in a bunch of places
where the callee is perfectly capable of handling a
non-address value.

Swift SVN r30107
2015-07-11 02:58:49 +00:00
Joe Groff
d84993108b SILGen: Emit Clang-imported witness tables by need too.
The other part of rdar://problem/21444126. This is a little trickier since SIL doesn't track uses of witness tables in a principled way. Track uses in SILGen by putting a "SILGenBuilder" wrapper in front of SILBuilder, which marks conformances from apply, existential erasure, and metatype lookup instructions as used, so we can avoid emitting shared Clang importer witnesses when they aren't needed.

Swift SVN r29544
2015-06-22 03:08:41 +00:00
Joe Groff
6ef872d471 SILGen: Handle optional variance in overrides before abstracting lowering their signatures.
Fixes rdar://problem/21154055.

Swift SVN r29406
2015-06-16 23:01:01 +00:00
Joe Groff
a14b83ba2c SILGen: Allow partial applications of enum cases.
This isn't as straightforward as it should be, since EnumElementDecls aren't AbstractFunctionDecls, but luckily there's only one trivial curry level with a thin metatype parameter.

Swift SVN r28991
2015-05-24 19:39:02 +00:00
John McCall
26a9a4d1e5 SILGen for 'rethrows'. WIP; committed to get broader testing
of an assertion.

Swift SVN r28733
2015-05-19 00:54:32 +00:00
John McCall
f0f8787b0f Generalize the 'transparent' flag passed around to
various emitApply routines.  NFC.

Swift SVN r28720
2015-05-18 20:30:12 +00:00
John McCall
312a9c1f6e Clean up correctly if a variadic argument throws.
rdar://20942603

Swift SVN r28622
2015-05-15 08:20:36 +00:00
Slava Pestov
60eb9780a1 SILGen: fix generation of switch containing try
When emitting try_apply, don't start a new scope between adding
a cleanup and forwarding the value. This would leave behind a
dead cleanup, which would fire an assertion in emitSharedCaseBlocks().

Fixes <rdar://problem/20923654>.

Swift SVN r28572
2015-05-14 17:22:47 +00:00
Chris Lattner
8a7b3f414e Revise the parser and AST representation of #available to be part of StmtCondition
instead of being an expression.

To the user, this has a couple of behavior changes, stemming from its non-expression-likeness.
 - #available cannot be parenthesized anymore
 - #available is in its own clause, not used in a 'where' clause of if/let.

Also, the implementation in the compiler is simpler and fits the model better.  This
fixes:
<rdar://problem/20904820> Following a "let" condition with #available is incorrectly rejected



Swift SVN r28521
2015-05-13 19:00:40 +00:00
Joe Groff
66e1f37e62 SILGen: Reabstraction for vtable entries.
When a derived class specializes its base class, e.g. 'class Derived: Base<Int>', the natural abstraction levels of its methods may differ from the original base class's more abstract methods. Handle this by using the reabstraction machinery to thunk values when necessary. Merge the existing optionality thunking support into the reabstraction code, where witness thunking and similar convention adjustments may also be able to use it, if we desire. Fixes rdar://problem/19760292.

Swift SVN r28505
2015-05-13 03:25:05 +00:00
Chris Lattner
3814b7003f Implement support for arbitrary pattern matching in for-each statements,
which composes straight-forwardly on top of the existing stuff for if/let
and guard.



Swift SVN r28371
2015-05-09 21:34:35 +00:00
Chris Lattner
f35677de14 Revise SILGen of foreach loops to only drop the optional generator result value in memory when it is address-only. This leads to better -O0 performance and easier to read silgen output. This is ongoing progress towards rdar://20642198.
This recommits r28318 with a fix that simplifies away a bunch of code and a bug.



Swift SVN r28328
2015-05-08 21:26:56 +00:00
Chris Lattner
5e7a45ff70 revert r28318, it is causing a problem on perftests. I'll investigate after lunch.
Swift SVN r28321
2015-05-08 18:02:24 +00:00
Chris Lattner
f379b1e15b Revise SILGen of foreach loops to only drop the optional generator result value
in memory when it is address-only.  This leads to better -O0 performance and 
easier to read silgen output.  This is ongoing progress towards rdar://20642198.


Swift SVN r28318
2015-05-08 17:46:20 +00:00
Michael Gottesman
8361ba44a9 Revert "Revert "[+0 self] Teach SILGen how to avoid emitting extra rr pairs when emitting RValues with Nominal Types and Tuples.""
This reapplies commit r27632 with additional fixes, tests.

I turned off the guaranteed struct optimization since we really don't have the
infrastructure to do it right and more importantly I don't have the time to do
so (and was not asked to do so).

We still have the class optimization though!

This means that there is one retain in ClassIntTreeMethod.find.

class ClassIntTreeNode {
  let value : Int
  let left, right : ClassIntTreeNode

  init() {}

  func find(v : Int) -> ClassIntTreeNode {
    if v == value { return self }
    if v < value { return left.find(v) }
    return right.find(v)
  }
}

Swift SVN r28264
2015-05-07 15:47:34 +00:00
Joe Groff
e2962ed213 SILGen: Implement recursive local function references.
Instead of immediately creating closures for local function declarations and treating them directly as capturable values, break function captures down and transitively capture the storage necessary to invoke the captured functions. Change the way SILGen emits calls to closures and local functions so that it treats the capture list as the first curry level of an invocation, so that full applications of closure literals or nested functions don't require a partial apply at all. This allows references among local functions with captures to work within the existing confines of partial_apply, and also has the nice benefit that circular references would work without creating reference cycles (though Sema unfortunately rejects them; something we arguably ought to fix.)

This fixes rdar://problem/11266246 and improves codegen of local functions. Full applications of functions, or immediate applications of closure literals like { }(), now never need to allocate a closure.

Swift SVN r28112
2015-05-04 05:33:55 +00:00
Chris Lattner
b6ad8d5efc Teach emitBindOptional() to disable the cleanup for the optional it is testing on the
nil path.  This avoids emitting a "release" operation on an optional temporary that is
always guaranteed to be dynamically nil.  This cleans up the generated code for x?.foo().



Swift SVN r28103
2015-05-04 00:13:18 +00:00
Chris Lattner
fec2f22459 Enhance SILGen of InjectIntoOptionalExpr/BindOptionalExpr/OptionalEvaluationExpr
to not drop optionals in memory all the time.  We now generate a lot better code
for them in many cases.  This makes generated SIL more readable and should help
-O0 perf.

This is progress towards <rdar://problem/20642198> SILGen shouldn't be dropping optionals into memory all the time




Swift SVN r28102
2015-05-03 23:27:28 +00:00
John McCall
5c171fd448 Parsing, type-checking, SILGen, and IRGen for try!.
Swift SVN r28085
2015-05-02 08:03:15 +00:00
John McCall
58b5e1dc0f Implement error handling in protocol witness and
reabstraction thunks.

rdar://20782111

Swift SVN r28075
2015-05-02 04:37:30 +00:00
Joe Groff
6de9d84b1f SILGen: Handle initializers in protocol extensions.
For the most part, this just involves spot fixes to make sure protocol inits follow the same paths as value type initializers would, with the extra wrinkle that we have to ensure we forward the correct metatype from the delegating initializer to the delegatee, in case the initializer is invoked with a different dynamic type from the static Self type. This should handle non-@objc delegations; @objc will need some additional work.

Swift SVN r27900
2015-04-29 02:40:30 +00:00
Nadav Rotem
3f675c5f06 Emit calls to swift_willThrow only in throw sites (not in re-throw sites).
rdar://20356658

Swift SVN r27891
2015-04-28 23:21:11 +00:00
Chris Lattner
caeca69466 fix <rdar://problem/19086357> SILGen crashes reabstracting default argument closure in members
When emitting default arguments for a parameter, emitApplyOfDefaultArgGenerator
was trying to reconstruct the original type by looking at the decl that it came
from.  However, it doesn't know the number of curry levels already applied, so it
was wrong in the case of default arguments on methods and initializers (which already
have self applied).

Fix this by having the caller pass this information in, since it knows the original
type.

While we're at it, remove the code for handling default arguments from the general 
SILGenExpr logic.  Default arguments in tuples are no longer a general thing, they 
are only valid in argument lists.



Swift SVN r27800
2015-04-27 04:29:32 +00:00
John McCall
d84a95f325 Handle foreign error conventions in foreign-to-native thunks.
Swift SVN r27737
2015-04-26 00:12:52 +00:00
Michael Gottesman
9c35c5eafd Revert "[+0 self] Teach SILGen how to avoid emitting extra rr pairs when emitting RValues with Nominal Types and Tuples."
This reverts commit r27632. I broke a test. I am going to look at it later.

Swift SVN r27634
2015-04-23 10:42:46 +00:00
Michael Gottesman
b80c96a2e7 [+0 self] Teach SILGen how to avoid emitting extra rr pairs when emitting RValues with Nominal Types and Tuples.
Specifically this patch makes the following changes:

1. We properly propagate down the SGFContext of a tuple to its elements
when extracting from a tuple. There was a typo that caused us to use
data from the newly created default initialized copy instead of from the
actual SGFContext of the parent tuple.

2. If we are accessing a member ref of self in a guaranteed context:

  a. If self is a struct, regardless of whether or not the field is a
     var or a let we no longer emit a retain. This is b/c self in a
     guaranteed context is immutable. This implies even a var in the
     struct can not be reassigned to.

  b. If self is a class, if the field is a let, we no longer emit an
     extra retain.

This makes it so that the only rr traffic in IntTreeNode::find is in the
block of code where we return self.

class IntTreeNode {
 let value : Int
 let left, right : IntTreeNode

 init() {} // not needed for -emit-silgen

 func find(v : Int) -> IntTreeNode {
   if v == value { return self }
   if v < value { return left.find(v) }
   return right.find(v)
 }
}

One gets the same effect using a struct for IntTreeNode and a generic
box class, i.e.:

class Box<T> {
  let value: T
  init(newValue: T) { value = newValue }
}

class Kraken {}
struct IntTreeNode {
  let k : Kraken // Just to make IntTreeNode non-trivial for rr purposes.
  let left, right : Box<IntTreeNode>

  init() {} // not needed for -emit-silgen

  func find(v : Int) -> IntTreeNode {
    if v == value { return self }
    if v < value { return left.value.find(v) }
    return right.value.find(v)
  }
}

There is more work that can be done here by applying similar logic to
enums, i.e. switching on self in an enum should not generate any rr
traffic over the switch. Also it seems that there are some places in SILGen
where isGuaranteedValid is not being propagated to SILGenFunction::emitLoad. But
that is for another day.

I am going to gather data over night and send an email to the list.

rdar://15729033

Swift SVN r27632
2015-04-23 10:10:43 +00:00
Chris Lattner
935e95080c minor cleanups.
Swift SVN r27566
2015-04-22 06:04:35 +00:00
Chris Lattner
06ad473cab Switch if/let SILGen to use the same codepath as let/else, instead of using the
ClauseMatrix stuff.



Swift SVN r27565
2015-04-22 05:49:57 +00:00
Chris Lattner
d9083d2e50 rework SGF::emitOptionalToOptional to keep loadable SILValue's as
registers instead of eagerly dumping them in memory and operating on
them by-address.  This avoids a lot of temporaries and traffic to 
manipulate them.

As part of this, add some new SGF::getOptionalNoneValue/getOptionalSomeValue
helper methods for forming loading optional values.

Many thanks to JoeG for helping with the abstraction difference change in
getOptionalSomeValue.



Swift SVN r27537
2015-04-21 22:54:08 +00:00