Commit Graph

652 Commits

Author SHA1 Message Date
Andrew Trick
d653b0ccd0 Merge pull request #84739 from atrick/lifedep-unsafeaddress
LifetimeDependenceDefUseWalker: store to unsafeMutableAddress
2025-10-09 18:40:32 -07:00
Erik Eckstein
87a9d4ca2e Optimizer: notify that operand instructions of a deleted instruction have changed
This makes sure that instruction simplification doesn't terminate too early, missing some optimization opportunities.
2025-10-08 17:48:37 +02:00
Andrew Trick
4378995a78 LifetimeDependenceDefUseWalker: store to unsafeMutableAddress
Handle storing to a mutable property implemented as unsafeMutableAddress. In
SIL, the stored address comes from pointer_to_address. Recognize the addressor
pattern and handle the store as if it writes to a regular property of 'self'.

Required for UnsafePointer<~Escapable>.pointee.
2025-10-07 13:22:27 -07:00
Andrew Trick
4a7895e15b SwiftCompilerSources: bridge Function.isAddressor() 2025-10-07 10:44:42 -07:00
eeckstein
401a2ac2bc Merge pull request #84704 from eeckstein/closure-specialization
ClosureSpecialization: support for OSSA and a big overhaul
2025-10-07 06:59:08 +02:00
Erik Eckstein
08696eeb92 Cloner: set the cloner's builder insertion point to the entry block when creating a new entry block in the cloned function
This allows clients to directly clone specific instructions into the new entry block
2025-10-06 09:47:40 +02:00
Erik Eckstein
898b8754bf SIL: add some Instruction/Builder APIs
* `var UncheckedValueCastInst.fromValue`
* `BeginApplyInst.isNonThrowing` and `BeginApplyInst.isNonAsync`
* `Builder.createUncheckedValueCast`
2025-10-06 09:47:40 +02:00
Erik Eckstein
c3612bafb8 SIL: make var OperandArray.values available for all kind of operand sequences 2025-10-06 09:47:40 +02:00
Andrew Trick
771e9b522e Lifetimes: add a diagnostic note for implicit accessors
Lifetime diagnostics may report an error within an implicit initializer or
accessor. The source location is misleading in these cases and causes much
consternation.
2025-10-05 20:16:44 -07:00
Arnold Schwaighofer
7853ba0a7f Merge pull request #84178 from aschwaighofer/inline_always
Add experimental feature `@inline(always)`
2025-10-01 07:23:24 -07:00
Arnold Schwaighofer
25a071efc8 Add experimental feature @inline(always)
The intent for `@inline(always)` is to act as an optimization control.
The user can rely on inlining to happen or the compiler will emit an error
message.

Because function values can be dynamic (closures, protocol/class lookup)
this guarantee can only be upheld for direct function references.

In cases where the optimizer can resolve dynamic function values the
attribute shall be respected.

rdar://148608854
2025-09-30 08:36:26 -07:00
Erik Eckstein
7bbe5c6fe2 LoopInvariantCodeMotion: don't hoist loads and stores if the memory location is not initialized at loop exits.
If the memory is not initialized at all exits, it would be wrong to insert stores at exit blocks.
2025-09-30 10:39:51 +02:00
eeckstein
9412e192c1 Merge pull request #84482 from eeckstein/smallprojectionpath-tests
SIL: use `Test` for the SmallProjectionPath's unit tests
2025-09-25 17:37:04 +02:00
Anthony Latsis
3d898f71e5 SwiftCompilerSources/SIL: More robust IntegerLiteralInst construction
ac619010e3 backfired when building the
stdlib on rebranch. This time the problem is reversed: we should be
interpreting an integer as unsigned where we no longer do, here:
8f7af45115/SwiftCompilerSources/Sources/Optimizer/InstructionSimplification/SimplifyLoad.swift (L78).

Rather than treating integers as signed by default, make
`Builder.createIntegerLiteral` accept a generic `FixedWidthInteger`, and
manipulate the value based on the generic type argument's signedness.

This doesn't entirely define away unintentional sign extensions, but
makes this mistake of humouring the parameter type less likely.
2025-09-25 08:05:57 +01:00
Anthony Latsis
275deaf154 Merge pull request #84052 from swiftlang/jepa-main2
SwiftCompilerSources/SIL: Fix APInt assertion failure on rebranch
2025-09-24 17:39:04 +01:00
Erik Eckstein
39b9969049 SIL: use Test for the SmallProjectionPath's unit tests
Now that we have `Test` available in the SIL module, we can use it for the SmallProjectionPath's unit tests and get rid of the RunUnitTests pass.
2025-09-24 11:46:34 +02:00
Anthony Latsis
ac619010e3 SwiftCompilerSources/SIL: Fix APInt assertion failure on rebranch
The assertion is hit through `TypeValueInst.simplify` when constructing
an integer literal instruction with a negative 64-bit `Swift.Int` and a
bit width of 32 (the target pointer bit width for arm64_32 watchOS).
This happens because we tell the `llvm::APInt` constructor to treat the
input integer as unsigned by default in `getAPInt`, and a negative
64-bit signed integer does not fit into 32 bits when interpreted as
unsigned.

Fix this by flipping the default signedness assumption for the Swift API
and introducing a convenience method for constructing a 1-bit integer
literal instruction, where the correct signedness assumption depends on
whether you want to use 1 or -1 for 'true'.

In the context of using an integer to construct an `llvm::APInt`, there
are 2 other cases where signedness matters that come to mind:
1. A non-decimal integer literal narrower than 64 bits, such as
   `0xABCD`, is used.
2. The desired bit width is >64, since `llvm::APInt` can either
   zero-extend or sign-extend the 64-bit integer it accepts.

Neither of these appear to be exercised in SwiftCompilerSources, and
if we ever do, the caller should be responsible for either (1)
appropriately extending the literal manually, e.g.
`Int(Int16(bitPattern: 0xABCD))`, or (2) passing along the appropriate
signedness.
2025-09-24 09:37:42 +01:00
Erik Eckstein
935e5ecd7a SIL: fix Builder.emitDestroy(of:)
Do nothing for values with address types.

Fixes a crash in ConstantCapturePropagation
rdar://160816390
2025-09-23 10:50:26 +02:00
eeckstein
6557efc81d Merge pull request #84437 from eeckstein/fix-smallprojectionpath
fix handling of large indices in SmallProjectionPath
2025-09-23 07:13:31 +02:00
Jakub Florek
bf16a4248c Merge pull request #84398 from MAJKFL/bring-back-licm-ownership
Reintroduce licm support for ownership
2025-09-22 17:57:15 +01:00
Erik Eckstein
da17c26bae fix handling of large indices in SmallProjectionPath
* Fix the right shift operator which didn't work if the number of bits is exactly 64
* Detect overflow when combining indices

Such large indices usually don't appear in real code, except in internal String operations where (potentially large) integer values are treated as pointers.

Fixes a compiler crash
https://github.com/swiftlang/swift/issues/84372
rdar://160863199
2025-09-22 18:49:24 +02:00
Jakub Florek
38f28c1049 Reapply "Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-ownership"
This reverts commit d2cd281d4c.
2025-09-19 16:06:35 +01:00
Kavon Farvardin
84cf0a3551 Merge pull request #81858 from kavon/kavon/manual-ownership
introduce @_manualOwnership performance attribute
2025-09-18 16:15:20 -07:00
Kavon Farvardin
61fe8a9b8e introduce @_manualOwnership performance attribute
This attribute forces programmers to acknowledge every
copy that is required to happen in the body of the
function. Only those copies that make sense according
to Swift's ownership rules should be "required".

The way this is implemented as of now is to flag each
non-explicit copy in a function, coming from SILGen, as
an error through PerformanceDiagnostics.
2025-09-17 13:51:57 -07:00
Jakub Florek
d2cd281d4c Revert "Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-ownership"
This reverts commit a5c6156525, reversing
changes made to 2b6ea81b9e.
2025-09-17 15:52:48 +01:00
Jakub Florek
a5c6156525 Merge pull request #84045 from MAJKFL/new-sil-licm-pass-copy-ownership
Ownership support for LICM
2025-09-16 11:02:28 +01:00
Meghana Gupta
d3e41e5f2b Update SwiftCompilerSources' OwnershipLiveness utility for borrow accessors
It uses a check on conformance to ForwardInstruction for walking down guaranteed forwarding uses.
Since apply of borrow accessors cannot be represented as ForwardingInstruction, handle them separately.

Representing apply of borrow accessors for consistent handling in the optimizer is TBD.
2025-09-14 23:38:10 -07:00
Meghana Gupta
51c64d5fda Update SideEffects for borrow accessors returning addresses
Add a read effect on the self parameter. Without this, the self parameter can get dead code eliminated by the GenericSpecializer.
2025-09-14 23:38:03 -07:00
Jakub Florek
df24019195 Rename Cloner.cloneRecursivelyToGlobal. 2025-09-10 16:18:50 +01:00
Meghana Gupta
ce128e71e2 Update SIL verification for borrow accessors 2025-09-09 15:25:08 -07:00
Meghana Gupta
0bec28f510 Update borrowed from for guaranteed results of borrow accessors 2025-09-09 15:20:05 -07:00
Meghana Gupta
a798f21fd2 Introduce ResultConvention::Guaranteed and ResultConvention::GuaranteedAddress in SwiftCompilerSources 2025-09-09 14:45:41 -07:00
Andrew Trick
592642087f Merge pull request #84010 from atrick/lifedep-mutating-default
@_lifetime defaults
2025-09-04 15:44:46 -07:00
Erik Eckstein
d802b5955f SIL: Fix ResultInfo.init(bridged: OptionalBridgedResultInfo)
The initializer has to be optional because OptionalBridgedResultInfo can be nil
2025-09-04 08:15:46 +02:00
Erik Eckstein
b8a49692eb Optimizer: add TypeSubstitutionCloner and func cloneAndSpecializeFunction
Also move `func cloneFunction` from ContextCommon.swift to OptUtils.swift
2025-09-04 08:15:45 +02:00
Erik Eckstein
231042b9a8 SIL: some Cloner cleanups and improvements
* move some Cloner utilities from ContextCommon.swift directly into Cloner.swift
* add an `cloneRecursively` overload which doesn't require the `customGetCloned` closure argument
* some small cleanups
2025-09-04 08:15:45 +02:00
Erik Eckstein
65d69fe965 SIL/AST: add some APIs
* `GenericSignature.isEmpty`
* `Builder.emitDestroy`
* `Function.abi`
* `KeyPathInst.substitutionMap`
* `KeyPathInst.hasPattern`
2025-09-04 08:15:44 +02:00
Andrew Trick
ed9283e1ad [NFC] drive-by comment 2025-09-03 22:42:13 -07:00
Janat Baig
f21eb5375e Merge branch 'main' into temp-branch 2025-09-02 20:23:25 -04:00
Jakub Florek
eae7864370 Merge pull request #83988 from MAJKFL/new-sil-licm-pass-copy
New SIL LICM pass
2025-09-01 10:28:17 +01:00
Erik Eckstein
3de5d6a13e SIL: add get+set for enforcement in BeginAccessInst 2025-08-30 07:29:15 +02:00
nate-chandler
9fe43836f6 Merge pull request #83907 from nate-chandler/rdar158149082
[AllocBoxToStack] Don't destroy in dead-ends.
2025-08-28 13:36:28 -07:00
Jakub Florek
07ac8b3478 Add new loop invariant code motion. 2025-08-28 21:00:33 +01:00
Jakub Florek
3514b2b9df Bridge array semantics call and loop tree 2025-08-28 21:00:09 +01:00
Jakub Florek
e3140e0ae0 Add new generalized cloner. 2025-08-28 20:57:57 +01:00
eeckstein
8cea77eac0 Merge pull request #83913 from eeckstein/fix-deinit-devirtualization-for-arrays
Optimizer: de-virtualize deinits of `builtin "destroyArray"`
2025-08-28 18:21:07 +02:00
Erik Eckstein
e5081a511f AliasAnalysis: fix aliasing of ref_tail_addr access bases
In contrast to `ref_element_addr`, tail addresses can also be obtained via a superclass (in case the derived class doesn't add any stored properties).
Therefore if the instance types differ by sub-superclass relationship, they may alias.

Fixes a miscompile
2025-08-28 08:09:39 +02:00
Erik Eckstein
3da2c1476b SIL: add Builder.createCondBranch 2025-08-28 08:04:50 +02:00
Erik Eckstein
ab10dc23a5 SIL: add Context.getTupleType 2025-08-28 08:04:49 +02:00
Nate Chandler
f94168c875 [AllocBoxToStack] Restore isDeadEnd check.
The rewrite was missing the intentional omission of `dealloc_stack`s
corresponding to `[dead_end]` `dealloc_box`es.  Add the necessary
bridging to get to parity with the original.

Without this check, `dealloc_box [dead_end]`s are promoted to
`dealloc_stack`s but the memory projected out of such `alloc_box`s need
not be valid.

rdar://159271158
2025-08-27 17:02:27 -07:00