Commit Graph

674 Commits

Author SHA1 Message Date
Erik Eckstein
094b246874 SIL: FunctionArgument.copyFlags needs a MutatingContext argument
SIL may only be modified through a MutatingContext. Otherwise analysis notifications may get lost.
2025-06-20 08:14:59 +02:00
Erik Eckstein
d025e9f7a5 SIL: add var Argument.decl: ValueDecl? 2025-06-20 08:14:58 +02:00
Anthony Latsis
722bc0f086 ASTBridging: Bridge swift::DiagID directly 2025-06-19 12:29:27 +01:00
eeckstein
2237d8615e Merge pull request #82142 from eeckstein/mandatory-perf-opt
MandatoryPerformanceOptimizations: some refactoring
2025-06-10 17:45:17 +02:00
Erik Eckstein
8d73f881e8 SIL: let getNominalFields return nil for a struct with unreferenceable storage
Like C bitfields.
Fixes a crash in the InitializeStaticGlobals pass in case a global having a C struct type with bitfield is initialized statically.
2025-06-10 10:55:40 +02:00
Erik Eckstein
f0b3ec05b4 MandatoryPerformanceOptimizations: some refactoring
NFC
2025-06-10 08:10:42 +02: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
eeckstein
a33ff9879a Merge pull request #81969 from eeckstein/temp-lvalue-elimination
Optimizer: improve TempLValueOpt
2025-06-06 15:30:29 +02:00
eeckstein
98aeec6563 Merge pull request #82014 from eeckstein/address-walkers
SIL: handle `drop_deinit` in WalkUtils
2025-06-06 15:28:09 +02:00
Erik Eckstein
70ba193cd4 SIL: handle drop_deinit in WalkUtils
Also, handle some other missing instructions in the AddressDefUseWalker, which are visited in the AddressUseDefWalker.

This enables various other optimizations, like copy elimination, in the presence of `drop_deinit`.

rdar://152307747
2025-06-05 14:13:51 +02:00
Erik Eckstein
b4010e61a3 SIL: add a small utility Instruction.isUsing which checks if a value is in its operand list 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
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
Erik Eckstein
d8b5b3dc95 Swift SIL: Fix argument conventions for functions which have both, a direct and indirect result.
The function convention for the first argument is not identified as indirect-out.
This lets alias analysis assume that the memory pointed to by argument 0 cannot be written by the called function.

The problem is that subscripting a LazyFilterCollection (with the base index, e.g. `Int`) does not work as expected, because it returns the nth element of the base collection!
The fix is to implement the subscript "manually".

Fixes a mis-compile.
rdar://152160748
2025-05-28 18:05:34 +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
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
e3b770853a SIL: add GlobalVariable.type 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
f6ce61cf43 SIL: support vector_base_addr in AccessUtils and WalkUtils
This enables alias analysis and optimizations, like, redundant load elimination, to benefit from it.
2025-05-12 19:24:32 +02:00
Erik Eckstein
0e790fcc5d SIL: support vector_base_addr in SmallProjectionPath
Add a `vectorBase` field kind which corresponds to `vector_base_addr`.
Also, improve `mayOverlap`: it didn't catch the case that the first element of a vector cannot overlap with another element.
2025-05-12 19:24:32 +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
ff2341e83a Swift AST/SIL: add Type APIs for Builtin.FixedArray
* `Type.isBuiltinFixedArray`
* `Type.builtinFixedArrayElementType`
2025-05-12 19:24:31 +02:00
Erik Eckstein
e6b9374b49 SIL: add AccessBase.storageIsLexical 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
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
Meghana Gupta
ba845de874 Fix newly introduced warnings in LifetimeDependenceScopeFixup 2025-05-05 11:22:16 -07:00
Erik Eckstein
93c41c649c Swift SIL: add Builder.createUncheckedOwnershipConversion 2025-05-02 18:07:57 +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
Andrew Trick
b670a6d9cc Merge pull request #81191 from atrick/var-walker-recursion
LifetimeDependenceDiagnostics: bug fixes and output clarity
2025-04-30 15:36:32 -07:00
Meghana Gupta
35d62a4a36 Introduce end_cow_mutation_addr instruction 2025-04-30 13:39:45 -07:00
Andrew Trick
83b0ce1098 LifetimeDependenceDiagnostics: note for unsupported closure capture
Add a note explaining that dependence on closure captures is not
supported. Otherwise, the diagnostics are very confusing:
"it depends on a closure capture; this is not yet supported"
2025-04-29 23:55:55 -07:00
Andrew Trick
25e9cbf3f1 [NFC] SwiftCompilerSource: bridge Function.accessorKindName 2025-04-29 22:09:42 -07:00
Andrew Trick
49755bd0ed Refactor debugVarDecl so arguments also support debug_value users. 2025-04-29 22:09:42 -07:00
Erik Eckstein
ffc17322f9 SIL: bridge Function.isDeinitBarrier back from Swift to C++ SIL
This API already existed in Swift, but not in the C++ SIL.
2025-04-29 20:30:21 +02:00
Erik Eckstein
65178170b1 ComputeSideEffects: handle program termination functions which are defined in the same module
This case was neglected. The fix can result in better side effect analysis, e.g. in the stdlib core module.
2025-04-29 20:30:21 +02:00
Erik Eckstein
d80f7a3560 SIL: make BeginBorrowInstruction.endOperands and BeginBorrowValue.scopeEndingOperands consistent.
* rename `ScopedInstruction.endOperands` -> `scopeEndingOperands`
* let them behave the same way. For `load_borrow` there was a difference because `endOperands` didn't consider branches to re-borrow phis.
2025-04-28 10:24:56 +02:00
Erik Eckstein
cbf5c196b2 SIL: rename protocol BorrowIntroducingInstruction -> BeginBorrowInstruction
to match with `BeginBorrowValue`
2025-04-28 10:07:45 +02:00
Erik Eckstein
0dee86590d SIL: make protocol ScopeInstruction an Instruction protocol
This allows to remove the `var instruction` property.

NFC
2025-04-28 09:40:06 +02:00
Andrew Trick
33fbe11bf8 Fix LifetimeDependenceScopeFixup: read access can depend on 'inout'
Allow a dependency on a local [read] access scope to be transfered to the
caller's [modify] access.

Fixes rdar://149560133 (Invalid lifetime dependence error when
trying to return Span from an inout argument)
2025-04-22 11:54:53 -07:00
Andrew Trick
a1aaed9159 LifetimeDependenceScopeFixup: handle returning dependent Optional
Add support for returnValue phis (e.g. to return an Optional .some or .none).

Fixes rdar://149397018 (Wrapping non escapable in an Optional
(or any copy lifetime wrapper) is an escape)
2025-04-21 22:33:38 -07:00
Erik Eckstein
7a8a50a2b3 Swift SIL: add Type.hasValidSignatureForEmbedded 2025-04-18 06:58:40 +02: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
Erik Eckstein
cab62320eb Swift SIL: improvements for Location
* make `var sourceLoc` return nil if the location doesn't have a valid line number
* add `var decl`
2025-04-18 06:58:39 +02:00
Erik Eckstein
5a67af507e Swift SIL: add Function.isSpecialization 2025-04-18 06:58:39 +02:00
Erik Eckstein
dbf1bf0e43 Swift SIL: add a comment for VTable.specializedClassType 2025-04-18 06:58:39 +02:00
Erik Eckstein
e154e12913 Swift SIL: add APIs for box types 2025-04-18 06:58:39 +02:00