Commit Graph

753 Commits

Author SHA1 Message Date
Arnold Schwaighofer
e4006b949d SILGen: Soften assertions it is okay for an base value of an boxed existentials not to be an address
rdar://46343977
2018-12-13 10:34:38 -08:00
Slava Pestov
06c2e980cb Merge pull request #21133 from slavapestov/lazy-implicit-inits
Lazy synthesis of implicit constructors in non-primary files
2018-12-07 22:45:40 -05:00
Slava Pestov
6c012b2aec AST: Remove some unnecessary LazyResolver * parameters from ASTContext methods 2018-12-07 20:39:27 -05:00
Slava Pestov
aa747dcd81 Remove property behaviors 2018-12-07 20:38:33 -05:00
Slava Pestov
0a8ee10621 AST: Refactor AbstractStorageDecl::getAccessStrategy() for keypath resilience
Instead of passing in a DeclContext, which we don't have when emitting a keypath
accessor, pass in a ModuleDecl and ResilienceExpansion.

Keypaths now work well enough in inlinable contexts that we can check in an
end-to-end resilience test.
2018-11-16 23:18:30 -05:00
John McCall
a4ea4d1756 Tolerate scalars when opening a non-opaque existential l-value component.
rdar://45956703
2018-11-12 15:36:56 -05:00
John McCall
44e0f44040 Merge pull request #20493 from rjmccall/keypath-compiler-abi
Change the compiler ABI of keypaths.
2018-11-10 14:18:44 -05:00
John McCall
3e5165d1ab Change the compiler ABI of keypaths.
Previously, the stdlib provided:

- getters for AnyKeyPath and PartialKeyPath, which have remained;

- a getter for KeyPath, which still exists alongside a new read
  coroutine; and

- a pair of owned mutable addressors that provided modify-like behavior
  for WritableKeyPath and ReferenceWritableKeyPath, which have been
  replaced with modify coroutines and augmented with dedicated setters.

SILGen then uses the most efficient accessor available for the access
it's been asked to do: for example, if it's been asked to produce a
borrowed r-value, it uses the read accessor.

Providing a broad spectrum of accessor functions here seems acceptable
because the code-size hit is fixed-size: we don't need to generate
extra code per storage declaration to support more alternatives for
key paths.

Note that this is just the compiler ABI; the implementation is still
basically what it was.  That means the implementation of the setters
and the read accessor is pretty far from optimal.  But we can improve
the implementation later; we can't improve the ABI.

The coroutine accessors have to be implemented in C++ and used via
hand-rolled declarations in SILGen because it's not currently possible
to declare independent coroutine accessors in Swift.
2018-11-10 02:08:04 -05:00
John McCall
731da3b991 [NFC] Improve some SILGen functions for working with begin_apply. 2018-11-10 02:08:04 -05:00
Slava Pestov
c7338d06ca AST: Remove owning addressors 2018-11-09 20:49:44 -05:00
John McCall
c0285a744c Always use the l-value logic for emitting key path applications.
Not NFC because it also fixes an evaluation order bug (and reorders
some less-important stuff): the key-path expression needs to be
evaluated immediately during formal evaluation and cannot be delayed
until start-of-access.
2018-11-09 02:42:17 -05:00
Arnold Schwaighofer
9b889e72ea Fix a use-after-free error I introduced
rdar://45894044
2018-11-07 19:31:45 -08:00
Arnold Schwaighofer
b102c7f6b4 Parser/Sema/SILGen changes for @_dynamicReplacement(for:)
Dynamic replacements are currently written in extensions as

extension ExtendedType {
  @_dynamicReplacement(for: replacedFun())
  func replacement() { }
}

The runtime implementation allows an implementation in the future where
dynamic replacements are gather in a scope and can be dynamically
enabled and disabled.

For example:

dynamic_extension_scope CollectionOfReplacements {
  extension ExtentedType {
    func replacedFun() {}
  }

  extension ExtentedType2 {
    func replacedFun() {}
  }
}

CollectionOfReplacements.enable()
CollectionOfReplacements.disable()
2018-11-06 09:58:36 -08:00
John McCall
c1b31b4e2f Make the general path of emitRValueForStorageLoad use LValues.
Beyond just being better code, this also fixes problems where
the duplicate code didn't handle e.g. _read accessors.

I believe this finishes unblocking _read in the stdlib (rdar://45230566).
2018-11-06 01:58:59 -05:00
John McCall
71e2e8eae8 [NFC] Move some storage-access code into SILGenLValue. 2018-11-06 01:58:59 -05:00
John McCall
90ca9fe8b4 Fix a bunch of minor issues with read accessors. 2018-11-05 20:57:58 -05:00
John McCall
7da688d75a Always manage subobject projections with formal-access cleanups.
To make that work, enter appropriate scopes (ArgumentScopes and
FormalEvaluationScopes) at a bunch of places.  But note that l-value
emission generally can't enter such a scope, so in generic routines
like emitOpenExistentialExpr we have to just assert that we're
already in a scope.
2018-11-03 02:14:06 -04:00
John McCall
abdba1d3f4 Change the integer-literal type from Int2048 to IntLiteral.
Part of SR-290.
2018-10-31 23:14:58 -04:00
John McCall
3162b513a9 Strengthen some assertions with cleanup scopes; NFC. 2018-10-25 21:50:00 -07:00
Vinicius Vendramini
b61df445ae Cleans up calls to print/dump for the AST Dumper
The `Stmt` and `Expr` classes had both `dump` and `print` methods that behaved similarly, making it unclear what each method was for. Following a conversation in https://forums.swift.org/t/unifying-printing-logic-in-astdumper/15995/6 the `dump` methods will be used to print the S-Expression-like ASTs, and the `print` methods will be used to print the more textual ASTPrinter-based representations. The `Stmt` and `Expr` classes seem to be where this distinction was more ambiguous. These changes should fix that ambiguity.

A few other classes also have `print` methods used to print straightforward representations that are neither the S-Expressions nor ASTPrinters. These were left as they are, as they don't cause the same ambiguity.

It should be noted that the ASTPrinter implementations themselves haven't yet been finished and aren't a part of these changes.
2018-10-22 16:04:02 -03:00
Joe Groff
dab6891573 SILGen: Borrow the base of accessor LValueComponents.
The same base value is necessary to invoke other accessors as part of the same access, but we would end up consuming it as part of materializing the base value for calls into nonmutating setters.
Fixes SR-8990 | rdar://problem/45274900.
2018-10-15 20:08:17 -07:00
John McCall
6f1fd82986 Optimize read accessors to just borrow yielded storage refs. 2018-10-09 15:34:54 -04:00
John McCall
aface462a5 Merge pull request #19558 from rjmccall/lvalue-improvements
Don't materialize l-value base components unnecessarily
2018-09-26 16:21:33 -04:00
John McCall
4e83a62c43 Don't always materialize a temporary for l-value base values. 2018-09-26 12:05:56 -04:00
Slava Pestov
53ff62ee40 SILGen: Simplify getBaseFormalType() 2018-09-25 23:12:46 -07:00
John McCall
e24964c035 Unify some LValue path component APIs; NFC. 2018-09-26 01:23:46 -04:00
Slava Pestov
493491b614 SILGen: Remove ArgumentSource::getSubstType()
This pushes an InOutType usage down to PreparedArguments, which still
needs to be refactored to use AnyFunctionType::Param.
2018-09-14 13:37:43 -07:00
Saleem Abdulrasool
d281b98220 litter the tree with llvm_unreachable
This silences the instances of the warning from Visual Studio about not all
codepaths returning a value.  This makes the output more readable and less
likely to lose useful warnings.  NFC.
2018-09-13 15:26:14 -07:00
John McCall
1bc0a5b2fc Pass along the base value in the end_access writeback for diagnostics.
rdar://44147745
2018-09-05 17:02:29 -04:00
John McCall
dd77a2037e Pass the indices for writeback-conflict diagnostics on coroutines.
To do this, I had to introduce a way to unsafely copy an argument
list for the purposes of diagnostics.

rdar:://43802132
2018-08-31 04:20:47 -04:00
John McCall
6d4c724101 Distinguish different kinds of l-value reads in SILGen.
This is NFC for now, but I plan to build on this to (1) immediately
remove some unnecessary materialization and loads of the base value
and (2) to allow clients to load a borrowed value.
2018-08-30 19:42:53 -04:00
John McCall
41e4c454a1 Simplify getting a SILDeclRef for an accessor. 2018-08-30 19:42:53 -04:00
John McCall
b80618fc80 Replace materializeForSet with the modify coroutine.
Most of this patch is just removing special cases for materializeForSet
or other fairly mechanical replacements.  Unfortunately, the rest is
still a fairly big change, and not one that can be easily split apart
because of the quite reasonable reliance on metaprogramming throughout
the compiler.  And, of course, there are a bunch of test updates that
have to be sync'ed with the actual change to code-generation.

This is SR-7134.
2018-08-27 03:24:43 -04:00
John McCall
7eb703bd74 Switch subscript index emission to use SILGenApply. NFC.
As always, most of the work here went into working around the AST
representations of parameter and argument lists.
2018-08-27 02:14:21 -04:00
Andrew Trick
7f902f5667 [SILGen] force immeidate LValue assignment cleanups.
In SILGenFunction::emitAssignToLValue, use an ArgumentScope instead of
a FormalEvaluationScope. This ensures that the lifetime of objects
needed to materialize each LValue component do not overlap.

For example in this tuple assignment:

public func testTupleAssign(x: inout [Int]) {
  (x[0], x[1]) = (0, 1)
}

Array.subscript.nativeOwningMutableAddressor keeps a reference to the
array across the assignment, unnecessarily forcing a copy of the
array.

Fixes SR-8621: SILGen creates destroys for tuple assignment at the
wrong places.
2018-08-24 16:39:43 -07:00
Erik Eckstein
6ba45473df Remove the pinning addressors
It was used for Array + related types.
With exclusivity checking the pinned addressors are not useful anymore.

rdar://problem/35401528
2018-08-23 12:47:56 -07:00
John McCall
fae2ec3b38 Assorted minor improvements to the cleanup system. 2018-08-22 01:55:10 -04: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
Joe Groff
76534e1a58 SILGen: Fix ordering of key path application writebacks with other lvalue components.
Leaving the owner pointer returned by the runtime function to get released at scope end was incorrect, since this would lead to writebacks accrued during key path traversal happening out of sequence with non-key-path components in the same formal access. Add them as "writebacks" to the formal evaluation scope so that they get destroyed in proper order relative to adjacent lvalue components, fixing SR-7695 | rdar://problem/40295854.
2018-07-30 13:51:47 -07: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
swift-ci
48eb400a93 Merge pull request #17826 from brentdax/public-optional 2018-07-20 14:12:55 -07:00
John McCall
2d5276d84d Clean up l-value emission in SILGen.
There were several bits of code which were unnecessarily
repeating the core logic of breaking down an access strategy
and either setting up an LValue or directly emitting it.
These places have now been unified to just create and then
load or othrwise use an LValue.

Introduce a visitor which handles the common parts of breaking
down an access strategy and computing information like the
LValueTypeData.  In addition to its direct benefits (which are
somewhat lost in the boilerplate of capturing local state into
the visitor subclass), this eliminates some of the ad-hocness
of how the various emission paths use AccessStrategy.

Finally, implement the MaterializeToTemporary strategy in its
full generality by using the actual read and write sub-strategies
instead of always falling back on calling the getter and setter.
This part is not NFC because it causes us to perform the read
part of a read/write to a stored-with-observers property by
directly accessing the storage instead of calling the getter.
2018-07-19 22:06:04 -04:00
Brent Royal-Gordon
b6e35038b2 [SILGen] Output a different message for failed IUO force-unwraps
Modifies SILGen and the `Swift._diagnoseUnexpectedNilOptional` call to print a slightly different message for force unwraps which were implicitly inserted by the compiler for IUOs. The message is chosen based on the presence of certain flags in the `ForceValueExpr`, not on the type of the value being unwrapped.
2018-07-12 19:09:56 -07:00
John McCall
34b0cbc11d Merge pull request #16237 from davezarzycki/metaprogram_ref_storage_types
[AST] NFC: Enable reference storage type meta-programming
2018-07-05 14:45:38 -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
John McCall
65042f9662 Move l-value code for non-member VarDecls into SILGenValue. NFC. 2018-07-03 16:46:03 -04:00
David Zarzycki
057bbb366a [IRGen] Adopt reference storage type meta-programming macros
This commit also fixes reference storage extra inhabitant bugs.
2018-06-30 11:48:47 -04:00
David Zarzycki
54e85ba49a [SILGen] NFC: Adopt reference storage type meta-programming macros 2018-06-30 06:44:33 -04:00
John McCall
9bee3cac5a Generalize storage implementations to support generalized accessors.
The storage kind has been replaced with three separate "impl kinds",
one for each of the basic access kinds (read, write, and read/write).
This makes it far easier to mix-and-match implementations of different
accessors, as well as subtleties like implementing both a setter
and an independent read/write operation.

AccessStrategy has become a bit more explicit about how exactly the
access should be implemented.  For example, the accessor-based kinds
now carry the exact accessor intended to be used.  Also, I've shifted
responsibilities slightly between AccessStrategy and AccessSemantics
so that AccessSemantics::Ordinary can be used except in the sorts of
semantic-bypasses that accessor synthesis wants.  This requires
knowing the correct DC of the access when computing the access strategy;
the upshot is that SILGenFunction now needs a DC.

Accessor synthesis has been reworked so that only the declarations are
built immediately; body synthesis can be safely delayed out of the main
decl-checking path.  This caused a large number of ramifications,
especially for lazy properties, and greatly inflated the size of this
patch.  That is... really regrettable.  The impetus for changing this
was necessity: I needed to rework accessor synthesis to end its reliance
on distinctions like Stored vs. StoredWithTrivialAccessors, and those
fixes were exposing serious re-entrancy problems, and fixing that... well.
Breaking the fixes apart at this point would be a serious endeavor.
2018-06-30 05:19:03 -04:00
Andrew Trick
4353e27db2 Exclusivity access marker verification. Handle Unsafe access.
Now that SILGen change adds Unsafe access markers to addressors and
materializeForSet, we can use that as a sentinel to enable strict
verification everywhere.
2018-06-28 23:25:07 -07:00