Commit Graph

206 Commits

Author SHA1 Message Date
Erik Eckstein
85228f2c75 Optimizer: do mark_dependence simplification at Onone
* re-implement the SILCombine peephole as a Swift instruction simplification
* run this simplification also in the OnoneSimplification pass
2025-05-23 18:53:57 +02:00
Andrew Trick
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -07:00
Arnold Schwaighofer
2f30161563 Merge pull request #80097 from aschwaighofer/remove_cond_fails_by_message
SIL: Allow to selectively disabled cond_fails by cond_fail message
2025-03-19 10:26:21 -07:00
Arnold Schwaighofer
1fd53d23a3 SIL: Allow to selectively disabled cond_fails by cond_fail message
The standard library uses `_precondition` calls which have a message argument.

Allow disabling the generated cond_fail by these message arguments.

For example:

 _precondition(source >= (0 as T), "Negative value is not representable")

Results in a `cond_fail "Negative value is not representable"`.

This commit allows for specifying a file that contains these messages on
each line.

/path/to/disable_cond_fails:
```
Negative value is not representable
Array index is out of range
```

The optimizer will remove these cond_fails if the swift frontend is invoked with
`-Xllvm -cond-fail-config-file=/path/to/disable_cond_fails`.
2025-03-18 13:04:03 -07:00
Erik Eckstein
46035305aa Optimizer: improve simplification of alloc_stack
* Reimplement most of the logic in Swift as an Instruction simplification and remove the old code from SILCombine
* support more cases of existential archetype replacements:

For example:
```
  %0 = alloc_stack $any P
  %1 = init_existential_addr %0, $T
  use %1
```
is transformed to
```
  %0 = alloc_stack $T
  use %0
```

Also, if the alloc_stack is already an opened existential and the concrete type is known,
replace it as well:
```
  %0 = metatype $@thick T.Type
  %1 = init_existential_metatype %0, $@thick any P.Type
  %2 = open_existential_metatype %1 : $@thick any P.Type to $@thick (@opened("X", P) Self).Type
  ...
  %3 = alloc_stack $@opened("X", any P) Self
  use %3
```
is transformed to
```
  ...
  %3 = alloc_stack $T
  use %3
```
2025-03-07 15:59:34 +01: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
Erik Eckstein
69b16faa04 SILCombine: enable alloc_stack optimization for OSSA 2024-12-17 09:55:39 +01:00
Andrew Trick
e56dbb4695 Improve SILCombine mark_dependence: handle address dependence. 2024-12-14 16:26:58 -08:00
Erik Eckstein
bf496aa4f6 Optimizer: add simplification for fix_lifetime
Canonicalize a `fix_lifetime` from an address to a `load` + `fix_lifetime`:
```
   %1 = alloc_stack $T
   ...
   fix_lifetime %1
```
->
```
   %1 = alloc_stack $T
   ...
   %2 = load %1
   fix_lifetime %2
```

This transformation is done for `alloc_stack` and `store_borrow` (which always has an `alloc_stack` operand).
The benefit of this transformation is that it enables other optimizations, like mem2reg.

This peephole optimization was already done in SILCombine, but it didn't handle store_borrow.
A good opportunity to make an instruction simplification out of it.

This is part of fixing regressions when enabling OSSA modules:
rdar://140229560
2024-12-13 12:06:20 +01:00
Erik Eckstein
6b38f2aab4 Optimizer: simplify load_borrow
* Remove dead `load_borrow` instructions (replaces the old peephole optimization in SILCombine)
* If the `load_borrow` is followed by a `copy_value`, combine both into a `load [copy]`
2024-12-11 12:32:33 +01:00
Meghana Gupta
d58d97d97a Fix SILCombine of inject_enum_addr of ~Copyable values
Fixes rdar://138798467
2024-10-29 14:09:57 -07:00
Erik Eckstein
c97502374b Optimizer: add constant folding of classify_bridge_object
Constant fold `classify_bridge_object` to `(false, false)` if the operand is known to be a swift class.
2024-10-08 16:24:46 +02:00
Allan Shortlidge
02dbb96b94 AST: Rename AvailabilityContext to AvailabilityRange.
The generality of the `AvailabilityContext` name made it seem like it
encapsulates more than it does. Really it just augments `VersionRange` with
additional set algebra operations that are useful for availability
computations. The `AvailabilityContext` name should be reserved for something
pulls together more than just a single version.
2024-09-13 16:25:18 -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
Andrew Trick
e06fc99667 Fix SILCombine to delete dead end_access instructions.
Otherwise, the SILVerifier will raise an error.

Fixes rdar://121599876 (SILCombine should delete instructions in
blocks dominated by cond_fail -1)
2024-05-31 23:01:34 -07:00
Egor Zhdan
fd04cc3a4e [cxx-interop][Runtime] Initialize metadata of a Swift array of C++ references correctly
This fixes a crash at runtime when destroying a Swift array of values of a C++ foreign reference type.

Swift optimizes the amount of metadata emitted for `_ContiguousArrayStorage<Element>` by reusing `_ContiguousArrayStorage<AnyObject>` whenever possible (see `getContiguousArrayStorageType`). However, C++ foreign reference types are not `AnyObject`s, since they have custom retain/release operations.

This change disables the `_ContiguousArrayStorage` metadata optimization for C++ reference types, which makes sure that `swift_arrayDestroy` will call the correct release operation for elements of `[MyCxxRefType]`.

rdar://127154770
2024-05-15 19:20:44 +01:00
Emil Pedersen
9984d1d3a6 [SILCombine] Correctly drop debug info when fragmenting an enum 2024-05-10 18:47:28 -07:00
Emil Pedersen
5f2926ca29 [DebugInfo] [SILCombine] Fix variable for opened existential
The type of the new variable should be opened too.
If the variable has an expression modifying the type, it cannot be rewritten and is dropped.
2024-05-10 16:12:56 -07:00
Nate Chandler
3070f83568 [SILCombine] Dominance check for inject_enum_addr.
If stores of empty values to the address don't dominate the
inject_enum_addr, the inject_enum_addr can't be eliminated--it's the
only indication of the case that's in the address.
2024-04-15 19:10:19 -07:00
Erik Eckstein
e14c1d1f62 SIL, Optimizer: update and handle borrowed-from instructions
Compute, update and handle borrowed-from instruction in various utilities and passes.
Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`.
Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
2024-04-10 13:38:10 +02:00
Meghana Gupta
d774305050 Enable inject_enum_addr silcombine in ossa 2024-04-03 10:30:55 -07:00
Meghana Gupta
af228139a5 Use swift::findUnreferenceableStorage to ensure we can construct an struct value 2024-03-20 20:07:28 -07:00
Meghana Gupta
18c24a18b3 Fix silcombine of inject_enum_addr when we have an empty struct with unreferenceable storage 2024-03-15 13:12:36 -07:00
Joe Groff
b0655407c6 Merge pull request #72281 from jckarter/moveonly-optional-chain-3
SIL: Optimizer fixes for noncopyable optional chains.
2024-03-13 08:11:39 -07:00
Joe Groff
8a65c9be94 SIL: Optimizer fixes for noncopyable optional chains.
Don't attempt a SILCombine transform on `select_enum` that inserts a copy when an
enum is noncopyable. Adjust the cleanup handling for a consuming optional chain to
ensure a `destroy_value` still gets emitted on the `none` path; this shouldn't
actually matter since `none` is a trivial case, but the memory verifier isn't
that fancy during OSSA, and we can optimize it later. Fixes rdar://124426918.
2024-03-12 18:11:21 -07:00
Meghana Gupta
50f0fd232c Enable SILCombine of inject_enum_addr for empty types 2024-03-12 13:01:44 -07: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
Kavon Farvardin
107475b224 SILType: Avoid asking ASTType if it is Noncopyable
It's better to ask SILType if it is MoveOnly than go to the AST type and
 ask if it is noncopyable, because some types in SIL do not have a
 well-defined notion of conformance in the AST.
2024-01-23 22:42:38 -08:00
Erik Eckstein
e718bfe8ed Optimizer: reimplement simplifications for copy_value and destroy_value in swift
So that they can run in the OnoneSimplification pass
2023-10-27 10:47:07 +02:00
nate-chandler
397a25c377 Merge pull request #68608 from nate-chandler/rdar115468707
[CanonicalizeOSSALifetime] Extend lexical lifetimes to unreachables.
2023-09-21 20:25:46 -07:00
Nate Chandler
b902ad7711 [SILCombine] Keep lexical destroys post cond_fails
Such destroys are part of the lifetime of lexical values inserted to
prevent shortening lifetimes over deinit baarriers by
CanonicalizeOSSALifetime.  After OSSALifetimeCompletion is enabled, more
instructions will need to be preserved.
2023-09-21 14:02:43 -07:00
Kavon Farvardin
f1142d5da4 [nfc] rename or eliminate isPureMoveOnly APIs
I think from SIL's perspective, it should only worry about whether the
type is move-only. That includes MoveOnlyWrapped SILTypes and regular
types that cannot be copied.

Most of the code querying `SILType::isPureMoveOnly` is in SILGen, where
it's very likely that the original AST type is sitting around already.
In such cases, I think it's fine to ask the AST type if it is
noncopyable. The clarity of only asking the ASTType if it's noncopyable
is beneficial, I think.
2023-09-20 15:23:17 -07:00
Erik Eckstein
d91759daf8 SILCombine: remove the dead alloc_ref peephole optimization
It's not needed because dead alloc_refs are removed by the DeadObjectElimination pass
2023-09-19 15:10:30 +02:00
Meghana Gupta
349acca2c2 Disable a few mark_dependence sil combines in ossa
There are 2 reasons to disable this:

1. OSSA rauw creates copies for replacement, copies created in this case
cannot be canonicalized away due to the presence of pointer escape.
(See test test_mark_dependence_ossa_silcombine1 after -sil-combine -copy-propagation)

2. SILCombine of mark_dependence (enum can cause a potential miscompile.

ClosureLifetimeFixup specifically inserts enums to lifetime extend the closure
lifetime until the end of the function. Replacing mark_dependence's base operand
with a copy of the enum's operand can end up shortening the lifetime of the base.
(See test test_mark_dependence_ossa_silcombine2 after -sil-combine -dce -semantic-arc-opts)
2023-09-11 10:39:34 -07:00
Andrew Trick
a946922798 Fix SILCombine miscompile for discard on non-copyable types
Fixes rdar://113214179 (Raw layout types don't adhere to discard self)

// swift-frontend ./rawdeinit.swift -enable-experimental-feature RawLayout \
// -enable-builtin-module -emit-sil -o rawdeinit.sil

import Builtin

@_rawLayout(like: T)
public struct Cell<T>: ~Copyable {
  var ptr: UnsafeMutablePointer<T> {
    .init(Builtin.unprotectedAddressOfBorrow(self))
  }

  init(_ initialValue: T) {
    ptr.initialize(to: initialValue)
  }

  deinit {
    ptr.deinitialize(count: 1)
  }

  public consuming func get() -> T {
    let value = ptr.move()
    discard self // SILCombine inserts a destroy of self here
    return value
  }
}
2023-08-23 17:07:07 -07:00
Erik Eckstein
b08710d911 SIL: add a bare attribute to alloc_ref
The `bare` attribute indicates that the object header is not used throughout the lifetime of the object.
This means, no reference counting operations are performed on the object and its metadata is not used.
The header of bare objects doesn't need to be initialized.
2023-06-29 06:57:05 +02:00
Evan Wilde
f3ff561c6f [NFC] add llvm namespace to Optional and None
This is phase-1 of switching from llvm::Optional to std::optional in the
next rebranch. llvm::Optional was removed from upstream LLVM, so we need
to migrate off rather soon. On Darwin, std::optional, and llvm::Optional
have the same layout, so we don't need to be as concerned about ABI
beyond the name mangling. `llvm::Optional` is only returned from one
function in
```
getStandardTypeSubst(StringRef TypeName,
                     bool allowConcurrencyManglings);
```
It's the return value, so it should not impact the mangling of the
function, and the layout is the same as `std::optional`, so it should be
mostly okay. This function doesn't appear to have users, and the ABI was
already broken 2 years ago for concurrency and no one seemed to notice
so this should be "okay".

I'm doing the migration incrementally so that folks working on main can
cherry-pick back to the release/5.9 branch. Once 5.9 is done and locked
away, then we can go through and finish the replacement. Since `None`
and `Optional` show up in contexts where they are not `llvm::None` and
`llvm::Optional`, I'm preparing the work now by going through and
removing the namespace unwrapping and making the `llvm` namespace
explicit. This should make it fairly mechanical to go through and
replace llvm::Optional with std::optional, and llvm::None with
std::nullopt. It's also a change that can be brought onto the
release/5.9 with minimal impact. This should be an NFC change.
2023-06-27 09:03:52 -07:00
Erik Eckstein
26832c8407 SILCombine: correctly set the [stack] flag when replacing alloc_ref_dynamic with alloc_ref
Fixes a verifier crash.

https://github.com/apple/swift/issues/66312
2023-06-21 11:52:39 +02:00
Erik Eckstein
50c23a1640 Optimizer: implement the SILCombine peephole optimizations for retain_value and release_value in Swift 2023-06-07 14:18:38 +02:00
Andrew Trick
46dbf8e555 Cleanup SILCombine hasValueDeinit checks 2023-06-06 09:17:53 -07:00
Andrew Trick
9345ba45d6 [move-only] Fix SILCombine to preserve value deinit
Fix a special case in visitReleaseValueInst for enum-with-deinit.
2023-06-06 09:17:53 -07:00
Andrew Trick
280761f0d1 [move-only] Fix SILOptimizer code motion to preserve value deinits
Multiple code motion and ARC related passes were removing struct/enum
deinits.

Passes fixed include:
- SILCombine
- EarlyCodeMotion
- ReleaseHoisting
- *many* passes that rely on ARC analysis (RCIndentity)
2023-06-06 09:17:53 -07:00
Andrew Trick
dc974e8c00 Fix SILCombine to preserve a release of a move-only type
The release needs to be preserved in case a user-defined deinit is
present in the released type. Checking for move-only is slightly
conservative.

Fixes rdar://109846094 ([move-only] SILCombine eliminates struct deinitialization)
2023-05-25 17:30:43 -07:00
Erik Eckstein
f3851f7503 Optimizer: implement load simplification in Swift 2023-05-22 15:34:26 +02:00
Erik Eckstein
2b117fd3ee Swift Optimizer: add APIs to copy from or to a global static initializer
* `Context.copyStaticInitializer(fromInitValue:, to:)`
* `FunctionPassContext.createStaticInitializer(for:,initValue:)`
2023-05-08 21:23:36 +02:00
Slava Pestov
08965406cb SILOptimizer: Add various pack peepholes to SILCombiner 2023-04-12 18:29:06 -04:00
eeckstein
60378e148f Merge pull request #64233 from eeckstein/optimize-convert-function
SILCombine: handle `convert_escape_to_noescape` in the apply-of-convert-function optimization
2023-03-13 12:46:40 +01:00
Meghana Gupta
89161f9d0f Disable SILCombine::visitInjectEnumAddrInst for empty tuple types. 2023-03-09 15:15:20 -08:00
Erik Eckstein
dd95922006 SILCombine: fix a problem in visitDifferentiableFunctionExtractInst
fixes a crash when trying to optimize ABI incompatible function types
2023-03-09 14:38:14 +01:00
Anton Korobeynikov
a5e83817b2 Reapply "Implement several peephole optimizations to unblock further optimizations of autodiff code" with correctness fix (#62012)
* Implement several peephole optimizations to unblock further optimizations of autodiff code

1. Simplify differentiable_function_extract of differentiable_function.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = differentiable_function_extract [original] %x
After:
%y = %orig

2. Push conversion instructions inside of differentiable_function.
This unblocks inlining and specialization.
Before:
%x = differentiable_function(%orig, %jvp, %vjp)
%y = convert_escape_to_noescape %x
After:
%orig' = convert_escape_to_noescape %orig
%jvp' = convert_escape_to_noescape %jvp
%vjp' = convert_escape_to_noescape %vjp
%y = differentiable_function(%orig', %jvp', %vjp')

3. Another peephole is needed for reordering function conversion instructions to enable full inlining:
(convert_escape_to_noescape (convert_function (thin_to_thick_function x)))
=>
(convert_escape_to_noescape (thin_to_thick_function (convert_function x)))

Co-authored-by: Dan Zheng <danielzheng@google.com>
2022-11-16 23:21:27 -08:00