Commit Graph

1246 Commits

Author SHA1 Message Date
John McCall
e14f2bc0c7 [NFC] Add a method to just ask if a tuple AP vanishes under substitution 2023-06-29 19:39:51 -04:00
Erik Eckstein
6b1697eb06 use new llvm::Optional APIs to fix deprecation warnings 2023-06-28 14:28:38 +02:00
Evan Wilde
250082df25 [NFC] Reformat all the LLVMs
Reformatting everything now that we have `llvm` namespaces. I've
separated this from the main commit to help manage merge-conflicts and
for making it a bit easier to read the mega-patch.
2023-06-27 09:03:52 -07:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Joe Groff
ca7b8e2890 Merge pull request #66445 from jckarter/noncopyable-computed-property-address-only-base
SILGen: Don't copy a borrowed noncopyable address-only base of a computed property access.
2023-06-08 13:42:41 -07:00
Joe Groff
f06621907e SILGen: Don't copy a borrowed noncopyable address-only base of a computed property access.
We can probably avoid this copy in more circumstances, but make the change only for
noncopyable types for now, since that's the case where it's most semantically apparent.
rdar://109161396
2023-06-08 08:36:04 -07:00
Michael Gottesman
59c8cff917 [borrowing] Add support for borrowing/consuming copyable types to be a noimplicitcopy type.
rdar://108383660
2023-06-06 18:12:29 -04:00
Adrian Prantl
958a1577b5 Ensure calls to getters have a source location.
Calls to getters are implicit because the compiler inserts them on a property
access, but the location is useful in backtraces so it should be preserved.

rdar://109123395
2023-05-31 17:41:26 -07:00
Michael Gottesman
2b785e4a98 [move-only] Teach SILGen how to recognize a borrowed read subscript when the type also has a modify.
The form of the AST changes slightly when a type has a read and a modify.
Specifically, we now have a load on the subscript and an inout_expr on the base.
I dealt with this by making the inout_expr something that when we look for
storage we look through and by tweaking the load lookthrough code.
2023-05-26 15:31:35 -07:00
Michael Gottesman
7b66c70bda [move-only] Restrict 508bf8ae21 so it only applies if a subscript has a _read accessor.
We want the result of getters to still be separate values.
2023-05-26 12:34:53 -07:00
Michael Gottesman
508bf8ae21 [move-only] Teach SILGenApply how to emit subscripts with borrowed base values.
I also added a bunch of tests that showed the behavior of subscripts/other accessors with the following combinations of semantics:

1. get only.
2. get/set.
3. get/modify.
4. read/set.
5. read/modify.

rdar://109746476
2023-05-25 14:57:32 -07:00
Michael Gottesman
81a09b384e When using a store_borrow for borrowed things, make sure to only store_borrow if we are using lowered addresses.
When opaque values are enabled, we do not need this store_borrow. This was
caught by the following tests:

  Swift(macosx-x86_64) :: SILGen/opaque_values_silgen.swift
  Swift(macosx-x86_64) :: SILGen/opaque_values_silgen_resilient.swift
2023-05-11 12:44:04 -07:00
Michael Gottesman
59fdfaddd1 [move-only] Spill noncopyable types that are objects using store_borrow if we call a resilient function that takes it in_guaranteed.
This ensures that given a class that contains a noncopyable type that contains
another noncopyable type:

```
@_moveOnly struct S2 {}
@_moveOnly struct S { var s2: S2 }
class C { var s: S }
```

if we call a resilient function that takes C.S.S2:

```
borrowVal(c.s.s2)
```

we properly spill s2 onto the stack using a store_borrow.

Why Do This?
------------

Currently SILGenLValue treats ref_element_addr as a base that it needs to load
from for both copyable and non-copyable types. We keep a separation of concerns
and require emission of resilient functions to handle these loaded values. For
copyable types this means copying the value and storing it into a temporary
stack allocation. For noncopyable types, we never actually implemented this so
we would hit an error in SILGenApply telling us that our resilient function
expected an address argument, but we are passing an object.

To work around this, I updated how we emit borrowed lvalue arguments to in this
case to spill the value into a temporary allocation using a store_borrow. I also
included a test that validates that we properly have a read exclusivity scope
around the original loaded from memory for the entire call site so even though
we are performing a load_borrow and then spilling it, we still have read
exclusivity to the original memory for the entire region meaning that we still
preserve the semantics.

rdar://109171001
2023-05-10 14:12:34 -07:00
Allan Shortlidge
65de2a5a9d SILGen: In unavailable stubs apply diagnostic func's back deployment thunk.
Part of rdar://107388493.
2023-05-03 15:19:32 -07:00
Allan Shortlidge
d1416ddd56 SILGen: Stub unavailable functions.
When `-unavailable-decl-optimization=stub` is specified, insert a call to
`_diagnoseUnavailableCodeReached()` at the beginning of the function to cause
it to trap if executed at run time.

Part of rdar://107388493
2023-05-03 15:19:31 -07:00
Michael Gottesman
438974a2b9 [move-only] When emitting borrows for move only types, use a load [copy] instead of a load_borrow.
The reason why I am doing this is that otherwise if one has a function that
takes both a guaranteed and an owned parameter, we will break OSSA invariants
since the load [take] will invalidate the load_borrow. So instead, we put in a
load_borrow knowing that the move checker will convert it to a load_borrow
assuming that the two pass exclusivity checking.

NOTE: Because of some missing functionality in subsequent tests, I had to
disable one test (moveonly_escaping_definite_initialization.swift) and also add
some checks for copy of noncopyable object errors. They will go away in the next
2 commits.

rdar://108510987
2023-04-25 10:51:03 -07:00
John McCall
65510fb7fe Handle vanishing tuples correctly (or more correctly) in arg emission. 2023-04-03 23:17:24 -04:00
John McCall
df82443138 Count lowered parameters for a formal parameter using just the
AbstractionPattern.

The old logic was wrong for vanishing tuples: a non-tuple substituted
type might correspond to multiple lowered parameters if it's the result
of substituting into a vanishing tuple that also contains empty pack
expansions.

Test to follow later in this PR.
2023-04-03 23:04:19 -04:00
John McCall
debc8d9ebd [NFC] Move forEachTupleElement to use a generator 2023-03-22 22:04:36 -04:00
John McCall
d18b914fc3 Do a proper orig+subst walk of tuple expression elements in call
argument emission.
2023-03-22 15:40:02 -04:00
John McCall
cd3765c9fa Reabstract pack values when passing them as arguments 2023-03-17 22:07:11 -04:00
John McCall
4499e3d055 [NFC] Introduce new APIs for traversing orig/subst parameters in parallel 2023-03-16 01:38:46 -04:00
John McCall
9ab4dc494c [NFC] Add better APIs for parallel destructuring of orig+subst types
As I've been iterating on this work, I've been gradually mulling these
over, and I think this is the way to go for now.  These should make it
a lot less cumbersome to write these kinds of traversals correctly.

The intent is to the sunset the existing expanded-components stuff
after I do a similar pass for function parameters.
2023-03-16 00:19:30 -04:00
Holly Borla
dce70f373f [SILGen] Emit MaterializePackExprs.
The subexpression of a MaterializePackExpr (which is always a tuple value
currently) is emitted while preparing to emit a pack expansion expr, and its
elements are projected from within the dynamic pack loop. This means that a
materialized pack is only evaluated once, rather than being evaluated on
every iteration over the pack elements.
2023-03-09 21:44:03 -08:00
John McCall
239777aacb Fix parameter binding for tuples containing pack expansions
More missing infrastructure.  In this case, it's really *existing*
missing infrastructure, though; we should have been imploding tuples
this way all along, given that we're doing it in the first place.

I don't like that we're doing all these extra tuple copies.  I'm not
sure yet if they're just coming out of SILGen and eliminated immediately
after in practice; maybe so.  Still, it should be obvious that they're
unnecessary.
2023-03-09 02:28:29 -05:00
John McCall
81f11c19ab Implement the caller side of return types containing variadic packs
This is all relatively nicely abstracted, which is not to say that
it didn't take an awful lot of plumbing to get it to work.  The basic
problem here is inherent: we need to do component-specific setup and
teardown, and unfortunately in the current representation we have to
do that with separate loops and without any dominance relationships.
(This is the same thing preventing us from doing borrows in the
general case.)  The result is that the general case of result emission
is to emit every element of the expansion into a temporary tuple
(requiring a pack loop before the call to initialize the pack), then
process those elements in the body of a second pack loop after the
call.  And that's terrible enough that we really have to do the work
to try to avoid it, which makes all the APIs more complicated.

Anyway, most of the way through the basic plumbing for variadic
generics now.  Next is reabstraction, I think, which I hope will
mostly mean fixing bugs in the infrastructure I've already written.
2023-03-07 03:15:31 -05:00
John McCall
81d9e6865a Add a couple convenience APIs for working with abstraction patterns 2023-03-07 03:15:31 -05:00
John McCall
6c066502a2 Add an API to map contextual types from the pack to element environment
I'm not really convinced that the existing implementation here is
correct in general; it might work for the type checker's use cases,
but I don't think we can rely on not seeing opened element archetypes
from other expansions in the type we're processing here.  But we can
at least tread water while offering a more convenient API.
2023-03-07 03:15:31 -05:00
John McCall
157be3420c Implement the callee side of returning a tuple containing a pack expansion.
This required quite a bit of infrastructure for emitting this kind of
tuple expression, although I'm not going to claim they really work yet;
in particular, I know the RValue constructor is going to try to explode
them, which it really shouldn't.

It also doesn't include the caller side of returns, for which I'll need
to teach ResultPlan to do the new abstraction-pattern walk.  But that's
next.
2023-03-06 04:26:18 -05:00
John McCall
b3b90a82be Merge pull request #64048 from rjmccall/variadic-pack-expansion-arguments
Implement the emission of pack expansion arguments in SILGen
2023-03-03 11:46:00 -05:00
swift-ci
d0f88433da Merge pull request #63822 from kavon/resilient-noncopyable
Support resilient properties of move-only type
2023-03-03 01:24:37 -08:00
John McCall
06a7468e4f Implement the emission of pack expansion arguments in SILGen
Mostly fixing some existing code.
2023-03-03 02:52:32 -05:00
Kavon Farvardin
60772f17aa avoid copying from begin_apply when _read-ing a move-only type
When an expression accessing a move-only type through a `_read`
accessor, it can only be borrowed. While a simple access like
`someClass.moveOnlyField` would get recognized in SILGen and properly
borrowed, if you did any further access off of that then you'd get an
illegal sequence involving a copy that is hard to eliminate. In
particular, if you wrote `someClass.moveOnlyField.method()` then we
would emit something like:

```
(%borrowedMO, %coro) = begin_apply #SomeClass.moveOnlyField!.read(...)
%copiedMO = copy_value %borrowedMO
end_apply %coro
= apply #MoveOnlyType.method(%copiedMO) // param is just @guaranteed
```

That's wrong, since we need to use the borrow to call the method, but
that borrow's lifetime ends at the `end_apply`.

Turns out the fix is rather subtle. The reason the access above wouldn't
work is that it did _not_ have a `LoadExpr` around it, which is what the
code in the `ArgEmitter` was expecting when doing an ad-hoc check to see
if it needs to emit as a borrow.
2023-03-02 15:14:24 -08:00
Joe Groff
17c803724f Implement consuming and borrowing declaration-level modifiers from SE-0377.
`borrowing func`/`consuming func` control the ownership convention of `self` for
methods.
2023-03-01 11:58:59 -08:00
John McCall
fb9578133b Steps towards supporting pack expansions properly in signature
lowering and argument emission.

Pack expansions in argument emission don't work yet, but I wanted
to land this bit of incremental progress.
2023-02-24 18:34:52 -05:00
Allan Shortlidge
f1a8740ba5 AST: Only treat @backDeployed functions as fragile on platforms with an active attribute.
Previously, typechecking and SILGen would treat a function body as fragile as long as the declaration had a `@backDeployed` attribute, regardless of the platform specified by the attribute. This was overly conservative since back deployed functions are only emitted into the client on specific platforms. Now a `@backDeployed` function can reference non-`public` declarations on the platforms it is resilient on:

```
@backDeployed(before: iOS 15)
public func foo() {
  #if os(iOS)
  // Fragile; this code may be emitted into the client.
  #else
  // Resilient; this code won't ever be exposed to clients.
  #endif
}
```

Resolves rdar://105298520
2023-02-23 10:39:42 -08:00
Joe Groff
69e4b95fb8 SIL: Model noescape partial_applys with ownership in OSSA.
Although nonescaping closures are representationally trivial pointers to their
on-stack context, it is useful to model them as borrowing their captures, which
allows for checking correct use of move-only values across the closure, and
lets us model the lifetime dependence between a closure and its captures without
an ad-hoc web of `mark_dependence` instructions.

During ownership elimination, We eliminate copy/destroy_value instructions and
end the partial_apply's lifetime with an explicit dealloc_stack as before,
for compatibility with existing IRGen and non-OSSA aware passes.
2023-02-16 21:43:53 -08:00
Michael Gottesman
5acb6c939a [move-only] Perform an exclusive borrow when passing a var to a consuming var.
Consider the following example:

```
class Klass {}

@_moveOnly struct Butt {
  var k = Klass()
}

func mixedUse(_: inout Butt, _: __owned Butt) {}

func foo() {
    var y = Butt()
    mixedUse(&y, y)
}
```

In this case, we want to have an exclusivity violation. Before this patch, we
did a by-value load [copy] of y and then performed the inout access. Since the
access scopes did not overlap, we would not get an exclusivity violation.
Additionally, since the checker assumes that exclusivity violations will be
caught in such a situation, we convert the load [copy] to a load [take] causing
a later memory lifetime violation as seen in the following SIL:

```
sil hidden [ossa] @$s4test3fooyyF : $@convention(thin) () -> () {
bb0:
  %0 = alloc_stack [lexical] $Butt, var, name "y" // users: %4, %5, %8, %12, %13
  %1 = metatype $@thin Butt.Type                  // user: %3
  // function_ref Butt.init()
  %2 = function_ref @$s4test4ButtVACycfC : $@convention(method) (@thin Butt.Type) -> @owned Butt // user: %3
  %3 = apply %2(%1) : $@convention(method) (@thin Butt.Type) -> @owned Butt // user: %4
  store %3 to [init] %0 : $*Butt                  // id: %4
  %5 = begin_access [modify] [static] %0 : $*Butt // users: %7, %6
  %6 = load [take] %5 : $*Butt                    // user: %10                // <————————— This was a load [copy].
  end_access %5 : $*Butt                          // id: %7
  %8 = begin_access [modify] [static] %0 : $*Butt // users: %11, %10
  // function_ref mixedUse2(_:_:)
  %9 = function_ref @$s4test9mixedUse2yyAA4ButtVz_ADntF : $@convention(thin) (@inout Butt, @owned Butt) -> () // user: %10
  %10 = apply %9(%8, %6) : $@convention(thin) (@inout Butt, @owned Butt) -> ()
  end_access %8 : $*Butt                          // id: %11
  destroy_addr %0 : $*Butt                        // id: %12
  dealloc_stack %0 : $*Butt                       // id: %13
  %14 = tuple ()                                  // user: %15
  return %14 : $()                                // id: %15
} // end sil function '$s4test3fooyyF'
```

Now, instead we create a [consume] access and get the nice exclusivity error we
are looking for.

NOTE: As part of this I needed to tweak the verifier so that [deinit] accesses
are now allowed to have any form of access enforcement before we are in
LoweredSIL. I left in the original verifier error in LoweredSIL and additionally
left in the original error in IRGen. The reason why I am doing this is that I
need the deinit access to represent semantically what consuming from a
ref_element_addr, global, or escaping mutable var look like at the SIL level so
that the move checker can error upon it. Since we will error upon such
consumptions in Canonical SIL, such code patterns will never actually hit
Lowered/IRGen SIL, so it is safe to do so (and the verifier/errors will help us
if we make any mistakes). In the case of a non-escaping var though, we will be
able to use deinit statically and the move checker will make sure that it is not
reused before it is reinitialized.

rdar://101767439
2023-02-10 19:43:58 -08:00
Allan Shortlidge
d2524a6de8 AST: Implement parsing support for the accepted spelling of @backDeployed for SE-0376.
For source compatibility `@_backDeploy` continues to be accepted as a spelling.

rdar://102792909
2023-02-01 22:04:33 -08:00
John McCall
d25a8aec8b Add explicit lowering for value packs and pack expansions.
- SILPackType carries whether the elements are stored directly
  in the pack, which we're not currently using in the lowering,
  but it's probably something we'll want in the final ABI.
  Having this also makes it clear that we're doing the right
  thing with substitution and element lowering.  I also toyed
  with making this a scalar type, which made it necessary in
  various places, although eventually I pulled back to the
  design where we always use packs as addresses.

- Pack boundaries are a core ABI concept, so the lowering has
  to wrap parameter pack expansions up as packs.  There are huge
  unimplemented holes here where the abstraction pattern will
  need to tell us how many elements to gather into the pack,
  but a naive approach is good enough to get things off the
  ground.

- Pack conventions are related to the existing parameter and
  result conventions, but they're different on enough grounds
  that they deserve to be separated.
2023-01-29 03:29:06 -05:00
Allan Shortlidge
80295fdaa1 SILGen: Avoid using back deployment thunks for high enough deployment targets.
If a function body references a declaration with the `@_backDeploy(before:)` attribute and that function body will only execute on deployment targets for which the ABI version of the decl is available then it is unnecessary to thunk the reference to the decl. Function bodies that may be emitted into other modules (e.g. `@inlinable`) must always use the thunk.

Resolves rdar://90729799
2023-01-25 17:22:23 -08:00
Michael Gottesman
ef98a3cfad [borrow-operator] Add initial support for applying _borrow to self when calling a method.
Example: (_borrow f).callFunction()

This still does not support _borrow for get/set and more general accessors. That
change will be coming in a forthcoming commit.

rdar://103888591
2023-01-04 14:32:20 -08:00
Michael Gottesman
94f1391fbe [borrow-expr] Wire up borrow expr to SILGenApply. 2022-12-22 13:02:04 -08:00
Nate Chandler
8d8577e5b0 [SIL] Removed Indirect_In_Constant convention.
It is no different from @in.

Continue parse @in_constant in textual and serialized SIL, but just as
an alias for @in.
2022-12-09 21:54:00 -08:00
Joe Groff
d9c4bed487 Merge pull request #62312 from jckarter/mix-read-or-address-with-setter-reabstraction
SILGen: Match up abstraction level when materializing mixed set and read/unsafeAddress properties.
2022-11-30 14:51:22 -08:00
Joe Groff
d1a9f9a6b3 SILGen: Match up abstraction level when materializing mixed set and read/unsafeAddress properties.
Addresses part of rdar://102525437.
2022-11-29 21:24:32 -08:00
Michael Gottesman
d03ee03eb9 Merge pull request #62287 from gottesmm/pr-74833c7154cdbcf5812c7deb2677c4e1f78607a2
[move-only] Borrow copyable typed arguments contained within a move only struct/enum.
2022-11-29 04:46:46 -08:00
Michael Gottesman
ec9db3ffbb [move-only] Borrow copyable typed arguments contained within a move only struct/enum.
Otherwise, we down the normal load [copy] path which will cause us to have a
tight exclusivity scope.

One thing to note is that if one accesses a field of a moveonly class one will
still get the tight exclusivity scope issue since SILGenLValue will emit the
moveonly class as a base of the field causing us to use the non-borrow codegen.

rdar://102746971
2022-11-28 20:32:15 -08:00
Erik Eckstein
ab1b343dad use new llvm::Optional API
`getValue` -> `value`
`getValueOr` -> `value_or`
`hasValue` -> `has_value`
`map` -> `transform`

The old API will be deprecated in the rebranch.
To avoid merge conflicts, use the new API already in the main branch.

rdar://102362022
2022-11-21 19:44:24 +01:00
Erik Eckstein
1f4ac2b542 A few more deprecated iterator fixes
std::iterator is deprecated in C++17
2022-11-14 20:37:00 +01:00