Commit Graph

330 Commits

Author SHA1 Message Date
Slava Pestov
9ebb5f2e03 AST: Rename VarDecl::getType() to VarDecl::getTypeInContext()
This is a futile attempt to discourage future use of getType() by
giving it a "scary" name.

We want people to use getInterfaceType() like with the other decl kinds.
2023-08-04 14:19:25 -04:00
Joe Groff
9b783560ad Fix @_rawLayout initialization to avoid spurious lifetime ends.
We can't really treat them as always-initialized because that makes move checking
think that there's a value to destroy even on initialization, causing deinits to
run on uninitialized memory. Remove my previous hack, and use a `zeroInitializer`
to initialize the value state when emitting `init`, which is where we really need
the bootstrapping-into-initialized behavior. rdar://113057256
2023-08-01 08:34:02 -07:00
Pavel Yaskevich
8af81a39ce Merge pull request #67585 from xedin/rdar-112984795
[SILGenConstructor] InitAccessors: Make sure that accessed fields are initialized before init accessors
2023-07-28 08:26:53 -07:00
Pavel Yaskevich
f6017cc69f [SILGenConstructor] InitAccessors: Make sure that accessed fields are initialized before init accessors
Initializations for all of the fields accessed by init accessor
should be emitted before init accessor property even if they are
declared after it in the source order.

Resolves: rdar://112984795
2023-07-27 15:25:52 -07:00
Pavel Yaskevich
3bb81e3c63 [SILGenConstructor] NFC: Extract initialization of individual fields into a method
This is a preliminary step in order to enable re-ordering of initializations
when init accessors are involved.
2023-07-27 15:22:14 -07:00
Joe Groff
aee071bf4e Introduce an experimental @_rawLayout attribute.
This attribute can be attached to a noncopyable struct to specify that its
storage is raw, meaning the type definition is (with some limitations)
able to do as it pleases with the storage. This provides a basis for
implementing types for things like atomics, locks, and data structures
that use inline storage to store conditionally-initialized values.
The example in `test/Prototypes/UnfairLock.swift` demonstrates the use
of a raw layout type to wrap Darwin's `os_unfair_lock` APIs, allowing
a lock value to be stored inside of classes or other types without
needing a separate allocation, and using the borrow model to enforce
safe access to lock-guarded storage.
2023-07-24 14:28:19 -07:00
Pavel Yaskevich
aa52e42b3e [SILGen] InitAccessors: Start emitting init property initialization expressions
Similar to regular stored properties emit initialization expressions
for properties with init accessors at constructor's prolog.
2023-07-18 17:19:42 -07:00
Pavel Yaskevich
eeb70cfbf4 [SILGen] InitAccessors: Lower initializes/accesses property types before using them in init accessor type 2023-07-14 13:45:02 -07:00
Pavel Yaskevich
28a39d6df1 [AST] NFC: Add a way to request initialized/accesses properties from init accessor 2023-07-07 10:00:36 -07:00
Erik Eckstein
b08710d911 SIL: add a bare attribute to alloc_ref
The `bare` attribute indicates that the object header is not used throughout the lifetime of the object.
This means, no reference counting operations are performed on the object and its metadata is not used.
The header of bare objects doesn't need to be initialized.
2023-06-29 06:57:05 +02:00
Doug Gregor
fe2dec5ee5 [SE-0400] All properties with init accessors become part of the memberwise init
Per the clarification during the review thread, all properties with
init accessors (including those that do not initialize any underlying
storage) are part of the memberwise initializer.
2023-06-27 17:28:53 -07: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
Doug Gregor
e89fdd3084 Requestify the computation of the list of memberwise initialized properties.
Fixes rdar://110776763, a case where initialized stored properties
introduced by peer macros weren't working alongside declared stored
properties.
2023-06-23 11:34:52 -07:00
Doug Gregor
e306c11a22 [Macros] Ensure that we visit *all* members when emitting memberwise initializer
Fixes rdar://111122261.
2023-06-21 16:12:11 -07:00
Pavel Yaskevich
f58d4077c4 [Sema/SILGen] Consider init exprs associated with init accessor properties for memberwise init 2023-06-15 13:12:16 -07:00
John McCall
43f4b9f3c8 Load pack elements in memberwise initializers if they're loadable
SILGen expects this of values before we enter general routines.

Fixes rdar://109911655
2023-06-14 21:29:21 -04:00
Pavel Yaskevich
34c8cf60f1 [Sema/SILGen] InitAccessors: Memberwise initializers with init accessors should follow field order
Skip stored properties that are initialized via init accessors and
emit parameters/initializations in field order which allows us to
cover more use-cases.
2023-06-13 13:56:33 -07:00
Pavel Yaskevich
cffc3fd73d [Sema/SILGen] InitAccessors: Don't synthesize memberwise init if initializes intersect
If some property is initializable by one than one init accessor
let's not sythesize a memberwise initializer in that case because
it's ambiguous what is the best init accessor to use.
2023-06-13 12:40:41 -07:00
Pavel Yaskevich
ddcfe01ba5 [Sema/SILGen] InitAccessors: Emit intersecting init accessor calls in memberwise init
If init accessor initialize the same properties, let's emit them
in sequence and emit `destroy_addr` in-between to make sure that
there is no double initialization.
2023-06-13 10:58:50 -07:00
Pavel Yaskevich
ad95e911a8 [Sema/SIL] InitAccessors: Incorporate init accessors into memberwise initializers
Properties with init accessors are used in place of properties they
are supposed to initialize in order of stored properties.
2023-06-06 18:59:46 -07:00
Pavel Yaskevich
6c0e3914b8 [SILGen] InitAccessor: Emit init accessor function
Emit all initializes/accesses properties as arguments
in custom prolog and map them back to the originating
property declarations.
2023-06-06 18:59:13 -07:00
Slava Pestov
f61d8c5573 SILGen: Simplify getSubstitutionsForPropertyInitializer() 2023-06-01 22:36:24 -04:00
Allan Shortlidge
235922d5ae SILGen: Stub unavailable protocol witness thunks.
Part of rdar://107388493
2023-05-03 15:19:31 -07:00
Allan Shortlidge
8bc1112029 SILGen: Stub unavailable constructors and destructors.
Part of rdar://107388493
2023-05-03 15:19:31 -07:00
Doug Gregor
593c2364e8 [Macros] "Subsume" the initializer when an accessor macros adds non-observers
When an accessor macro adds a non-observing accessor to a property, it
subsumes the initializer. We had previously modeled this as removing
the initializer, but doing so means that the initializer could not be
used for type inference and was lost in the AST.

Explicitly mark the initializer as "subsumed" here, and be more
careful when querying the initializer to distinguish between "the
initializer that was written" and "the initializer that will execute"
in more places. This distinction already existed at the
pattern-binding level, but not at the variable-declaration level.

This is the proper fix for the circular reference issue described in
rdar://108565923 (test case in the prior commit).
2023-04-28 09:50:00 -07:00
Michael Gottesman
19f1dedf1f [move-only] When emitting an initializer for an empty struct, store an instance to make sure it is initialized.
I ran into this while fixing the parent commit when attempting to add the
interpreter test in this commit into the aforementioned parent commit.

rdar://107974302
2023-04-12 20:56:31 -07:00
Adrian Prantl
158772c2ab Rebase SILScope generation on top of ASTScope.
This patch replaces the stateful generation of SILScope information in
SILGenFunction with data derived from the ASTScope hierarchy, which should be
100% in sync with the scopes needed for local variables. The goal is to
eliminate the surprising effects that the stack of cleanup operations can have
on the current state of SILBuilder leading to a fully deterministic (in the
sense of: predictible by a human) association of SILDebugScopes with
SILInstructions. The patch also eliminates the need to many workarounds. There
are still some accomodations for several Sema transformation passes such as
ResultBuilders, which don't correctly update the source locations when moving
around nodes. If these were implemented as macros, this problem would disappear.

This necessary rewrite of the macro scope handling included in this patch also
adds proper support nested macro expansions.

This fixes

rdar://88274783

and either fixes or at least partially addresses the following:

rdar://89252827
rdar://105186946
rdar://105757810
rdar://105997826
rdar://105102288
2023-04-04 15:20:11 -07:00
Joe Groff
8e21bfcc47 MoveOnlyAddressChecker: Confine analysis to current formal access.
Code can only locally interact with a mutable memory location within a
formal access, and is only responsible for maintaining its invariants
during that access, so the move-only address checker does not need to,
and should not, observe operations that occur outside of the access
marked with the `mark_must_check` instruction. And for immutable
memory locations, although there are no explicit formal accesses, that's
because every access must be read-only, so although individual
accesses are not delimited, they are all compatible as far as
move-only checking is concerned. So we can back out the changes to SILGen
to re-project a memory location from its origin on every access, a
change which breaks invariants assumed by other SIL passes.
2023-04-02 16:33:57 -07:00
John McCall
481f9c7362 Fix memberwise initializers for structs with variadic-tuple fields 2023-03-22 22:04:37 -04:00
John McCall
4a8a3ac454 [NFC] Use a generator in prolog emission to generate parameters.
This adds an assertion that we're using all of the parameters, so
pass prolog emission the number of lowered parameters to ignore.
That's easy for the callers to provide, since they do actually
still need to add function arguments for those parameters.
2023-03-20 20:06:08 -04:00
Konrad `ktoso` Malawski
41f99fc2ae [Executors][Distributed] custom executors for distributed actor (#64237)
* [Executors][Distributed] custom executors for distributed actor

* harden ordering guarantees of synthesised fields

* the issue was that a non-default actor must implement the is remote check differently

* NonDefaultDistributedActor to complete support and remote flag handling

* invoke nonDefaultDistributedActorInitialize when necessary in SILGen

* refactor inline assertion into method

* cleanup

* [Executors][Distributed] Update module version for NonDefaultDistributedActor

* Minor docs cleanup

* we solved those fixme's

* add mangling test for non-def-dist-actor
2023-03-15 23:42:55 +09: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
Holly Borla
fa3ffc6a44 [SILGen] Update a few callers of SubstitutionMap::get to use getSingletonPackExpansion,
and update variadic generics SILGen tests for the representation change.
2023-03-06 22:05:11 -08:00
Michael Gottesman
f4e1b2a8f2 [move-only] Update SILGen/MoveCheckers so that vars are emitted in eagerly projected box form.
This is the first slice of bringing up escaping closure support. The support is
based around introducing a new type of SILGen VarLoc: a VarLoc with a box and
without a value. Because the VarLoc only has a box, we have to in SILGen always
eagerly reproject out the address from the box. The reason why I am doing this
is that it makes it easy for the move checker to distinguish in between
different accesses to the box that we want to check separately. As such every
time that we open the box, we insert a mark_must_check
[assignable_but_not_consumable] on that project. If allocbox_to_stack manages to
determine that the box can be stack allocated, we eliminate all of the
mark_must_check and place a new mark_must_check [consumable_and_assignable] on
the alloc_stack.  The end result is that we get the old model that we had before
and also can support escaping closures.
2023-02-20 11:04:21 -08:00
Michael Gottesman
85ea8b5d5b [move-only] Rename CheckKind::NoImplicitCopy -> CheckKind::ConsumableAndAssignable
This fits the name of the check better. The reason I am doing this renaming is
b/c I am going to add a nonconsumable but assignable check for
global_addr/ref_element_addr/captures with var semantics.
2023-02-10 13:46:19 -08:00
Becca Royal-Gordon
7d93b7543d Merge pull request #63534 from beccadax/default-is-not-init-stars
Make objcImpl classes derive inherited inits
2023-02-09 12:02:44 -08:00
Becca Royal-Gordon
31d8cadcd7 Make objcImpl classes derive inherited inits
A class that uses @objcImplementation but does not explicitly declare any designated initializers previously did not override the superclass initializers, so its stored properties would not be initialized. Opt these classes into that logic and adjust it to add the initializers to the @objcImplementation extension instead of the ClassDecl itself.

Fixes rdar://105008242.
2023-02-08 15:58:17 -08:00
Meghana Gupta
75a9137f89 Enclose with begin_access[signed]/end_access during SILGen of implicit value ctor 2023-02-07 00:17:32 -08:00
Allan Shortlidge
efd5c1ecfc SIL: Move the implementation of Lowering::usesObjCAllocator() to the SIL library.
The definition of `Lowering::usesObjCAllocator()` was previously implemented in SILGen, which does not match the library membership of the declaration. The implementation of `SILSymbolVisitor` in the SIL library uses this and since SIL is a dependency of SILGen instead of vice-versa this resulted in a linker error.
2022-11-16 17:25:27 -08:00
Hamish Knight
5a8c14d583 [Profiler] Fix up a couple uses of emitProfilerIncrement
Remove a case where we know we don't have a
profiler, and avoid incrementing for a distributed
factory method. Otherwise such cases could pass
a null ASTNode, which will become an assertion
failure in a future commit.

While we're here, let's standardize on emitting
the profiler increment for function entry after
the prolog, as if there's e.g an unreachable
parameter, the increment can be safely elided
anyway.
2022-10-13 19:42:36 +01:00
Michael Gottesman
65c21b61bf [move-only] Begin implementing support for concrete move only types.
This is just the very beginning... I still need to implement more parts of
SILGen for this. But all great things start small. I am going to iterate on top
of this and just wanted to get some initial parts of the work in as I go.
2022-07-21 15:33:17 -07:00
Kavon Farvardin
6c0cfac69a remove dependence on system parameter in SILGen/DefiniteInit 2022-03-24 16:18:49 -07:00
Konrad `ktoso` Malawski
6d502fc042 [Distributed] Implicit Codable conformance for Dist Actors
[Witness] implement dump() on witness
2022-03-11 18:07:14 +09:00
Arnold Schwaighofer
9f2b6a4ebb Reuse _ContiguousArrayStorage<AnyObject> metadata for any class or objc generic type
Reduces the number of _ContiguousArrayStorage metadata.

In order to support constant time bridging we do need to set the correct
metadata when we bridge to Objective-C. This is so that the type check
succeeds when bridging back from Objective-C to reuse the storage
instance rather than bridging the elements.

To support dynamically setting the `_ContiguousArrayStorage` element
type i needed to add support for optimizing `alloc_ref_dynamic`
throughout the optimizer.

Possible future improvements:
* Use different metadata such that we can disambiguate native Swift
  classes during destruction -- allowing native release rather then unknown
  release usage.
* Optimize the newly added semantic function
  getContiguousArrayStorageType

rdar://86171143
2022-02-16 07:55:34 -08:00
Doug Gregor
a37f291c18 Remove ActorIsolation::DistributedActorInstance.
The distributed case is distinguishable from the non-distributed case
based on the actor type itself for those rare cases where we care. The
vast majority of code is simplified by treating this identically to
`ActorInstance`.
2022-02-09 11:16:23 -08:00
Kavon Farvardin
d610493603 fix bugs with actor inits and flow-isolation
This is a combination of fixes:

- inject hops in self-isolated delegating actor initializers
  after self becomes initialized.

- fix sendable and isolation for convenience inits

- fix bug in distributed actor inits that I introduced when
  implementing flow-isolation.

- fix / add test coverage.
2022-02-02 13:31:14 -07:00
Kavon Farvardin
8432f94cd5 (rdar://87412308) fix missing actor hop for global-actor isolated inits of an actor type.
For global-actor isolated async inits within an actor, we will have separate restrictions
on access to self's differently-isolated stored properties to prevent data races. But,
we also must be on the global actor while in such initializers. The problem was only
that the prologue was missing a hop because of a bad conditional check. Previously
the check assumed that actors cannot have global-actor isolated initializers, but they
can currently.
2022-02-02 13:31:14 -07:00
John McCall
716f4b9e2f Hop to the generic executor in non-actor-isolated async functions.
Async functions are now expected to set ExpectedExecutor in their
prologue (and, generally, immediately hop to it).  I updated the
prologue code for a bunch of function emission, most of which was
uninteresting.  Top-level code was not returning to the main
executor, which is now fixed; fortunately, we weren't assuming
that we were on the main executor yet.

We had some code that only kicked in when an ExpectedExecutor
wasn't set which made us capture the current executor before
a hop and then return to it later.  This code has been removed;
there's no situation in which save-and-return is the semantically
correct thing to do given the possibility of hop optimization.
I suspect it could also have led to crashes if the current
executor is being kept alive only because it's currently running
code.  If we ever add async functions that are supposed to inherit
their caller's executor, we should have the caller pass the right
executor down to it.

This is the first half of SE-0338; the second, sendability
enforcement, is much more complicated, and Doug has volunteered
to do it.

Fixes rdar://79284465, as well as some tests that were XFAILed
on Windows.
2022-01-27 01:53:19 -05:00
zoecarver
fc3b3a1d71 [cxx-interop] Implement foreign reference types.
This is an expiremental feature to allow an attribute, `import_as_ref`, to import a C++ record as a non-reference-counted reference type in Swift.
2021-12-08 15:35:18 +00:00