Commit Graph

2592 Commits

Author SHA1 Message Date
Erik Eckstein
480d3f490e SIL Optimizer: handle begin_dealloc_ref and end_init_let_ref in various optimizations 2023-09-19 15:10:30 +02:00
Erik Eckstein
5bc036661c SIL optimizer: add the LetPropertyLowering pass
It lowers let property accesses of classes.
Lowering consists of two tasks:

* In class initializers, insert `end_init_let_ref` instructions at places where all let-fields are initialized.
  This strictly separates the life-range of the class into a region where let fields are still written during
  initialization and a region where let fields are truly immutable.

* Add the `[immutable]` flag to all `ref_element_addr` instructions (for let-fields) which are in the "immutable"
  region. This includes the region after an inserted `end_init_let_ref` in an class initializer, but also all
  let-field accesses in other functions than the initializer and the destructor.

This pass should run after DefiniteInitialization but before RawSILInstLowering (because it relies on `mark_uninitialized` still present in the class initializer).
Note that it's not mandatory to run this pass. If it doesn't run, SIL is still correct.

Simplified example (after lowering):

  bb0(%0 : @owned C):                           // = self of the class initializer
    %1 = mark_uninitialized %0
    %2 = ref_element_addr %1, #C.l              // a let-field
    store %init_value to %2
    %3 = end_init_let_ref %1                    // inserted by lowering
    %4 = ref_element_addr [immutable] %3, #C.l  // set to immutable by lowering
    %5 = load %4
2023-09-19 15:10:30 +02:00
Erik Eckstein
e5eb15dcbe Swift SIL: replace the set_deallocating instruction with begin_dealloc_ref
Codegen is the same, but `begin_dealloc_ref` consumes the operand and produces a new SSA value.
This cleanly splits the liferange to the region before and within the destructor of a class.
2023-09-19 15:10:30 +02:00
swift-ci
89b27b5068 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-18 14:53:34 -07:00
Allan Shortlidge
497034ef64 AST: Requestify generic signature building for @_specialized attributes.
In order to support lazy typechecking during module emission for modules
containing specialized functions, the computation of generic signatures for
`@_specialized` attributes must be requestified.

Resolves rdar://115569606
2023-09-16 00:02:33 -07:00
swift-ci
26372e82a7 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-15 07:15:21 -07:00
Nate Chandler
e912c036f6 [Reachability] NFC: Split initial from barrier.
Although by analogy with def instructions as barrier instructions one
could understand how a block where the def appears as a phi could be
regarded as a barrier block, the analogy is nonobvious.

Reachability knows the difference between an initial block and a barrier
block.  Although most current clients don't care about this distinction,
one does.  Here, Reachability calls back with visitInitialBlock for the
former and visitBarrierBlock for the latter.

Most clients are updated to have the same implementation in both
visitBarrierBlock and visitInitialBlock.  The findBarriersBackward
client is updated to retain the distinction and pass it on to its
clients.  Its one client, CanonicalizeOSSALifetime is updated to have a
simpler handling for barrier edges and to ignore the initial blocks.
2023-09-14 17:11:20 -07:00
swift-ci
ae30cba296 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-13 10:20:48 -07:00
Kuba Mracek
56085205d8 [embedded] specialize generic classes based on MetatypeInst too, fix lazy emit assert 2023-09-12 21:24:14 -07:00
Kuba Mracek
e2de477609 [embedded] avoid metadata accessors, fix vtable map, add a diagnostic on non-final generic functions 2023-09-12 16:57:09 -07:00
Kuba Mracek
d0c2a4ccf8 [embedded] Initial support for generic classes in embedded Swift
- VTableSpecializer, a new pass that synthesizes a new vtable per each observed concrete type used
- Don't use full type metadata refs in embedded Swift
- Lazily emit specialized class metadata (LazySpecializedClassMetadata) in IRGen
- Don't emit regular class metadata for a class decl if it's generic (only emit the specialized metadata)
2023-09-12 09:44:54 -07:00
swift-ci
ab9f05c4e8 Merge remote-tracking branch 'origin/main' into rebranch 2023-09-05 10:53:23 -07:00
Meghana Gupta
f177be06ab Merge pull request #68325 from meg-gupta/fixnonestoremem2reg
Fix handling of none value stores to non-trivial enums in Mem2Reg
2023-09-05 10:39:25 -07:00
Meghana Gupta
c89dcd8019 Fix handling of none value stores to non-trivial enums in Mem2Reg
We allow none values to be stored to a non-trivial enum.
For such store_borrow, `LiveValues::forValues` used
an Owned storage type, but endLexicalLifetime expected Guaranteed
storage type, leading to a compiler crash.

For store_borrow, use the LiveValues::forGuaranteed and for store
use LiveValues::forOwned to avoid this.

Fixes rdar://114390472
2023-09-04 23:31:29 -07:00
Sophia Poirier
86d368f364 Merge remote-tracking branch 'upstream/main' into fix-rebranch-automerger 2023-08-31 14:10:52 -07:00
Michael Gottesman
37d60a08bb [move-only] Rename mark_must_check -> mark_unresolved_non_copyable_value.
I was originally hoping to reuse mark_must_check for multiple types of checkers.
In practice, this is not what happened... so giving it a name specifically to do
with non copyable types makes more sense and makes the code clearer.

Just a pure rename.
2023-08-30 22:29:30 -07:00
Nate Chandler
6ccd08b4e0 Merge branch 'main' into rebranch.
Resolve conflicts introduced in
https://github.com/apple/swift/pull/67944 as follows:

```
diff --git a/include/swift/Runtime/RuntimeFunctions.def b/include/swift/Runtime/RuntimeFunctions.def
index 3c973b5884b..44cde707d17 100644
--- a/include/swift/Runtime/RuntimeFunctions.def
+++ b/include/swift/Runtime/RuntimeFunctions.def
@@ -2537,16 +2537,10 @@ FUNCTION(AutoDiffCreateLinearMapContextWithType,
          swift_autoDiffCreateLinearMapContextWithType, SwiftCC,
          DifferentiationAvailability,
          RETURNS(RefCountedPtrTy),
-<<<<<<< HEAD
-         ARGS(SizeTy),
+         ARGS(TypeMetadataPtrTy),
          ATTRS(NoUnwind),
          EFFECT(AutoDiff),
          MEMEFFECTS(ArgMemOnly))
-=======
-         ARGS(TypeMetadataPtrTy),
-         ATTRS(NoUnwind, ArgMemOnly),
-         EFFECT(AutoDiff))
->>>>>>> public-github/main

 // void *swift_autoDiffProjectTopLevelSubcontext(AutoDiffLinearMapContext *);
 FUNCTION(AutoDiffProjectTopLevelSubcontext,
@@ -2563,16 +2557,10 @@ FUNCTION(AutoDiffAllocateSubcontextWithType,
          swift_autoDiffAllocateSubcontextWithType, SwiftCC,
          DifferentiationAvailability,
          RETURNS(Int8PtrTy),
-<<<<<<< HEAD
-         ARGS(RefCountedPtrTy, SizeTy),
+         ARGS(RefCountedPtrTy, TypeMetadataPtrTy),
          ATTRS(NoUnwind),
          EFFECT(AutoDiff),
          MEMEFFECTS(ArgMemOnly))
-=======
-         ARGS(RefCountedPtrTy, TypeMetadataPtrTy),
-         ATTRS(NoUnwind, ArgMemOnly),
-         EFFECT(AutoDiff))
->>>>>>> public-github/main
```
2023-08-24 16:24:32 -07:00
Kshitij Jain
d5a3e2e630 [AutoDiff] Fixes memory leaks in autodiff linear map context allocation builtins (#67944)
When the differentiating a function containing loops, we allocate a linear map context object on the heap. This context object may store non-trivial objects, such as closures, that need to be released explicitly. Fix the autodiff linear map context allocation builtins to correctly release such objects and not just free the memory they occupy.
2023-08-24 13:57:10 -07:00
swift-ci
bfd11fb143 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-24 07:13:46 -07:00
nate-chandler
3687ddfac6 Merge pull request #68090 from nate-chandler/rdar114323803
[CopyPropagation] Don't try to delete owned values on which canonicalization bails.
2023-08-24 07:03:06 -07:00
Nate Chandler
a67c22d905 [CopyPropagation] Only delete if canonicalized.
Owned lifetime canonicalization bails on move-only values.

Previously, though, every value that was fed to canonicalization was
then attempted to be deleted.  For dead move-only values, the result
could be to shorten move-only lifetimes, which is illegal per language
rules.

Here, this is fixed by not attempting to delete owned values for which
canonicalization bailed.

rdar://114323803
2023-08-23 11:42:57 -07:00
swift-ci
174b6bc00c Merge remote-tracking branch 'origin/main' into rebranch 2023-08-18 19:34:12 -07:00
swift-ci
978395bc11 Merge pull request #62959 from meg-gupta/simplifycfgossapr3
Migrate simple and dominator based jump threading to OSSA
2023-08-18 17:18:10 -07:00
Meghana Gupta
9311e948a5 Avoid unnecessary block arguments in SimplifyCFG::threadEdge and SimplifyCFG::simplifyThreadedTerminators 2023-08-17 10:33:22 -07:00
Meghana Gupta
bbd6a87e2b Fix simplifyArguments for a few edges cases in OSSA 2023-08-16 23:14:36 -07:00
swift-ci
d3fe8a6f97 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-15 06:54:22 -07:00
Nate Chandler
0403ab102d [Mem2Reg] Don't canonicalize erased vals.
To eliminate copies which become newly spurious, Mem2Reg canonicalizes
the lifetimes of values that are stored and of newly introduced phis
after rewriting.

It's possible, however, for the values that are stored to be deleted
during canonicalization if a value and its copy are both stored to the
address.  Such values must not be canonicalized.  So check whether
values have been erased before canonicalizing them.

rdar://113762355
2023-08-14 15:08:57 -07:00
Meghana Gupta
93d6f93435 Fixes to SimplifyCFG::threadEdge for OSSA 2023-08-14 11:36:05 -07:00
Meghana Gupta
05c7d64511 We don't consider it profitable to jump thread when we could optimize arc in OSSA.
Copy propagation should be able to handle such cases without jump threading.
2023-08-14 11:36:04 -07:00
Meghana Gupta
e3a98e88f3 Add new flags to enable specific SimplifyCFG operations 2023-08-14 11:36:04 -07:00
Meghana Gupta
a303bb4519 Disable SimplifyCFG::tailDuplicateObjCMethodCallSuccessorBlocks in OSSA
Support for this needs to be added
2023-08-14 11:36:04 -07:00
Meghana Gupta
bf68e3ff39 Disable ArgumentSplitter for non trivial types in ossa 2023-08-14 11:36:04 -07:00
swift-ci
3561327bdb Merge remote-tracking branch 'origin/main' into rebranch 2023-08-08 21:33:48 -07:00
Meghana Gupta
1075f77822 Merge pull request #67521 from meg-gupta/migratedoe
Migrate DeadObjectElimination to OSSA
2023-08-08 21:27:06 -07:00
swift-ci
9742461a27 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-08 13:17:04 -07:00
Meghana Gupta
848466acbe Ensure no module transform is added in the function pipeline 2023-08-08 11:04:46 -07:00
Meghana Gupta
996fd8b978 Bail out from deleting a dead objection on seeing stores with lexical non-trivial source 2023-08-08 10:48:09 -07:00
Meghana Gupta
abdb9f0dba DeadObjectElimination: Handle allocating apply in ossa 2023-08-07 14:58:57 -07:00
Meghana Gupta
2831f2f528 Add findPointerEscape check on store source, before shortening its lifetime by inserting destroy at the store 2023-08-07 10:21:40 -07:00
Meghana Gupta
c3c78fa8f8 DeadObjectElimination: Handle alloc_ref in ossa 2023-08-07 10:21:40 -07:00
Meghana Gupta
f5c5c9e36b In non-ossa, ban non-trivial stores to alloc_ref's projections even the dtor is not inlined
There is no implementation to handle insertion of compensating releases of the non-trivial store's src for this case.
This never really happens, because destructor analysis is very limited and we end up banning alloc_ref with ref count instructions.
2023-08-07 10:21:40 -07:00
Meghana Gupta
768d98e1c4 Add a TODO in destructor analysis 2023-08-07 10:21:40 -07:00
Meghana Gupta
15e5364d25 DeadObjectElimination: Handle keypaths with non-trivial operands in OSSA 2023-08-07 10:21:40 -07:00
Meghana Gupta
41697b061c DeadObjectElimination: Handle non-trivial stores while processing alloc_stack 2023-08-07 10:21:40 -07:00
swift-ci
8308afaaf0 Merge remote-tracking branch 'origin/main' into rebranch 2023-08-01 17:33:45 -07:00
Joe Groff
03eec7da2b Remove redundant SILType::isMoveOnlyNominalType (NFC)
and implement `SILType::isPureMoveOnly` in terms of `Type::isPureMoveOnly`.
2023-08-01 14:07:38 -07:00
swift-ci
7bef9fd31a Merge remote-tracking branch 'origin/main' into rebranch 2023-07-31 21:34:34 -07:00
Nate Chandler
4f16bb1a74 [Mem2Reg] Don't canonicalize undef.
They aren't the kind of values whose lifetimes are eligible for
canonicalization.

rdar://113121424
2023-07-31 16:20:37 -07:00
Nate Chandler
f276ebf37e [Mem2Reg] Only lifetime canonicalize in OSSA.
Bail both on collecting stores and canonicalizing values if the function
being optimized doesn't have ownership.
2023-07-31 16:16:28 -07:00
swift-ci
1c1be7d116 Merge remote-tracking branch 'origin/main' into rebranch 2023-07-28 15:36:57 -07:00