Commit Graph

1173 Commits

Author SHA1 Message Date
Valeriy Van
761bee9d09 Fix typo in func replaceOpenedArchetypeInSubstituations -> replaceOpenedArchetypeInSubstitutions 2025-06-08 11:30:04 +03:00
Valeriy Van
dd2468cf58 Fix typo in local var 2025-06-08 11:27:30 +03:00
Valeriy Van
8a0c1db164 Fix typo in property allContainedAddresss -> allContainedAddresses 2025-06-08 11:26:01 +03:00
Valeriy Van
949c2bad67 Fix some typos in SwiftCompilerSources/Sources 2025-06-08 11:22:45 +03:00
Erik Eckstein
d6cbaf6ebd MandatoryPerformanceOptimizations: only set the [perf_constraint] flag for callees of performance constraint functions
It used to also set it for functions which are referenced from a global with a const/section attribute - even if not performance attribute was present in the whole module. This is unnecessary and can lead to worse code generation.

rdar://152665294
2025-06-06 20:08:16 +02:00
eeckstein
a33ff9879a Merge pull request #81969 from eeckstein/temp-lvalue-elimination
Optimizer: improve TempLValueOpt
2025-06-06 15:30:29 +02:00
Andrew Trick
227f8028e8 LifetimeDependenceScopeFixup: handle yielded value copies.
When extending an access scope over a coroutines, instead of simply
considering the lifetime of the coroutine scope, recurse through all
uses of yielded values. They may be copyable, non-Escapable values
that depend on the coroutine operand.

Fixes rdar://152693622 (Extend coroutines over copied yields)
2025-06-05 20:21:40 -07:00
Andrew Trick
95f7a12c98 [NFC] LifetimeDependence: fix internal debug output 2025-06-05 20:09:06 -07:00
Doug Gregor
67b55566fe Merge pull request #82005 from DougGregor/generalize-checked-cast-options
[SIL] Generalize CastingIsolatedConformances to CheckedCastInstOptions
2025-06-05 08:43:09 -07:00
Erik Eckstein
2b9b2d243c Optimizer: improve TempLValueOpt
* re-implement the pass in swift
* support alloc_stack liveranges which span over multiple basic blocks
* support `load`-`store` pairs, copying from the alloc_stack (in addition to `copy_addr`)

Those improvements help to reduce temporary stack allocations, especially for InlineArrays.

rdar://151606382
2025-06-05 06:45:18 +02:00
Erik Eckstein
6428a7b50c Optimizer: move protocol CopyLikeInstruction from TempRValueElimination to OptUtils, so that other passes can use it as well 2025-06-05 06:45:18 +02:00
Erik Eckstein
502c0eb7b0 Optimizer: add InstructionWorklist.pushSuccessors 2025-06-05 06:45:17 +02:00
Erik Eckstein
294a50ced8 SIL: allow inserting specific instruction classes into an InstructionSet
Instead, remove `Operand.users(ofType:)` which returned a sequence of `Instruction` - which was a kind of replacement for the missing `InstructionSet` API
2025-06-05 06:45:17 +02:00
Erik Eckstein
8b6a03b38b AliasAnalysis: compute more precise memory behavior for debug_value undef and the prepareInitialization and zeroInitializer builtins 2025-06-05 06:45:17 +02: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
Andrew Trick
c030b78c7d LifetimeDependenceDiagnostics: fix source loc for implicit variables
Diagnostics on an indirect result ($return_value) did not report a source
location.
2025-06-04 11:41:15 -07:00
Andrew Trick
1d09c06ab1 LifetimeDependenceDiagnostics: diagnose indirect closure results.
Add support for diagnosing calls to closures that return a generic
non-Escapable result.

Closures do not yet model lifetime dependencies. The diagnostics have
a special case for handling nonescaple result with no lifetime
dependence, but it previously only handled direct results. This fix handles
cases like the following:

    func callIndirectClosure<T>(f: () -> NE<T>) -> NE<T> {
      f()
    }

Fixes rdar://134318846 ([nonescapable] diagnose function types with nonescapable results)
2025-06-04 11:40:42 -07:00
Erik Eckstein
cf55b9bede MandatoryPerformanceOptimizations: make sure to handle de-serialized vtable methods
When de-serializing a function and this function allocates a class, the methods of the de-serialized vtable must be handled, too.

Fixes an IRGen crash
rdar://152311945
2025-06-02 11:43:34 +02:00
Andrew Trick
51090b62bb LifetimeDependenceInsertion: allow dependency on Builtin.addressof()
This mostly makes it easier to test dependency corner cases. The analysis still
doesn't recognize UnsafeRawPointer.init(), so regular users still need to use
_overrideLifetime.

Fixes rdar://137608270 ([borrows] Add Builtin.addressof() support
for @addressable arguments)
2025-05-29 10:27:03 -07:00
Erik Eckstein
1a5ea19a85 SimplifyApply: don't do the raw-enum comparison optimization for custom RawRepresentable enums
Because we don't know what the custom `rawValue` can do.

Fixes a miscompile
rdar://152143111
2025-05-29 08:12:17 +02:00
Erik Eckstein
cac594fb86 Optimizer: peephole optimization for raw-value enum comparsions
Optimize (the very inefficient) RawRepresentable comparison function call to a simple compare of enum tags.
For example,
```
  enum E: String {
    case  a, b, c
  }
```
is compared by getting the raw values of both operands and doing a string compare.
This peephole optimizations replaces the call to such a comparison function with a direct compare of the enum tags, which boils down to a single integer comparison instruction.

rdar://151788987
2025-05-27 12:11:03 +02: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
8464b97771 AliasAnalysis: workaround a bug in the move-only checker
This is a workaround for a bug in the move-only checker: rdar://151841926.
The move-only checker sometimes inserts destroy_addr within read-only static access scopes.
Therefore don't consider static access scopes as immutable scopes.
2025-05-23 18:53:57 +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
Erik Eckstein
d10602ea28 SIL: improve and fix mark-dependence instruction APIs
* Move the mutating APIs into Context.swift, because SIL can only be mutated through a MutatingContext
* move the `baseOperand` and `base` properties from the instruction classes to the `MarkDependenceInstruction` protocol
* add `valueOrAddressOperand` and `valueOrAddress` in the `MarkDependenceInstruction` protocol
2025-05-23 18:53:57 +02:00
eeckstein
efa07a7557 Merge pull request #81702 from eeckstein/fix-test
InitializeStaticGlobals: handle `InlineArray.init(repeating:)` also for arm64_32
2025-05-23 00:55:20 +02:00
Erik Eckstein
b760ab0679 DeadStoreElimination: don't assume that the operand of an dealloc_stack is an alloc_stack.
It can also be a `partial_apply`.
Fixes a compiler crash
https://github.com/swiftlang/swift/issues/81698
rdar://151822502
2025-05-22 19:08:30 +02:00
Erik Eckstein
9e70eb814c InitializeStaticGlobals: handle InlineArray.init(repeating:) also for arm64_32
This needs matching a different builtin for scalar conversion for the `index_addr` index.

Fixes a test failure on watchos
rdar://151761870
2025-05-22 15:33:27 +02: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
Erik Eckstein
47db3fcfbb Optimizer: move the getGlobalInitialization utility from OptUtils.swift to SimplifyLoad.swift
Because it's not used anywhere else anymore
2025-05-20 20:46:40 +02:00
Erik Eckstein
51fda635a5 InitializeStaticGlobals: rewrite the pass to better optimize InlineArrays
Instead of looking for a single store to the global in a global-init function, build a GlobalInitValue tree.
This is a data structure representing the init value of the global. It can handle complex InlineArray initializations,
like `init(repeating:)`.

rdar://150859232
2025-05-20 20:46:40 +02:00
Erik Eckstein
d7580e4919 Optimizer: move the Value.lookThroughTruncOrBitCast from SimplifyPointerToAddress.swift to OptUtils.swift 2025-05-20 20:46:33 +02: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
140b883b9a SIL: let TypeValueInst.value return an optional Int
and don't require the client to check if it's an integer type.
Also, implement `var value` natively and without bridging.
2025-05-15 21:29:02 +02:00
Erik Eckstein
5dc71aa0a5 AST/SIL: support source location in diagnostics for de-serialized debug info
Diagnostics only work with `SourceLoc` which is basically a pointer into a buffer of the loaded source file.
But when debug info is de-serialized, the SIL `Location` consists of a filename+line+column.
To "convert" this to a `SourceLoc`, the file must be loaded.
This change adds `DiagnosticEngine.getLocationFromExternalSource` for this purpose.
Also, the new protocol `ProvidingSourceLocation` - to which `SourceLoc` and `Location` conform - help to generalize the helper struct `Diagnostic` and make this "conversion" happen automatically.
2025-05-14 11:43:47 +02:00
Erik Eckstein
5d24a26f5e TempRValueElimination: handle store instructions to the temporary stack location.
Treat `load`-`store` pairs as if they were `copy_addr` instructions.
This can catch more cases. For example it can eliminate unnecessary copies of InlineArray when subscripting it.

rdar://149250346
2025-05-13 08:39:26 +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
d28384dbd7 SILGen: use vector_base_addr in InlineArray literals 2025-05-12 19:25:12 +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
17fbcb5314 Swift SIL: fix SIL.Type.builtinVectorElementType
The element type must be lowered. Otherwise it would crash for e.g. function types as element types.
2025-05-12 19:24:31 +02:00
Erik Eckstein
be322877b1 EmbeddedSwiftDiagnostics: improve error message for non-specialized generic function calls
Tell the user if the specialization isn't done because of a dynamic Self type.

rdar://150865684
2025-05-08 19:21:08 +02:00
Erik Eckstein
f52f491936 EmbeddedSwiftDiagnostics: check apply diagnostics also for begin_apply and partial_apply 2025-05-08 19:21:08 +02:00
Erik Eckstein
d9c7a68249 EmbeddedSwiftDiagnostics: fix a wrong "cannot use co-routines (like accessors) in -no-allocations mode" error
rdar://150890424
2025-05-08 19:21:08 +02:00
Erik Eckstein
9348f5e8d4 MandatoryPerformanceOptimizations: force inlining of transparent co-routines
This might be necessary if `-enable-testing` is turned on, because in this mode function linkages are different than in a regular build.
2025-05-08 19:21:08 +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
Erik Eckstein
d2f8c0cf49 Swift SIL: add some Instruction APIs
* `OpenExistentialAddrInst.isImmutable`
* `YieldInst.convention`
* `CopyAddrInst.set(isTakeOfSource:)` and `CopyAddrInst.set(isInitializationOfDestination:)`
2025-05-06 12:35:21 +02:00
Erik Eckstein
8f39c3cad8 Swift SIL: rename isTakeOfSrc -> isTakeOfSource and isInitializationOfDest -> isInitializationOfDestination
Trying to avoid abbreviations in ABI names
2025-05-06 12:35:21 +02:00
Erik Eckstein
67425a541e Swift Optimizer: introduce InstructionSetWithCount
It's based on `InstructionSet` but also provides a `count` property
2025-05-06 12:35:20 +02:00
Andrew Trick
170c563b00 [NFC] cleanup Instruction/Value.findVarDecl() APIs.
These APIs are quite convoluted. The checks for var_decl need to be performed in
just the right order. The is a consequence of complexity in the SIL
representation itself, not a problem with the APIs.

It is common for code to accidentally call a less-complete form of the API. It
is essential that they be defined in a central location, and the we get the same
answer whether we start with an Instruction, Argument, or Value. The primary
public interface should always check for debug_value users. The varDecl property
is actually an implementation detail.

It is questionable whether a function like findVarDecl() that returns a basic
property of SIL and does not require arguments should be a property instead. It
is a function to hint that it may scan the use-list, which is not something
we normally want SIL properties to do. Use-lists can grow linearly in function
size. But, again, this is a natural result of the SIL representation and needs
to be considered an implementation detail.
2025-05-05 22:51:01 -07:00