Commit Graph

317 Commits

Author SHA1 Message Date
Adrian Prantl
c4c99811e6 Fix a case where guard let bindings were not emitted in their own scope.
This is a follow-up to e9d557ae28.  The
debug_value for the guard let binding is introduced by SGF.emitStmtCondition(),
so we also need to enter the debug scope before running that function.

rdar://74538257
2021-02-19 18:11:00 -08:00
Erik Eckstein
ec64f2a255 SILLocation: replace CleanupLocation::get(loc) with CleanupLocation(loc)
No need to have a static get function - the constructor can be used directly.
NFC
2021-01-29 20:28:21 +01:00
Philippe Hausler
6e05240426 AsyncSequence and protocol conformance rethrows (#35224)
* Initial draft of async sequences

* Adjust AsyncSequence associated type requirements

* Add a draft implementation of AsyncSequence and associated functionality

* Correct merge damage and rename from GeneratorProtocol to AsyncIteratorProtocol

* Add AsyncSequence types to the cmake lists

* Add cancellation support

* [DRAFT] Implementation of protocol conformance rethrowing

* Account for ASTVerifier passes to ensure throwing and by conformance rethrowing verifies appropriately

* Remove commented out code

* OtherConstructorDeclRefExpr can also be a source of a rethrowing kind function

* Re-order the checkApply logic to account for existing throwing calculations better

* Extract rethrowing calculation into smaller functions

* Allow for closures and protocol conformances to contribute to throwing

* Add unit tests for conformance based rethrowing

* Restrict rethrowing requirements to only protocols marked with @rethrows

* Correct logic for gating of `@rethrows` and adjust the determinates to be based upon throws and not rethrows spelling

* Attempt to unify the async sequence features together

* Reorder try await to latest syntax

* revert back to the inout diagnosis

* House mutations in local scope

* Revert "House mutations in local scope"

This reverts commit d91f1b25b59fff8e4be107c808895ff3f293b394.

* Adjust for inout diagnostics and fall back to original mutation strategy

* Convert async flag to source locations and add initial try support to for await in syntax

* Fix case typo of MinMax.swift

* Adjust rethrowing tests to account for changes associated with @rethrows

* Allow parsing and diagnostics associated with try applied to for await in syntax

* Correct the code-completion for @rethrows

* Additional corrections for the code-completion for @rethrows this time for the last in the list

* Handle throwing cases of iteration of async sequences

* restore building XCTest

* First wave of feedback fixes

* Rework constraints checking for async sequence for-try-await-in checking

* Allow testing of for-await-in parsing and silgen testing and add unit tests for both

* Remove async sequence operators for now

* Back out cancellation of AsyncIteratorProtocols

* Restructure protocol conformance throws checking and cache results

* remove some stray whitespaces

* Correct some merge damage

* Ensure the throwing determinate for applying for-await-in always has a valid value and adjust the for-await-in silgen test to reflect the cancel changes

* Squelch the python linter for line length
2021-01-25 18:48:50 -08:00
Erik Eckstein
b7351780f7 SIL: move all the block-list modifying APIs to SILFunction.
... and remove SILFunction::getBlocks().

It's just a cleanup, NFC.
2021-01-14 17:35:31 +01:00
Varun Gandhi
a9c4f15677 Merge pull request #34979 from varungandhi-apple/vg-fix-silgen-for-loop
[SILGen] Branch on result of next() before conversion.
2021-01-12 14:54:11 -08:00
Varun Gandhi
02c4ad3850 [SILGen] Branch on result of next() before conversion.
Fixes SR-8688 (rdar://problem/47162940).
2021-01-12 09:09:48 -08:00
Slava Pestov
269818686a SILGen: Introduce a new debug scope for GuardStmts
A GuardStmt can shadow bindings from outer scopes, so make sure
we actually create a new debug scope here.

Note that we push the scope, but pop it when the next innermost
debug scope ends. To keep track of this I added a new bit of state
to the debug scope stack, indicating that this scope originated
from a GuardStmt.

Ideally, debug info would rely on ASTScope which is the canonical
source of truth for this information. For now, this is a spot fix.

I filed <rdar://problem/72954242> to track implementing the longer
term solution.

Fixes <rdar://problem/72900354>.
2021-01-11 22:47:41 -05:00
Michael Gottesman
c026e95cce [ownership] Extract out SILOwnershipKind from ValueOwnershipKind into its own type and rename Invalid -> Any.
This makes it easier to understand conceptually why a ValueOwnershipKind with
Any ownership is invalid and also allowed me to explicitly document the lattice
that relates ownership constraints/value ownership kinds.
2020-11-10 14:29:11 -08:00
Erik Eckstein
c5bbe516c7 [concurrency] SILGen: emit hop_to_executor instructions
Emit hop_to_executor instruction in the prolog of actor-isolated async functions and after all async calls in such functions.

rdar://problem/70299168
2020-11-04 09:57:40 +01:00
Josh Learn
45b670c740 [SR-13639][Diag] Don't diagnose local type declarations as unreachable
Local types can be used earlier within a scope than they were declared.
We currently diagnose these type declarations as unreachable when they
appear after a return, even though they may have been used prior to
the return statement. This change stops that diagnostic.

Resolves SR-13639, rdar://69845495
2020-10-29 15:53:49 -07:00
Suyash Srijan
6ef4b0db4b [SILGen] Emit unreachable code diagnostic for single expression closures with implicit returns too (#33604)
* [SILGen] Emit unreachable code diagnostics for single expression closures with implicit returns too

* [Test] Update diagnostic in test/SILGen/functions_uninhabited_param.swift
2020-09-21 21:58:14 +01:00
Slava Pestov
b9cd6caa72 SILGen: Emit hoisted declarations at the top level 2020-09-03 16:16:51 -04:00
Owen Voorhees
43e2d107e1 [SE-0276] Implement multi-pattern catch clauses
Like switch cases, a catch clause may now include a comma-
separated list of patterns. The body will be executed if any
one of those patterns is matched.

This patch replaces `CatchStmt` with `CaseStmt` as the children
of `DoCatchStmt` in the AST. This necessitates a number of changes
throughout the compiler, including:
- Parser & libsyntax support for the new syntax and AST structure
- Typechecking of multi-pattern catches, including those which
  contain bindings.
- SILGen support
- Code completion updates
- Profiler updates
- Name lookup changes
2020-04-04 09:28:26 -07:00
Doug Gregor
97b5a0d5fb [Type checker] Stop devirtualizing the reference to IteratorProtocol.next().
Rather than having the type checker look for the specific witness to
next() when type checking the for-each loop, which had the effect of
devirtualizing next() even when it shouldn't be, leave the formation
of the next() reference to SILGen. There, form it as a witness
reference, so that the SIL optimizer can choose whether to
devirtualization (or not).
2020-01-02 14:39:00 -08:00
Doug Gregor
f22a7e2880 [AST] Drop makeIterator reference from ForEachStmt.
Rather than having the type checker form the ConcreteDeclRef for
makeIterator, have SILGen do it, because it's fairly trivial.
Eliminates some redundant state from the AST.
2020-01-02 13:29:13 -08:00
Slava Pestov
4017a16684 Revert "SILGen: Simplify SILGenFunction::emitClosure()"
This change has a subtle impact on debug info emission and causes
a regression with a subsequent patch I'm about to commit.

This reverts commit 62d1adb409.
2019-12-19 23:51:53 -05:00
Robert Widmann
3e1a61f425 [NFC] Fold The Tri-State In Optional<ProtocolConformanceRef>
ProtocolConformanceRef already has an invalid state.  Drop all of the
uses of Optional<ProtocolConformanceRef> and just use
ProtocolConformanceRef::forInvalid() to represent it.  Mechanically
translate all of the callers and callsites to use this new
representation.
2019-10-29 16:55:56 -07:00
Joe Groff
03c7919b4a SIL: Add fields to SILFunctionType for substituted function types.
https://forums.swift.org/t/improving-the-representation-of-polymorphic-interfaces-in-sil-with-substituted-function-types/29711

This prepares SIL to be able to more accurately preserve the calling convention of
polymorphic generic interfaces by letting the type system represent "substituted function types".
We add a couple of fields to SILFunctionType to support this:

- A substitution map, accessed by `getSubstitutions()`, which maps the generic signature
  of the function to its concrete implementation. This will allow, for instance, a protocol
  witness for a requirement of type `<Self: P> (Self, ...) -> ...` for a concrete conforming
  type `Foo` to express its type as `<Self: P> (Self, ...) -> ... for <Foo>`, preserving the relation
  to the protocol interface without relying on the pile of hacks that is the `witness_method`
  protocol.

- A bool for whether the generic signature of the function is "implied" by the substitutions.
  If true, the generic signature isn't really part of the calling convention of the function.
  This will allow closure types to distinguish a closure being passed to a generic function, like
  `<T, U> in (*T, *U) -> T for <Int, String>`, from the concrete type `(*Int, *String) -> Int`,
  which will make it easier for us to differentiate the representation of those as types, for
  instance by giving them different pointer authentication discriminators to harden arm64e
  code.

This patch is currently NFC, it just introduces the new APIs and takes a first pass at updating
code to use them. Much more work will need to be done once we start exercising these new
fields.

This does bifurcate some existing APIs:

- SILFunctionType now has two accessors to get its generic signature.
  `getSubstGenericSignature` gets the generic signature that is used to apply its
  substitution map, if any. `getInvocationGenericSignature` gets the generic signature
  used to invoke the function at apply sites. These differ if the generic signature is
  implied.
- SILParameterInfo and SILResultInfo values carry the unsubstituted types of the parameters
  and results of the function. They now have two APIs to get that type. `getInterfaceType`
  returns the unsubstituted type of the generic interface, and
  `getArgumentType`/`getReturnValueType` produce the substituted type that is used at
  apply sites.
2019-10-25 13:38:51 -07:00
pschuh
7b9a51108a Bugfix for crasher SR-11269. (#26590) 2019-08-13 16:23:37 -07:00
Parker Schuh
687ff25157 Convert ForEachStmt to not use tc.callWitness().
For reference, everything else except string interpolation has been converted
not to call this method.
2019-06-28 14:20:42 -07:00
Azoy
4d2b5d4b2f emit apply 2019-03-31 11:54:03 -05:00
Robert Widmann
426fe886dc [SR-8272] Drop the last remnants of LogicValue
Removes the _getBuiltinLogicValue intrinsic in favor of an open-coded
struct_extract in SIL.  This removes Sema's last non-literal use of builtin
integer types and unblocks a bunch of cleanup.

This patch would be NFC, but it improves line information for conditional expression codegen.
2018-12-19 23:14:59 -05:00
Suyash Srijan
5587edf13e [silgen] fix brace indentation (again) 2018-11-26 21:59:55 +00:00
Suyash Srijan
59a5d71931 [silgen] run git-clang-format to fix all indentation issues 2018-11-26 21:57:44 +00:00
Suyash Srijan
ce40755849 [silgen] fix brace indent 2018-11-26 21:56:14 +00:00
Suyash Srijan
4a26a746db [silgen] fix indentation 2018-11-26 21:54:47 +00:00
Suyash Srijan
3b8128ccf9 [silgen] Move diagnostic note creation to SILGenStmt 2018-11-26 21:50:31 +00:00
Marc Rasi
bf18697b4f parsing, typechecking, and SILGen for #assert
`#assert` is a new static assertion statement that will let us write
tests for the new constant evaluation infrastructure that we are working
on. `#assert` works by lowering to a `Builtin.poundAssert` SIL
instruction. The constant evaluation infrastructure will look for these
SIL instructions, const-evaluate their conditions, and emit errors if
the conditions are non-constant or false.

This commit implements parsing, typechecking and SILGen for `#assert`.
2018-11-07 16:34:17 -08:00
Michael Gottesman
447a11a19a [silgenpattern] Change catch emission to not be conditional on ownership verification being enabled.
We were missing the code-coverage needed to show this so I added some examples
to test/SILGen/errors.swift.

rdar://29791263
2018-10-23 10:05:35 -07:00
Andrew Trick
2ccc089b27 SILGenFunction::mergeCleanupBlocks.
Avoid emitting unnecessary basic block for cleanup chains. This is a
general approach that handles all cases while simplifying SILGen
emission and keeping the CFG in a valid state during SILGen.
2018-10-19 22:22:23 -07:00
Andrew Trick
c8e4d2b014 SILGen KeyPath: Create a basic block to split the critical edge.
This is the one place in SILGen where we need to explicitly split the
critical edge. createBasicBlockAndBranch is a simply utility for that.
2018-10-19 22:22:23 -07:00
Andrew Trick
1c701b2362 Simplify SILGen/Condition.
This code was attempting to generate a critical edge on one side of a
condition. Not only is that undesirable, but it breaks the abstraction
and results an a lot of complexity.

As usual, fixing critical edges simply means removing unnecessary complexity.
2018-10-19 22:22:23 -07:00
Andrew Trick
e04378f136 SILGen: Always emit the cleanup block.
This avoids critical edges, which will be split in the optimizer
anyway.
2018-10-19 22:22:23 -07:00
Michael Gottesman
d57a88af0d [gardening] Rename references to SILPHIArgument => SILPhiArgument. 2018-09-25 22:23:34 -07:00
John McCall
512e55683e Make it easy to create a SILBasicBlock immediately before a target block.
Also, make "after" requests explicit in the API.
2018-08-18 12:36:36 -04:00
John McCall
7a4aeed570 Implement generalized accessors using yield-once coroutines.
For now, the accessors have been underscored as `_read` and `_modify`.
I'll prepare an evolution proposal for this feature which should allow
us to remove the underscores or, y'know, rename them to `purple` and
`lettuce`.

`_read` accessors do not make any effort yet to avoid copying the
value being yielded.  I'll work on it in follow-up patches.

Opaque accesses to properties and subscripts defined with `_modify`
accessors will use an inefficient `materializeForSet` pattern that
materializes the value to a temporary instead of accessing it in-place.
That will be fixed by migrating to `modify` over `materializeForSet`,
which is next up after the `read` optimizations.

SIL ownership verification doesn't pass yet for the test cases here
because of a general fault in SILGen where borrows can outlive their
borrowed value due to being cleaned up on the general cleanup stack
when the borrowed value is cleaned up on the formal-access stack.
Michael, Andy, and I discussed various ways to fix this, but it seems
clear to me that it's not in any way specific to coroutine accesses.

rdar://35399664
2018-07-23 18:59:58 -04:00
John McCall
a3bdc89d47 Tell cleanups whether they're being emitted for the normal or unwind path.
NFC, but this may become semantically important for coroutines, because
an active coroutine must be aborted instead of ended on the unwind path.
2018-07-05 02:48:41 -04:00
Ravi Kandhadai
895c1f0355 [DefiniteInitialization] Check whether globals captured by top-level
defer statements are initialized.

<rdar://30720636>
2018-07-02 11:40:21 -07:00
Slava Pestov
2c65f5d3b3 SILGen: Don't emit unreachable code warnings for explicit return statement 2018-06-04 23:06:03 -07:00
Vedant Kumar
d3ea09fafc [Coverage] Fix mappings of regions following do-catch stmts
Coverage reporting for source regions following do-catch statements was
incorrect, because it did not properly reflect early exits.

rdar://34244637
2018-05-18 12:11:14 -07:00
Michael Gottesman
a92f617973 [+0-all-args] When emitting for each statements, make sure to call unreachableExit from the Switch Case Scope.
This will allow me to enforce with an assert that the switch case scope is
always exitted in an explicit way.
2018-03-15 17:28:47 -07:00
Michael Gottesman
aa9b27802a [silgen] Add helper methods to SwitchEnumBuilder for creating .Some, .None cases.
I also used it to clean up a few uses of SwitchEnumBuilder.
2018-03-15 17:28:47 -07:00
Michael Gottesman
6b2ca79381 [silgen] Make NormalCaseHandler and DefaultCaseHandler take SwitchCaseFullExpr as movable rvalues.
This fits more with the notion of the API, that the user is being given a scope
that was already setup for them and that they must decide when to pop.
2018-03-15 17:28:47 -07:00
Michael Gottesman
76af5c5b16 [silgen] Centralize SwitchEnumBuilder and SwitchCaseFullExpr into the same file: SwitchEnumBuilder.{h,cpp}.
Previously SwitchEnumBuilder was in SILGenBuilder.{h,cpp} and
SwitchEnumCaseFullExpr was in its own file. This really made no sense since:

1. The two classes are inherently related to each other so really should be
together in the source base.
2. SwitchEnumBuilder uses a SILGenBuilder, but is really a separate independent
concept/entity, so there really is no reason to keep it in SILGenBuilder.cpp.

Just a quick fix to eliminate something that was bugging me.

NFC.
2018-02-17 13:09:23 -08:00
Mark Lacey
b4b66bc8e8 Replace getAnyOptionalObjectType with getOptionalObjectType. 2018-02-05 23:59:00 -08:00
Vedant Kumar
dd560d2aa6 [Coverage] Refactor SIL generation for profiling
This patch moves the ownership of profiling state from SILGenProfiling
to SILFunction, where it always belonged. Similarly, it moves ownership
of the profile reader from SILGenModule to SILModule.

The refactor sets us up to fix a few outstanding code coverage bugs and
does away with sad hacks like ProfilerRAII. It also allows us to locally
guarantee that a profile counter increment actually corresponds to the
SILFunction at hand.

That local guarantee causes a bugfix to accidentally fall out of this
refactor: we now set up the profiling state for delayed functions
correctly. Previously, we would set up a ProfilerRAII for the delayed
function, but its counter increment would never be emitted :(. This fix
constitutes the only functional change in this patch -- the rest is NFC.

As a follow-up, I plan on removing some dead code in the profiling
logic and fixing a few naming inconsistencies. I've left that for later
to keep this patch simple.
2018-01-05 17:20:20 -08:00
Vedant Kumar
cccee1df03 Revert "[Coverage] Refactor SIL generation for profiling" 2018-01-03 21:57:49 -08:00
Vedant Kumar
aba9d53736 [Coverage] Refactor SIL generation for profiling
This patch moves the ownership of profiling state from SILGenProfiling
to SILFunction, where it always belonged. Similarly, it moves ownership
of the profile reader from SILGenModule to SILModule.

The refactor sets us up to fix a few outstanding code coverage bugs and
does away with sad hacks like ProfilerRAII. It also allows us to locally
guarantee that a profile counter increment actually corresponds to the
SILFunction at hand.

That local guarantee causes a bugfix to accidentally fall out of this
refactor: we now set up the profiling state for delayed functions
correctly. Previously, we would set up a ProfilerRAII for the delayed
function, but its counter increment would never be emitted :(. This fix
constitutes the only functional change in this patch -- the rest is NFC.

As a follow-up, I plan on removing some dead code in the profiling
logic and fixing a few naming inconsistencies. I've left that for later
to keep this patch simple.
2018-01-03 11:18:40 -08:00
Michael Gottesman
567e3596e5 [+0-all-args] Fix address only for-each emission to not hack around forwarding.
rdar://34222540
2017-11-27 16:13:46 -08:00
Michael Gottesman
6de8011be6 [+0-normal] Use Scope::popPreservingValue instead of using a hand-rolled version.
I would have used popPreservingValue if it was available at the time... but it
was not.

Just chopping this off from a larger commit since it can stand on its own.

rdar://34222540
2017-11-27 10:01:40 -08:00