Commit Graph

184 Commits

Author SHA1 Message Date
Doug Gregor
e0b52cd20e [SIL] Extend checked-cast instructions with "prohibit isolated conformances" flag
When performing a dynamic cast to an existential type that satisfies
(Metatype)Sendable, it is unsafe to allow isolated conformances of any
kind to satisfy protocol requirements for the existential. Identify
these cases and mark the corresponding cast instructions with a new flag,
`[prohibit_isolated_conformances]` that will be used to indicate to the
runtime that isolated conformances need to be rejected.
2025-03-26 22:31:47 -07:00
Andrew Trick
97b249bd11 Merge pull request #80263 from atrick/markdep-addr
SIL: add mark_dependence_addr
2025-03-26 10:33:42 -07:00
Erik Eckstein
1844c14c44 SIL: add var ClassMethodInst.member 2025-03-26 08:45:07 +01:00
Andrew Trick
e7000e4668 SIL: Add mark_dependence_addr 2025-03-25 23:02:42 -07:00
Andrew Trick
995c74966d Fix DestroyHoisting: InteriorLiveness for noescape closures
InteriorLiveness has a new "visitInnerUses" mode used by DestroyHoisting. That
mode may visit dependent values, which was not valid for noescape
closures. ClosureLifetimeFixup inserts destroys of noescape closures after the
destroys of the captures. So following such dependent value could result in an
apparent use-after-destroy. This causes DestroyHoisting to insert redundant
destroys.

Fix: InteriorUses will conservatively only follow dependent values if they are
escapable. Non-escapable values, like noescape closures are now considered
escapes of the original value that the non-escapable value depends on. This can
be improved in the future, but we may want to rewrite ClosureLifetimeFixup first.

Fixes the root cause of: rdar://146142041
2025-03-13 09:17:31 -07:00
Erik Eckstein
0f55220cc8 SIL: add var FullApplySite.singleDirectErrorResult 2025-03-07 15:59:34 +01:00
Erik Eckstein
7bbd9a9c99 SIL: add var Instruction.typeDependentOperands
And simplify the implementation of `var Instruction.definedOperands`
2025-03-07 15:59:34 +01:00
Erik Eckstein
f7466eac78 SIL: fix bridging of SILDebugVariable
The optional C++ type was bridged to a non-optional Swift type.
The correct way is to bridged the non-optional C++ type to the non-optional Swift type.
2025-03-07 15:59:33 +01:00
Erik Eckstein
31057332ff Swift SIL: add some Instruction APIs
* getting the formal source and target types of casts
* `isFromVarDecl` and `usesMoveableValueDebugInfo` for AllocStackInst
* WitnessMethod APIs
* `TryApply.isNonAsync`
2025-03-07 15:59:33 +01:00
Andrew Trick
a0afb188b9 Add ScopedInstruction.instruction
So we can use existentials of this type.

This seems really inefficient, but I don't have another solution.
2025-03-05 02:09:55 -08:00
Andrew Trick
dc8778f33d Add MarkDependenstInst.hasScopedLifetime 2025-02-25 23:08:55 -08:00
Erik Eckstein
e0b4f71af6 SIL: remove the alloc_vector instruction
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
Andrew Trick
f56d98408c Fix VariableScopeInstruction.endOperands for extend_lifetime.
Consider end_lifetime to be part of the endOperands set, which is used for
linear liveness, even though it does not end the lifetime.
2025-02-10 09:11:22 -08:00
Arnold Schwaighofer
0fe3cf0096 SIL: Update formal source type in SimplifyRefCasts when possible
We use the formal source type do decide whether a checked_cast_br is
known to succeed/fail. If we don't update it we loose that optimization

That is:

```
  checked_cast_br AnyObject in %2 : X to X, bb1, bb2
```

Will not be simplified even though the operand and the destintation type
matches.
2025-01-29 09:07:21 -08:00
Erik Eckstein
3ec5d7de24 SIL: replace the is_escaping_closure instruction with destroy_not_escaped_closure
The problem with `is_escaping_closure` was that it didn't consume its operand and therefore reference count checks were unreliable.
For example, copy-propagation could break it.
As this instruction was always used together with an immediately following `destroy_value` of the closure, it makes sense to combine both into a `destroy_not_escaped_closure`.
It
1. checks the reference count and returns true if it is 1
2. consumes and destroys the operand
2025-01-24 19:23:27 +01:00
Michael Gottesman
05511c955a [sil] Add conformance to UnaryInstruction for IgnoredUseInst. 2025-01-23 10:58:45 -08:00
Michael Gottesman
7ae56aab2e [sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but
instead only suppress unused variable warnings. This patch just adds the
instruction.

Eventually, we can use it to move the unused variable warning from Sema to SIL
slimmming the type checker down a little bit... but for now I am using it so
that other diagnostic passes can have a SIL instruction (with SIL location) so
that we can emit diagnostics on code like _ = x. Today we just do not emit
anything at all for that case so a diagnostic SIL pass would not see any
instruction that it could emit a diagnostic upon. In the next patch of this
series, I am going to add SILGen support to do that.
2025-01-22 21:12:36 -08:00
Erik Eckstein
43b6fd4edb SIL: add the IndexingInstruction protocol
It defines (and implements) the `base` and `index` properties, which are used in the conforming classes `IndexRawPointerInst`, `IndexAddrInst` and `TailAddrInst`
2024-12-21 08:28:21 +01:00
Erik Eckstein
969a788e7f SIL: add the Builtin.name getter and Builtin.arguments 2024-12-21 08:28:21 +01:00
Erik Eckstein
1856d4e94c SIL: add APIs to set and get the alignment of a pointer_to_address instruction.
Also add a getter for the `isInvariant` property.
2024-12-21 08:28:21 +01:00
Erik Eckstein
ca7facde35 SIL: add some use-list APIs
* `users`: maps from a sequence of operands to a sequence of instructions
* `users(ofType:)` : as `users`, but filters users of a certain instruction type
* `Value.users`: = `Value.uses.users`
2024-12-19 20:53:45 +01:00
Andrew Trick
f5fc17804e [NFC] SwiftCompilerSources: Add VariableScopeInstruction utility
To diagnose dependence on trivial variables.
2024-12-14 22:46:54 -08:00
Erik Eckstein
c5efad1e2d SIL: make isLexical a protocol requirement of Value
This avoids the need for checking if a value is a `move_value`, `begin_borrow` or function argument to get the is-lexical information.
2024-12-10 16:28:11 +01:00
Andrew Trick
c05044c65c Add MarkDependenceInst.settleToEscaping
This allows emitting mark_dependence [unresolved] in some cases even if the
diagnostics can't currently handle them.
2024-11-18 22:02:44 -08:00
Andrew Trick
5be742fd8c SwiftCompilerSources: fix BeginApplyInst.endOperands.
isScopeEndingUse is not the same as endsLifetime.
2024-11-18 22:02:44 -08:00
Andrew Trick
cdb646dc95 SwiftCompilerSources: add ScopedInstruction.endInstructions.
Add endInstructions requirement so it can be called on an unknown
ScopedInstruction.
2024-11-18 01:37:00 -08:00
Andrew Trick
801c192af1 SwiftCompilerSources: fix EndApplyInst to be SingleValueInstruction.
To be consistent with the C++ implementation.
2024-11-18 01:37:00 -08:00
Andrew Trick
b5bef612ed SwiftCompilerSources: fix BeginApply.endOperands()/endInstructions()
Only return the EndApplyInst and AbortApplyInst uses.
2024-11-18 01:37:00 -08:00
Michael Gottesman
3c38c79f7a [region-isolation] Implement MergeIsolationRegionInst.
I am adding this instruction to express artificially that two non-Sendable
values should be part of the same region. It is meant to be used in cases where
due to unsafe code using Sendable, we stop propagating a non-Sendable dependency
that needs to be made in the same region of a use of said Sendable value. I
included an example in ./docs/SIL.rst of where this comes up with @out results
of continuations.
2024-11-01 11:25:53 -07:00
Erik Eckstein
62317d86a6 SwiftCompilerSources: add the LoadBorrowInst.isUnchecked API 2024-10-11 09:41:36 +02:00
Michael Gottesman
561662d6cc [sil] Add a new instruction called ThunkInst.
For now this will only be used for HopToMainActorIfNeeded thunks. I am creating
this now since in the past there has only been one option for creating
thunks... to create the thunk in SILGen using SILGenThunk. This code is hard to
test and there is a lot of it. By using an instruction here we get a few benefits:

1. We decouple SILGen from needing to generate new kinds of thunks. This means
that SILGenThunk does not need to expand to handle more thunks.

2. All thunks implemented via ThunkInst will be easy to test in a decoupled way
with SIL tests.

3. Even though this stabilizes the patient, we still have many thunks in SILGen
and various parts of the compiler. Over time, we can swap to this model,
allowing us to hopefully eventually delete SILGenThunk.
2024-10-02 14:15:49 -07:00
Erik Eckstein
10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00
Erik Eckstein
f3a9b08e7a SwiftCompilerSources: bridged ProtocolConformance
And use that in some Builder APIs
2024-09-25 19:31:40 +02:00
Erik Eckstein
897ef2c5d4 SIL: add label to SubstitutionMap.init's bridged argument 2024-09-25 19:31:39 +02:00
Kuba Mracek
df38313572 The metatype operand on AllocRefDynamicInst is not always at index 1, use getNumTailTypes() 2024-09-23 15:42:38 -07:00
Alejandro Alonso
b35ac50d3c Optimize TypeValueInst in Swift 2024-09-04 15:13:48 -07:00
Alejandro Alonso
75c2cbf593 Implement value generics
Some requirement machine work

Rename requirement to Value

Rename more things to Value

Fix integer checking for requirement

some docs and parser changes

Minor fixes
2024-09-04 15:13:25 -07:00
Erik Eckstein
345f9c1dfc AliasAnalysis: handle destroy_value [dead_end] in computeMemoryEffect
We don't have to take deinit effects into acount for a `destroy_value [dead_end]`.
Such destroys are lowered to no-ops and will not call any deinit.
2024-08-05 17:02:06 +02:00
Erik Eckstein
084d60d7b7 Swift SIL: add some APIs to instructions and other small improvments 2024-07-29 17:33:44 +02:00
Erik Eckstein
95bd329b1f Swift SIL: bridge the hasPointerEscape flags of MoveValueInst and BeginBorrowInst 2024-07-29 17:33:43 +02:00
Nate Chandler
2a5d07522d [SIL] Add extend_lifetime instruction.
It indicates that the value's lifetime continues to at least this point.
The boundary formed by all consuming uses together with these
instructions will encompass all uses of the value.
2024-06-05 16:28:26 -07:00
Emil Pedersen
d39d838c2b Skip meta instructions for Builder.init with automatic location 2024-05-24 16:01:11 -07:00
Kshitij
ab751d57ab [Autodiff] Adds logic to generate specialized functions in the closure-spec pass 2024-05-13 11:16:42 -07:00
Kshitij
003c9082cb [Autodiff] Adds SwiftCompilerSources changes in preparation for the Autodiff closure-spec optimization pass 2024-05-02 09:14:05 -07:00
Erik Eckstein
ac4bc89c9a SIL: add the borrowed-from instruction.
It declares from which enclosing values a guaranteed phi argument is borrowed from.
2024-04-10 13:38:10 +02:00
Andrew Trick
6fcec2ff96 SwiftCompilerSources: organize parameter pack instructions 2024-04-01 21:46:18 -07:00
Andrew Trick
66dcf6a35c SwiftCompilerSources: fix PackElementSetInst
It is not a SingleValueInstruction.
2024-04-01 21:46:10 -07:00
Andrew Trick
a1bb9f401a Fix SILVerifier and AddressUtils handling of addr casts.
These core utils did not handle UnconditionalCheckedCastAddrInst or
UncheckedRefCastAddrInst correctly.
2024-04-01 21:46:10 -07:00
Andrew Trick
a0b2ae9c2c Add AccessBase.storeBorrow.
Don't treat StoreBorrow addresses as unknown bases. While they are never the base of a formal access, they are returned
as the AccessBase when querying the enclosing scope of an address.
2024-03-22 11:51:58 -07:00
Andrew Trick
7da493498a Merge pull request #72407 from atrick/pointer-escape-bailout
Fix lifetime dependence in the presence of pointer escapes.
2024-03-19 11:40:38 -07:00