Commit Graph

149 Commits

Author SHA1 Message Date
eeckstein
021bd4a2c4 Merge pull request #81684 from eeckstein/fix-mark-dependence-addr
SIL: define `mark_dependence_addr` to read and write to its address operand
2025-05-22 07:10:24 +02:00
nate-chandler
393465262b Merge pull request #81581 from nate-chandler/rdar147207926
[TypeLowering] Record packs used in signatures.
2025-05-21 13:37:10 -07:00
Erik Eckstein
e0f5888a8d SIL: define mark_dependence_addr to read and write to its address operand
This prevents simplification and SILCombine passes to remove (alive) `mark_dependence_addr`.
The instruction is conceptually equivalent to
```
  %v = load %addr
  %d = mark_dependence %v on %base
  store %d to %addr
```

Therefore the address operand has to be defined as writing to the address.
2025-05-21 20:03:53 +02:00
Nate Chandler
aa49b8540d [TypeLowering] Record packs used in signatures.
To determine whether an instruction may require pack metadata, the types
of its operands are examined.

Previously, the top level type was checked for having a pack in its
signature, and the whole type was checked for having a type anywhere in
its layout (via TypeLowering).  This didn't account for the case where
the metadata was required for a resilient type which uses a pack in its
signature.

Here, during type lowering, a type having a pack in its signature is
counted towards the type having a pack.

Fixes a compiler crash.

rdar://147207926
2025-05-20 16:53:28 -07:00
Erik Eckstein
9052652651 add the prepareInitialization builtin.
It is like `zeroInitializer`, but does not actually initialize the memory.
It only indicates to mandatory passes that the memory is going to be initialized.
2025-05-20 20:46:33 +02:00
Andrew Trick
e7000e4668 SIL: Add mark_dependence_addr 2025-03-25 23:02:42 -07:00
Meghana Gupta
2b072a56c1 Allow duplicating mark_dependence [nonescaping] on addresses 2025-02-28 17:11:59 -08:00
Andrew Trick
1cb4d50877 Fix BorrowingOperand::visitScopeEndingUses() invariants.
This API only makes sense for a scoped borrow-introducer such as:
- reborrow
- owned mark_dependence

Borrowing operands that forward guaranteed values do not have scope-ending uses.
2025-02-25 23:08:54 -08:00
Andrew Trick
6fa0b2ec98 Add BorrowedFromInst::isReborrow.
borrowed-from instructions have different ownership semantics if the are
attached to reborrows vs. guaranteed forwarding phis.
2025-02-25 23:08:54 -08:00
Meghana Gupta
a31a603e49 Handle type_value instruction in CSE 2025-02-13 10:30:25 -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
Erik Eckstein
a88cb49ee8 SIL: define a memory-read effect on the mark_dependence base value
If the base value of a mark_dependence is an address, that memory location must be initialized at the mark_dependence.
This requires that the mark_dependence is considered to read from the base address.
2025-02-10 17:57:47 +01:00
Andrew Trick
01c3343b05 Fix mark_dependence ownership: compatibility with owned values
This fixes an OSSA verification bug introduced here:

    commit 79b649854b
    Author: Meghana Gupta <meghanavgupta@gmail.com>
    Date:   Wed Jan 22 01:24:49 2025

        Fix operand ownership of mark_dependence [nonescaping] of address values

The bug only showed up with mark_dependence [nonescaping], which means mainly
affects `~Escapable` types. Adddressors happened to work because SILGen was
emitting a borrow scope around them.

Rather than reverting the change above, this fix migrates mark_dependence
[nonescaping] to an implicit borrow scope.

Fixes rdar://144199759 (Assert: mark_dependence [nonescaping]:
ownership incompatible with an owned value)
2025-02-05 16:23:14 -08:00
Nate Chandler
41bee47169 [Gardening] Sunk static function to use.
Now that the other caller of the function has been removed, it's more
convenient to have it adjacent to the only remaining caller.
2025-01-28 10:44:30 -08:00
Nate Chandler
ad9e090243 [SIL] Only visit final on-stack pai lifetime ends.
In OSSA, the `partial_apply [on_stack]` instruction produces a value
with odd characteristics that correspond to the fact that it is lowered
to a stack-allocating instruction.  Among these characteristics is the
fact that copies of such values aren't load bearing.

When visiting the lifetime-ending uses of a `partial_apply [on_stack]`
the lifetime ending uses of (transitive) copies of the partial_apply
must be considered as well.  Otherwise, the borrow scope it defins may be
incorrectly short:

```
%closure = partial_apply %fn(%value) // borrows %value
%closure2 = copy_value %closure
destroy_value %closure // does _not_ end borrow of %value!
...
destroy_value %closure2 // ends borrow of %value
...
destroy_value %value
```

Furthermore, _only_ the final such destroys actually count as the real
lifetime ends.  At least one client (OME) relies on
`visitOnStackLifetimeEnds` visiting at most a single lifetime end on any
path.

Rewrite the utility to use PrunedLiveness, tracking only destroys of
copies and forwards.  The final destroys are the destroys on the
boundary.

rdar://142636711
2025-01-28 10:44:30 -08:00
Nate Chandler
f9ddf8d4d7 [Test] Add for PAI::visitOnStackLifetimeEnds. 2025-01-28 10:44:29 -08:00
nate-chandler
8c63134570 Merge pull request #78682 from nate-chandler/rdar142636711_2
[TempRValueOpt] Invalidate insts when completing.
2025-01-27 12:53:57 -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
Nate Chandler
2eb3a86928 [NFC] SIL: Replaced a recursion with a worklist. 2025-01-16 08:18:30 -08:00
Nate Chandler
8c7f1484bb [Gardening] SIL: Move comment.
Move it to the lambda which it talks about.  The old location is in a
function which now enjoys multiple callers, and the comment applies to
only one.
2025-01-16 08:18:29 -08:00
Arnold Schwaighofer
fa01d8d2f0 SIL: builtin willThrow does not modify memory or release
This enables access enforcement analysis to classify a dynamic begin_access in
access patterns (such as the one below) involving a throwing function as not
having nested conflicts.

```
struct Stack {
  var items : [UInt8]

  mutating func pop() throws -> UInt8 {
    guard let item = items.popLast() else { throw SomeErr.err }
    return item
  }
  ...
}

class Container {
  private var ref : Stack

  @inline(never)
  internal func someMethod() throws {
     try ref.pop()
  }
  ...
}
```
rdar://141182074
2024-12-10 11:27:27 -08:00
Erik Eckstein
156f1fc759 Optimizer: prevent duplicating blocks with dead-end begin-borrow instructions.
In case the control flow ends in a dead-end block there can be begin-borrow instructions which have no corresponding end-borrow uses.
After duplicating such a block, the re-borrow flags cannot be recomputed correctly for inserted phi arguments.
Therefore just disable duplicating such blocks, e.g. in jump-threading.
2024-11-12 10:04:09 +01:00
Nate Chandler
c64d56c224 [NFC] SIL: Added SILInstructionContext.
It has a module or maybe a function.  In the future we may want to
change this to be either a SILFunction or a SILGlobalVariable.
2024-10-29 21:44:06 -07:00
Nate Chandler
71239d6357 [CoroutineAccessors] SIL represents callee alloc.
When its operand has coroutine kind `yield_once_2`, a `begin_apply`
instruction produces an additional value representing the storage
allocated by the callee.  This storage must be deallocated by a
`dealloc_stack` on every path out of the function.  Like any other stack
allocation, it must obey stack discipline.
2024-10-11 08:25:03 -07:00
Slava Pestov
b68a78cd54 SIL: Track local environments instead of root local archetypes 2024-08-29 16:18:13 -04:00
Nate Chandler
cfdbd992cd [Test] Underscored instruction_move_before.
Use underscores rather than hyphens so that text editors understand the
name as a single word.
2024-07-25 13:50:25 -07:00
Meghana Gupta
154989463b Add support for lifetime dependence in parameter position 2024-07-10 14:20:03 -07:00
Meghana Gupta
12484ff919 Merge pull request #74842 from meg-gupta/addopenpackedgecase
Fix isTriviallyDuplicatable to handle open_pack_element
2024-07-01 10:53:53 -07:00
Meghana Gupta
5282194682 Fix isTriviallyDuplicatable to handle open_pack_element
SILInstruction::clone doesn't know how to clone instructions that produce
the archetype uuid. SILCloner is equipped to handle such instructions.

Optimizations like LoopRotate use  SILInstruction::clone and will be
incorrect for such instructions.

rdar://130047619
2024-06-29 13:42:05 -07:00
Ben Barham
d72f5b12c4 Update StringRef::equals references to operator==
`equals` has been deprecated upstream, use `operator==` instead.
2024-06-27 19:14:06 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Emil Pedersen
f6232a937d [DebugInfo] Fix wrong scope being used in AllocBoxToStack 2024-04-30 16:29:49 -07:00
Andrew Trick
581010d00c SIL: Make drop_deinit a ForwardingInstruction.
drop_deinit forwards ownership while effectively stripping the deinitializer. It is similar to a type cast.

Fixes rdar://125590074 ([NonescapableTypes] Nonescapable types
cannot have deinits)
2024-04-03 18:53:18 -07:00
Andrew Trick
d7b9149ee5 Fix visitNonEscapingLifetimeEnds to handle mark_dependence uses
Now it visits unknown uses separately rather than asserting.
2024-03-22 14:29:57 -07:00
Meghana Gupta
50b358c64f Merge pull request #72359 from meg-gupta/skiphismdi
Fix utilities that may see phis of mark_dependence [nonescaping]
2024-03-21 18:28:08 -07:00
Erik Eckstein
e45b4bd329 SIL: clear operand bit fields of instructions which are moved between functions
This fixes a bug with can cause OperandSet to misbehave for instructions which were moved from another function.
2024-03-21 15:52:39 +01:00
Meghana Gupta
3ebef80261 Fix utilities that may see phis of mark_dependence [nonescaping] 2024-03-20 15:44:11 -07:00
Meghana Gupta
c901f97d8f Update visitRecursivelyLifetimeEndingUses for users with multiple results 2024-02-23 15:29:06 -08:00
Ben Barham
ef8825bfe6 Migrate llvm::Optional to std::optional
LLVM has removed llvm::Optional, move over to std::optional. Also
clang-format to fix up all the renamed #includes.
2024-02-21 11:20:06 -08:00
Andrew Trick
f713587aa5 Fix OSSA support for mark_dependence [nonescaping]
Handle escapes to function results.
2024-02-12 09:57:14 -08:00
Meghana Gupta
c3c7465a44 Merge pull request #71125 from meg-gupta/rdar121485860
Don't duplicate builtin "once"
2024-01-25 01:49:15 -08:00
Meghana Gupta
1369043c6c Don't duplicate builtin "once"
Fixes rdar://121485860
2024-01-24 14:40:43 -08:00
Andrew Trick
246da83f0f Merge pull request #71055 from atrick/lifetime-dependence
Lifetime dependence utilities
2024-01-23 08:19:24 -08:00
Andrew Trick
37171e698a Handle mark_dependence [nonescaping] like a borrowing instruction. 2024-01-22 23:57:03 -08:00
Joe Groff
960938f87e SILGen: Always match noncopyable values by borrowing.
Even if the final pattern ends up consuming the value, the match itself
must be nondestructive, because any match condition could fail and cause
us to have to go back to the original aggregate. For copyable values,
we can always copy our way out of consuming operations, but we don't
have that luxury for noncopyable types, so the entire match operation
has to be done as a borrow.

For address-only enums, this requires codifying part of our tag layout
algorithm in SIL, namely that an address-only enum will never use
spare bits or other overlapping storage for the enum tag. This allows
us to assume that `unchecked_take_enum_data_addr` is safely non-side-
effecting and match an address-only noncopyable enum as a borrow.
I put TODOs to remove defensive copies from various parts of our
copyable enum codegen, as well as to have the instruction report
its memory behavior as `None` when the projection is nondestructive,
but this disturbs SILGen for existing code in ways SIL passes aren't
yet ready for, so I'll leave those as is for now.

This patch is enough to get simple examples of noncopyable enum switches
to SILGen correctly. Additional work is necessary to stage in the binding
step of the pattern match; for a consuming switch, we'll need to end
the borrow(s) and then reproject the matched components so we can
consume them moving them into the owned bindings. The move-only checker
also needs to be updated because it currently always tries to convert
a switch into a consuming operation.
2024-01-19 13:59:04 -08:00
nate-chandler
bb24b8c045 Merge pull request #70720 from nate-chandler/rdar119829826
[IRGen] Pack metadata may be allocated based on type layouts.
2024-01-04 19:31:47 -08:00
Nate Chandler
73d68a3f28 [IRGen] Pack metadata may be alloc'd for layouts.
Previously, mayRequirePackMetadata only considered whether a type
involved a pack.  That failed to account for the case of outlined value
functions that require pack metadata when the type involves a pack in
its layout.  Here, mayRequirePackMetadata now considers also whether the
layout corresponding to a type involves a pack.

rdar://119829826
2024-01-04 12:21:47 -08:00
Andrew Trick
861216cb38 Add release diagnostics for partial_apply [on_stack] uses. 2024-01-03 12:35:53 -08:00
Andrew Trick
a4070e627c Prevent partial_apply cloning.
partial_apply cannot be cloned, even in OSSA. OSSA lowering does
not know how to allocate for multiple partial applies.

Fixes rdar://119768691 (OwnershipModelEliminator triggers assertion
when lowering certain [on_stack] partial_applys in certain
circumstances)
2024-01-03 10:31:01 -08:00
Erik Eckstein
e652f2c92e SIL: add the alloc_vector and vector instructions
* `alloc_vector`: allocates an uninitialized vector of elements on the stack or in a statically initialized global
* `vector`: creates an initialized vector in a statically initialized global
2023-12-09 18:49:55 +01:00