Commit Graph

1122 Commits

Author SHA1 Message Date
Meghana Gupta
21cc1a185b Add memory lifetime verification support for borrow accessors 2025-11-06 10:55:42 -08:00
John McCall
a7d7970e29 Turn finishAsyncLet into a builtin.
This is necessary because we need to model its stack-allocation
behavior, although I'm not yet doing that in this patch because
StackNesting first needs to be taught to not try to move the
deallocation.

I'm not convinced that `async let` *should* be doing a stack allocation,
but it undoubtedly *is* doing a stack allocation, and until we have an
alternative to that, we will need to model it properly.
2025-11-03 16:33:40 -08:00
John McCall
cd67912a50 Remove the deprecated and unused endAsyncLet builtin. 2025-11-03 13:45:18 -08:00
John McCall
1d95fe14de Remove the deprecated and unused startAsyncLet builtin 2025-11-03 13:44:58 -08:00
John McCall
7a6ee1134c [NFC] Add a utility function for testing for a specific BuiltinInst 2025-11-03 13:44:18 -08:00
Kavon Farvardin
0849b2ea63 Merge pull request #85119 from kavon/fspl-debuginfo-rdar163281183
DebugInfo: fix FSPL SILLocation assertion issue
2025-10-24 17:28:01 -07:00
Kavon Farvardin
fd06bb1eeb DebugInfo: fix FSPL SILLocation assertion issue
We were reusing the SILLocation from return instructions
to generate projections to ultimately destroy values.

This fix improves on what we were doing before, by
converting the insertion point's SILLocation into a
RegularLocation _without dropping_ the source location.

If the SILLocation was tied to an ASTNode, it'll carry
over the line location for this new regular location.
Otherwise, it'll fallback to the prior strategy of
producing a line 0 autogenerated location.

resolves rdar://163281183
2025-10-24 11:57:34 -07:00
Meghana Gupta
d9a2ff4ff2 Update lifetime completion utility to consider return_borrow
When return_borrow has both lifetime ending and non-lifetime ending use, consider it lifetime ending.
2025-10-23 05:19:13 -07:00
Meghana Gupta
01680de084 Fixup ownership of borrow accessor results
SILGen may produce a borrow accessor result from within a local borrow scope. Such as:

```
  %ld = load_borrow %self
  %fwd = unchecked_ownership %ld
  %ex = struct_extract %fwd, #Struct.storedProperty
  end_borrow %ld
  return %ex
```

This is illegal OSSA, since the return uses a value outside it's borrow scope.

Add a new SILGenCleanup transform, to turn this into valid OSSA:

```
  %ld = load_borrow %self
  %ex = struct_extract %ld, #Struct.storedProperty
  return_borrow %ex from_scopes %ld
```
2025-10-23 05:19:11 -07:00
Meghana Gupta
1dc5c9611c Intoduce unchecked_ownership instruction in raw SIL
This instruction can be used to disable ownership verification on it's result and
will be allowed only in raw SIL.

Sometimes SILGen can produce invalid ownership SSA, that cannot be resolved until
mandatory passes run. We have a few ways to piecewise disable verification.
With unchecked_ownership instruction we can provide a uniform way to disable ownership
verification for a value.
2025-10-23 05:19:08 -07:00
Meghana Gupta
e116df3628 Introduce return_borrow instruction 2025-10-23 05:18:59 -07:00
Erik Eckstein
4035c18470 SIL: relax the runtime effect of copy_addr
It only needs metadata if the copied type has an archetype
2025-10-20 20:19:54 +02:00
Michael Gottesman
390afe3e7d [concurrency] Implement bit masking for TBI when available or in tagged pointer bits otherwise.
Specifically, when TBI is available we use the bottom two bits of the top nibble
(bits 60,61). On platforms without TBI, we use the bottom two tagged pointer
bits (bits 0, 1).

rdar://156525771
2025-10-16 10:52:05 -07:00
Michael Gottesman
788abd0b96 [silgen] Use Builtin.ImplicitActor instead of Optional<any Actor> to represent the implicit isolated parameter.
NOTE: We are not performing any bitmasking at all now. This is so that we can
transition the code base/tests to expect Builtin.ImplicitActor instead
of Optional<any Actor>.

NOTE: The actual test changes are in the next commit. I did this to make it
easier to review the changes.

This should not have any user visible changes.
2025-10-16 10:52:05 -07:00
Michael Gottesman
fe9c21fd87 [sil] Add a new instruction cast_implicit_actor_to_optional_actor.
This instruction converts Builtin.ImplicitActor to Optional<any Actor>. In the
process of doing so, it masks out the bits we may have stolen from the witness
table pointer of Builtin.ImplicitActor. The bits that we mask out are the bottom
two bits of the top nibble of the TBI space on platforms that support TBI (that
is bit 60,61 on arm64). On platforms that do not support TBI, we just use the
bottom two tagged pointer bits (0,1).

By using an instruction, we avoid having to represent the bitmasking that we are
performing at the SIL level and can instead just make the emission of the
bitmasking an IRGen detail. It also allows us to move detection if we are
compiling for AArch64 to be an IRGen flag instead of a LangOpts flag.

The instruction is a guaranteed forwarding instruction since we want to treat
its result as a borrowed projection from the Builtin.ImplicitActor.
2025-10-16 10:52:04 -07:00
Slava Pestov
4a63007414 SIL: Don't ask for stored properties of resilient types in TypeSubElementCount::TypeSubElementCount 2025-10-15 13:44:42 -04:00
John McCall
96afc1b00e Add methods to print various SIL things with a SILPrintContext.
Use those methods to make the tests I added in #84811 work even
in non-asserts builds, since apparently printID does not.
2025-10-13 21:03:11 -04:00
John McCall
46623cb6c7 Merge pull request #84811 from rjmccall/verify-dead-end-edges
Strengthen the SIL verifier's rules for edges into dead-end regions
2025-10-11 18:16:56 -04:00
John McCall
33bf18e3ab Add a utility to check for edges into dead-end regions.
This is subtly different from just checking whether the destination of
an edge is dead-end, because edges *internal* to dead-end regions
generally still need to be treated normally. Fundamentally, such an edge
must be part of a loop.
2025-10-11 02:12:18 -04:00
Slava Pestov
6e53f8c974 SIL: Fix crash in remapParentFunction() due to missing generic signature
We need to pass down the generic signature of the caller to correctly
mangle the substitution map, because the replacement types in this
substitution maps are interface types for the caller's generic signature.

Fixes rdar://problem/161968922.
2025-10-08 15:56:49 -04:00
Erik Eckstein
08696eeb92 Cloner: set the cloner's builder insertion point to the entry block when creating a new entry block in the cloned function
This allows clients to directly clone specific instructions into the new entry block
2025-10-06 09:47:40 +02:00
Meghana Gupta
c2dab58876 Update SILGen for ~Copyable borrow accessors
Introduce copy_value + mark_unresolved_non_copyable_value + begin_borrow at the return value of
borrow accessor apply to drive move-only diagnostics.

Also strip the copy_value + mark_unresolved_non_copyable_value + begin_borrow trio in a few places, since
they create an artificial scope out of which we cannot return values in a borrow accessor
without resorting to unsafe SIL operations currently.

Borrow accessor diagnostics allow stripping these instructions safely in the following places:

- return value of a borrow accessor
- self argument reference in the borrow accessor return expression and borrow accessor apply
2025-10-02 07:18:23 -07:00
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Erik Eckstein
a31a1219a8 GenericSpecializationMangler: don't assume a SIL function name can always be demangled
Demangling can fail if the user provides a custom not-demangable `@_silgen_name` (but still containing the "$s" mangling prefix).
2025-09-23 11:37:58 +02:00
Kavon Farvardin
61fe8a9b8e introduce @_manualOwnership performance attribute
This attribute forces programmers to acknowledge every
copy that is required to happen in the body of the
function. Only those copies that make sense according
to Swift's ownership rules should be "required".

The way this is implemented as of now is to flag each
non-explicit copy in a function, coming from SILGen, as
an error through PerformanceDiagnostics.
2025-09-17 13:51:57 -07:00
Kavon Farvardin
2dea3ca3fc sil: load [copy] affects reference counting
Based on what I see from OwnershipModelEliminator,
`%x = load [copy] %y` can turn into a plain load
followed by a retain of the loaded value.

See `NonTrivialLoadableTypeLowering::emitLoad`
2025-09-16 15:25:20 -07:00
Meghana Gupta
518c3ac091 Updates to MemAccessUtils to handle borrowed address returns 2025-09-09 14:45:47 -07:00
Erik Eckstein
b8a49692eb Optimizer: add TypeSubstitutionCloner and func cloneAndSpecializeFunction
Also move `func cloneFunction` from ContextCommon.swift to OptUtils.swift
2025-09-04 08:15:45 +02:00
Erik Eckstein
231042b9a8 SIL: some Cloner cleanups and improvements
* move some Cloner utilities from ContextCommon.swift directly into Cloner.swift
* add an `cloneRecursively` overload which doesn't require the `customGetCloned` closure argument
* some small cleanups
2025-09-04 08:15:45 +02:00
Erik Eckstein
65d69fe965 SIL/AST: add some APIs
* `GenericSignature.isEmpty`
* `Builder.emitDestroy`
* `Function.abi`
* `KeyPathInst.substitutionMap`
* `KeyPathInst.hasPattern`
2025-09-04 08:15:44 +02:00
Janat Baig
f21eb5375e Merge branch 'main' into temp-branch 2025-09-02 20:23:25 -04:00
Jakub Florek
e3140e0ae0 Add new generalized cloner. 2025-08-28 20:57:57 +01:00
Erik Eckstein
bdea6063d7 Mem2Reg: Fix lifetime completion for enum case values.
Enum types may have incomplete lifetimes in address form for trivial case values. When promoted to value form, they will end up with incomplete ossa lifetimes.
Because we know that the incomplete enum values are trivial enum cases we complete their lifetimes with `end_lifetime` instead of `destroy_value`.
This is especially important for Embedded Swift where we are not allowed to insert destroys which were not there before.

Fixes a compiler crash in Embedded Swift caused by de-virtualizing such an inserted destroy and ending up with a non-specialized generic function.

rdar://158807801
2025-08-25 09:35:46 +02:00
Janat Baig
798c0f51a4 Merge branch 'main' into temp-branch 2025-08-23 11:11:04 -04:00
JanBaig
4c61096be7 [SIL] Remove AssignByWrapper handling from analysis and utils 2025-08-22 23:15:16 -04:00
JanBaig
c2850c33c9 [SIL] Remove AssignByWrapper definition and registration 2025-08-22 23:06:59 -04:00
nate-chandler
d1637af144 Merge pull request #83789 from nate-chandler/nfc/20250818/1
[NFC] Renamed three SIL utilities.
2025-08-20 13:25:04 -07:00
Nate Chandler
aa85694237 [NFC] OSSACompleteLifetime: Renamed. 2025-08-18 09:45:19 -07:00
Nate Chandler
d8710fc83d [OSSACompleteLifetime] Fix error message. 2025-08-18 08:44:10 -07:00
Doug Gregor
7a196ac3ba Merge pull request #83739 from DougGregor/embedded-weak-definitions-of-imports
[Embedded] Emit weak definitions for imported symbols
2025-08-17 20:25:02 -07:00
Doug Gregor
c91a4822d3 Serialize the "markedAsUsed" bit for SILGlobalVariable 2025-08-17 15:25:50 -07:00
Nate Chandler
0a6c712195 [OSSACompleteLifetime] Flag disables leak checking
The utility effectively determines whether a value leaks as part of its
work, a subset of the checking done by the ownership verifier.  Disable
that checking when the flag that disables the ownership verifier is
passed.
2025-08-15 11:06:18 -07:00
Nate Chandler
c197ca107e [OSSACompleteLifetime] Promote assertion.
Use llvm::report_fatal_error instead.  Also, add logging to clarify that
reaching this point is a bug in the incoming SIL.
2025-08-15 09:09:04 -07:00
nate-chandler
fa05c1e492 Merge pull request #83639 from nate-chandler/rdar157772187
[SIL] Fix visitAccessedAddress at end_borrow.
2025-08-12 06:22:49 -07:00
Nate Chandler
66a9b77835 [SIL] Fix visitAccessedAddress @ end_borrow
`end_borrow` instructions may end the scopes introduced by `load_borrow`
or `store_borrow` instructions, or those introduced by `begin_borrow`
whose operand's guaranteed root includes a `load_borrow`.

Previously, such scope ending instructions which end the scope
associated with a borrowing load or store were not regarded as accessing
the loaded-from or stored-to address.  One consequence of this is that
they were not regarded as accessing pointers and thus not as deinit
barriers; that in turn resulted in `destroy_addr`s being hoisted into
such borrow scopes.

Here this is fixed by regarding such `end_borrow`s to access the
loaded-from or stored-to address of the scope-introducing `load_borrow`
or `store_borrow` respectively.

rdar://157772187
2025-08-11 08:38:52 -07:00
Nate Chandler
cbe383524c [NFC] OwnershipUtils: Add a UsePoint type.
The type is a union of an Operand (a real use) and a SILInstruction (an
implicit use).  Such a type is needed to reflect the fact that with
incomplete lifetimes, values can be implicitly destroyed at the
terminators of blocks in dead end regions (along the vaule's
availability boundary).
2025-08-08 15:08:20 -07:00
Nate Chandler
99091c93ae [NFC] OwnershipUtils: Factored out function.
For historical reasons, the existing function
(`areUsesWithinExtendedScope`) trafficked in operands rather than
instructions.  Now that PrunedLiveness has an API that deals with
instructions, add a function (`areWithinExtendedScope`) which does as
well.  Factor the existing function through this new function.
2025-08-08 14:59:36 -07:00
Nate Chandler
d49e9ea682 [NFC] PrunedLiveness: Factor out areWithinBoundary
For historical reasons, there was an API to check whether operands were
within the boundary which just checked whether those operands' users
were within the buondary.  Make a copy of the method deal in
instructions and factor the original API through it.
2025-08-08 14:53:01 -07:00
Erik Eckstein
2ab045235f InitializeStaticGlobals: allow statically initialized globals of non-loadable types 2025-08-03 17:25:43 +02:00
John McCall
46be95847b Extract TypeLowering's recursive type properties into a header, add
functions to compute them directly without a TypeLowering object, and
change a lot of getTypeLowering call sites to just use that.

There is one subtle change here that I think is okay: SILBuilder used to
use different TypeExpansionContexts when inserting into a global:
- getTypeLowering() always used a minimal context when inserting into
  a global
- getTypeExpansionContext() always returned a maximal context for the
  module scope
The latter seems more correct, as AFAIK global initializers are never
inlinable. If they are, we probably need to configure the builder with
an actual context properly rather than making global assumptions.

This is incremental progress towards computing this for most types
without a TypeLowering, and hopefully eventually removing TL entirely.
2025-08-01 15:00:57 -04:00