Commit Graph

6648 Commits

Author SHA1 Message Date
Erik Eckstein
f962d76362 GenericSpecializer: don't pre-specialize C++ reference type for AnyObject
Reference counting is not compatible.
Fixes a runtime crash if a C++ reference type is put into an Array.

https://github.com/swiftlang/swift/issues/83082
rdar://155919841
2025-07-16 21:30:36 +02:00
Michael Gottesman
e18c68a552 Merge pull request #83083 from gottesmm/pr-e0e1571656b80b6c2d45f1a0cad842fbf6ae4800
[concurrency] Make optimize hop to executor more conservative for 6.2 around caller isolation inheriting functions.
2025-07-16 11:55:35 -07:00
Michael Gottesman
b3942424c8 [concurrency] Make optimize hop to executor more conservative for 6.2 around caller isolation inheriting functions.
Specifically for 6.2, we are making optimize hop to executor more conservative
around caller isolation inheriting functions. This means that we are:

1. No longer treating calls to caller isolation inheriting functions as having a
hop in their prologue. In terms of this pass, it means that when determining
dead hop to executors, we no longer think that a caller isolation inheriting
function means that an earlier hop to executor is not required.

2. Treating returns from caller isolation inheriting callees as requiring a
hop. The reason why we are doing this is that we can no longer assume that our
caller will hop after we return.

Post 6.2, there are three main changes we are going to make:

* Forward Dataflow

Caller isolation inheriting functions will no longer be treated as suspension
points meaning that we will be able to propagate hops over them and can assume
that we know the actor that we are on when we enter the function. Practically
this means that trees of calls that involve just nonisolated(nonsending) async
functions will avoid /all/ hop to executor calls since we will be able to
eliminate all of them since the dataflow will just propagate forward from the
entrance that we are already on the actor.

* Backwards Dataflow

A caller isolation inheriting call site will still cause preceding
hop_to_executor functions to be live. This is because we need to ensure that we
are on the caller isolation inheriting actor before we hit the call site. If we
are already on that actor, the hop will be eliminated by the forward pass. But
if the hop has not been eliminated, then the hop must be needed to return us to
the appropriate actor.

We will also keep the behavior that returns from a caller isolation inheriting
function are considered to keep hop to executors alive. If we were able to
propagate to a hop to executor before the return inst with the forward dataflow,
then we know that we are guaranteed to still be on the relevant actor. If the
hop to executor is still there, then we need it to ensure that our caller can
treat the caller isolation inheriting function as a non-suspension point.

rdar://155905383
2025-07-15 17:03:49 -07:00
Anthony Latsis
6eb5d7d857 Bridging: Bridge swift::SourceLoc directly 2025-07-15 21:33:06 +01:00
Anthony Latsis
0e0fbc4160 Bridging: Bridge swift::Identifier directly 2025-07-10 17:58:13 +01:00
Anthony Latsis
f131fece3f [NFC] Bridging: Rework include caveats 2025-07-10 17:58:13 +01:00
Anthony Latsis
b803861257 Bridging: Rename USED_IN_CPP_SOURCE and define it in Basic/SwiftBridging.h 2025-07-10 17:58:13 +01:00
Joe Groff
8ac81dc755 SILGen: Bitcast indirect returns that differ only in concurrency annotations.
A call to a `@preconcurrency` function goes through a function conversion
that removes `Sendable` from existentials among other things. Implement
support for this by bitcasting indirect return slots whose type differs
from the formal indirect return type in concurrency markings only.

Fixes rdar://154240007
2025-07-08 17:02:15 -07:00
Slava Pestov
a1b008cba8 SILCloner: Fix logic error when cloning without a substitution map
If we have no substitution map, we still substitute types appearing
in the original function, because we need to remap any local
archetypes, which are always cloned.

However, the conformance lookup callback used for this substitution
was wrong. We should only do mapTypeOutOfContext() if we're going
to callSubstitutionMap::lookupConformance(), otherwise we form a
new abstract conformance with an interface type, and not a primary
archetype as expected.
2025-07-02 11:59:39 -04:00
Erik Eckstein
1343dc562d SILBridging: remove OptionalBridgedSILDebugVariable
This didn't work because the BridgedSILDebugVariable destructor was called even in the "none" case.

Fixes a compiler crash
rdar://154689481
2025-07-01 10:31:30 +02:00
Arnold Schwaighofer
7ddf75c791 Merge pull request #82107 from tobias-stadler/remarks-fix-llvm-setup
[IRGen] Setup LLVMRemarkStreamer using existing RemarkStreamer
2025-06-30 13:50:31 -07:00
Arnold Schwaighofer
c6b7b1f6b8 Merge pull request #82110 from tobias-stadler/remarks-fix-mangled-functionnames
[SIL] RemarkStreamer: Use mangled function names
2025-06-27 09:57:15 -07:00
nate-chandler
17c1fbd5f4 Merge pull request #82313 from jamieQ/copy-prop-ossa-dead-ends-build-time-fix
[SILOptimizer]: slow OSSA lifetime canonicalization mitigation
2025-06-21 08:50:58 -07:00
eeckstein
1d3895610e Merge pull request #82349 from eeckstein/alloc-box-to-stack
Optimizer: re-implement and improve the AllocBoxToStack pass
2025-06-21 07:28:18 +02:00
Erik Eckstein
fc8f264d56 SIL: add some instruction APIs
* some APIs for `MarkUnresolvedNonCopyableValueInst`
* `AllocBoxInst.hasDynamicLifetime`
2025-06-20 08:15:03 +02:00
Erik Eckstein
2259fe6972 SIL: add some instruction creation functions in Builder 2025-06-20 08:15:03 +02:00
Erik Eckstein
c19aa69940 SIL: implement Function.isSerialized and Function.isAnySerialized with serializedKind
No need for bridging functions
2025-06-20 08:15:02 +02:00
Erik Eckstein
c70298220d SIL: fix debug locations for various kind of instructions
There are some restrictions for which kind of locations can be on which kind of instructions.
This change implements them correctly.
2025-06-20 08:15:02 +02:00
Erik Eckstein
c482b09878 SIL: let Builder.createAllocStack specify a debugVariable 2025-06-20 08:15:02 +02:00
Erik Eckstein
28dd6f7064 Optimizer: improve the SpecializationCloner
* add `cloneFunctionBody` without an `entryBlockArguments` argument
* remove the `swift::ClosureSpecializationCloner` from the bridging code and replace it with a more general `SpecializationCloner`
2025-06-20 08:15:02 +02:00
Erik Eckstein
9bd85c6723 SIL: add some Function APIs
* `isReferencedInModule`
* `shouldOptimize`
2025-06-20 08:15:01 +02:00
Erik Eckstein
d8e4e501f6 Optimizer: add some SIL modification APIs to Context
* `insertFunctionArgument`
* `BeginAccessInst.set(accessKind:)`
* `erase(function:)`
2025-06-20 08:15:01 +02:00
Erik Eckstein
63cb683cb7 SIL: improve some Location APIs
* rename `var autoGenerated` -> `var asAutoGenerated`
* add `var asCleanup`
* add `func withScope`
2025-06-20 08:15:00 +02:00
Erik Eckstein
1f304e5609 SIL: improve APIs for Box types
* move `isBox` from `SIL.Type` to `TypeProperties` to make it also available for AST types
* add `BoxFieldsArray.isMutable(fieldIndex:)`
2025-06-20 08:14:59 +02:00
Erik Eckstein
d025e9f7a5 SIL: add var Argument.decl: ValueDecl? 2025-06-20 08:14:58 +02:00
Anthony Latsis
7de8827169 BasicBridging: Turn BridgedOptional into a macro 2025-06-19 18:11:38 +01:00
Jamie
1f3f830fc7 [SILOptimizer]: slow OSSA lifetime canonicalization mitigation
OSSA lifetime canonicalization can take a very long time in certain
cases in which there are large basic blocks. to mitigate this, add logic
to skip walking the liveness boundary for extending liveness to dead
ends when there aren't any dead ends in the function.

Updates `DeadEndBlocks` with a new `isEmpty` method and cache to
determine if there are any dead-end blocks in a given function.
2025-06-18 17:52:14 -05:00
Anthony Latsis
08d71a46eb Basic, SIL: Include <swift/bridging> when avaiable instead of defining our own macros 2025-06-13 23:13:26 +01:00
Tobias Stadler
76568372f4 [IRGen] Setup LLVMRemarkStreamer using existing RemarkStreamer
Calling setupLLVMOptimizationRemarks overwrites the MainRemarkStreamer
in the LLVM context. This prevents LLVM from serializing the remark meta
information for the already emitted SIL remarks into the object file.
Without the meta information bitstream remarks don't work correctly.

Instead, emit SIL remarks and LLVM remarks to the same RemarkSerializer,
and keep the file stream alive until after CodeGen.
2025-06-12 18:31:41 +01:00
Tobias Stadler
4b0c0001bc [SIL] RemarkStreamer: Use mangled function names
Remarks emitted by LLVM use the mangled function name. Be consistent
with LLVM when serializing SIL remarks, so external tools can properly
filter for all remarks attributed to a certain function.
2025-06-12 18:29:35 +01:00
Meghana Gupta
8396a6d8c0 Fix an inliner crash when inlining begin_apply with scoped lifetime dependence
LifetimeDependenceInsertion inserts mark_dependence on token result of a begin_apply
when it yields a lifetime dependent value. When such a begin_apply gets inlined,
the inliner can crash because of the remaining uses of the token result.

Fix this by inserting mark_dependence on parameter operands that are lifetime dependence sources
and deleting the mark_dependence on token results in the inliner.

Fixes rdar://151568816
2025-06-12 01:35:36 -07:00
Erik Eckstein
73da214986 GenericSpecializer: remove some kind of instructions if their operands become trivial after specialization
Fixes a SIL verifier crash.

https://github.com/swiftlang/swift/issues/82093
rdar://152807200
2025-06-10 16:49:54 +02:00
Doug Gregor
bc4cf1236b [SIL] Generalize CastingIsolatedConformances to CheckedCastInstOptions
We are going to need to add more flags to the various checked cast
instructions. Generalize the CastingIsolatedConformances bit in all of
these SIL instructions to an "options" struct that's easier to extend.

Precursor to rdar://152335805.
2025-06-04 17:12:28 -07:00
Nate Chandler
33d5c8648b [MemAccessUtils] Add to strip access storage casts
The new function stripAccessAndAccessStorageCasts is analogous to the
existing function stripAccessAndIdentityCasts but differs in that the
latter uses isAccessStorageIdentityCast whereas the new function uses
isAccessStorageCast.
2025-06-03 15:32:26 -07:00
Nate Chandler
974d179571 [MemAccessUtils] This instruction changes the type
moveonlywrapper_to_copyable_box changes the type so it's a type cast not
an identity cast.
2025-06-03 15:32:26 -07:00
nate-chandler
dd75d2ed82 Merge pull request #81854 from nate-chandler/rdar152195094
[DestroyAddrHoisting] Don't destructure NE aggs.
2025-05-30 11:32:12 -07:00
Nate Chandler
f7ca26ce93 [NFC] SIL: This utility takes a func not a module.
In preparation to use the function in the implementation.
2025-05-29 15:36:15 -07:00
Erik Eckstein
d10602ea28 SIL: improve and fix mark-dependence instruction APIs
* Move the mutating APIs into Context.swift, because SIL can only be mutated through a MutatingContext
* move the `baseOperand` and `base` properties from the instruction classes to the `MarkDependenceInstruction` protocol
* add `valueOrAddressOperand` and `valueOrAddress` in the `MarkDependenceInstruction` protocol
2025-05-23 18:53:57 +02: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
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
Erik Eckstein
e3b770853a SIL: add GlobalVariable.type 2025-05-20 20:46:33 +02:00
Hamish Knight
3ec3421987 Merge pull request #81604 from hamishknight/pretty-crash
Adopt `ABORT` throughout the compiler
2025-05-20 10:05:03 +01:00
Joe Groff
10a80f544f Merge pull request #81583 from jckarter/resilient-types-arent-addressable-inside-resilience-domain
SIL: Resilient types don't need to be treated as addressable-for-dependencies inside their resilience domain.
2025-05-19 17:29:43 -07:00
Hamish Knight
edca7c85ad Adopt ABORT throughout the compiler
Convert a bunch of places where we're dumping to stderr and calling
`abort` over to using `ABORT` such that the message gets printed to
the pretty stack trace. This ensures it gets picked up by
CrashReporter.
2025-05-19 20:55:01 +01:00
Michael Gottesman
abcc134621 Merge pull request #81542 from gottesmm/rdar150209093_rdar151394209
[concurrency] Fix a few issues around not emitted correct protocol witness and vtable thunks for nonisolated(nonsending)
2025-05-19 10:43:18 -07:00
Joe Groff
21c17901b9 SIL: Resilient types don't need to be treated as addressable-for-dependencies inside their resilience domain.
Outside of the resilience domain, they have to be treated as opaque and therefore potentially
addressable-for-dependencies, but inside of the resilience domain, we may take advantage of
knowing the type layout to load indirect parameters out of memory and break the (unnecessary)
dependency on a fixed memory location. Fixes rdar://151268401.

We do still however have problems when the type is actually `@_addressableForDependencies`
inside of its resilience domain (rdar://151500074). I'll fix that in a follow up.
2025-05-19 09:07:44 -07:00
Michael Gottesman
ff1cbea576 [silgen] Begin placing isolation on protocol witness thunks.
This will cause tests today to crash since even though we are placing the
isolation now, to make it easier to read, I left in the old isolation selecting
code. This code uses the witness's isolation instead of the requirement's
isolation which is incorrect since the protocol witness thunk needs to look the
requirement from an ABI perspective since the two must be substitutable. The
crash comes from the ABI verification I added in earlier commits.
2025-05-15 14:10:43 -07:00
Erik Eckstein
140b883b9a SIL: let TypeValueInst.value return an optional Int
and don't require the client to check if it's an integer type.
Also, implement `var value` natively and without bridging.
2025-05-15 21:29:02 +02:00
Erik Eckstein
7f6fb74418 SILBridging: move BridgedInstruction.OptionalInt -> BridgedOptionalInt in BasicBridging.h 2025-05-15 21:29:02 +02:00
Erik Eckstein
5dc71aa0a5 AST/SIL: support source location in diagnostics for de-serialized debug info
Diagnostics only work with `SourceLoc` which is basically a pointer into a buffer of the loaded source file.
But when debug info is de-serialized, the SIL `Location` consists of a filename+line+column.
To "convert" this to a `SourceLoc`, the file must be loaded.
This change adds `DiagnosticEngine.getLocationFromExternalSource` for this purpose.
Also, the new protocol `ProvidingSourceLocation` - to which `SourceLoc` and `Location` conform - help to generalize the helper struct `Diagnostic` and make this "conversion" happen automatically.
2025-05-14 11:43:47 +02:00