Commit Graph

490 Commits

Author SHA1 Message Date
WeZZard
4c479824ef [SILOptimizer] Eliminate non-case handling for implicit Optional non-Optional casting of unchecked_ref_cast in ValueDefUseWalker. 2025-03-11 09:39:50 +08:00
WeZZard
2237faca04 [SILOptimizer] Refines the implementation of of the bridged SILType for Swift. 2025-03-11 09:29:11 +08:00
WeZZard
35f82ba402 [SILOptimizer] Fixes the ValueDefUseWalker to handle potential unchecked_ref_cast between optional and non-optional class types.
The `unchecked_ref_cast` is designed to be able to cast between
`Optional<ClassType>` and `ClassType`. We need to handle these cases by
checking if the type is optional and adjust the path accordingly.
2025-03-10 19:17:31 +08:00
Erik Eckstein
77dab77702 Swift SIL: make isLegalFormalType available in AST.Type 2025-03-07 15:59:34 +01:00
Erik Eckstein
0f55220cc8 SIL: add var FullApplySite.singleDirectErrorResult 2025-03-07 15:59:34 +01:00
Erik Eckstein
213da24649 SIL: add an overload of Sequence<Operand>.users(ofType:) which returns a sequence of the specific instruction class and not a sequence of Instruction 2025-03-07 15:59:34 +01:00
Erik Eckstein
7bbd9a9c99 SIL: add var Instruction.typeDependentOperands
And simplify the implementation of `var Instruction.definedOperands`
2025-03-07 15:59:34 +01:00
Erik Eckstein
810064b7dc Cleanup and additions to AST and SIL Type/CanonicalType
* factor out common methods of AST Type/CanonicalType into a `TypeProperties` protocol.
* add more APIs to AST Type/CanoncialType.
* move `MetatypeRepresentation` from SIL.Type to AST.Type and implement it with a swift enum.
* let `Builder.createMetatype` get a CanonicalType as instance type, because the instance type must not be a lowered type.
2025-03-07 15:59:33 +01:00
Erik Eckstein
4595d5da22 SIL Builder: make createInitExistentialMetatype more usable by passing the conformances in an Array
Instead of a ConformanceArray
2025-03-07 15:59:33 +01:00
Erik Eckstein
db03a55c79 Swift SIL: add some Builder APIs
* `createCheckedCastAddrBranch`
* `createUnconditionalCheckedCastAddr`
* `createDebugValue`
* `createWitnessMethod`
* `createInitEnumDataAddr`
2025-03-07 15:59:33 +01:00
Erik Eckstein
f7466eac78 SIL: fix bridging of SILDebugVariable
The optional C++ type was bridged to a non-optional Swift type.
The correct way is to bridged the non-optional C++ type to the non-optional Swift type.
2025-03-07 15:59:33 +01:00
Erik Eckstein
31057332ff Swift SIL: add some Instruction APIs
* getting the formal source and target types of casts
* `isFromVarDecl` and `usesMoveableValueDebugInfo` for AllocStackInst
* WitnessMethod APIs
* `TryApply.isNonAsync`
2025-03-07 15:59:33 +01:00
Erik Eckstein
8adec32bf9 SIL: fix two comments in Type.swift
There is no `isNominal` property in SIL.Type.
2025-03-07 15:59:33 +01:00
Erik Eckstein
48db89031a SIL: be more tolerant when a user is deleted during use-list iteration.
Check if an operand's instruction has been deleted in `UseList.next()`.
This allows to delete an instruction, which has two uses of a value, during use-list iteration.
2025-03-07 15:59:32 +01:00
Andrew Trick
a0afb188b9 Add ScopedInstruction.instruction
So we can use existentials of this type.

This seems really inefficient, but I don't have another solution.
2025-03-05 02:09:55 -08:00
Andrew Trick
bc6200e592 SwiftCompilerSources: bridge '@'_addressable dependencies 2025-03-03 11:56:37 -08:00
Andrew Trick
dc8778f33d Add MarkDependenstInst.hasScopedLifetime 2025-02-25 23:08:55 -08:00
Erik Eckstein
e0b4f71af6 SIL: remove the alloc_vector instruction
It's not needed anymore, because the "FixedArray" experimental feature is replaced by inline-arrays.
2025-02-12 10:51:14 +01:00
Andrew Trick
fa3ebb5c1e LifetimeDependence.Scope add global variable support.
This fixes functions that return @lifetime(immortal).
2025-02-10 09:11:22 -08:00
Andrew Trick
18de6051de Add BeginAccess.AccessKind.isCompatible(with:ArgumentConvention)
General utility to determine whether an argument already has an access scope
that is compatibly with this BeginAccess.
2025-02-10 09:11:22 -08:00
Andrew Trick
128b0e711a Add Value.isForwarded(from:)
A simple utility on ForwardingInstructions that should be defined alongside
similar utilities rather than inside a pass.
2025-02-10 09:11:22 -08:00
Andrew Trick
f56d98408c Fix VariableScopeInstruction.endOperands for extend_lifetime.
Consider end_lifetime to be part of the endOperands set, which is used for
linear liveness, even though it does not end the lifetime.
2025-02-10 09:11:22 -08:00
Andrew Trick
c0ad676ac6 Add AccessBaseAndScopes API: outerAddress, enclosingAddress. 2025-02-10 09:11:22 -08:00
Andrew Trick
0f537b220e Add BeginAccessInst.unsafeAddressorSelf. 2025-02-10 09:11:21 -08:00
Andrew Trick
84e9e0157a SwiftCompilerSources: Add SingleInlineArray.popLast() 2025-02-10 09:11:21 -08:00
Andrew Trick
e705a6d7c3 Temporarily introduce AnyInteriorPointer operand ownership.
This is necessary to fix a recent OSSA bug that breaks common occurrences on
mark_dependence [nonescaping]. Rather than reverting that change above, we make
forward progress toward implicit borrows scopes, as was the original intention.

In the near future, all InteriorPointer instructions will create an implicit
borrow scope. This means we have the option of not emitting extraneous
begin/end_borrow instructions around intructions like ref_element_addr,
open_existential, and project_box. After that, we can also migrate
GuaranteedForwarding instructions like tuple_extract and struct_extract.
2025-02-05 16:23:02 -08:00
Arnold Schwaighofer
0fe3cf0096 SIL: Update formal source type in SimplifyRefCasts when possible
We use the formal source type do decide whether a checked_cast_br is
known to succeed/fail. If we don't update it we loose that optimization

That is:

```
  checked_cast_br AnyObject in %2 : X to X, bb1, bb2
```

Will not be simplified even though the operand and the destintation type
matches.
2025-01-29 09:07:21 -08:00
Erik Eckstein
3ec5d7de24 SIL: replace the is_escaping_closure instruction with destroy_not_escaped_closure
The problem with `is_escaping_closure` was that it didn't consume its operand and therefore reference count checks were unreliable.
For example, copy-propagation could break it.
As this instruction was always used together with an immediately following `destroy_value` of the closure, it makes sense to combine both into a `destroy_not_escaped_closure`.
It
1. checks the reference count and returns true if it is 1
2. consumes and destroys the operand
2025-01-24 19:23:27 +01:00
Michael Gottesman
05511c955a [sil] Add conformance to UnaryInstruction for IgnoredUseInst. 2025-01-23 10:58:45 -08:00
Michael Gottesman
7ae56aab2e [sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but
instead only suppress unused variable warnings. This patch just adds the
instruction.

Eventually, we can use it to move the unused variable warning from Sema to SIL
slimmming the type checker down a little bit... but for now I am using it so
that other diagnostic passes can have a SIL instruction (with SIL location) so
that we can emit diagnostics on code like _ = x. Today we just do not emit
anything at all for that case so a diagnostic SIL pass would not see any
instruction that it could emit a diagnostic upon. In the next patch of this
series, I am going to add SILGen support to do that.
2025-01-22 21:12:36 -08:00
Andrew Trick
b0f2ca03a7 AccessUtils: allow mark_deps to be tracked by the EnclosingScope.
This encourages AccessPathWalker clients to handle enclosing mark_deps. In
some cases, it is necessary. The accessBaseWithScopes API now provides both
nested begin_access and mark_dependence.
2025-01-11 15:40:22 -08:00
Erik Eckstein
43b6fd4edb SIL: add the IndexingInstruction protocol
It defines (and implements) the `base` and `index` properties, which are used in the conforming classes `IndexRawPointerInst`, `IndexAddrInst` and `TailAddrInst`
2024-12-21 08:28:21 +01:00
Erik Eckstein
969a788e7f SIL: add the Builtin.name getter and Builtin.arguments 2024-12-21 08:28:21 +01:00
Erik Eckstein
1856d4e94c SIL: add APIs to set and get the alignment of a pointer_to_address instruction.
Also add a getter for the `isInvariant` property.
2024-12-21 08:28:21 +01:00
Erik Eckstein
2d10da001a SIL: add some Builder-create functions 2024-12-21 08:28:20 +01:00
Erik Eckstein
f8e013a21c Fix a compiler warning in SIL/Function.swift 2024-12-20 09:10:17 +01:00
Erik Eckstein
4fdf16de1b SIL: add CanonicalType.canBeClass
And move the implementation of `SIL.Type.canBeClass` to the AST Type. The SIL Type just calls the AST Type implementation.
Also rename `SIL.Type.canonicalASTType` -> `SIL.Type.astType`.
2024-12-19 20:53:46 +01:00
Erik Eckstein
ca7facde35 SIL: add some use-list APIs
* `users`: maps from a sequence of operands to a sequence of instructions
* `users(ofType:)` : as `users`, but filters users of a certain instruction type
* `Value.users`: = `Value.uses.users`
2024-12-19 20:53:45 +01:00
Erik Eckstein
1545e01ab5 SIL: Let SubstitutionMap.replacementTypes return AST types rather than optional SIL types.
This is what the C++ SubstitutionMap does. One has to use `Type.loweredType` to get from the AST type to the SIL type.
2024-12-19 20:53:45 +01:00
Erik Eckstein
1f4332ad39 SIL: add AST.Type.loweredType and CanonicalType.loweredType 2024-12-19 20:53:45 +01:00
Erik Eckstein
50bf66e217 SIL: replace CanonicalType.objectType and CanonicalType.addressType with silType
`SIL.Type` itself has `objectType` and `addressType` to convert between both variants.
2024-12-19 20:53:45 +01:00
Hamish Knight
95ae8d1b16 Merge pull request #78288 from hamishknight/use-typename
[SwiftCompilerSources] Use interpolation instead of `String(describing:)`
2024-12-19 19:26:50 +00:00
Hamish Knight
f728466273 [SwiftCompilerSources] Use interpolation instead of String(describing:)
`String(describing:)` does a bunch of dynamic casts
that can be pretty slow. Use interpolation instead,
which bypasses them.

For `swift-frontend`, this brings the time taken
for type-checking an empty file down from ~100ms
to ~70ms.

For `swift build`, this brings the time taken for
a null build down from ~600ms to ~450ms (the
larger delta is presumably due to the fact that
there's much more Swift code in `swift-package`).
2024-12-19 15:33:39 +00:00
Andrew Trick
7a11d5a3f1 SwiftCompilerSources: bridge Function.sourceFileKind. 2024-12-18 16:56:45 -08:00
Andrew Trick
3897929e4a LifetimeDependence: handle dependence on trivial values. 2024-12-16 16:09:37 -08:00
Andrew Trick
98da813f02 [NFC] SwiftCompilerSources: add isConvertPointerToPointerArgument 2024-12-14 22:46:54 -08:00
Andrew Trick
f5fc17804e [NFC] SwiftCompilerSources: Add VariableScopeInstruction utility
To diagnose dependence on trivial variables.
2024-12-14 22:46:54 -08:00
Erik Eckstein
bd08d6afa1 SIL: make Operand Equatable
To be able to use `!=` for operands.
2024-12-12 08:36:58 +01:00
Erik Eckstein
62b26dc8c4 SIL: make isEmpty and singleElement available for all Sequences
and not just for `CollectionLikeSequence`.
To support stdlib's lazy map and filter sequences.
2024-12-10 16:28:11 +01:00
Erik Eckstein
bcdbc05260 SIL: make Builde.insertionPoint public 2024-12-10 16:28:11 +01:00