Commit Graph

384 Commits

Author SHA1 Message Date
Robert Widmann
d07eb71d9c Merge pull request #9457 from rintaro/parse-ifconfig-switchcase
[Parse] Allow #if to guard switch case clauses
2017-06-28 10:18:37 -07:00
Huon Wilson
4e3a010d22 [SILGen] Update the maximum value in a find-index-with-largest-value loop.
This is a loop that attempts to find the index of something with the longest
prefix, but wasn't actually saving the new longest length when finding something
with a longer length, meaning the loop always chose the last index. The update was
accidentally lost in f40e9ef0ae.
2017-06-22 12:48:55 -07:00
Rintaro Ishizaki
5d478bdb3b [Parse] Allow #if to guard switch case clauses
Resolves:
https://bugs.swift.org/browse/SR-4196
https://bugs.swift.org/browse/SR-2
2017-06-17 10:03:03 +09:00
Michael Gottesman
bf71ec8599 [silgen] Error nicely on multiple pattern cases with address only patterns.
We silently miscompiled previously the following code:

     protocol Gesture {}
     struct Foo {}
     struct Bar {}
     enum FooOrBar {
       case foo(Foo)
       case bar(Bar)
     }

     func main(_ f : FooOrBar) {
       switch f {
       case .foo(let data as Gesture),
            .bar(let data as Gesture):
         ...
       }
       ...
     }

This was because the multiple pattern per case code never implemented support
for address only types.

Now instead of miscompiling such programs, we do the following:

1. We emit an error.
2. When we construct the arguments for the named bindings, we just skip the
address only types. Everything else is normal.
3. In the case block, we use a SILUndef for the address only value.

This ensures that we preserve as many other diagnostics as possible.

rdar://32525952

P.S. As an additional benefit, this eliminates a source of address phi nodes
from SILGen.
2017-06-02 20:35:09 -07:00
Robert Widmann
3b202c18d8 Use 'hasAssociatedValues'
Use 'hasAssociatedValues' instead of computing and discarding the
interface type of an enum element decl.  This change has specifically not
been made in conditions that use the presence or absence of the
interface type, only conditions that depend on the presence or absence
of associated values in the enum element decl.
2017-05-22 09:54:47 -07:00
Robert Widmann
ede48cd58f [NFC] Minor cleanup in patterns
Some things noticed by inspection:

- Old spelling of Optional.some
- pattern dump was using the wrong iterator variable and would crash
- addColumns is never used because the row matrix doesn’t expand tuples
2017-04-25 00:34:10 -04:00
Joe Groff
dfdece2ab6 SILGen: Push 'usingImplicitVariablesForPattern' hack into 'where' clause expr evaluation.
We swapped the pattern variables at the wrong level, leaving them bound incorrectly on the cleanup path through a failed 'where' clause check. Fixes rdar://problem/31539726.
2017-04-14 11:58:38 -07:00
Greg Titus
b9ecde5fc7 If a shared case block dest got created before it's case's pattern vars were
initialized, the block arg could get created with the type from an outer scope.
2017-04-01 09:20:15 -07:00
Michael Gottesman
56adc0cdb2 [silgen] Make emitTupleDispatchWithOwnership use proper ownership.
rdar://31145255
2017-03-27 16:41:54 -07:00
Michael Gottesman
26b0b72390 [silgen] Eliminate address handling code from emitTupleDispatchWithOwnership. 2017-03-27 16:34:56 -07:00
Michael Gottesman
b69497886c [silgen] Hoist specialization code in emitTupleDispatch so that it does not cause the SIL emission code to be split. 2017-03-27 16:33:38 -07:00
Michael Gottesman
631d20f701 [gardening] Rename two instances of gen => SGF. 2017-03-27 16:31:13 -07:00
Michael Gottesman
022cb547e2 [silgen] Extract out tuple pattern emission for objects whenownership is enabled into its own method. 2017-03-27 16:28:56 -07:00
Michael Gottesman
eaa229b9f8 [semantic-sil] Make emitEnumElementDispatchWithOwnership conform to ownership.
This means inserting an eager copy on the switch operand, eliminating the
unforwarding, and destroying the copy in the default case as well as the normal
cases.

I did not refactor this code to use the switch enum builder, but at some point
it should be refactored as such.
2017-03-27 13:33:11 -07:00
Michael Gottesman
ba1e9f76d8 [silgen] Copy the body of emitEnumElementDispatch to emitEnumElementDispatchWithOwnership.
The reason I am doing this is that I am going to in the next couple of commits
change enum element dispatch to with or without semantic SIL use proper
ownership.

In this commit, I just did the copy and eliminated any parts of the code that
were predicated on having an address.
2017-03-27 10:28:08 -07:00
Michael Gottesman
43a851736c [silgen] When compiling with sil-ownership use borrows+CopyOnSuccess instead of TakeOnSuccess.
This is NFC since all changes are behind a flag. I took a look at the codegen
when this is enabled. It looks pretty optimizable for a -Onone ARC pass. Until I
have that pass though I need this behind a flag.

Basically you see a lot of this pattern:

%0 = begin_borrow %foo
%1 = copy_value %0
...
bb1:
  %2 = begin_borrow %1
  %3 = copy_value %2
  ...
  destroy_value %3
  end_borrow %2 from %1
  destroy_value %1
  end_borrow %0 from %foo
  ...

bb2:
  destroy_value %1
  end_borrow %0 from %foo
  ...

This is really easy to optimize since one can easily see that all of %1's users
are borrows or a final destroy.

rdar://31145255
2017-03-26 15:30:15 -07:00
John McCall
897f5ab7c5 Restore CanType-based micro-optimizations.
This reverts commit 5036806e5a.
However, it preserves a pair of changes to the SIL optimizer
relating to walking through optional types.
2017-03-14 11:38:11 -04:00
Slava Pestov
5036806e5a AST: Remove some unnecessary getCanonicalType() calls 2017-03-13 02:24:36 -07:00
Michael Gottesman
80e297d6aa [silgen] Go through and fix up places where during bringup of load_borrow I just put in load_borrow without any end_borrow cleanups. Now use real cleanups.
rdar://29791263
2017-03-02 19:53:33 -08:00
Michael Gottesman
455c126238 [semantic-sil] Pass the uncasted argument as an @owned arg in the failed checked_cast_br cast.
Previously, we would put a destroy_value directly on the value that we tried to
cast. Since checked_cast_br is consuming, this would cause the destroy_value on
the failure path to be flagged as a double consume.

This commit causes SILGen to emit the value consumed by checked_cast_br as an
@owned argument to the failure BB, allowing semantic arc rules to be respected.

As an additional benefit, I also upgraded the ownership_model_eliminator test to
use semantic sil verification.

One issue that did come up though is that I was unable to use the new code in
all locations in the compiler. Specifically, there is one location in
SILGenPattern that uses argument unforwarding. I am going to need to undo
argument unforwarding in SILGenPattern in order to completely eliminate the old
code path.
2017-02-28 17:29:03 -05:00
Joe Groff
fafe62f33e SILGen: Fix overrelease of pattern bindings shared from multiple patterns.
The `cmv.getValue() == value` check here seems fishy, since the downstream case block should always take ownership of the bound values. Now that `copy_value`s produce new defs, it's also never true when it would've been true using the `retain/release_value` model, leading us to fail to copy values when necessary.
2017-02-22 17:49:57 -08:00
Hugh Bellamy
f001b7562b Use relatively new LLVM_FALLLTHROUGH instead of our own SWIFT_FALLTHROUGH 2017-02-12 10:47:03 +07:00
Slava Pestov
a4fd57647c AST: Remove unused resolver parameter from getTypeOfMember() 2017-02-03 19:55:39 -08:00
Slava Pestov
dca292c652 Serialization: Don't serialize contextual enum argument type
Storing this separately is unnecessary since we already
serialize the enum element's interface type. Also, this
eliminates one of the few remaining cases where we serialize
archetypes during AST serialization.
2017-01-30 00:08:53 -08:00
Michael Gottesman
ea1f804207 [semantic-sil] Eliminate ValueOwnershipKind::Any from SILPHIArguments in Semantic SIL.
Most of this involved sprinkling ValueOwnershipKind::Owned in many places. In
some of these places, I am sure I was too cavalier and I expect some of them to
be trivial. The verifier will help me to track those down.

On the other hand, I do expect there to be some places where we are willing to
accept guaranteed+trivial or owned+trivial. In those cases, I am going to
provide an aggregate ValueOwnershipKind that will then tell SILArgument that it
should disambiguate using the type. This will eliminate the ackwardness from
such code.

I am going to use a verifier to fix such cases.

This commit also begins the serialization of ValueOwnershipKind of arguments,
but does not implement parsing of value ownership kinds. That and undef are the
last places that we still use ValueOwnershipKind::Any.

rdar://29791263
2017-01-10 20:05:23 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Hugh Bellamy
95af3aa7c8 Fix errors and warnings building swift/SILGen on Windows using MSVC 2016-12-22 11:57:33 +00:00
Michael Gottesman
4e8ff35df5 [semantic-sil] Add ValueOwnershipKind field to SILPHIArgument and split Argument creation methods into one for SILPHIArgument and another for SILFunctionArgument.
We preserve the current behavior of assuming Any ownership always and use
default arguments to hide this change most of the time. There are asserts now in
the SILBasicBlock::{create,replace,insert}{PHI,Function}Argument to ensure that
the people can only create SILFunctionArguments in entry blocks and
SILPHIArguments in non-entry blocks. This will ensure that the code in tree
maintains the API distinction even if we are not using the full distinction in
between the two.

Once the verifier is finished being upstreamed, I am going to audit the
createPHIArgument cases for the proper ownership. This is b/c I will be able to
use the verifier to properly debug the code. At that point, I will also start
serializing/printing/parsing the ownershipkind of SILPHIArguments, but lets take
things one step at a time and move incrementally.

In the process, I also discovered a CSE bug. I am not sure how it ever worked.
Basically we replace an argument with a new argument type but return the uses of
the old argument to refer to the old argument instead of a new argument.

rdar://29671437
2016-12-18 14:48:35 -08:00
practicalswift
38be6125e5 [gardening] C++ gardening: Terminate namespaces, fix argument names, ...
Changes:
* Terminate all namespaces with the correct closing comment.
* Make sure argument names in comments match the corresponding parameter name.
* Remove redundant get() calls on smart pointers.
* Prefer using "override" or "final" instead of "virtual". Remove "virtual" where appropriate.
2016-12-17 00:32:42 +01:00
Michael Gottesman
38ec08f45f [gardening] Standardize SILBasicBlock successor/predecessor methods that deal with blocks rather than the full successor data structure to have the suffix 'Block'.
This was already done for getSuccessorBlocks() to distinguish getting successor
blocks from getting the full list of SILSuccessors via getSuccessors(). This
commit just makes all of the successor/predecessor code follow that naming
convention.

Some examples:

getSingleSuccessor() => getSingleSuccessorBlock().
isSuccessor() => isSuccessorBlock().
getPreds() => getPredecessorBlocks().

Really, IMO, we should consider renaming SILSuccessor to a more verbose name so
that it is clear that it is more of an internal detail of SILBasicBlock's
implementation rather than something that one should consider as apart of one's
mental model of the IR when one really wants to be thinking about predecessor
and successor blocks. But that is not what this commit is trying to change, it
is just trying to eliminate a bit of technical debt by making the naming
conventions here consistent.
2016-11-27 12:32:51 -08:00
Michael Gottesman
96837babda Merge pull request #5920 from gottesmm/vacation_gardening
Vacation gardening
2016-11-25 09:17:21 -06:00
Michael Gottesman
0a8c54d04f [gardening] Always create new SILArguments using SILBasicBlock::createArgument instead of inline placement new.
The reasoning here is the same as in e42bf07.
2016-11-25 01:14:45 -06:00
Michael Gottesman
bf6920650c [gardening] Drop BB from all argument related code in SILBasicBlock.
Before this commit all code relating to handling arguments in SILBasicBlock had
somewhere in the name BB. This is redundant given that the class's name is
already SILBasicBlock. This commit drops those names.

Some examples:

getBBArg() => getArgument()
BBArgList => ArgumentList
bbarg_begin() => args_begin()
2016-11-25 01:14:36 -06:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Joe Groff
9348059b6d Revert "Simpler implementation for multiple matching patterns in case with variable bindings"
This reverts commit 9508fdc8a7 from #5094. Reemitting
case bodies doesn't work if the body contains nested closures or
declarations, since SILGen expects these to only ever be visited once.
2016-11-16 13:00:18 -08:00
Michael Gottesman
34ec32bc14 [semantic-arc] Handle the rest of the unqualified mem opts in SILGen.
Keep in mind that these are approximations that will not impact correctness
since in all cases I ensured that the SIL will be the same after the
OwnershipModelEliminator has run. The cases that I was unsure of I commented
with SEMANTIC ARC TODO. Once we have the verifier any confusion that may have
occurred here will be dealt with.

rdar://28685236
2016-11-09 11:37:52 -08:00
Michael Gottesman
e2419b75fd [semantic-arc] Qualify most of the stores in SILGen as store [init].
All of these cases were trivially inits since they involved storing into
a newly created temporary allocation.

rdar://28685236
2016-10-31 15:51:26 -07:00
Michael Gottesman
bffa7addaf [semantic-arc] Eliminate default {Load,Store}OwnershipQualification argument to SILBuilder::create{Load,Store}(...)
Today, loads and stores are treated as having @unowned(unsafe) ownership
semantics. This leaves the user to specify ownership changes on the loaded or
stored value independently of the load/store by inserting ARC operations. With
the change to Semantic SIL, this will no longer be true. Instead loads, stores
have ownership semantics that one must reason about such as copy, take, and
trivial.

This change moves us closer to that world by eliminating the default
OwnershipQualification argument from create{Load,Store}. This means that the
compiler developer cannot ignore reasoning about the ownership semantics of the
memory operation that they are creating.

Operationally, this is a NFC change since I have just gone through the compiler
and updated all places where we create loads, stores to pass in the former
default argument ({Load,Store}OwnershipQualifier::Unqualified), to
SILBuilder::create{Load,Store}(...). For now, one can just do that in situations
where one needs to create loads/stores, but over time, I am going to tighten the
semantics up via the verifier.

rdar://28685236
2016-10-30 13:07:06 -07:00
Joe Groff
e4c67e2d5a SIL: Give project_box a field index operand.
Allow project_box to get the address of any field in a multi-field box.
2016-10-24 13:10:41 -07:00
gregomni
9508fdc8a7 Simpler implementation for multiple matching patterns in case with variable bindings
Backed off of the implementation with a shared body block, which got
messy really fast with multiple patterns containing address-only values
to pass in, as it led to separated stack alloc/cleanup requirements and
passing stack alloc’d values as block args, aliasing of addresses for
stack cleanup verifying, etc. A mess.

This now does the conservative thing of repeating the body in each
matching pattern block.
2016-10-02 15:25:55 -07:00
John McCall
34fb15e375 Abstract the object type of an optional type according to the
abstraction pattern of the type rather than always using the
most-general pattern, and erase ImplicitlyUnwrappedOptional from
the SIL type system.
2016-09-08 23:26:19 -07:00
Chris Lattner
62e4811dac Remove support for nominal type patterns, which have never been supported in an
official swift release and have bitrotted.
2016-07-31 18:44:08 -07:00
gregomni
fc4fe97332 Memory management bug fix here for indirect nested enums. 2016-07-13 21:41:46 -07:00
Gary Liu
ac53a1ef45 New test case to show the problem fixed by commit 61ce47014b
Fix new test case from commit fcaba99c81edcb8320856cb55367e1d8a9176675

Adding the review changes to #3193
2016-06-30 15:11:25 -04:00
Bryan Chan
61ce47014b Case statements are not the only kind that require wildcard dispatches and have guard branches. 2016-06-22 08:40:46 -04:00
Joe Groff
dfc3e7fb0c 80 columns, NFC 2016-05-16 10:58:49 -07:00
Doug Gregor
945831023a [SILGen] Eliminate SILGen-specific handling of imported, synthesized conformances
Now that Sema communicates its "used" conformances to SILGen, there is
no reason for SILGen to separately look for "used" conformances. NFC
2016-05-09 20:59:30 -07:00
Roman Levenstein
2e77b3990b Add [nonatomic] attribute to all SIL reference counting instructions. 2016-04-06 01:52:43 -07:00
gregomni
63f810d2fd Variables in 'case' labels with multiple patterns.
Parser now accepts multiple patterns in switch cases that contain variables.
Every pattern must contain the same variable names, but can be in arbitrary
positions. New error for variable that doesn't exist in all patterns.

Sema now checks cases with multiple patterns that each occurence of a variable
name is bound to the same type. New error for unexpected types.

SILGen now shares basic blocks for switch cases that contain multiple
patterns. That BB takes incoming arguments from each applicable pattern match
emission with the specific var decls for the pattern that matched.

Added tests for all three of these, and some simple IDE completion
sanity tests.
2016-02-24 15:46:36 -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