Commit Graph

412 Commits

Author SHA1 Message Date
Michael Gottesman
37d60a08bb [move-only] Rename mark_must_check -> mark_unresolved_non_copyable_value.
I was originally hoping to reuse mark_must_check for multiple types of checkers.
In practice, this is not what happened... so giving it a name specifically to do
with non copyable types makes more sense and makes the code clearer.

Just a pure rename.
2023-08-30 22:29:30 -07:00
Pavel Yaskevich
e96f9e5b33 [SILGen] InitAccessors: Materialize default argument if it's required by init/setter
We do this in `GetterSetterComponent` but overlooked it for default initialization
expressions. If init/setter require `newValue` to be passed indirectly we have to
make sure to materialize it before handing to `assign_or_init` instruction.

Resolves: rdar://114350227
2023-08-28 17:04:53 -07:00
Pavel Yaskevich
fce0d33714 Merge pull request #68155 from xedin/issue-67827
[SILGen] InitAccessors: Make sure that `assign_or_init` always directly references self
2023-08-28 09:36:53 -07:00
Pavel Yaskevich
10947decec [SILGen] InitAccessors: Make sure that assign_or_init always directly references self
`nonmutating set` gets a copy of "self" in `GetterSetterComponent`
which is expected for partial application of the setter but doesn't
work for "self" reference that `assign_or_init` instruction needs
to emit references to stored properties during lowering. We need to
make sure that "self" is always a reference to rootself of the
constructor before passing it to `assign_or_init`.

Resolves: https://github.com/apple/swift/issues/67827
Resolves: rdar://114433261
2023-08-25 15:32:50 -07:00
Nate Chandler
91159ae217 [Gardening] Removed stray comment. 2023-08-23 21:01:58 -07:00
Nate Chandler
ff302212a0 [OpaqueValues] Fix closure handling.
Rewrote and tested all currently emittable variations of closure
handling for opaque values.
2023-08-22 07:48:39 -07:00
Michael Gottesman
543063ef0e [silgen] Make ManagedValue::forUnmanaged private and change users to use other more specific APIs.
I have been doing this over the past couple of days in preparation for changing
ManagedValue to specify the type of scope its cleanup is connected to.
2023-08-19 14:11:44 -07:00
Michael Gottesman
104379adab [silgen] Rename SILGenFunction::emitManagedRetain -> emitManagedCopy.
This is an old API that should have been renamed a long time ago. It just kept
its early name due to inertia.
2023-08-19 14:11:44 -07:00
Pavel Yaskevich
c38b9b1e08 Merge pull request #67945 from xedin/remove-runtimeMetadata
[Frontend] NFC: Remove code and tests related to rejected @runtimeMetadata feature
2023-08-16 17:16:46 -07:00
Pavel Yaskevich
23d838af32 [SIL/SILGen] [AST] NFC: Remove @runtimeMetadata related code 2023-08-15 12:17:31 -07:00
Michael Gottesman
ff948f7309 [silgen] Convert even more cases of using trivial and values without ownership to use for*RValueWithoutOwnership APIs. 2023-08-15 11:04:01 -07:00
nate-chandler
9d5b175220 Merge pull request #67846 from nate-chandler/nfc/20230809/1/silgen-closure-simplification
[SILGen] Remove subtle identity function call.
2023-08-10 06:57:25 -07:00
Nate Chandler
368536ce92 [SILGen] Remove subtle identity function call.
Back in 33f4f57cc4 of
https://github.com/apple/swift/pull/28044 fame,
non-`CaptureKind::Constant:` uses of `Entry.val` in
`SILGenFunction::emitCaptures` were replaced with a use of the newly
added lambda `getAddressValue` applied at `Entry.val`.

The replacement of `Entry.value` with `getAddressValue(Entry.value)` in
the case of `CaptureKind::Box` had no effect.

Back then, the reason was that the condition

    SGM.Types
        .getTypeLowering(
            valueType,
            TypeExpansionContext::noOpaqueTypeArchetypesSubstitution(
                expansion.getResilienceExpansion()))
            .isAddressOnly() &&
        !entryValue->getType().isAddress()

under which something other than the input was returned would never
hold: CaptureKind::Box is used for LValues and those never satisfy
`!entryValue->getType().isAddress()`.

Since that PR, the getAddressValue lambda has grown.  There are two
additional aspects to consider: (1) forceCopy, (2) isPack.  (1) is not
relevant because `false` was being passed for the `CaptureKind::Box`
case.  (2) can not currently happen because pack lvalues haven't been
implemented.  But even if they were implemented, the argument passed to
the lambda would still need to be an address.

The same all holds for the `CaptureKind::ImmutableBox` case which only
differs from the `CaptureKind::Box` by a couple of lines.
2023-08-09 16:55:55 -07:00
Nate Chandler
e894a34ef8 [SILGen] Gardening: Deleted comment.
Way back in

commit d0bb0274e9
Author: Joe Groff <jgroff@apple.com>
Date:   Mon Nov 23 11:47:09 2015 -0800

the convention was changed such that LValues were passed only by box
rather than by box and address.  Delete the comment that says that both
are passed.
2023-08-09 16:49:30 -07:00
Nate Chandler
c8d1ea5aae [SILGen] Gardening: Line wrapping. 2023-08-08 16:45:13 -07:00
Nate Chandler
6e26b416f4 [SILGen] Gardening: Moved comment. 2023-08-08 16:45:13 -07:00
Zak Kent
7dae2e6905 [SILGen] Implement SILGenTopLevel
Implement SILGenTopLevel, a class that walks a file
run in script mode to generate all toplevel code
at once.
2023-08-08 11:25:11 -07:00
Michael Gottesman
85e38afbac Merge pull request #67780 from gottesmm/pr-d9b586775f6ea84fe9868857eb8dd2eeaeb39f6f
[silgen] Rename forTrivialObjectRValue -> forObjectRValueWithoutOwnership and change a bunch of forUnmanaged to use this API
2023-08-08 09:58:33 -07:00
Michael Gottesman
2208caac32 [silgen] Convert a bunch of forUnmanaged -> forObjectRValueWithoutOwnership.
NFCI.
2023-08-07 13:36:02 -07:00
Allan Shortlidge
cb7982d8d5 Merge pull request #67761 from tshortli/ast-unavailable-decl-queries
NFC: Hoist queries for unavailable decl optimizations into the AST library
2023-08-06 10:01:27 -07:00
Allan Shortlidge
ca8bf981a4 NFC: Hoist queries for unavailable decl optimizations into the AST library.
Moving the query implementation up to the AST library from SIL will allow
conveniences to be written on specific AST element classes. For instance, this
will allow `EnumDecl` to expose a convenience that enumerates element decls
that are available during lowering.

Also, improve naming and documentation for these queries.
2023-08-04 17:39:26 -07:00
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
Pavel Yaskevich
ddad2a7dad [SIL] InitAccessors: Reference a field that assign_or_init is associated with
The field removes the need to dig through init accessor reference
to find what stored properties and handled by the instruction.
2023-07-28 08:32:37 -07:00
Pavel Yaskevich
1f87ee8ca3 [SILGen] InitAccessors: Extract emission of assign_or_init into a separate method
New method is going to be used to emit default value initializations
for user-defined constructors.
2023-07-18 17:19:42 -07:00
Pavel Yaskevich
5fd502b150 [SILGen] InitAccessors: Generalize AccessorComponent::emitValue
Instead of taking a setter type, let's switch over to a more general
`AccessorKind` which allows us to cover init accessors and simplify
`emitApplySetterToBase`.
2023-07-18 17:19:42 -07:00
Pavel Yaskevich
75ac13807e [SILGen] NFC: Extract application of setter to base into a method on SILGenFunction
This is a preliminary step towards enabling default initialization of
init accessor properties in user-defined initializers because this logic
would have to be shared by multiple places during SILGen.
2023-07-18 17:19:42 -07:00
Joe Groff
c52ae08c7d SILGen: Don't reuse the Initialization across branches of an if or switch expression.
`Initialization` is stateful and not meant to be emitted into multiple times across different contexts.
If emitting into an initialization causes it to be split or aborted, that will carry over into
further uses of the initialization. This was happening during `if` and `switch` expression
emission, leading to miscompiles or compiler crashes. Fix this by saving only the buffer when
we prepare emission for a statement expression, and creating the initialization in the scope
where the expression for a branch actually gets emitted. Fixes rdar://112213253.
2023-07-14 14:21:57 -07:00
John McCall
c0777e611d Handle vanishing and variadic tuple results in reabstraction thunks.
Fixes rdar://110391963
2023-06-30 02:08:57 -04:00
Holly Borla
7a46466b65 Merge pull request #66967 from hborla/extension-macro
[Macros] Generalize `conformance` macros as `extension` macros
2023-06-29 08:57:04 -07:00
Kuba (Brecka) Mracek
d427696bf9 Allow @_silgen_name to be used on globals and add a @_silgen_name(raw: ...) version that skips mangling (#66540)
Attribute @_silgen_name is today only allowed to be used on functions, this change allows usage on globals as well. The motivation for that is to be able to "forward declare" globals just like it's today possible to do with functions (for the cases where it's not practical or convenient to use a bridging header).

Separately, this change also adds a @_silgen_name(raw: ...) syntax, which simply avoids mangling the name (by using the \01 name prefix that LLVM uses). The motivation for that is to be able to reference the "magic Darwin linker symbols" that can be used to look up section bounds (in the current dylib/module) -- those symbols don't use the underscore prefix in their mangled names.
2023-06-29 08:37:51 -07:00
Holly Borla
725374e0d8 [Macros] Implement attached extension macros. 2023-06-27 21:22:12 -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
Adrian Prantl
fef3578484 Temporarily disable assertion. 2023-06-16 14:50:04 -07:00
Adrian Prantl
329e42849a Compute VarDeclScopeMap up front.
The previous lazy discovery did not always work because sometimes a debug_value
is emitted before the first SIL instruction in the variable's scope.

rdar://110841130
2023-06-16 14:32:30 -07:00
Holly Borla
cd752cca22 [NameLookup] Plumb source location arguments through all name lookup APIs.
This source location will be used to determine whether to add a name lookup
option to exclude macro expansions when the name lookup request is constructed.
Currently, the source location argument is unused.
2023-06-11 23:09:47 -07:00
Pavel Yaskevich
d670067c65 [SILGen] InitAccessors: Add a way to check whether property is redirected to argument
Instance properties referenced by init accessors are mapped to synthesized
arguments.
2023-06-06 18:59:13 -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
b74cdf19e8 Derive the SILDebugScope for a variable declaration from its owning ASTScope.
The previous code made the assumption that the ASTScope for a variable
declaration should be the one of the declaration's source location. That is not
necessarily the case, in some cases it should be an ancestor scope. This patch
introduces a map from ValueDecl -> ASTScope that is derived from querying each
ASTScope for its locals, which matches also what happens in name lookup.  This
patch also fixes the nesting of SILDebugScopes created for guard statement
bodies, which are incorrectly nested in the ASTScope hierarchy.

rdar://108940570
2023-05-10 12:29:42 -07:00
Adrian Prantl
e0206daf01 Merge pull request #65741 from adrian-prantl/108618562
Fix SILDebugScopes for closures expanded from an expression macro.
2023-05-07 09:10:25 -07:00
Adrian Prantl
00b24c909b Fix SILDebugScopes for closures expanded from an expression macro.
An expression macro can expand not just to code inside the function, but also to
a closure. Such a closure needs to be treated similar to any functions generated
from a freestanding macro: Its instructions should have locations that point
straight into the macro buffer. Instructions that are expanded into the same
function as the macro expansion can be represented using inline locations
pointing back to the macro expansion, but this is not an option for top-level
function declaration.

https://github.com/apple/swift/issues/65484
rdar://108618562
2023-05-06 09:23:49 -07:00
Slava Pestov
ebb99e5e84 SILGen: Wrap captures of parameter packs inside tuples
Fixes rdar://problem/108481933.
2023-05-05 22:45:03 -04: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
Adrian Prantl
dcd881e105 SILDebugScopes: Remove a stateful workaround for Property Wrappers.
In an earlier version of the ASTScope -> SILdebugScope translation a workaround
was put into place that would select the current debug scope if an ASTScope was
marked as ignoreInDebugInfo. Removing this workaround makes the translation more
predictable as it eliminated the dependency on the current SILBuilder state. The
property wrapper tests still pass without this.

This uncovers a situations with let bindings where the SIL instructions are
emitted RHS before LHS, which does violate the di-hole verifier. This is
addressed by relaxing the verifier for now.

rdar://108736443
2023-05-01 17:52:44 -07:00
Adrian Prantl
3901219eeb Add support for nested ASTScopes inside of macro expansions.
Before this patch the parents of SILDebugScopes representing macro expansions
were missing the inlinedAt field, which resulted in incorrent LLVM IR being
produced. This is fixed by first computing the inlined call site for a macro
expansion and then computing the nested SILDebugScope for the ASTScope of the
expanded nodes; adding the inlinedAt field to all of levels of parent scopes.

rdar://108323748
2023-04-24 14:11:19 -07:00
swift-ci
703222583c Merge pull request #65047 from adrian-prantl/107764966
Ignore profile counter instructions in the dihole verifier.
2023-04-12 21:36:02 -07:00
Adrian Prantl
e51937f29e Don't emit freestanding macros in inlined scopes 2023-04-12 16:48:57 -07:00
Adrian Prantl
bccc080888 Fix handling of implicit locations for variables 2023-04-10 18:13:42 -07:00
Adrian Prantl
e3445a8e73 Delete commented-out code. NFC 2023-04-06 14:39:07 -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