Commit Graph

763 Commits

Author SHA1 Message Date
Erik Eckstein
367dc1a299 SwiftCompilerSources: add OperandSet and OperandWorklist
Implemented by bridging the C++ OperandSet, similar to BasicBlockSet and NodeSet
2024-03-21 15:52:39 +01:00
Kuba Mracek
b642d771be [embedded] Compile-time (literal) KeyPaths for Embedded Swift
Enable KeyPath/AnyKeyPath/PartialKeyPath/WritableKeyPath in Embedded Swift, but
for compile-time use only:

- Add keypath optimizations into the mandatory optimizations pipeline
- Allow keypath optimizations to look through begin_borrow, to make them work
  even in OSSA.
- If a use of a KeyPath doesn't optimize away, diagnose in PerformanceDiagnostics
- Make UnsafePointer.pointer(to:) transparent to allow the keypath optimization
  to happen in the callers of UnsafePointer.pointer(to:).
2024-03-20 15:35:46 -07:00
Andrew Trick
7da493498a Merge pull request #72407 from atrick/pointer-escape-bailout
Fix lifetime dependence in the presence of pointer escapes.
2024-03-19 11:40:38 -07:00
Erik Eckstein
1d4054ca32 Optimizer: add simplification for checked_cast_addr_br instructions 2024-03-19 11:58:15 +01:00
Erik Eckstein
d34dec835c SimplifyBuiltin: use dynamic cast utilities in folding metatype comparisons 2024-03-19 11:58:15 +01:00
Erik Eckstein
308b5f9ff7 Swift Optimizer: add bridging for dynamic cast utilities
* `func canDynamicallyCast`
* `var CheckedCastAddrBranchInst.dynamicCastResult`
2024-03-19 10:54:38 +01:00
Andrew Trick
3e7d9dba71 AddressUseVisitor: follow mark_dependence [nonescaping].
Treat mark_dependence [nonescaping] as a dependent value even if the dependence base does not have a recognizable
scope (e.g. a multiply-defined alloc_stack). This happens because ClosureLifetimeFixup creates redundant mark_dependence
instructions for partial_apply captures. We constantly need to work around this broken representation of nonescaping closures.
2024-03-18 17:45:33 -07:00
Andrew Trick
e77fff3d23 LocalVariableUtils logging 2024-03-18 17:45:33 -07:00
Andrew Trick
f1fbbcd438 Handle escaping addresses in LocalVariableAccessWalker
Fixes rdar://124564951 (Compiler crash when evaluating pointer escape
in autoclosure; LifetimeDepenenceScopeFixup; Fatal error: check
findPointerEscape() before computing interior liveness.)
2024-03-18 17:45:33 -07:00
Andrew Trick
73e28df4be ForwardingUtils logging 2024-03-18 17:45:33 -07:00
Andrew Trick
00fbf4f1af LocalVariableUtils logging 2024-03-18 17:45:33 -07:00
Andrew Trick
352724ea0d LifetimeDependenceScopeFixup logging 2024-03-18 17:45:33 -07:00
Andrew Trick
b7eaee9448 Disable verbose LifetimeDependenceUtils 2024-03-18 17:45:28 -07:00
Andrew Trick
009bc58045 Disable verbose AddressUtils 2024-03-18 17:45:28 -07:00
Andrew Trick
ff14633532 Handle ConvertEscapeToNoEscapeInst in NonEscapingClosureDefUseWalker 2024-03-18 17:38:12 -07:00
Andrew Trick
5e9e282b62 LifetimeDependenceUtils comment clarification and cleanup. 2024-03-18 17:38:12 -07:00
Andrew Trick
95a100a9c0 Add a comment in LifetimeDependenceUseDefWalker. 2024-03-18 17:38:12 -07:00
Andrew Trick
bdabc2145a Add computeKnownLiveness utility
To fix LifetimeDependenceScopeFixup in the presense of pointer escapes.
2024-03-18 17:38:12 -07:00
Andrew Trick
aa208bbf91 Add SILType::containsNoEscapeFunction()
Add PartialApplyInst.hasNoescapeCapture
Add PartialApplyInst.mayEscape

Refactor DiagnoseInvalidEscapingCaptures. This may change functionality because tuples containing a noescape closure are now correctly recognized. Although I'm not sure such tupes can ever be captured directly.
2024-03-18 17:38:12 -07:00
Kuba Mracek
f5797941b7 [embedded] Specialize superclasses in VTableSpecializer as part of MandatoryPerformanceOptimizations 2024-03-15 21:30:08 -07:00
Erik Eckstein
c5c2688633 InitializeStaticGlobals: don't merge stores for structs with unreferencable storage
like C bitfields.

Fixes a compiler crash
rdar://122360051
2024-03-08 11:14:48 +01:00
Andrew Trick
3364648f2b Merge pull request #72148 from atrick/enable-lifetime-diagnostics
Enable lifetime dependence diagnostics for Nonescapable types.
2024-03-07 18:54:33 -08:00
John McCall
8499640224 Merge pull request #72143 from rjmccall/builtin-create-task
Unify and simplify the task-creation builtins
2024-03-07 21:45:06 -05:00
Andrew Trick
8419cc599f Guard LifetimeDependenceScopeFixup under experimental NonescapableTypes
Out of paranoia, don't even try to run this path unless we see
-enable-experimental-feature NonescapableTypes.
2024-03-07 15:13:50 -08:00
Andrew Trick
df7326d790 Fix SILType::isEscapable for box types.
SILBoxTypes have their own generic signature and substitution
map. This means that every time we query isEscapable or mayEscape, we
need to extract the type of the box's field and perform type
substitution so that the AST query only sees types from the function's
generic environment.

Fixes rdar://124179106 (Assertion failed in SIL:
(!type->hasTypeParameter() && "caller forgot to mapTypeIntoContext!"))
2024-03-07 13:34:46 -08:00
John McCall
0a282c044f Unify all of the task-creation builtins coming out of SILGen.
We've been building up this exponential explosion of task-creation
builtins because it's not currently possible to overload builtins.
As long as all of the operands are scalar, though, it's pretty easy
to peephole optional injections in IRGen, which means we can at
least just use a single builtin in SIL and then break it apart in
IRGen to decide which options to set.

I also eliminated the metadata argument, which can easily be recreated
from the substitutions.  I also added proper verification for the builtin,
which required (1) getting `@Sendable` right more consistently and (2)
updating a bunch of tests checking for things that are not actually
valid, like passing a function that returns an Int directly.
2024-03-06 22:21:12 -05:00
eeckstein
d1ec089c72 Merge pull request #72088 from language-core/main
chore: fix some comments
2024-03-06 10:08:50 +01:00
Andrew Trick
31b3a91912 Merge pull request #71703 from atrick/lifetime-extend
LifetimeDependenceScopeFixup: fix handling of returned dependence
2024-03-05 23:17:21 -08:00
Andrew Trick
3bee32639b LifetimeDependenceDefUseWalker: use LocalVariableReachableUses. 2024-03-05 18:08:16 -08:00
Andrew Trick
ffe9c48976 Add LocalVariableUtils
This includes a reaching-def analysis that can be used by diagnostic passes. Diagnostics should be flow-sensitive.
2024-03-05 18:08:16 -08:00
Andrew Trick
75f2f88bce Add AddressOwnershipLiveRange
A live range representing the ownership of addressible memory.

This live range represents the minimal guaranteed lifetime of the object being addressed. Uses of derived addresses
may be extended up to the ends of this scope without violating ownership.

.liveOut objects (@in_guaranteed, @out and globals) have no instruction range.

.local objects (alloc_stack, yield, @in, @inout) report the single live range of the full assignment that reaches
this address.

.owned values (boxes and references) simply report OSSA liveness.

.borrow values report each borrow scope's range. The effective live range is their intersection. A valid use must
lie within
2024-03-05 17:08:13 -08:00
Andrew Trick
e84bcf520f Add computeBorrowLiveRange
Compute the live range for the borrow scopes of a guaranteed value. This returns a separate instruction range for
each of the value's borrow introducers. Unioning those ranges would be incorrect. We typically want their
intersection.
2024-03-05 17:08:13 -08:00
Andrew Trick
d01d74e20c LifetimeDependenceDiagnostics: find the correct variable introducer. 2024-03-05 17:08:13 -08:00
Andrew Trick
73f7f8f28a Add NonEscapingClosureDefUseWalker
To find all effective applications of partial_apply [on-stack].
2024-03-05 17:08:13 -08:00
Andrew Trick
a3fe978cc4 LifetimeDependenceUtils: handle borrowed ProjectBox 2024-03-05 17:08:13 -08:00
Andrew Trick
5c956f7437 Review feedback; only rewrite access scopes when necessary 2024-03-05 17:08:13 -08:00
Andrew Trick
7dea051bf6 Review feedback; Builder.createEndAccess() 2024-03-05 17:08:13 -08:00
Andrew Trick
d1a75e725c LifetimeDependenceScopeFixup: handle non-dominated dependent uses. 2024-03-05 17:08:13 -08:00
Andrew Trick
1122cc445f LifetimeDependenceScopeFixup: fix handling of returned dependence
Only rewrite the mark_dependence to depend on the function argument when the
dependent value is actually returned.

Also, find all uses even if an escaping use is seen.
2024-03-05 17:08:13 -08:00
Andrew Trick
8aa1d91a2c LifetimeDependence: clarify log headers 2024-03-05 17:08:13 -08:00
Andrew Trick
ef940c5297 Redesign gatherBorrowIntroducers to return BeginBorrowValues.
This avoids a lot of confusion because the callers expect this type. Fixing it just required some redundancy and
bridging in the EnclosigValues implementation.
2024-03-05 17:08:13 -08:00
Andrew Trick
a0c85be52c OwnershipLiveness: handle ProjectBox like an interior pointer. 2024-03-05 17:08:13 -08:00
Alejandro Alonso
1752b48a78 Merge pull request #71997 from Azoy/fix-zero-init-escape
[EscapeAnalysis] Handle atomic instructions in escape analysis
2024-03-05 13:44:46 -08:00
Alejandro Alonso
254e4f041f Handle atomic instructions in escape analysis
Split atomic loads/stores

no more guard

invert condition
2024-03-05 08:32:51 -08:00
cui fliter
127077b3aa chore: fix some comments
Signed-off-by: cui fliter <imcusg@gmail.com>
2024-03-05 17:23:22 +08:00
Andrew Trick
7aab3c54ec Factor InstructionRange.beginningInstruction for use elsewhere. 2024-03-04 16:03:54 -08:00
Erik Eckstein
8137adf89e SwiftCompilerSources: support Undef.parentFunction and PlaceholderValue.parentFunction
After support in the C++ SIL, we can implement parentFunction for those values in swift, too.
It simplifies a few things.
2024-03-01 09:07:12 +01:00
Michael Gottesman
11f0ff6e32 [sil] Ensure that all SILValues have a parent function by making it so that SILUndef is uniqued at the function instead of module level.
For years, optimizer engineers have been hitting a common bug caused by passes
assuming all SILValues have a parent function only to be surprised by SILUndef.
Generally we see SILUndef not that often so we see this come up later in
testing. This patch eliminates that problem by making SILUndef uniqued at the
function level instead of the module level. This ensures that it makes sense for
SILUndef to have a parent function, eliminating this possibility since we can
define an API to get its parent function.

rdar://123484595
2024-02-27 13:14:47 -08:00
Meghana Gupta
4fe0429705 Merge pull request #71880 from meg-gupta/ossasilcombine
Add silcombines for destructure_tuple and destructure_struct
2024-02-26 14:01:05 -08:00
Meghana Gupta
7508a90af0 Enable destructure_struct/destructure_tuple simplifications in silcombine 2024-02-26 10:58:08 -08:00