Commit Graph

1095 Commits

Author SHA1 Message Date
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
Anthony Latsis
fec049e5e4 Address llvm::PointerUnion::{is,get} deprecations
These were deprecated in
https://github.com/llvm/llvm-project/pull/122623.
2025-07-29 18:37:48 +01:00
Erik Eckstein
319f49ad9f SwiftCompilerSources: move the Verifier to the SIL module 2025-07-28 14:19:11 +02:00
Erik Eckstein
e95283ba38 SwiftCompilerSources: make the testing infrastructure available in the SIL module
add `Test`, which is the SIL-equivalent of `FunctionTest`.
It's invocation closure gets a `TestContext` instead of a `FunctionContext`.

^ The commit message #2 will be skipped:

^ - test
2025-07-28 14:19:10 +02:00
Erik Eckstein
65c9828cb3 SwiftCompilerSources: move the Context protocols from the Optimizer to the SIL module
This allows to move many SIL APIs and utilities, which require a context, to the SIL module.

The SIL-part of SwiftPassInvocation is extracted into a base class SILContext which now lives in SIL.

Also: simplify the begin/end-pass functions of the SwiftPassInvocation.
2025-07-28 14:19:07 +02:00
Meghana Gupta
40e372f80b Merge pull request #83211 from meg-gupta/fixcastopt3
Fix the optimization  of checked_cast_addr_br to scalar cast
2025-07-23 22:24:09 -07:00
Meghana Gupta
b90cd31d66 Fix optimization of checked_cast_addr_br to checked_cast_br 2025-07-23 17:03:13 -07:00
Erik Eckstein
b68cafb870 SIL: allow casts to class-bound archetypes to be done as scalar-casts instead of address-casts
This allows such casts to be done in embedded swift.

rdar://156302495
2025-07-22 07:04:37 +02:00
nate-chandler
17c1fbd5f4 Merge pull request #82313 from jamieQ/copy-prop-ossa-dead-ends-build-time-fix
[SILOptimizer]: slow OSSA lifetime canonicalization mitigation
2025-06-21 08:50:58 -07:00
Jamie
1f3f830fc7 [SILOptimizer]: slow OSSA lifetime canonicalization mitigation
OSSA lifetime canonicalization can take a very long time in certain
cases in which there are large basic blocks. to mitigate this, add logic
to skip walking the liveness boundary for extending liveness to dead
ends when there aren't any dead ends in the function.

Updates `DeadEndBlocks` with a new `isEmpty` method and cache to
determine if there are any dead-end blocks in a given function.
2025-06-18 17:52:14 -05:00
Doug Gregor
bc4cf1236b [SIL] Generalize CastingIsolatedConformances to CheckedCastInstOptions
We are going to need to add more flags to the various checked cast
instructions. Generalize the CastingIsolatedConformances bit in all of
these SIL instructions to an "options" struct that's easier to extend.

Precursor to rdar://152335805.
2025-06-04 17:12:28 -07:00
Nate Chandler
f3c28515b1 [Gardening] Tweaked comments. 2025-06-03 15:32:26 -07:00
Nate Chandler
a9c31b9462 [DestroyAddrHoisting] Don't destructure NC aggs.
When hoisting destroys of aggregates, an attempt is made to fold the
destroys of individual fields into the foregoing instructions.  If the
aggregate is noncopyable, this transformation is illegal.
2025-05-29 16:50:56 -07:00
Nate Chandler
f5a16e4c13 [DestroyAddrHoisting] Don't destructure NE aggs.
When hoisting destroys of aggregates, an attempt is made to fold the
destroys of individual fields into the foregoing instructions.  If the
aggregate is nonescapable, this transformation is illegal.

rdar://152195094
2025-05-29 16:49:46 -07:00
Nate Chandler
f7ca26ce93 [NFC] SIL: This utility takes a func not a module.
In preparation to use the function in the implementation.
2025-05-29 15:36:15 -07:00
Erik Eckstein
9052652651 add the prepareInitialization builtin.
It is like `zeroInitializer`, but does not actually initialize the memory.
It only indicates to mandatory passes that the memory is going to be initialized.
2025-05-20 20:46:33 +02:00
Hamish Knight
edca7c85ad Adopt ABORT throughout the compiler
Convert a bunch of places where we're dumping to stderr and calling
`abort` over to using `ABORT` such that the message gets printed to
the pretty stack trace. This ensures it gets picked up by
CrashReporter.
2025-05-19 20:55:01 +01:00
Erik Eckstein
a38db6439a SIL: add the vector_base_addr instruction
It derives the address of the first element of a vector, i.e. a `Builtin.FixedArray`, from the address of the vector itself.
Addresses of other vector elements can then be derived with `index_addr`.
2025-05-12 19:24:31 +02:00
eeckstein
cc11a9baf3 Merge pull request #81203 from eeckstein/fix-objc-class-casting
CastOptimizer: don't assume dynamic casts from ObjectiveC classes to unrelated classes will fail
2025-05-02 07:07:56 +02:00
Meghana Gupta
35d62a4a36 Introduce end_cow_mutation_addr instruction 2025-04-30 13:39:45 -07:00
Erik Eckstein
7a5dbdc20b CastOptimizer: don't assume dynamic casts from ObjectiveC classes to unrelated classes will fail
In case of ObjectiveC classes, the runtime type can differ from its declared type.
Therefore a cast between (compile-time) unrelated classes may succeed at runtime.

rdar://149810124
2025-04-30 16:42:42 +02:00
nate-chandler
b93ed3e32b Merge pull request #81081 from swiftlang/revert-80839-moveonly-trivial-field-as-reinitialize
Revert "MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations."
2025-04-24 19:34:08 -07:00
nate-chandler
e4d3207b40 Revert "MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations." 2025-04-24 12:10:07 -07:00
Nate Chandler
02cad73cef [FSPL] Look through unchecked addr casts.
rdar://149850921
2025-04-23 21:29:22 -07:00
Erik Eckstein
f7d81f7ac7 Swift SIL: deal with a generic self type in SubstitutionMap.getMethodSubstitutions
If the method is a default witness methods (`selfType` != nil) it has generic self type.
In this case the generic self parameter is at depth 0 and the actual generic parameters of the substitution map are at depth + 1, e.g:
```
    @convention(witness_method: P) <τ_0_0><τ_1_0 where τ_0_0 : GenClass<τ_1_0>.T>
                                      ^      ^
                                   self      params of substitution map at depth + 1
```
2025-04-18 06:58:39 +02:00
Joe Groff
3860023081 Merge pull request #80839 from jckarter/moveonly-trivial-field-as-reinitialize
MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations.
2025-04-16 17:38:55 -07:00
Joe Groff
a1ff2e38dc MoveOnlyChecker: Treat trivial stores as reinitializations rather than initializations.
A trivial store is allowed to occur on an existing live value, and should not
trigger an attempt to destroy the original value completely. Fixes rdar://147791932.
2025-04-15 13:40:22 -07:00
nate-chandler
9cc1947527 Merge pull request #80766 from nate-chandler/rdar149007151
[DCE] Don't delete instructions which consume escaping values.
2025-04-12 08:31:12 -07:00