Commit Graph

1084 Commits

Author SHA1 Message Date
Andrew Trick
058d22b2b6 Fix LifetimeDependenceScopeFixup for loops.
Handle situations in which the uses of the new scope are inside a loop so the
new range has no end instructions.
2024-07-30 15:57:56 -07:00
Andrew Trick
f3cf529b5d [SwiftCompilerSources] use debugLog in diagnostic passes 2024-07-30 15:57:56 -07:00
Andrew Trick
d065a3e576 Merge pull request #75562 from atrick/nfc-lifedep
[nfc] LifetimeDiagnostics cleanup
2024-07-30 09:03:24 -07:00
Andrew Trick
1113c757f0 Improve function convention printing. 2024-07-29 23:44:02 -07:00
Andrew Trick
20905b2f8e LifetimeDependenceDiagnostics comment. 2024-07-29 23:44:02 -07:00
Andrew Trick
b4cdbd276b LifetimeDependenceUtils comment typo. 2024-07-29 23:44:02 -07:00
Andrew Trick
f6d147855f AddressUtils comment 2024-07-29 23:44:02 -07:00
Andrew Trick
c4c66b4066 [SwiftCompilerSources] add parameter dependencies to conventions.
Add interfaces to FunctionConventions and ArgumentConventions for querying
lifetime dependencies where the target is another parameter.
2024-07-29 23:44:02 -07:00
Andrew Trick
103c59b273 [SwiftCompilerSources] Add assert to Builder.init(after:)
This API is incorrect for terminators. Assert that it isn't used
incorrectly. Later, we should rename it.
2024-07-29 23:44:02 -07:00
Andrew Trick
22f01a956a [SwiftCompilerSources] add BridgedOStream and debugLog().
So we can trace optimizer passes using the same output stream as the
C++ passes and don't get garbled output.
2024-07-29 21:25:43 -07:00
Erik Eckstein
f9b524b1cb AliasAnalysis: a complete overhaul of alias- and memory-behavior analysis
The main changes are:

*) Rewrite everything in swift. So far, parts of memory-behavior analysis were already implemented in swift. Now everything is done in swift and lives in `AliasAnalysis.swift`. This is a big code simplification.

*) Support many more instructions in the memory-behavior analysis - especially OSSA instructions, like `begin_borrow`, `end_borrow`, `store_borrow`, `load_borrow`. The computation of end_borrow effects is now much more precise. Also, partial_apply is now handled more precisely.

*) Simplify and reduce type-based alias analysis (TBAA). The complexity of the old TBAA comes from old days where the language and SIL didn't have strict aliasing and exclusivity rules (e.g. for inout arguments). Now TBAA is only needed for code using unsafe pointers. The new TBAA handles this - and not more. Note that TBAA for classes is already done in `AccessBase.isDistinct`.

*) Handle aliasing in `begin_access [modify]` scopes. We already supported truly immutable scopes like `begin_access [read]` or `ref_element_addr [immutable]`. For `begin_access [modify]` we know that there are no other reads or writes to the access-address within the scope.

*) Don't cache memory-behavior results. It turned out that the hit-miss rate was pretty bad (~ 1:7). The overhead of the cache lookup took as long as recomputing the memory behavior.
2024-07-29 17:33:46 +02:00
Erik Eckstein
ceda41ca90 AccessUtils: fix handling of store_borrow in AccessBase.isDistinct
The result of a store_borrow can "escape" to other access bases, like a "pointer" access base.
Then a store-borrow access base is _not_ distinct from such another base.
2024-07-29 17:33:46 +02:00
Erik Eckstein
983d95509d AccessUtils: do a simple type-based alias analysis in AccessBase.isDistinct
If the operand types (which are classes) of `ref_element_addr` or `ref_tail_addr` differ, then we know those can't be the same objects.
2024-07-29 17:33:46 +02:00
Erik Eckstein
cf99536d7f Optimizer Utilities: add InstructionWorklist.pushPredecessors(of: Instruction, ignoring:) 2024-07-29 17:33:45 +02:00
Erik Eckstein
3fb7032aa1 SIL Type: add Type.aggregateIsOrContains 2024-07-29 17:33:45 +02:00
Erik Eckstein
6cfec91f69 EscapeInfo: handle store_borrow 2024-07-29 17:33:45 +02:00
Erik Eckstein
c6e3f51217 EscapeInfo: handle unchecked_addr_cast
It's dangerous to continue walking over an `unchecked_addr_cast` which casts between two different types.
We can only do this if the result is known to be the end of the walk, i.e. the cast result is not used in a relevant way.
2024-07-29 17:33:44 +02:00
Erik Eckstein
1440ae7b71 EscapeInfo: handle is_unique 2024-07-29 17:33:44 +02:00
Erik Eckstein
da5857d5ec EscapeInfo: improve handling of begin_applys which yield an address value
Instead of just aborting if a `begin_apply` gets an address argument, follow the yielded values and see if they actually escape
2024-07-29 17:33:44 +02:00
Erik Eckstein
084d60d7b7 Swift SIL: add some APIs to instructions and other small improvments 2024-07-29 17:33:44 +02:00
Erik Eckstein
0f359ff6f8 SideEffects: fix effects of indirect-in apply arguments
Indirect-in arguments are consumed by the caller and that not only counts as read but also as a write.
2024-07-29 17:33:43 +02:00
Erik Eckstein
4c49e0039b Verifier: in the swift verifier call the bridged C++ verificationFailure function in case of a failure
This brings all the nice verifier features to the swift verifier, like printing the surrounding instructions in case of a failure, etc.
2024-07-29 17:33:43 +02:00
Erik Eckstein
95bd329b1f Swift SIL: bridge the hasPointerEscape flags of MoveValueInst and BeginBorrowInst 2024-07-29 17:33:43 +02:00
Erik Eckstein
9eddb24c41 EscapeUtils: replace ...byWalkingDown APIs with an initialWalkingDirection parameter
It simplifies the EscapeUtils API
NFC
2024-07-29 17:33:43 +02:00
Erik Eckstein
7512acfd31 SwiftCompilerSources: fix debug description of Set
Make each instruction to appear in a separate line
2024-07-29 17:33:43 +02:00
Erik Eckstein
f388361885 SimplifyBuiltin: fix a wrong constant folding of the is_same_metatype builtin for function types
We need to compare the not lowered types, because function types may differ in their original version but are equal in the lowered version, e.g.
```
  ((Int, Int) -> ())
  (((Int, Int)) -> ())
```

Fixes a miscompile
2024-07-29 17:33:42 +02:00
Erik Eckstein
04e0907ab0 SIL: rename Type.instanceTypeOfMetatype -> Type.loweredInstanceTypeOfMetatype
The same for SILType

It needs to be made clear that this is not the "original", but the lowered SIL type.

NFC
2024-07-29 17:33:36 +02:00
Andrew Trick
25825e25ee SwiftCompilerSources: use Type.isVoid instead of Type.isEmpty. 2024-07-26 17:32:23 -07:00
Erik Eckstein
b739c6263b WalkUtils: handle unsafe_ref_cast which casts between AnyObject and a class
We need to ignore such casts because otherwise the constructed walking path wouldn't contain the right `existential` field kind.
Fixes a miscompile caused by redundant load elimination.

rdar://132364917
2024-07-24 11:35:08 +02:00
Andrew Trick
c32c238720 Fix LifetimeDependenceDiagnostics to handle invalid SIL types
Invalid types are not considered Escapable. This makes it difficult to make any
assumptions about nonescapable types.

Fixes rdar://132348528 (Fix LifetimeDependenceDiagnostics to handle invalid SIL types)
2024-07-23 16:03:32 -07:00
Andrew Trick
c41dc33c10 LocalVariableUtils: handle projections inside initializers.
These projections don't have access scopes, so the utility was treating them
like escapes.

Fixes: rdar://131499478 (Difficulties composing non-escapable types)
2024-07-12 19:58:14 -07:00
Meghana Gupta
154989463b Add support for lifetime dependence in parameter position 2024-07-10 14:20:03 -07:00
Andrew Trick
ad2e58ed43 Assert that mark_dependence operands are distinct.
Various downstream passes assume that the dependent value is not the same as the
base value.
2024-07-05 10:42:31 -07:00
Erik Eckstein
19dccf78ce MandatoryPerformanceOptimizations: don't crash with empty global-init-once functions
Instead ignore empty global-init-once functions

https://github.com/swiftlang/swift/issues/73487
rdar://130041582
2024-07-02 14:03:01 +02:00
eeckstein
8455309dc2 Merge pull request #74766 from eeckstein/remove-stderr-reference
SwiftCompilerSources: remove references to C's stderr
2024-06-30 14:46:19 +02:00
Akira Hatanaka
42bc49d3fe Add a new parameter convention @in_cxx for non-trivial C++ classes that are passed indirectly and destructed by the caller (#73019)
This corresponds to the parameter-passing convention of the Itanium C++
ABI, in which the argument is passed indirectly and possibly modified,
but not destroyed, by the callee.

@in_cxx is handled the same way as @in in callers and @in_guaranteed in
callees. OwnershipModelEliminator emits the call to destroy_addr that is
needed to destroy the argument in the caller.

rdar://122707697
2024-06-27 09:44:04 -07:00
Erik Eckstein
638205e8c6 SwiftCompilerSources: remove references to C's stderr
Fixes a linux build error.
The `var standardError` was only used in one place for logging, which can be done with `log` anyway.
Instead of using such C library constructs directly we should bridge to higher level APIs. This is more platform independent.

https://github.com/swiftlang/swift/issues/73252
https://github.com/swiftlang/swift/issues/74701
2024-06-27 10:14:44 +02:00
eeckstein
31c07c95b0 Merge pull request #74689 from eeckstein/refactor-dynamic-self-check
SwiftCompilerSources: refactor `Function.mayBindDynamicSelf`
2024-06-26 08:47:18 +02:00
Erik Eckstein
c61733f985 SwiftCompilerSources: refactor Function.mayBindDynamicSelf
Instead of bridging the whole function, just bridge `hasDynamicSelfMetadata` and do the other work in swift.
2024-06-25 17:59:23 +02:00
Erik Eckstein
81005c18a2 SwiftCompilerSources: forward assertion and precondition failures to the assertion-handling in the C++ code base. 2024-06-25 14:14:43 +02:00
Erik Eckstein
1f9588e5c6 MandatoryPerformanceOptimizations: prevent inlining of dynamic-self class methods
This fixes a compiler crash in embedded swift.

rdar://129241915
2024-06-24 15:01:02 +02:00
Andrew Trick
279eb78d27 [windows] Temporarily guard lifetime dependence diagnostics
by -enable-experimental-feature NonescapableTypes
on the Windows platform

These passes do nothing unless the above feature flag is enabled, so
the only reason to run the pass is to exercise SwiftCompilerSources
and catch invalid SIL.

These passes rely on fundamental SwiftCompilerSources abstractions
which have not yet been tested outside of the passes. They don't yet
handle all SIL patterns, and SIL continues to evolve. We would like to
can these issues quickly as we hit them, but only if we have a way of
reproducing the failure. Currently, we don't have a way of reproducing
Windows-arm64 failures.

Workaround for:
rdar://128434000 ([nonescapable] [LifetimeDependenceInsertion]
Package resolution fails with arm64 Windows toolchain)
2024-06-20 16:51:21 -07:00
Erik Eckstein
715a765b6e Deinit-Devirtualization: fix a crash when trying to de-virtualize a non-copyable generic
rdar://129980818
2024-06-17 13:47:46 +02:00
Ellie Shin
41ee82a589 [SwiftCompilerSources] Remove warning: add unknown default case for bridged function serializedkind. 2024-06-13 14:42:13 -07:00
Ellie Shin
8b00478bc6 [SwiftCompilerSources] Remove unnecessary default case from SerializedKind funcs 2024-06-11 23:59:43 -07:00
Meghana Gupta
af1d6017f9 Merge pull request #74132 from meg-gupta/deleteresultdependson
Remove resultDependsOn/resultDependsOnSelf
2024-06-06 10:23:31 -07:00
Nate Chandler
81fa65772f [OwnershipLiveness] End at extend_lifetime.
When visiting consumes, also visit `extend_lifetime` instructions.
These instructions are not lifetime ending, but together with the
consumes, they enclose the users of a value.

Add a flag to LinearLiveness to control whether these instructions are
added so that the verifier can use verify that all such instructions
appear outside the linear lifetime boundary (not including them).
2024-06-05 16:28:28 -07:00
Nate Chandler
2a5d07522d [SIL] Add extend_lifetime instruction.
It indicates that the value's lifetime continues to at least this point.
The boundary formed by all consuming uses together with these
instructions will encompass all uses of the value.
2024-06-05 16:28:26 -07:00
Meghana Gupta
470fa2f365 Remove resultDependsOn/resultDependsOnSelf 2024-06-05 11:36:16 -07:00
Ellie Shin
6216ec648f Merge pull request #73902 from apple/elsh/pkg-cmo-inline
[SIL][PackageCMO] Allow optimizing [serialized_for_pkg] functions
2024-06-04 11:39:19 -07:00