Commit Graph

1035 Commits

Author SHA1 Message Date
Erik Eckstein
a54502aee9 MemAccessUtils: handle the obsolete allocVector builtin
This builtin was removed. But to be able to parse old Swift.swiftinterface files it is required to handle the builtin in MemAccessUtils.
2025-03-11 08:03:17 +01:00
Erik Eckstein
810064b7dc Cleanup and additions to AST and SIL Type/CanonicalType
* factor out common methods of AST Type/CanonicalType into a `TypeProperties` protocol.
* add more APIs to AST Type/CanoncialType.
* move `MetatypeRepresentation` from SIL.Type to AST.Type and implement it with a swift enum.
* let `Builder.createMetatype` get a CanonicalType as instance type, because the instance type must not be a lowered type.
2025-03-07 15:59:33 +01:00
Meghana Gupta
cf6c4c9422 Merge pull request #79699 from meg-gupta/prboundscheck
Add support for bounds check optimization of Span and InlineArray
2025-03-03 14:32:27 -08:00
Andrew Trick
2fe155ac30 OSSALifetimeCompletion: check liveness failure
This transformation is invalid without complete liveness. Add a release assert
to check this. Temporarily hide the assert under -verify-lifetime-completion
until the passes that use this utility are fixed to check escapes.
2025-03-02 23:51:34 -08:00
Andrew Trick
d73a081fe7 Add pointer escape and dependent use tracking to InteriorLiveness
Rig TransitiveAddressWalker to keep track of enough information for passes to
correctly check for pointer escapes and dependence uses. Requires for precise
bail-outs and asserts.
2025-03-02 23:51:34 -08:00
Meghana Gupta
9ce8bb3682 Add a new abstraction FixedStorageSemanticsCall 2025-02-28 09:50:58 -08:00
Slava Pestov
541757a491 AST: Remove TypeBase::isOpenedExistential()
At one point, OpenedArchetypeType did not exist as a separate subclass
of ArchetypeType, so this method did something. Now, it's just
equivalent to calling is<> or isa<>.

I also removed a couple of asserts that were obvious no-ops as a result.
2025-02-27 09:55:36 -05:00
Andrew Trick
edcaecc09d Fix BorrowedValue::visitInteriorPointerOperandHelper
to handle borrowed-from (as a ForwardingInstruction).
2025-02-25 23:08:55 -08:00
Andrew Trick
5f8b93c66a Fix interior liveness to report the escaping instruction as a use. 2025-02-25 23:08:55 -08:00
Andrew Trick
f29bb44d07 OwnershipUseVisitor::visitInnerBorrowUses: support dependent values
Add liveness support for dependent values: borrowed-from & mark_dependence so
they aren't reported as unknown uses.

Centralize the logic in BorrowingOperand::getScopeIntroducingUserResult
and BorrowingOperand::getDependentUserResult().
2025-02-25 23:08:55 -08:00
Andrew Trick
e8af00769e Remove adjacent phis from OwnershipLiveness.
The extra complexity for traversing phis is not needed now that it handles
borrowed-from instructions. Remove the redundant logic because it complicates
the liveness algorithm and generates confusing results.
2025-02-25 23:08:54 -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
e78aa38204 Remove BorrowingOperand.getImplicitUses.
It is buggy/misleading because of dead-def handling. The current operand may be
re-pushed if it is a dead borrow scope.
2025-02-25 23:08:54 -08:00
Andrew Trick
b3a445fb55 Fix BorrowingOperand::getBorrowIntroducingUserResult()
To handle borrowing operands that produce a dependence value but do not create a
nested borrow scope. This includes non-reborrow borrowed-from and guaranteed
mark_dependence [nonescaping].
2025-02-25 23:08:54 -08:00
Andrew Trick
f1792d80b3 Fix BorrowingOperand.visitScopeEndingUses
Only return false if the visitor returns false. Clients were ignoring the
result.

If the BorrowingOperand does not create a borrow scope, call visitUnknownUse
instead.

Until we have complete lifetimes, to avoid breaking code that cannot handle dead
defs, consider a dead borrow scope to be an unknown use.
2025-02-25 23:08:54 -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
6407f9a0bd remove the allocVector builtin
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
6a6d3f0b13 SIL: handle mark_dependence in the MemoryLifetimeVerifier
The check is not perfect, because it only checks that the base operand is alive _at_ the mark_dependence.
Ideally it should check that the base operand is alive during the whole lifetime of the value operand.
2025-02-10 17:57:47 +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
Andrew Trick
e705a6d7c3 Temporarily introduce AnyInteriorPointer operand ownership.
This is necessary to fix a recent OSSA bug that breaks common occurrences on
mark_dependence [nonescaping]. Rather than reverting that change above, we make
forward progress toward implicit borrows scopes, as was the original intention.

In the near future, all InteriorPointer instructions will create an implicit
borrow scope. This means we have the option of not emitting extraneous
begin/end_borrow instructions around intructions like ref_element_addr,
open_existential, and project_box. After that, we can also migrate
GuaranteedForwarding instructions like tuple_extract and struct_extract.
2025-02-05 16:23:02 -08:00
Anthony Latsis
34f9b80cbc Merge pull request #78750 from AnthonyLatsis/oryza-sativa
[Gardening] Fix some set but not used variables
2025-01-31 04:29:05 +00:00
Anthony Latsis
a84dfc8387 [Gardening] Fix some set but not used variables 2025-01-30 21:34:38 +00:00
Nate Chandler
95b7b06bef [Test] Renamed test case.
Replaced a - with a _.
2025-01-28 10:44:29 -08:00
eeckstein
95f1dfbf07 Merge pull request #78885 from eeckstein/fix-without-actually-escaping
Fix two problems with `withoutActuallyEscaping`
2025-01-27 07:59:02 +01:00
Andrew Trick
f0144ab7b1 Merge pull request #78830 from atrick/fix-lifedep-trivial-deadend
SILGenCleanup: extend to handle trivial local var scopes
2025-01-25 15:34:35 -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
Andrew Trick
1b72c7bbf6 SILGenCleanup: extend to handle trivial local var scopes
Improves OSSALifetimeCompletion to handle trivial variables when running from
SILGenCleanup. This only affects lifetime dependence diagnostics.

For the purpose of lifetime diagnostics, trivial local variables are only valid
within their lexical scope. Sadly, SILGen only know how to insert cleanup code
on normal function exits. SILGenCleanup relies on lifetime completion to fix
lifetimes on dead end paths.

  %var = move_value [var_decl]
  try_apply %f() : $..., normal bb1, error error
error:
  extend_lifetime %var <=== insert this
  unreachable

This allows Span to depend on local unsafe pointers AND be used within
throwing closures:

    _ = a.withUnsafeBufferPointer {
      let buffer = $0
      let view = Span(_unsafeElements: buffer)
      return view.withUnsafeBufferPointer(\.count)
    }
2025-01-23 12:42:43 -08:00
Michael Gottesman
082b824a8e [rbi] Change Region Based Isolation for closures to not use the AST and instead just use SIL.
The reason why I am doing this is that in certain cases the AST captures indices
will never actually line up with partial apply capture indices since we seem to
"smush" together closures and locally defined functions.

NOTE: The reason for the really small amount of test changes is that this change
does not change the actual output by design. The only cases I had to change were
a case where we began to emit a better diagnostic and also where I added code
coverage around _ and let _ since those require ignored_use to be implemented so
that they would be diagnosed (previously we just did not emit anything so we
couldn't emit the diagnostic at the SIL level).

rdar://142661388
2025-01-22 21:12:36 -08:00
Michael Gottesman
7ae56aab2e [sil] Add a new instruction ignored_use.
This is used for synthetic uses like _ = x that do not act as a true use but
instead only suppress unused variable warnings. This patch just adds the
instruction.

Eventually, we can use it to move the unused variable warning from Sema to SIL
slimmming the type checker down a little bit... but for now I am using it so
that other diagnostic passes can have a SIL instruction (with SIL location) so
that we can emit diagnostics on code like _ = x. Today we just do not emit
anything at all for that case so a diagnostic SIL pass would not see any
instruction that it could emit a diagnostic upon. In the next patch of this
series, I am going to add SILGen support to do that.
2025-01-22 21:12:36 -08:00
Becca Royal-Gordon
0ce403d680 [NFC] Add some pretty stack traces
These would have helped us to debug rdar://142693093 more quickly.
2025-01-21 17:33:09 -08:00
Nate Chandler
4135540f62 [NFC] PrunedLiveness: Permit postDomBlocks dupes.
According to the documentation,

```
It's ok if postDomBlocks has duplicates or extraneous blocks, as long
as they jointly post-dominate all live blocks that aren't on dead-end
paths.
```

Previously, though, duplicates resulted in trapping: each element of
`postDomBlocks` is pushed into a `BasicBlockWorklist`; when the second
occurrence of an element in `postDomBlocks` was encountered,
`BasicBlockWorklist::push` would trigger an assertion failure.

Here, `pushIfNotVisited` is called instead.
2025-01-13 17:00:30 -08:00
Nate Chandler
9f87e0a8c6 [SIL] Print AST types with print-module-on-error. 2025-01-12 15:32:54 -08:00
Meghana Gupta
6f375aa6b6 Look through forwarding instructions while querying ownership root
Fixes rdar://142507746
2025-01-10 11:52:06 -08:00
Erik Eckstein
41dd3dc453 DeadObjectElimination: handle OSSA instructions when analyzing class destructors
Handle typical patterns which are generated by SILGen.
2024-12-24 12:00:22 +01:00
Erik Eckstein
4fdf16de1b SIL: add CanonicalType.canBeClass
And move the implementation of `SIL.Type.canBeClass` to the AST Type. The SIL Type just calls the AST Type implementation.
Also rename `SIL.Type.canonicalASTType` -> `SIL.Type.astType`.
2024-12-19 20:53:46 +01:00
Meghana Gupta
349c3f19a6 Merge pull request #78217 from meg-gupta/fixreborrowverify
Fix reborrow verifier to look through borrowed from
2024-12-16 15:58:53 -08:00
Meghana Gupta
4cab04e190 Fix reborrow verifier to look through borrowed from 2024-12-16 10:23:18 -08:00
Nate Chandler
bf80307c03 [OSSACompleteLifetime] Recurse into scoped addrs.
Previously, when determining and completing lifetimes of scoped
addresses, `computeTransitiveLiveness` was used to determine the
liveness used for completing the lifetime.

That approach had two problems:
(1) The function does not find scope-ending uses of `load_borrow`s.
    The result was determining that the lifetime of the enclosing
    `store_borrow` ended before that of the `load_borrow`.
(2) The function did not complete lifetimes of values defined within the
    scoped address whose lifetimes the scoped address had to contain.
    This was an inconsistency between the handling of scoped addresses
    and that of values.

Here, both are addressed by implementing a `TransitiveAddressWalker` (as
`computeTransitiveLiveness`'s callee does) which not only visits
existing `end_borrow`s of `load_borrows` but completes them (and other
inner guaranteed values or scoped addresses).

rdar://141246601
2024-12-13 15:04:29 -08:00
Nate Chandler
618b99c3e9 [NFC] OSSACompleteLifetime: Extracted method.
In preparation to expand it.
2024-12-13 12:22:31 -08:00
Nate Chandler
be7518b389 [OSSACompleteLifetime] Handle scoped addresses.
Complete scopes of scoped addresses (introduced by `store_borrow` and
`begin_access`) in dead end blocks via
`ScopedAddressValue::computeTransitiveLiveness`.

rdar://141037060
2024-12-06 16:22:24 -08:00
Nate Chandler
7fb5d31261 [NFC] OSSACompleteLifetime: Move function up.
Avoid a separate declaration.
2024-12-06 16:22:24 -08:00
Nate Chandler
36203de6d4 [NFC] OSSACompleteLifetime: Extracted function.
In preparation for adding a second caller.
2024-12-06 16:22:24 -08:00
Nate Chandler
c47c8ea72b [NFC] Return instruction from createScopeEnd. 2024-12-06 16:22:24 -08:00
nate-chandler
5637eaf3ca Merge pull request #77968 from nate-chandler/rdar139842132
[OSSACanonicalizeOwned] Record traversed defs and don't traverse copies of guaranteed values.
2024-12-06 07:00:20 -08:00
Nate Chandler
7012edc06f [Gardening] Test: Updated comment. 2024-12-05 07:31:19 -08:00
Kuba Mracek
9c77074cac [Mangling] Establish a new mangling prefix for Embedded Swift: $e 2024-12-02 15:01:24 -08:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Doug Gregor
077fec052c Pretend that swift_willThrowTypedImpl doesn't use its generic metadata 2024-11-21 12:39:41 -08:00
Andrew Trick
4612728581 [NFC] Add BeginApplyInst::getEndApplyUses() API.
A begin_apply token may be used by operands that do not end the coroutine:
mark_dependence.

We need an API that gives us only the coroutine-ending uses. This blocks
~Escapable accessors.

end_borrow is considered coroutine-ending even though it does not actually
terminate the coroutine.

We cannot simply ask isLifetimeEnding, because end_apply and abort_apply do not
end any lifetime.
2024-11-18 01:37:00 -08:00