Commit Graph

11189 Commits

Author SHA1 Message Date
Arnold Schwaighofer
7ac551636b Merge pull request #81714 from aschwaighofer/se0460
SE-0460: Introduce @specialized attribute
2025-05-28 12:03:48 -07:00
eeckstein
89bdb311ab Merge pull request #81780 from eeckstein/optimize-enum-comparison
Optimize enum comparisons
2025-05-28 07:01:29 +02:00
Michael Gottesman
746414bcd8 Merge pull request #81716 from gottesmm/pr-b82786e1ebbf00a9de46d342f0694a28999bc178
[send-non-sendable] Recurse to the full underlying value computation instead of just the object one when computing the underlying object of an address.
2025-05-27 11:39:48 -07:00
Erik Eckstein
ed8922bc8e PerformanceInliner: always inline synthesized enum comparisons if one of the operands is a constant enum
If there is a "constant" enum argument to a synthesized enum comparison, we can always inline it, because most of it will be constant folded anyway.
This ensures the compiler is not creating terrible code for very simple enum comparisons, like
```
   if someEnum == .someCase {
      ...
   }
```

rdar://85677499
2025-05-27 12:11:03 +02:00
eeckstein
ddca2bae98 Merge pull request #81706 from eeckstein/mandatory-temprvalue-elimination
Optimizer: Improve performance of large InlineArrays at Onone
2025-05-26 07:56:42 +02:00
Daniil Kovalev
1e403ecf5c [AutoDiff] Support custom derivatives for @_alwaysEmitIntoClient functions (#78908)
Consider an `@_alwaysEmitIntoClient` function and a custom derivative
defined
for it. Previously, such a combination resulted different errors under
different
circumstances.

Sometimes, there were linker errors due to missing derivative function
symbol -
these occurred when we tried to find the derivative in a module, while
it
should have been emitted into client's code (and it did not happen).

Sometimes, there were SIL verification failures like this:

```
SIL verification failed: internal/private function cannot be serialized or serializable: !F->isAnySerialized() || embedded
```

Linkage and serialization options for the derivative were not handled
properly,
and, instead of PublicNonABI linkage, we had Private one which is
unsupported
for serialization - but we need to serialize `@_alwaysEmitIntoClient`
functions
so the client's code is able to see them.

This patch resolves the issue and adds proper handling of custom
derivatives
of `@_alwaysEmitIntoClient` functions. Note that either both the
function and
its custom derivative or none of them should have
`@_alwaysEmitIntoClient`
attribute, mismatch in this attribute is not supported.

The following cases are handled (assume that in each case client's code
uses
the derivative).

1. Both the function and its derivative are defined in a single file in
   one module.

2. Both the function and its derivative are defined in different files
which
   are compiled to a single module.

3. The function is defined in one module, its derivative is defined in
another
   module.

4. The function and the derivative are defined as members of a protocol
extension in two separate modules - one for the function and one for the
   derivative. A struct conforming the protocol is defined in the third
   module.

5. The function and the derivative are defined as members of a struct
extension in two separate modules - one for the function and one for the
   derivative.

The changes allow to define derivatives for methods of `SIMD`.

Fixes #54445
<!--
If this pull request is targeting a release branch, please fill out the
following form:

https://github.com/swiftlang/.github/blob/main/PULL_REQUEST_TEMPLATE/release.md?plain=1

Otherwise, replace this comment with a description of your changes and
rationale. Provide links to external references/discussions if
appropriate.
If this pull request resolves any GitHub issues, link them like so:

  Resolves <link to issue>, resolves <link to another issue>.

For more information about linking a pull request to an issue, see:

https://docs.github.com/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue
-->

<!--
Before merging this pull request, you must run the Swift continuous
integration tests.
For information about triggering CI builds via @swift-ci, see:

https://github.com/apple/swift/blob/main/docs/ContinuousIntegration.md#swift-ci

Thank you for your contribution to Swift!
-->
2025-05-25 09:47:15 -04:00
Arnold Schwaighofer
13ff5abdb8 Introduce @specialized attribute
Implements SE-0460 -- the non-underscored version of @specialized.

It allows to specify "internal" (not abi affecting) specializations.

rdar://150033316
2025-05-23 13:12:47 -07:00
Erik Eckstein
198d4ab0bb Optimizer: run TempRValueElimination also at Onone
Introduce a new pass MandatoryTempRValueElimination, which works as the original TempRValueElimination, except that it does not remove any alloc_stack instruction which are associated with source variables.

Running this pass at Onone helps to reduce copies of large structs, e.g. InlineArrays or structs containing InlineArrays.
Copying large structs can be a performance problem, even at Onone.

rdar://151629149
2025-05-23 18:56:56 +02:00
Erik Eckstein
85228f2c75 Optimizer: do mark_dependence simplification at Onone
* re-implement the SILCombine peephole as a Swift instruction simplification
* run this simplification also in the OnoneSimplification pass
2025-05-23 18:53:57 +02:00
Michael Gottesman
904ebc6784 [send-non-sendable] Recurse to the full underlying value computation instead of just the object one when computing the underlying object of an address.
Otherwise, depending on the exact value that we perform the underlying look up
at... we will get different underlying values. To see this consider the
following SIL:

```sil
  %1 = alloc_stack $MyEnum<T>
  copy_addr %0 to [init] %1
  %2 = unchecked_take_enum_data_addr %1, #MyEnum.some!enumelt
  %3 = load [take] %2
  %4 = project_box %3, 0
  %5 = load_borrow %4
  %6 = copy_value %5
```

If one were to perform an underlying object query on %4 or %3, one would get
back an underlying object of %1. In contrast, if one performed the same
operation on %5, then one would get back %3. The reason why this happens is that
we first see we have an object but that it is from a load_borrow so we need to
look through the load_borrow and perform the address underlying value
computation. When we do that, we find project_box to be the value. project_box
is special since it is the only address base we ever look through since from an
underlying object perspective, we want to consider the box to be the underlying
object rather than the projection. So thus we see that the result of the
underlying address computation is that the underlying address is from a load
[take]. Since we then pass in load [take] recursively into the underlying value
object computation, we just return load [take]. In contrast, the correct
behavior is to do the more general recurse that recognizes that we have a load
[take] and that we need to look through it and perform the address computation.

rdar://151598281
2025-05-22 10:11:40 -07:00
Anton Korobeynikov
799e0f81b0 [AutoDiff] Correct propagate adjoints for array literal values (#81676)
Fixes #81607
2025-05-22 03:03:28 -07:00
Erik Eckstein
e0f5888a8d SIL: define mark_dependence_addr to read and write to its address operand
This prevents simplification and SILCombine passes to remove (alive) `mark_dependence_addr`.
The instruction is conceptually equivalent to
```
  %v = load %addr
  %d = mark_dependence %v on %base
  store %d to %addr
```

Therefore the address operand has to be defined as writing to the address.
2025-05-21 20:03:53 +02:00
Anton Korobeynikov
bc94ac3342 [AutoDiff] Better diagnose non-differentiability for throwing functions (#81669)
Fixes #81608
2025-05-21 09:13:18 -07:00
eeckstein
0ceb5171ff Merge pull request #81649 from eeckstein/global-inlinearray-initialization
Allow more complex InlineArray initializations to end up in a statically initialized global
2025-05-21 06:24:28 +02:00
Michael Gottesman
a6901d8006 Merge pull request #81571 from gottesmm/pr-d7a7ebea7af14e2f9f1ce266febd0c983e3fc888
[rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe).
2025-05-20 11:56:06 -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
Erik Eckstein
3cbe94d7d1 inliner: tune the heuristic for constructors to be inlined into global initializer functions.
Inlining constructors into global initializers increase the changes that the global can be initialized statically.
2025-05-20 20:46:33 +02:00
Stephen Canon
3aa7b592b6 Implement Builtin.select binding llvm select instruction (#81598)
Not used (yet), but needed to implement SIMD.replacing(with:where:) idiomatically, and probably useful otherwise.
2025-05-20 13:30:59 -04:00
Hamish Knight
3ec3421987 Merge pull request #81604 from hamishknight/pretty-crash
Adopt `ABORT` throughout the compiler
2025-05-20 10:05:03 +01: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
nate-chandler
8959f60ab1 Merge pull request #81566 from nate-chandler/rdar151325025
[MoveOnlyChecker] Don't complete phis.
2025-05-19 07:21:21 -07:00
eeckstein
d985bf6435 Merge pull request #81567 from eeckstein/fix-builtin-emplace
SILGen: insert an `end_lifetime` in the throw-branch of a `Builtin.emplace`
2025-05-17 23:40:25 +02:00
Michael Gottesman
010443c854 [rbi] Treat a partial_apply as nonisolated(unsafe) if all of its captures are nonisolated(unsafe).
rdar://144111950
2025-05-17 12:21:21 -07:00
Meghana Gupta
f7529eae86 Merge pull request #81520 from meg-gupta/disableverifysemantics
Add a new semantics attribute to disable SIL verification on a function
2025-05-16 15:38:32 -07:00
Erik Eckstein
ddbc3a2fad SILGen: insert an end_lifetime in the throw-branch of a Builtin.emplace
When the called closure throws an error, it needs to clean up the buffer.
This means that the buffer is uninitialized at this point.

We need an `end_lifetime` so that the move-only checker doesn't insert a wrong `destroy_addr` because it thinks that the buffer is initialized.

Fixes a mis-compile.

rdar://151461109
2025-05-16 21:54:39 +02:00
Nate Chandler
9b3db3646a [MoveOnlyChecker] Don't complete phis.
Apply the MoveOnlyAddressChecker change from
https://github.com/swiftlang/swift/pull/73358 to the
MoveOnly[Value]Checker.

After 7713eef817, before running value
checking, all lifetimes in the function are completed.  That doesn't
quite work because lifetime completion expects not to encounter
reborrows or their adjacent phis.

rdar://151325025
2025-05-16 11:58:44 -07:00
Nate Chandler
d18a0178bb [Gardening] Whitespace cleanup. 2025-05-16 11:36:22 -07:00
nate-chandler
f4a2e46e39 Merge pull request #81541 from nate-chandler/rdar139666145
[MoveOnly] Fix consume of addr with mutated field.
2025-05-16 06:55:31 -07:00
Nate Chandler
1765f0b1e0 [MoveOnly] Fix consume of addr with mutated field.
Don't fail out of use visitation when encountering an apply which uses a
field of the value as an inout parameter.

rdar://139666145
2025-05-15 15:19:26 -07:00
Nate Chandler
dcde328fe9 [Gardening] MoveOnly: Move and add comment. 2025-05-15 15:18:50 -07:00
Meghana Gupta
aa01b2ad06 Add a new semantics attribute to disable SIL verification on a function
This provides a way to disable verification per function instead of the entire module.
2025-05-15 14:30:28 -07:00
Meghana Gupta
8c5fae087d Merge pull request #81499 from meg-gupta/fixdropdeinit
Fix InstructionDeleter for drop_deinit instruction
2025-05-14 15:22:05 -07:00
Meghana Gupta
16d1eadd5d Fix InstructionDeleter for drop_deinit instruction
Currently we delete dead drop_deinit instructions in InstructionDeleter.
For address results, we may end up with ownership errors after being promoted to value forms.
For value results, fixLifetimes mode of InstructionDeleter will insert an illegal destroy_value

rdar://151104993
2025-05-14 10:43:59 -07:00
eeckstein
7ff1066409 Merge pull request #81480 from eeckstein/fix-fso
FunctionSignatureOptimization: don't convert indirect `@in` to `@in_guaranteed` if the argument is mutated
2025-05-14 06:25:16 +02:00
Erik Eckstein
8cab792753 FunctionSignatureOptimization: don't convert indirect @in to @in_guaranteed if the argument is mutated
This fixes a verifier error because an `@in_guaranteed` argument must not be mutated.

https://github.com/swiftlang/swift/issues/81444
rdar://151147971
2025-05-13 18:53:31 +02:00
Erik Eckstein
85a49dcbbc Optimizer: make salvageDebugInfo optional when deleting instructions
Add a boolean parameter `salvageDebugInfo` to `Context.erase(instruction:)`.
Sometimes it needs to be turned off because the caller might require that after erasing the original instruction the operands no users anymore.
2025-05-13 07:37:45 +02:00
Erik Eckstein
bc4310b0eb Optimizer: simplify unchecked_addr_cast for vectors
Reimplement the simplification in swift and add a new transformation:
```
  %1 = unchecked_addr_cast %0 : $*Builtin.FixedArray<N, Element> to $*Element
```
->
```
  %1 = vector_base_addr %0 : $*Builtin.FixedArray<N, Element>
```
2025-05-12 19:25:12 +02: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
Erik Eckstein
2c27963ea1 SILCombine: handle mark_dependence in dead-closure elimination
rdar://150686370
2025-05-06 19:42:01 +02:00
Erik Eckstein
8dd76769bb SILCombine: handle mark_dependence in apply-of-partial_apply optimization
rdar://150686370
2025-05-06 19:42:01 +02:00
Erik Eckstein
c6b1e3e854 TempRValueElimination: re-implement the pass in swift
Beside cleaning up the source code, the motivation for the translation into Swift is to make it easier to improve the pass for some InlineArray specific optimizations (though I'm not sure, yet if we really need those).
Also, the new implementation doesn't contain the optimize-store-into-temp optimization anymore, because this is covered by redundant load elimination.
2025-05-06 13:08:09 +02:00
Pavel Yaskevich
778be225e1 Merge pull request #81277 from ludwwwig/remove-unused-header
[gardening] Delete an empty header
2025-05-06 00:23:06 -07:00
Ludwig Hollmann
39aa950660 Update file header comments for headers in lib. 2025-05-04 22:26:26 +02:00
Ludwig Hollmann
9482a1be6d [gardening] Delete an empty header.
This header was supposed to be deleted by #63783.
2025-05-03 22:43:48 +02:00
Meghana Gupta
3cad5c5924 Merge pull request #81043 from meg-gupta/fixcow
Insert end_cow_mutation_addr for lifetime dependent values dependent on mutable addresses
2025-05-01 07:26:28 -07:00
Meghana Gupta
35d62a4a36 Introduce end_cow_mutation_addr instruction 2025-04-30 13:39:45 -07:00
Slava Pestov
6ffa8fd489 AST: Change signature of LookupConformanceFn
Instead of passing in the substituted type, we pass in the
InFlightSubstitution. This allows the substituted type to be
recovered if needed, but we can now skip computing it for
the common case of LookUpConformanceInSubstitutionMap.
2025-04-30 13:42:20 -04:00
Kuba (Brecka) Mracek
3f61189657 Merge pull request #80914 from kubamracek/inliner-knobs
[SILOptimizer] Expose SIL inliner heuristics/constants as -Xllvm configurable knobs
2025-04-30 09:37:39 -07:00
eeckstein
e2ee00700f Merge pull request #81159 from eeckstein/deinit-barrier-optimizations
Some small deinit-barrier related optimizations
2025-04-30 15:33:46 +02:00
Slava Pestov
6b4710ed22 Merge pull request #81142 from slavapestov/more-type-subst-cleanup
Clean up duplicated opened existential archetype handling in SIL and more
2025-04-30 02:42:57 -04:00