Commit Graph

893 Commits

Author SHA1 Message Date
swift-ci
110aefa0aa Merge pull request #11758 from gottesmm/pr-e61caa58dce1a2e25fd7db36e27ca45b22967778 2017-09-04 11:11:06 -07:00
Michael Gottesman
dc8b1c2c3a [silgen] When pushing an RValue through a scope, use dealloc_box on any boxes we create instead of destroy_value.
It is only safe to perform a destroy_value on an alloc_box that contains an
initialized value. We preserve the original cleanups for the value we are
pushing through the scope implying that the box will not contain an initialized
value when its lifetime ends (just like an alloc_stack). Thus we must use
dealloc_box here.

The surprising thing about tracking down this error is that I was not hitting
any memory issues at -Onone. Instead what was happening was that at -O, we were
miscompiling (creating non-dominating uses of SSA values) in the face of an
address being taken twice.

This does not seem to hit any SILGen tests today (I hit this when testing a
patch I am trying to land today).

rdar://31521023
2017-09-04 10:46:37 -07:00
Michael Gottesman
adcf43b98c [silgen] Add the ability to push an RValue through an ArgumentScope.
This comes down to popping the formal evaluation scope and then performing a
popPreservingValue through the resulting normal Scope.

As a cleanup, I changed ArgumentScope.popPreservingValue(ManagedValue) to just
Scope::popPreservingValue instead of reimplementing said functionality.

rdar://31521023
2017-09-04 10:01:03 -07:00
Michael Gottesman
8f30ce51a6 [silgen] Delete CallEmission::getAtUncurryLevel!
rdar://33358110
2017-08-27 22:55:45 -07:00
Michael Gottesman
179aa4bd48 [silgen] Split getAtUncurryLevel into a typeinfo and a value API.
This decouples in SILGen when we produce a function's type info and when we
produce the actual object.

rdar://33358110
2017-08-27 09:09:17 -07:00
Michael Gottesman
5a19ef83f3 [silgen] Refactor the epilogue of getAtUncurryLevel into a helper and call the helper in the switch cases instead of after the switch.
This will make the routine easier to split.

rdar://33358110
2017-08-27 08:44:41 -07:00
Michael Gottesman
8669d9300b [silgen] Change getAtUncurryLevel to return a ManagedValue and a CalleeTypeInfo.
This is in prepatation for splitting getAtUncurryLevel into one function that
returns the CalleeTypeInfo (which is needed early) and a later one that returns
a ManagedValue, which can occur later.

rdar://33358110
2017-08-27 09:10:32 -06:00
Michael Gottesman
8a446ca1a1 [silgen] Eliminate the rest of the out parameters from CallEmission::apply* methods.
This is done by introducing a new result parameter called
FirstLevelApplicationResult. This communicates that the given parameters are not
inout parameters, but rather just result parameters.

As a result of this, many of the apply* methods only take the uncurry level and
the SILGenFunction context.

rdar://33358110
2017-08-26 19:40:33 -07:00
Michael Gottesman
2029c6ccff [silgen] Hoist computation of origFormalType from applyRemainingCallSites into apply.
rdar://33358110
2017-08-26 19:18:53 -07:00
Michael Gottesman
a623643570 [silgen] Sink foreignError into applyFirstLevelCallee leaf functions.
Again, we were not using foreignError as an out parameter despite the convention
and always passed in .None. In the places, where the leaf function was not
setting the foreignError before passing it off to another routine, I changed the
code to just pass in .None so I can just eliminate the variable entirely.

The only place where I had to actually sink instead of delete was
emitNormalApply.

rdar://33358110
2017-08-26 17:31:09 -07:00
Michael Gottesman
f3b4f1df10 [silgen] Sink foreignError parameter into applyFirstLevelCallee.
We were always passing in foreignError as an out parameter initialized to None
and then never used that value in the caller afterwards. So there is no reason
to keep it as a parameter to applyFirstLevelCallee.

rdar://33358110
2017-08-26 17:24:47 -07:00
Michael Gottesman
936f24b646 [silgen] Remove unused foreignError parameter from emitArgumentsForNormalApply.
Before we ever used the value, we were setting foreignError to be None. This is
another example of a mechanical refactoring of spaghetti code. The specific
transformation here was most likely a flattening of a conditional in the
original code.

rdar://33358110
2017-08-26 17:24:47 -07:00
Michael Gottesman
fe9a2770fc [silgen] Pass foreignError to emitArgumentsForNormalApply by const ref instead of ref.
foreignError is never written to in emitArgumentsForNormalApply.

rdar://33358110
2017-08-26 17:24:47 -07:00
Michael Gottesman
4d0767a293 [silgen] Delete dead foreignSelf parameter to applyEnumElementConstructor.
rdar://33358110
2017-08-26 16:59:22 -07:00
Michael Gottesman
7f9fadeec7 [silgen] Pass foreignSelf to applyPartiallyAppliedSuperMethod by value instead of by ref.
We never write to foreignSelf in applyPartiallyAppliedSuperMethod. So this
communicates more clearly what is going on.

rdar://33358110
2017-08-26 16:59:22 -07:00
Michael Gottesman
4ee0183630 [silgen] Pass foreignSelf by value instead of by ref to CallEmission::applySpecializedEmitter.
This is safe since applySpecializedEmitter does not write to foreignSelf.

rdar://33358110
2017-08-26 16:59:11 -07:00
Michael Gottesman
1d0f90d318 [silgen] Pass foreignSelf to emitArgumentsForNormalApply by value instead of by ref.
By passing it by ref, we make it seem like foreignSelf is either an out
parameter or an inout parameter. By changing the API of
emitArgumentsForNormalApply to take foreignSelf as a value, the API clearly
communicates that foreignSelf is a value type that is passed as an in parameter.

rdar://33358110
2017-08-26 16:46:07 -07:00
Michael Gottesman
7ddd5185bd [silgen] Pass ImportAsMemberStatus by value instead of by const ref.
ImportAsMemberStatus is just a byte. There is no reason to pass it by const &.

I think this was from a mechanical refactoring change where I was trying to be
very conservative.

rdar://33358110
2017-08-26 16:46:07 -07:00
Michael Gottesman
c777e59b04 [silgen] Rather than passing in initialOptions as an out parameter to emitArgumentsForNormalApply, just use a return value.
The reason to do this is that we are already always passing in
ApplyOptions::None for this value at all call sites. By using a reference to
create an out parameter, we make it look like something more complex is occuring
here. In contrast the result makes it clear to the reader that there is no state
from the caller before the callee that can affect this variable.

rdar://33358110
2017-08-26 16:22:05 -07:00
swift-ci
84029a8856 Merge pull request #11639 from gottesmm/pr-2a0b571d62a29a67143aaf7a2e36463c807fa0e1 2017-08-26 15:50:50 -07:00
swift-ci
03decd88af Merge pull request #11638 from gottesmm/pr-5de4dcda2025f659c90d7d2de03fe077daed81bc 2017-08-26 15:35:17 -07:00
Michael Gottesman
bb279c2ff2 [silgen] Rename getUncurriedOrigFormalType to getUncurriedOrigFormalResultType and simplify slightly.
When I was originally refactoring this code, I was confused about what this code
was actually supposed to do so I performed a simple manual transformation that
preserved correctness. After some thought, I have realized that this really is
about stripping off parameters from the origFormalType until we get what we call
on the calee origFormalResultType.

The slight simplification that I spoke of in the main title of the commit is
that I changed the routine to take an unsigned for the number of call sites,
rather than implicitly using the size parameter of the passed in uncurriedSites
list. There is no reason to tie this type to said API. And it will allow me to
make further refactorings as well!

rdar://33358110
2017-08-26 15:03:09 -07:00
Michael Gottesman
88b8e32ddc [silgen] Remove unused return value ApplyOptions from getAtUncurryLevel.
rdar://33358110
2017-08-26 14:45:40 -07:00
Slava Pestov
92f750aa3c Merge pull request #11637 from slavapestov/convenience-init-delegates-to-protocol-extension-init
Fix convenience init to protocol extension init delegation
2017-08-26 02:46:10 -07:00
Michael Gottesman
7430a74658 [silgen] Store self as an ArgumentSource in Callee instead of as a SILValue.
This will let me treat self during delegating initialization as an lvalue and
thus be emitted later without a scope. Thus I can simplify delegating
initialization slightly and land my argument scoping work.

rdar://33358110
2017-08-26 01:01:18 -07:00
Slava Pestov
e4d26a2eb2 SILGen: Use the ManagedValue form of createUncheckedRefCast() where possible 2017-08-25 23:50:52 -07:00
Michael Gottesman
f79b1db1fb [silgen] Add ArgumentSource::Kind::DelayedBorrowedRValue.
I am currently changing Callee to use an ArgumentSource instead of a SILValue to
represent self. Due to uncurrying/etc in certain cases, we need to communicate
that a value needs to be borrowed later before use. To do that in a clean way, I
am introducing a new form of ArgumentSource::Kind, DelayedBorrowedRValue.

This type of ArgumentSource can only be produced by calling
ArgumentSource::delayedBorrow(...). Such an argument source acts like a normal
RValue, except when you perform asKnownRValue, a borrow is performed before
returning the known rvalue.

Once uncurrying is ripped out/redone, this code can be removed in favor of just
using a borrowed ArgumentSource.

rdar://33358110
2017-08-25 20:30:16 -07:00
Michael Gottesman
acf4983922 [silgen] Change SILGenApply::applySuper to use RValues instead of ManagedValues.
This is a refactoring commit to prepare for Callee to store self as an
ArgumentSource.

rdar://33358110
2017-08-24 15:05:13 -07:00
swift-ci
f4df7cd96c Merge pull request #11585 from gottesmm/pr-e6ff9488a6ed8deac0e1e487f65961b20fd8d71f 2017-08-23 16:44:11 -07:00
Michael Gottesman
214253fdc4 [silgen] Add documentation to the fields of SILGenApply. 2017-08-23 15:52:58 -07:00
Michael Gottesman
4116a9faaa [gardening] Rename fields of SILGenApply to match SILGen naming conventions. 2017-08-23 12:37:27 -07:00
Michael Gottesman
7cf4cdf9d9 [silgen] Verify that all ManagedValues in all RValues that are loadable are actual loaded (i.e. have object type).
This is already an RValue invariant that used to be enforced upon RValue
construction. We put in a hack to work around a bug where that was not occuring
and changed RValue constructors to instead load stored objects when they needed
to. But the problem is that since then we have added more constructors that
provide other manners to create such an invalid RValue.

I added verification to many parts of RValue and exposed an additional verify
method that we can invoke at the end of emitRValue() eventually to verify our
invariants. This will give me the comfort to make that assumption in other parts
of SILGen without worry.

I also performed a small amount of cleanup of RValue construction.

rdar://33358110
2017-08-08 00:43:33 -07:00
Michael Gottesman
77f6a396aa [silgen] Cleanup ArgEmitter:emitDirect to use early returns instead of an if-else chain. NFC.
rdar://33358110
2017-08-01 12:03:07 -07:00
Andrew Trick
cb55edad63 [sil-opaque-values] Ownership. Emit a borrow for @in_guarateed arguments. 2017-07-31 14:47:56 -07:00
John McCall
6e4c83d30b Remove the need for magic numbers when making an ExternalUnion. NFC. 2017-07-27 22:52:51 -04:00
John McCall
c0b3bf1711 Suppress access enforcement when an l-value is converted to a pointer
just for pointer identity.

The current technique for deciding whether that's the case is *extremely*
hacky and need to be replaced with an attribute, but I'm reluctant to
take that on so late in the schedule.  The hack is terrible but not too
hard to back out in the future.  Anyone who names a method like this just
to get the magic behavior knows well that they are not on the side of
righteousness.

rdar://33265254
2017-07-21 23:40:04 -04:00
Robert Widmann
8cdddef2f8 Refactor Params to use flags
Also, begin to pass around base types instead of raw InOutType types.  Ideally, only Sema needs to deal with them, but this means that a bunch of callers need to unwrap any inouts that might still be lying around before forming these types.

Multiple parts of the compiler were slicing, dicing, or just dropping these flags.  Because I intend to use them for the new function type representation, I need them to be preserved all across the compiler.  As a first pass, this stubs in what will eventually be structural rules as asserts and tracks down all callers of consequence to conform to the new invariants.

This is temporary.
2017-07-19 09:49:32 -07:00
John McCall
63594f1e10 Fix a SILGen bug with variadic subscripts that I recently introduced
and a CSApply bug with variadic tuple subscripts that I did not.

The SILGen bug was exposed by the source-compat test suite as part
of rdar://33341584.
2017-07-17 18:52:17 -04:00
John McCall
80b180a9a1 Implement a syntactic peephole to recognize explicit bridging
conversions that reverse an implicit conversion done to align
foreign declarations with their imported types.

For example, consider an Objective-C method that returns an NSString*:
  - (nonnull NSString*) foo;
This will be imported into Swift as a method returning a String:
  func foo() -> String
A call to this method will implicitly convert the result to String
behind the scenes.  If the user then casts the result back to NSString*,
that would normally be compiled as an additional conversion.  The
compiler cannot simply eliminate the conversion because that is not
necessarily semantically equivalent.

This peephole recognizes as-casts that immediately reverse a bridging
conversion as a special case and gives them special power to eliminate
both conversions.  For example, 'foo() as NSString' will simply return
the original return value.  In addition to call results, this also
applies to call arguments, property accesses, and subscript accesses.
2017-07-15 01:13:41 -04:00
John McCall
d5441b92bd Switch ArgumentSource to use ExternalUnion, add the ability to
create a tuple of argument sources, and destructure such tuples
during argument emission.  NFC.
2017-07-15 01:12:55 -04:00
John McCall
7f22faf968 Substantially rework how SILGen handles bridging as part of laying the
ground work for the syntactic bridging peephole.

- Pass source and dest formal types to the bridging routines in addition
  to the dest lowered type.  The dest lowered type is still necessary
  in order to handle non-standard abstraction patterns for the dest type.

- Change bridging abstraction patterns to store bridged formal types
  instead of the formal type.

- Improve how SIL type lowering deals with import-as-member patterns.

- Fix some AST bugs where inadequate information was being stored in
  various expressions.

- Introduce the idea of a converting SGFContext and use it to regularize
  the existing id-as-Any conversion peephole.

- Improve various places in SILGen to emit directly into contexts.
2017-07-11 12:45:13 -04:00
Michael Gottesman
9f2bc267b8 [silgen] Fix 5b7b6d0b5e to work with semantic sil.
I tried to do a more complex fix, but it will take more time than I have now.
This change at least ensures that we maintain correctness both in terms of the
super types and in terms of the semantic sil verifier.

rdar://31880847
2017-06-30 13:13:41 -07:00
Michael Gottesman
d8139d36fa [silgen] Extract from SILGenApply::processAbstractFunctionDecl method "processProtocolDecl". NFC.
This is another large code block guarded by an if statement. This is really a
subroutine.

rdar://31880847
2017-06-26 16:25:49 -07:00
Michael Gottesman
9114d45e78 [silgen] Refactor out subroutine from large method SILGenApply::visitDeclRefExpr. NFC.
The routine is called processAbstractFunctionDecl and does just what you guess.
This reduces the size of visitDeclRefExpr. In a subsequent commit, I am going to
extract out a subroutine from processAbstractFunctionDecl as well for handling
protocols.

rdar://31880847
2017-06-26 16:25:39 -07:00
Michael Gottesman
3fdc9b6955 [silgen] Refactor out part of allocateObjCObject into a helper method. NFC.
This allows for the control flow in the function to be simplified by reducing
the maximum number of overlapping program paths. This makes it easier to reason
about.

rdar://31880847
2017-06-26 16:25:04 -07:00
Michael Gottesman
1fa5276f0f [gardening] Add a space in between two methods. This matches the other methods in the class. 2017-06-26 15:33:34 -07:00
Michael Gottesman
d6d14f6202 [gardening] Change an if-else block to use early exits instead. 2017-06-26 15:14:26 -07:00
Robert Widmann
3af359cb44 Merge pull request #10242 from CodaFi/in-through-the-out-door
[NFC] Move HasInOut bit out of recursive type properties
2017-06-14 13:11:19 -07:00
Robert Widmann
a4bf57f9d1 Move HasInOut bit out of recursive type properties
In anticipation of removing this bit, move it from the
recursive type property into TupleType - its only real
user.  This necessitates uglifying a bit of logic in the
short term that used to speak broadly of materializability
to instead speak about LValues and Tuples of InOut values
independently.
2017-06-14 09:54:19 -07:00
Slava Pestov
28c35ff325 SILGen: Fix problems with local generic functions
When evaluating if a call of a local function requires substitutions,
we checked if the immediate function being referenced captured
generic parameters, without checking if it transitively captured
generic parameters via other local functions it references.

Also, when calling accessors, we did not perform this check at all.

Fix both ordinary function calls and accessor calls to drop the
substitutions only if the *lowered* local captures say it is safe.

Finally, fix Sema to not consider substitutions in local function
references as generic parameter captures, since we now correctly
calculate the transitive closure of all captures.

Fixes <rdar://problem/32761305> and related issues.
2017-06-14 01:42:13 -07:00