Commit Graph

490 Commits

Author SHA1 Message Date
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
Erik Eckstein
aa274a213b WalkUtils: handle mark_dependence instruction in the ValueUseDefWalker
It was already handled in the ValueDefUseWalker
2024-11-28 09:40:13 +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
e5069281d4 SwiftCompilerSources: Add Operand.isScopeEndingUse. 2024-11-18 22:02:44 -08:00
Andrew Trick
442739a176 [NFC] Add a Builder.insertionBlock API.
Some utilities, like Builder.insert(after:) pass a Builder object that
represents an insertion point. That insertion point is sometimes needed to
determine which instructions to create (endApply vs. abortApply). But there is
no way to recover the insertion point. We don't want to simply return an
instruction, because that could be interpreted in different ways. Instead we
provide insertion block, but only in those situations where it makes sense and can't
be used incorrectly.
2024-11-18 01:37:00 -08:00
Andrew Trick
5fc103b8ac [NFC] AccessUtils: add accessBaseWithScopes
And simplify enclosingAccessScope.

These two APIs now directly compute what is being asked for. Previously, each
invocation of enclosingAccessScope would recompute the base and all nested
scopes, but throw away that information. Sometimes we do not want to compute
that information, and other times we need all of it. Instead, provide separate
APIs that do exactly what is needed.

These are common APIs used by other utilities. This avoids quadratic traversals in those
cases. It is also easier to understand and debug.

These utilities will be used in LifetimeDependenceScopeFixup.
2024-11-18 01:37:00 -08:00
Andrew Trick
0fb3cb96de [NFC] Add AccessBase.address
LifetimeDependence uses the address of the AccessBase to compute liveness. It is
more convenient to switch over all AccessBase kinds here rather than force all
clients to do it.
2024-11-18 01:37:00 -08:00
Andrew Trick
ec0026f7e5 [NFC] AccessBase: correct initialization from pointer_to_address
AccessBase already identifies a base address as either a .global (addressor) or
a .pointer. When initializing an AccessBase from a pointer_to_address, precisely
identify one of these kinds of accesses, rather than simply marking it
.unidentified.

This used to be a special case in the AccessPathWalker. But that's not the right
place to handle it.
2024-11-18 01:37:00 -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
Andrew Trick
3e26cf81d0 SwiftCompilerSources: bridge createEndApply and createAbortApply. 2024-11-18 01:37:00 -08:00
Erik Eckstein
f7d49a9680 SIL: add the Argument.set(reborrow:) and Argument.hasBorrowEndingUse APIs 2024-11-12 09:26:58 +01:00
Erik Eckstein
f0633d5638 AccessUtils: support computing "constant" access paths
Add `Value.constantAccessPath`. It is like `accessPath`, but ensures that the projectionPath only contains "constant" elements.
This means: if the access contains an `index_addr` projection with a non-constant index, the `projectionPath` does _not_ contain the `index_addr`.
Instead, the `base` is an `AccessBase.index` which refers to the `index_addr`.
2024-11-04 19:26:44 +01: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
709dfc2d21 MandatoryPerformanceOptimization: don't let not-inlinable functions to be inlined
Also refactor canInline.

Fixes a compiler crash.
rdar://137544788
2024-10-15 12:19:50 +02:00
Erik Eckstein
e0533e6125 SIL: add an API to replace all entries of a VTable
* add `ModulePassContext.replaceVTableEntries()`
* add `ModulePassContext.notifyFunctionTablesChanged()`
2024-10-14 14:43:11 +02:00
Erik Eckstein
62317d86a6 SwiftCompilerSources: add the LoadBorrowInst.isUnchecked API 2024-10-11 09:41:36 +02:00
Erik Eckstein
6bf7cc8f2f Swift SIL: add flag parameters to Builder.createBeginBorrow 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
eeckstein
4a069c9926 Merge pull request #76790 from eeckstein/add-ast-module
SwiftCompilerSources: introduce the AST module
2024-10-02 10:50:18 +02:00
Erik Eckstein
6fdb713e94 SwiftCompilerSources: implement WitnessTable.Entry as enum 2024-10-02 07:10:30 +02: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
fadff007a1 SwiftCompilerSources: move protocol BridgedRandomAccessCollection from the SIL to the Basic module 2024-10-02 07:10:29 +02:00
Allan Shortlidge
6f91b4156c ASTGen/SIL: Suppress retroactive conformance warnings.
When building ASTGen and SIL using the Swift 6.0 compiler the compiler emits
diagnostics about various retroactive conformances. Since Swift code in the
compiler needs to remain compatible with the Swift 5.10 compiler for now, use
module qualification syntax to suppress the warning. I've also included the
`@retroactive` attribute in a comment to document the retroactive conformance
acknowledgement more explicitly.

NFC.
2024-10-01 17:07:03 -07:00
Erik Eckstein
46d3909471 SIL: improve VTable and WitnessTable
* add missing APIs
* bridge the entries as values and not as pointers
* add lookup functions in `Context`
* make WitnessTable.Entry.Kind enum cases lower case
2024-09-25 19:32:07 +02:00
Erik Eckstein
c6f384c3b1 SIL: add some APIs to SubstitutionMap 2024-09-25 19:31:40 +02:00
Erik Eckstein
351d42b3b3 SIL: add some Type APIs and make Type a DiagnosticArgument 2024-09-25 19:31:40 +02:00
Erik Eckstein
7ecb407226 SwiftCompilerSources: bridge SILDeclRef
And move NominalTypeDecl and DeclRef from Type.swift into a new file Declarations.swift.
Also add some APIs to NominalTypeDecl
2024-09-25 19:31:40 +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
Kuba Mracek
c7a5569c4f [embedded] Add support for ManagedBuffer to Embedded Swift
This makes ManagedBuffer available and usable in Embedded Swift, by:

- Removing an internal consistency check from ManagedBuffer that relies on metatypes.
- Making the .create() API transparent (to hoist the metatype to the callee).
- Adding a AllocRefDynamicInst simplification to convert `alloc_ref_dynamic` to `alloc_ref`, which removes a metatype use.
- Adding tests for the above.
2024-09-23 13:32:50 -07:00
eeckstein
8b1e257bc1 Merge pull request #76277 from eeckstein/fix-borrowed-from-updating
SIL: fix `Phi.init` for arguments in unreachable blocks
2024-09-06 15:02:09 +02:00
Erik Eckstein
d4221f4b2a SIL: fix Phi.init for arguments in unreachable blocks
Treat arguments in unreachable blocks like a degenerate phi so we don't consider it a terminator result.

Fixes a compiler crash.
rdar://135336430
2024-09-05 13:12:13 +02: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
c96b196ffa SwiftCompilerSources: bridge SILLinkage
Make SILLInkage available in SIL as `SIL.Linkage`.
Also, rename the misleading Function and GlobalVariable ABI `isAvailableExternally` to `isDefinedExternally`
2024-08-22 08:56:27 +02:00
Alexander Cyon
c18a24e499 [SwiftCompilerSources] Fix typos 2024-08-08 22:22:39 -07:00
Erik Eckstein
def3b6fab0 AccessUtils: recognize addressor calls of globals as a "global" access base
Makes e.g. alias analysis more precise in the early stage of the pass pipeline where addressor calls are not inlined, yet.
2024-08-05 17:12:46 +02: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
Andrew Trick
d065a3e576 Merge pull request #75562 from atrick/nfc-lifedep
[nfc] LifetimeDiagnostics cleanup
2024-07-30 09:03:24 -07:00
Andrew Trick
1113c757f0 Improve function convention printing. 2024-07-29 23:44:02 -07:00
Andrew Trick
c4c66b4066 [SwiftCompilerSources] add parameter dependencies to conventions.
Add interfaces to FunctionConventions and ArgumentConventions for querying
lifetime dependencies where the target is another parameter.
2024-07-29 23:44:02 -07:00
Erik Eckstein
f9b524b1cb AliasAnalysis: a complete overhaul of alias- and memory-behavior analysis
The main changes are:

*) Rewrite everything in swift. So far, parts of memory-behavior analysis were already implemented in swift. Now everything is done in swift and lives in `AliasAnalysis.swift`. This is a big code simplification.

*) Support many more instructions in the memory-behavior analysis - especially OSSA instructions, like `begin_borrow`, `end_borrow`, `store_borrow`, `load_borrow`. The computation of end_borrow effects is now much more precise. Also, partial_apply is now handled more precisely.

*) Simplify and reduce type-based alias analysis (TBAA). The complexity of the old TBAA comes from old days where the language and SIL didn't have strict aliasing and exclusivity rules (e.g. for inout arguments). Now TBAA is only needed for code using unsafe pointers. The new TBAA handles this - and not more. Note that TBAA for classes is already done in `AccessBase.isDistinct`.

*) Handle aliasing in `begin_access [modify]` scopes. We already supported truly immutable scopes like `begin_access [read]` or `ref_element_addr [immutable]`. For `begin_access [modify]` we know that there are no other reads or writes to the access-address within the scope.

*) Don't cache memory-behavior results. It turned out that the hit-miss rate was pretty bad (~ 1:7). The overhead of the cache lookup took as long as recomputing the memory behavior.
2024-07-29 17:33:46 +02:00
Erik Eckstein
ceda41ca90 AccessUtils: fix handling of store_borrow in AccessBase.isDistinct
The result of a store_borrow can "escape" to other access bases, like a "pointer" access base.
Then a store-borrow access base is _not_ distinct from such another base.
2024-07-29 17:33:46 +02:00
Erik Eckstein
983d95509d AccessUtils: do a simple type-based alias analysis in AccessBase.isDistinct
If the operand types (which are classes) of `ref_element_addr` or `ref_tail_addr` differ, then we know those can't be the same objects.
2024-07-29 17:33:46 +02:00
Erik Eckstein
3fb7032aa1 SIL Type: add Type.aggregateIsOrContains 2024-07-29 17:33:45 +02:00
Erik Eckstein
084d60d7b7 Swift SIL: add some APIs to instructions and other small improvments 2024-07-29 17:33:44 +02:00