Commit Graph

6634 Commits

Author SHA1 Message Date
Meghana Gupta
28e33af799 Merge pull request #78123 from meg-gupta/newflag
Add new flag -sil-ownership-verify-all
2024-12-13 10:00:41 -08:00
Meghana Gupta
8b1ecb8a71 Add new flag -sil-ownership-verify-all
This flag enables ownership verification after every transform.
2024-12-12 23:55:37 -08:00
Usama Hameed
203f906364 Serialize/Deserialize source locations for instructions (#77281)
This commit adds support for serializing and deserializing source locations for instructions.
2024-12-12 16:15:44 +05:00
eeckstein
81c65758e3 Merge pull request #78059 from eeckstein/destroy-hoisting
Optimizer: add a new destroy-hoisting optimization
2024-12-11 06:18:05 +01:00
Erik Eckstein
c5efad1e2d SIL: make isLexical a protocol requirement of Value
This avoids the need for checking if a value is a `move_value`, `begin_borrow` or function argument to get the is-lexical information.
2024-12-10 16:28:11 +01:00
Saleem Abdulrasool
a3b60ed0f3 SIL: avoid truncation warnings on Windows (NFCI)
`enum` is a signed integer type on some platforms. The return value of
`std::numeric_limits<T>::max()` is a `size_t` which is unsigned. Further
manipulation retains the unsigned-ness until the assignment which
results in truncation. Change the type to a `constexpr` constant
definition and remove the `enum` type along with a type conversion to an
explicitly unsigned integral type.
2024-12-09 08:29:23 -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
c47c8ea72b [NFC] Return instruction from createScopeEnd. 2024-12-06 16:22:24 -08:00
Nate Chandler
be88e7c9d9 [NFC] OSSACompleteLifetime: Tweak control flow.
Use a switch rather than several ifs.
2024-12-06 16:22:24 -08:00
Kuba Mracek
6f4ae28520 [ASTMangler] Pass ASTContext to all instantiations of ASTMangler 2024-12-02 15:01:04 -08:00
Erik Eckstein
e156da9167 SIL: make sure to not crash if a function doesn't have a DebugScope
Change an assert to a bail-out condition.
I found that it _can_ happen to have a function without a debug scope.
In such a case even printing the SIL crashed.
Be a bit more tolerant.
2024-11-27 18:05:36 +01:00
Andrew Trick
c05044c65c Add MarkDependenceInst.settleToEscaping
This allows emitting mark_dependence [unresolved] in some cases even if the
diagnostics can't currently handle them.
2024-11-18 22:02:44 -08:00
Andrew Trick
ff21ddefa6 Merge pull request #77675 from atrick/endapply_utils
[NFC] Utilities for lifetime dependent coroutines
2024-11-18 09:44:25 -08:00
Andrew Trick
3e26cf81d0 SwiftCompilerSources: bridge createEndApply and createAbortApply. 2024-11-18 01:37:00 -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
Slava Pestov
47156e006b AST: Introduce ProtocolConformanceRef::forAbstract() 2024-11-16 16:16:06 -05: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
Erik Eckstein
7bf0010c92 SIL: remove computeIsReborrow
This API computes the re-borrow flags for guaranteed phis based on the presence of forwarding instructions in the incoming values.
This is not correct in all cases, because optimizations can optimize away forwarding instructions.

Fixes a verifier crash: rdar://139280579
2024-11-12 09:26:59 +01:00
Erik Eckstein
83224b255e SIL: automatically set the reborrow flags for phi-arguments when creating an end_borrow or setting the operand of an end_borrow
This is done for all transitively incoming phi-arguments of an end_borrow operand.
2024-11-12 09:26:58 +01:00
Erik Eckstein
f7d49a9680 SIL: add the Argument.set(reborrow:) and Argument.hasBorrowEndingUse APIs 2024-11-12 09:26:58 +01:00
Arnold Schwaighofer
34c417d9ff Merge pull request #77379 from aschwaighofer/enable_aggressive_reg2mem
Enable heuristic that tries to keep large values in memory
2024-11-06 11:36:33 -08:00
Erik Eckstein
15c6abf66d SIL: avoid creating SIL functions for not used imported specialization attributes
This ended up in creating a lot of Array functions, even if a program didn't use Array at all.
Now, only add specialization attributes if a function is already there.
Otherwise remember the attributes and add them to a function once it is created.
2024-11-05 20:37:15 +01:00
Arnold Schwaighofer
dc3c19164a PMO: Don't block pmo for large types - rather only block expansion of tuples 2024-11-04 17:06:24 -08:00
Michael Gottesman
1966980c6b Merge pull request #77354 from gottesmm/rdar136593706
[region-isolation] Fix an off by one error when mapping AST capture indices to SIL level parameter indices.
2024-11-01 20:03:42 -07:00
Michael Gottesman
3c38c79f7a [region-isolation] Implement MergeIsolationRegionInst.
I am adding this instruction to express artificially that two non-Sendable
values should be part of the same region. It is meant to be used in cases where
due to unsafe code using Sendable, we stop propagating a non-Sendable dependency
that needs to be made in the same region of a use of said Sendable value. I
included an example in ./docs/SIL.rst of where this comes up with @out results
of continuations.
2024-11-01 11:25:53 -07:00
Michael Gottesman
e926003cce Merge pull request #77288 from gottesmm/pr-8f042612c02c8c6af6e98fe9488fb6e20e95b92e
[sil] Move mark_unresolved_move_addr in SILNodes.def so it is not classified as a SingleValueInstruction.
2024-10-31 17:23:50 -07:00
Akira Hatanaka
f73c2e51bd Revert "[SILGen] Fix the type of closure thunks that are passed const reference structs (#76903)" (#77309)
This reverts commit 9c44b79189.

The commit caused swift's deserialization code to crash.

rdar://138726860
2024-10-31 15:16:00 -07:00
John McCall
56ec62570f Merge pull request #77308 from rjmccall/optional-isolation-check
Fix a crash when emitting isolation checks in a function with optional isolation
2024-10-31 10:50:27 -07:00
Michael Gottesman
c3d445831b [region-isolation] Fix an off by one error when mapping AST capture indices to SIL level parameter indices.
This problem comes up with the following example:

```swift
class A {
    var description = ""
}

class B {
    let a = A()

    func b() {
        let asdf = ""
        Task { @MainActor in
            a.description = asdf // Sending 'asdf' risks causing data races
        }
    }
}
```

The specific issue is that the closure we generate actually includes an
implicit(any) parameter at the SIL level which occurs after the callee operand
but before the captures. This caused the captured variable index from the AST
and the one we compute from the partial_apply to differ by 1. So we need to
subtract 1 in such a case. That is why we used to print 'asdf' instead of 'a'
above.

DISCUSSION: This shows an interesting difference between SIL applied arg indices
and AST indices. SIL applied arg indices would include the implicit(any)
parameter since it is a parameter in the SIL function type. In contrast, this
doesn't show up in the formal AST parameters or captures. To make it easier to
reason about this, I added a new API to ApplySite called
ApplySite::getASTAppliedArgIndex and added large comments to
getASTAppliedArgIndex and getAppliedArgIndex that explains the issue.

rdar://136593706
https://github.com/swiftlang/swift/issues/76648
2024-10-30 18:32:45 -07:00
John McCall
718390a615 Add a helper function for emitting a switch_enum that breaks down an Optional. 2024-10-30 14:41:50 -07:00
Nate Chandler
0597ec5989 [SIL] BuiltinInsts have type dependent operands.
Collect all types in the substitution map which constitute
type-dependent operands and record them in the instruction's operand
list.  Fixes a bug where open_existential_metatype (e.g.) is deleted as
dead because it has no users even when the type it defines is used in a
substitution map of a builtin.
2024-10-29 21:45:25 -07: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
Michael Gottesman
7fd2e46a96 [sil] Move mark_unresolved_move_addr in SILNodes.def so it is not classified as a SingleValueInstruction.
The specific problem here is that this causes MarkUnresolvedMoveAddr to be
within the SINGLE_VALUE_INST_RANGE which defines the classify method used to
determine if casting to a SingleValueInstruction is ok. This is of course not ok
in this case since mark_unresolved_move_addr is actually a NonValueInst.

I noticed this b/c I made the same mistake with a new instruction I am added and
hit a crash in the tests as a result of it being classified as a single value
instruction. After fixing that, I checked for any more instances of this issue
and found that mark_unresolved_move_addr was similarly afflicted.
2024-10-29 16:13:43 -07:00
Ben Barham
f59b2f19e6 Merge branch 'main' into 2024-rebranch-to-main 2024-10-28 13:03:35 -07:00
Erik Eckstein
ed67e36ce5 bridging: reduce #ifdef USED_IN_CPP_SOURCE in bridging headers
Especially avoid any constructors in `#ifdef USED_IN_CPP_SOURCE` blocks, because this breaks Windows ARM64.
2024-10-25 09:47:56 +02:00
Ben Barham
746517c9f2 Merge remote-tracking branch 'origin/main' into manually-merge-main-to-rebranch
Conflicts:
  - `test/IRGen/has_symbol.swift` test updates in rebranch. The new test
    will fail, will fix in the next commit.
2024-10-24 14:20:17 -07:00
Joe Groff
8242110bbe Merge pull request #76831 from jckarter/builtin-vector
Builtin.FixedArray
2024-10-23 05:20:21 -10:00
Joe Groff
a184782a38 Introduce a Builtin.FixedArray type.
`Builtin.FixedArray<let N: Int, T: ~Copyable & ~Escapable>` has the layout of `N` elements of type `T` laid out
sequentially in memory (with the tail padding of every element occupied by the array). This provides a primitive
on which the standard library `Vector` type can be built.
2024-10-22 16:21:45 -07:00
swift-ci
3d969906c5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-22 13:14:35 -07:00
Andrew Trick
17a3b8a8a3 [MoveAddrChecker] load from borrowed value looks through mark_depend
Required to add dependencies to unsafe addressors.
2024-10-22 09:27:02 -07:00
swift-ci
5bfc87531f Merge remote-tracking branch 'origin/main' into rebranch 2024-10-17 18:21:11 -07:00
Michael Gottesman
0e0665bfbd [concurrency] Add support for HopToMainActorIfNeededThunk.
It is behind the experimental flag GenerateForceToMainActorThunks.
2024-10-17 13:31:39 -07:00
Michael Gottesman
f0f5ad54fd [thunk-lowering] Add support for mangling identity thunks.
This is just again using identity thunks to show that the behavior works before
I add in hop to main actor.
2024-10-17 11:17:38 -07:00
swift-ci
8cce69d0ad Merge remote-tracking branch 'origin/main' into rebranch 2024-10-14 11:35:01 -07:00
Erik Eckstein
e0533e6125 SIL: add an API to replace all entries of a VTable
* add `ModulePassContext.replaceVTableEntries()`
* add `ModulePassContext.notifyFunctionTablesChanged()`
2024-10-14 14:43:11 +02:00
swift-ci
8a2edbc4c5 Merge remote-tracking branch 'origin/main' into rebranch 2024-10-11 17:05:06 -07:00
nate-chandler
249831f9f3 Merge pull request #76942 from nate-chandler/general-coro/20240927/1
[CoroutineAccessors] SIL represents callee alloc.
2024-10-11 16:39:53 -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
Nate Chandler
df1c4f6b25 [CoroutineAccessors] Add new SILFnTy CoroKind.
For `modify` and `read` coroutines, produce SILFunctionType's whose
coroutineKind is ::YieldOnce2.
2024-10-11 08:25:02 -07:00
swift-ci
cb83584ebd Merge remote-tracking branch 'origin/main' into rebranch 2024-10-11 06:34:12 -07:00