Commit Graph

508 Commits

Author SHA1 Message Date
Doug Gregor
e0b52cd20e [SIL] Extend checked-cast instructions with "prohibit isolated conformances" flag
When performing a dynamic cast to an existential type that satisfies
(Metatype)Sendable, it is unsafe to allow isolated conformances of any
kind to satisfy protocol requirements for the existential. Identify
these cases and mark the corresponding cast instructions with a new flag,
`[prohibit_isolated_conformances]` that will be used to indicate to the
runtime that isolated conformances need to be rejected.
2025-03-26 22:31:47 -07:00
Andrew Trick
97b249bd11 Merge pull request #80263 from atrick/markdep-addr
SIL: add mark_dependence_addr
2025-03-26 10:33:42 -07:00
Erik Eckstein
85a6df30d6 SIL: add lookup(method:) in VTable and WitnessTable 2025-03-26 08:45:23 +01:00
Erik Eckstein
1844c14c44 SIL: add var ClassMethodInst.member 2025-03-26 08:45:07 +01:00
Erik Eckstein
67556cfbf4 SIL: add some Function APIs
* `var wasDeserializedCanonical`
* `var genericSignature`
* `func mapTypeIntoContext`
* `var forwardingSubstitutionMap`
2025-03-26 08:45:07 +01:00
Erik Eckstein
1ce545ba3d SIL: add some DeclRef APIs
* `var decl`
* `func ==`
* `func calleesAreStaticallyKnowable`
2025-03-26 08:43:55 +01:00
Erik Eckstein
d523e303c3 SIL/AST: move some SIL.Type APIs to the TypeProperties protocol which makes them also available for AST.Type and AST.CanonicalType 2025-03-26 07:30:10 +01:00
Erik Eckstein
380ea889bc SIL: fix self-argument APIs in Function
`var selfArgument` should return an optional, i.e. nil if the function doesn't have a self argument.
2025-03-26 07:30:10 +01:00
Andrew Trick
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -07:00
Andrew Trick
e7000e4668 SIL: Add mark_dependence_addr 2025-03-25 23:02:42 -07:00
Andrew Trick
001b162dc9 Surface addressableForDeps in SILType and FunctionConventions
With this approach, you cannot tell whether a parameter is addressable only
from the function type. Instead you need the SILValue that will be passed to the
call site.
2025-03-23 19:00:42 -07:00
Andrew Trick
7cecf57e50 Add SILType::isAddressableForDeps 2025-03-23 19:00:42 -07:00
Andrew Trick
a431a7bc5f LifetimeDependence: conditionallyAddressableParamIndices
Preserve conditionallyAddressableParamIndices independent of any
addressableParamIndices. The conditional dependencies are subject to change
based on type substitution.
2025-03-23 16:59:11 -07:00
Slava Pestov
8bcd09aaa4 SIL: Preliminary refactoring of SILWitnessTable::AssociatedConformanceWitness
The Protocol field isn't really necessary, because the conformance
stores the protocol. But we do need the substituted subject type
of the requirement, just temporarily, until an abstract conformance
stores its own subject type too.
2025-03-18 19:38:42 -04:00
Andrew Trick
ce18a4bce8 Merge pull request #79985 from atrick/fix-destroyhoist
Fix DestroyHoisting: InteriorLiveness for noescape closures
2025-03-18 05:07:27 -07:00
Erik Eckstein
d31d82c6fe SIL: remove a workaround for a Windows 5.10 host compiler crash
https://github.com/apple/swift/issues/73253 should be fixed in 6.0, which is now used as host compiler.
2025-03-14 09:49:53 +01:00
Erik Eckstein
d52f7d1619 AST/SIL: Refactor and simplify AST.Type, AST.CanonicalType and SIL.Type
* let `SIL.Type` conform to `TypeProperties` to share the implementation of common type properties between the AST types and `SIL.Type`
* call references to an `AST.Type` `rawType` (instead of just `type`)
* remove unneeded stuff
* add comments
2025-03-14 09:40:22 +01:00
Andrew Trick
995c74966d Fix DestroyHoisting: InteriorLiveness for noescape closures
InteriorLiveness has a new "visitInnerUses" mode used by DestroyHoisting. That
mode may visit dependent values, which was not valid for noescape
closures. ClosureLifetimeFixup inserts destroys of noescape closures after the
destroys of the captures. So following such dependent value could result in an
apparent use-after-destroy. This causes DestroyHoisting to insert redundant
destroys.

Fix: InteriorUses will conservatively only follow dependent values if they are
escapable. Non-escapable values, like noescape closures are now considered
escapes of the original value that the non-escapable value depends on. This can
be improved in the future, but we may want to rewrite ClosureLifetimeFixup first.

Fixes the root cause of: rdar://146142041
2025-03-13 09:17:31 -07:00
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