Commit Graph

2610 Commits

Author SHA1 Message Date
Nate Chandler
a8c5d4e4ce [PerformanceInliner] Stack nest at OSSA lowering.
Now that in OSSA `partial_apply [on_stack]`s are represented as owned
values rather than stack locations, it is possible for their destroys to
violate stack discipline.  A direct lowering of the instructions to
non-OSSA would violate stack nesting.

Previously, when inlining, it was assumed that non-coroutine callees
maintained stack discipline.  And, when inlining an OSSA function into a
non-OSSA function, OSSA instructions were lowered directly.  The result
was that stack discipline could be violated.

Here, when inlining a function in OSSA form into a function lowered out
of OSSA form, stack nesting is fixed up.
2023-02-28 15:40:46 -08:00
Nate Chandler
32685ce68e [Inliner] Add per-inlining verification.
Previously, there was an -Xllvm option to verify after all inlining to a
particlar caller.  That makes it a chore to track down which apply's
inlining resulted in invalid code.  Here, a new option is added that
verifies after each run of the inliner.
2023-02-28 15:40:46 -08:00
Meghana Gupta
a0d1b08dd5 Ensure isEqual and getHashValue() are in sync with an invariant in CSE 2023-02-23 12:07:33 -08:00
swift-ci
4ab5024b3c Merge pull request #63791 from meg-gupta/csebug
Fix CSE `isEqual` and `HashVisitor` for escaped values
2023-02-23 03:54:38 -08:00
Meghana Gupta
619b4cd539 CSE: Don't look through ownership instructions for escaped values
CSE relies on OSSA RAUW for lifetime extension when replacing a redundant instruction.
OSSA RAUW however does not handle lifetime extension for escaped base values.
Values escape from ownership via pointer escape, bitwise escape, forwarding unowned operations
and have none or unowned ownership. For all such values do not look through ownership instructions
while determining equality. It is possible to CSE such values with equivalent operands, because the
operand use guarantees lifetime of the base operand.
2023-02-22 21:11:03 -08:00
Michael Gottesman
1dd896ded9 [move-only] Implement escaping closure semantics.
NOTE: A few of the test patterns need to be made better, but this patch series
is large enough, I want to get it into tree and iterate.
2023-02-20 11:04:21 -08:00
Michael Gottesman
f4e1b2a8f2 [move-only] Update SILGen/MoveCheckers so that vars are emitted in eagerly projected box form.
This is the first slice of bringing up escaping closure support. The support is
based around introducing a new type of SILGen VarLoc: a VarLoc with a box and
without a value. Because the VarLoc only has a box, we have to in SILGen always
eagerly reproject out the address from the box. The reason why I am doing this
is that it makes it easy for the move checker to distinguish in between
different accesses to the box that we want to check separately. As such every
time that we open the box, we insert a mark_must_check
[assignable_but_not_consumable] on that project. If allocbox_to_stack manages to
determine that the box can be stack allocated, we eliminate all of the
mark_must_check and place a new mark_must_check [consumable_and_assignable] on
the alloc_stack.  The end result is that we get the old model that we had before
and also can support escaping closures.
2023-02-20 11:04:21 -08:00
Michael Gottesman
ecb864c159 Merge pull request #63755 from gottesmm/pr-dac78af5673ab6d4a9bebea882b8440c37c9457c
[move-only] A few small changes in preparation for a larger patch
2023-02-18 17:03:50 -08:00
Michael Gottesman
969d776c15 [allocbox-to-stack] When we promote a heap -> stack of noncopyable type, move mark_must_check onto alloc_stack and always
ensure that we use consumable_to_assign.

What this patch is does is add an extra phase after alloc_box runs where we look
at uses of the alloc_stack and if we see any mark_must_check of any kind, we
delete them and rewrite a single mark_must_check [consumable_and_assignable] on
the alloc_stack and make all uses of the alloc_stack go through the
mark_must_check.

This has two effects:

1. In a subsequent PR when I add noncopyable semantics for escaping closures,
this will cause allocbox to stack to convert such boxes from having escaping
semantics to having non-escaping semantics. Escaping semantics means that we
always reproject out from the box and use mark_must_check
[assignable_but_not_consumable] (since we can't consume from the box, but can
assign to it). In contrast, non-escaping semantics means that the box becomes an
alloc_stack and we use the traditional var checker semantics. NOTE: We can do
this for lets represented as addresses and vars since the typechecker will
validate that the let is never actually written to even if at the SIL level we
would allow that.

2. In cases where we are implementing simple mark_must_check
[consumable_and_assignable] on one of the project_box and capture the box, we
used to have a problem where the direct box uses would be on the alloc_stack and
not go through the mark_must_check. Now, all uses after allocbox_to_stack occur
go through the mark_must_check. This is why I was able to remove instances of
the "compiler does not understand this pattern" errors... since the compiler
with this change can now understand them.
2023-02-18 12:38:38 -08:00
eeckstein
b8a4b874c1 Merge pull request #63748 from eeckstein/fix-keypath-inst
SIL: add type-dependent operands to the `keypath` instruction
2023-02-18 07:20:04 +01:00
Joe Groff
02385c895a AllocBoxToStack: Don't try to destroy promoted stack closure captures.
If the partial_apply is already [on_stack], then ClosureLifetimeFixup would
have already turned the captures into borrows and established their final
lifetimes.
2023-02-17 09:28:53 -08:00
Erik Eckstein
2c1d48b69c SIL: add type-dependent operands to the keypath instruction
It's need to correctly maintain dependencies from an open-existential instruction to a `keypath` instruction which uses the opened type.
Fixes a SILVerifier crash.

rdar://105517521
2023-02-17 17:48:55 +01:00
Nate Chandler
3019be9090 [Mem2Reg] Owned lexical lifetimes get moves. 2023-02-16 16:39:54 -08:00
Nate Chandler
66b8b2f827 [Mem2Reg] NFC: Split up lexical lifetime starts.
Split the beginning of lexical lifetimes up according to whether the
lifetimes (and alloc_stacks) are owned or guaranteed.
2023-02-16 16:39:54 -08:00
Nate Chandler
05642793ff [Mem2Reg] NFC: Entype'd LiveValues ownership.
Previously, LiveValues consisted always of three values: the value which
was stored, the borrow, and the copy.  For store_borrows, there never
was a copy.  Treating the two different scenarios as if they were the
same was confusing already.  It was only get when we switch to
representing owned lexical lifetimes with move_values.
2023-02-16 16:39:54 -08:00
Nate Chandler
a70aa13a43 [Gardening] Tweaked comment.
Fixed reference to renamed function argument.
2023-02-15 18:16:33 -08:00
Nate Chandler
1fada55c73 [Gardening] Described states variable could be in. 2023-02-15 18:16:33 -08:00
Nate Chandler
253888fbf8 [NFC] Eliminated spurious variable state.
The type Optional<Ty *> implied that there was a meaningful distinction
between None and Some(nullptr) but that was not the case here.  Replaced
it with a bare Ty *.
2023-02-15 18:16:33 -08:00
Nate Chandler
220597e68e [NFC] Removed unneeded variable.
No need to bind to the StoreBorrowInst to get the source because it's
already bound to the local variable `stored`.  And no need to bind to a
more specific type to find the next instruction.
2023-02-15 18:16:33 -08:00
Nate Chandler
a805179188 [Mem2Reg] Never borrow lifetimed stored_borrows.
Extend the definition of isGuaranteedLexicalValue--by means of which
Mem2Reg determines whether to borrow introducing a begin_borrow
[lexical] of a value which is store_borrow'd to an alloc_stack
[lexical]--to include every guaranteed lexical value.
2023-02-15 18:16:33 -08:00
Nate Chandler
b8c2269ff5 [NFC] Changed function name.
Because lexical borrows are already avoided for store_borrows of lexical
values, the function is already misnamed: it's not that Mem2Reg should
necessarily _add_ a lexical lifetime, but rather that it should ensure
that there is one.  Considering that we should do the same for owned
lexical values, the renaming will remain appropriate later.  Finally,
name it so that it can switch from being a boolean to returning a
tristate (none, guaranteed, owned) when that becomes necessary (as it
will when we need to distinguish among the states to determine what phis
look like).
2023-02-15 18:16:33 -08:00
Nate Chandler
06ed5d7801 [NFC] Removed spurious templating.
Now that StackAllocationPromoter::initializationPoints maps to either a
StoreInst or a StoreBorrowInst, there is no longer a subtype of
SILInstruction * at which the BlockToInstMap could be specialized, so
just eliminate the template argument and erase some angle brackets.
2023-02-15 18:16:33 -08:00
Erik Eckstein
748264c45f Add a unprotectedStackAlloc builtin
It's like `Builtin.stackAlloc`, but doesn't set the `[stack_protection]` flag on its containing function.
2023-02-15 08:09:38 +01:00
John McCall
d25a8aec8b Add explicit lowering for value packs and pack expansions.
- SILPackType carries whether the elements are stored directly
  in the pack, which we're not currently using in the lowering,
  but it's probably something we'll want in the final ABI.
  Having this also makes it clear that we're doing the right
  thing with substitution and element lowering.  I also toyed
  with making this a scalar type, which made it necessary in
  various places, although eventually I pulled back to the
  design where we always use packs as addresses.

- Pack boundaries are a core ABI concept, so the lowering has
  to wrap parameter pack expansions up as packs.  There are huge
  unimplemented holes here where the abstraction pattern will
  need to tell us how many elements to gather into the pack,
  but a naive approach is good enough to get things off the
  ground.

- Pack conventions are related to the existing parameter and
  result conventions, but they're different on enough grounds
  that they deserve to be separated.
2023-01-29 03:29:06 -05:00
Nate Chandler
469c3ec3bd [ObjectOutliner] See through move_value insts.
Enables the outlining of the ContiguousArrayStorage<StaticString> used
when initializing a RawRepresentable enum whose RawValue is String into
a global value to continue even when ContiguousArrayStorage has a
lexical lifetime.

Addresses the following regressions

StringEnumRawValueInitialization    400     7680    +1820.0%   **0.05x**
ArrayLiteral2                       78      647     +729.5%    **0.12x**
DataCreateSmallArray                1750    8850    +405.7%    **0.20x**

seen when enabling lexical lifetimes in the standard library.
2023-01-25 11:39:42 -08:00
Erik Eckstein
7d8bf37e5e change to the new llvm::Optional APIs
This is a follow-up of https://github.com/apple/swift/pull/62217
2023-01-25 09:18:36 +01:00
Ellie Shin
37af51dc4c Merge branch 'main' into es-pkg-acl 2023-01-19 16:18:17 -08:00
Ellie Shin
1c66d02f92 Add package access level to enum AccessLevel
Resolves rdar://104198440
2023-01-19 15:54:18 -08:00
Erik Eckstein
edce513580 SIL: simplify replaceBranchTarget
Instead of re-creating all kind of terminator instructions, just re-assign the terminator's successor.
2023-01-16 18:55:35 +01:00
Robert Widmann
6275e2298c Merge pull request #62681 from valeriyvan/SILCodeMotion
Fix bug wrong return value
2023-01-13 08:19:39 -07:00
Anthony Latsis
3bfe7a4d6e Merge pull request #62658 from valeriyvan/AccessStorage
Remove one of two identical sub-expressions 'accessKind == AccessStorage::Class' to the left and to the right of the '||' operator
2023-01-12 15:17:04 +03:00
John McCall
9aeddd0b41 Merge pull request #62955 from rjmccall/open_pack_element
Add the open_pack_element instruction
2023-01-11 13:17:46 -05:00
swift-ci
93a0a6d905 Merge pull request #62914 from ktoso/wip-task-group-twotypes
[Concurrency] DiscardingTaskGroup (rev 3)
2023-01-11 01:37:33 -08:00
John McCall
d673a45c88 More opened-existential vs. local-archetype fixes in SIL 2023-01-11 03:11:30 -05:00
nate-chandler
fb01580e41 Merge pull request #62873 from nate-chandler/bug/20230105/1
[SSADestroyHoisting] Don't fold over trivial use.
2023-01-09 07:05:22 -08:00
Meghana Gupta
0c29ebb57d Fix typo 2023-01-08 00:15:17 -08:00
Meghana Gupta
dac6c0303f Add tests for simple jump threading 2023-01-08 00:15:17 -08:00
Meghana Gupta
5d2967ca75 Fix SimplifyCFG::simplifySwitchEnumOnObjcClassOptional for OSSA 2023-01-08 00:15:17 -08:00
Meghana Gupta
cdbd0f4743 Fix SimplifyCFG::simplifyArgs for OSSA 2023-01-08 00:15:12 -08:00
Meghana Gupta
3660a1e6a9 Remove borrow scope adjustment for @guaranteed phi args 2023-01-06 23:50:07 -08:00
Meghana Gupta
900d8c3f63 Add ossa unit tests for SimplifyCFG::simplifyTermWithIdenticalDestBlocks 2023-01-06 23:50:07 -08:00
Meghana Gupta
73cd7a9dc1 Add ossa unit tests for SimplifyCFG::simplifySwitchEnumUnreachableBlocks 2023-01-06 23:50:07 -08:00
Meghana Gupta
1fc0d0aba1 SimplifyCFG: Modify DestBB before replacing branch target 2023-01-06 23:50:07 -08:00
Meghana Gupta
94f8841374 Fix SimplifyCFG::simplifySwitchEnumBlock for OSSA 2023-01-06 23:50:07 -08:00
Meghana Gupta
cdeb55a20a Fix DCE of begin_borrow with @guaranteed operand
For the lower most begin_borrows with @guaranteed operands, there
was no @guaranteed phi computation, restructure the code and fix this case.
2023-01-06 09:23:42 -08:00
Nate Chandler
4dec48f9a6 [SSADestroyHoisting] Don't fold over trivial use.
Given an aggregate addr `%agg` with trivial subobject addr `%triv` and
nontrivial subobject addr `%sub` which `%triv` is projected from,
```
  Aggregate   <- %agg
    Subobject <- %sub
      Trivial <- %triv
      ...
    ...
```

after `%sub` is destroyed, `%triv` is no longer initialized.  As a
result, it's not valid to fold a destroy_addr of %agg into a sequence of
`load [copy]`s and `copy_addr`s if there's an access to `%triv` after
the `load [copy]`/`copy_addr` of `%sub` (or some intermediate
subobject).

In other words, transforming
```
  copy_addr %sub
  load [trivial] %triv
  destroy_addr %agg
```
into
```
  copy_addr [take] %sub
  load [trivial] %triv
```
is invalid.

During destroy_addr folding, prevent that from happening by keeping
track of the trivial fields that have already been visited.  If a
trivial field is seen more than once, then bail on folding.  This is
the same as what is done for non-trivial fields except that there's no
requirement that all trivial fields be destroyed.
2023-01-05 15:18:36 -08:00
Konrad `ktoso` Malawski
f8b85015c1 prepare flags
wip on options

implement discardResults as a flag passed to grout init
2023-01-05 16:19:05 +09:00
Erik Eckstein
0c6f0bb982 StringOptimization: preserve debug scopes when creating calls to the string initializer 2023-01-02 13:52:21 +01:00
Erik Eckstein
cd45058e66 ReleaseCodeMotion: use a SmallVector instead of a SetVector for RCRootVault.
We don't need a set for this vector.
2023-01-02 13:52:21 +01:00
Erik Eckstein
ef12ec7b2c COWOpts: fix a crash in case a buffer "escapes" to a cond_br condition
rdar://103638121
2022-12-22 17:04:42 +01:00