Commit Graph

361 Commits

Author SHA1 Message Date
Andrew Trick
195ca7ceae Improve LifetimeDependence.Scope for mutable trivial variables.
Recognize dependence on the address of a trivial 'var' as an "access" dependence
instead of an "unknown" dependence. This allows the mark_dependence to be
resolved as "[nonescaping]".
2025-01-07 22:21:15 -08:00
Andrew Trick
49136f237f Fix LifetimeDependenceScopeFixup to avoid rewriting mark_dependence.
This pass rewrites mark_depenendence to ignore "useless" borrow scopes. It was
also accidentally rewriting a dependence on a loaded value, which may redirect the
dependence to the access scope used to load that value. That access scope may be
narrower than the lifetime of the loaded value which could result in invalid
SIL. Do not rewrite this mark_dependence:

  %access = begin_access [read] [unknown] %base
  %load = load [trivial] %access
  end_access %access
  %adr = pointer_to_address
  %md = mark_dependence [unresolved] %adr on %load

Fixes rdar://142424000 (Swift compiler crashes with Assertion failed
(isa<UnreachableInst>(block->getTerminator())))
2025-01-07 15:36:52 -08:00
eeckstein
f096787871 Merge pull request #78384 from eeckstein/enable-code-motion-in-ossa
Optimizer: enable SILCodeMotion in OSSA, but only for trivial values.
2025-01-02 20:36:29 +01:00
Erik Eckstein
c58f04f165 PhiUpdater: fix a problem when incrementally updating borrowed-from instructions
When replacing all uses, ignore existing borrowed-from instructions.

Fixes a SIL verifier error.
2025-01-02 10:42:00 +01:00
Erik Eckstein
51c6a60d9d SIL Verifier: check that an instruction with a guaranteed result is either a BeginBorrowValue or a ForwardingInstruction 2024-12-21 08:28:21 +01:00
Erik Eckstein
c5b14c2b93 BorrowUtils: make unchecked_ownership_conversion to guaranteed ownership a BeginBorrowValue
BeginBorrowValue needs to handle all cases where a guaranteed value is produced.
Fixes a compiler crash.
2024-12-21 08:28:21 +01:00
Ben Barham
8111fe9343 Merge pull request #78262 from bnbarham/skip-non-wmo-diag
[Embedded] Do not produce `cannot_specialize_class` for live issues
2024-12-20 10:40:07 -08:00
Ben Barham
a2fda1d9f3 [Embedded] Do not produce cannot_specialize_class for live issues
SourceKit explicitly disables WMO, silence the diagnostic in this case
(but leave it enabled for explicit non-WMO builds otherwise).
2024-12-19 15:31:41 -08:00
Erik Eckstein
cd7c533d42 Optimizer: add SingleValueInstruction.replace(with:) and use it throughout the optimizer
It replaces all uses and then erases the instruction.
2024-12-19 20:53:45 +01:00
Erik Eckstein
ca7facde35 SIL: add some use-list APIs
* `users`: maps from a sequence of operands to a sequence of instructions
* `users(ofType:)` : as `users`, but filters users of a certain instruction type
* `Value.users`: = `Value.uses.users`
2024-12-19 20:53:45 +01:00
Andrew Trick
3897929e4a LifetimeDependence: handle dependence on trivial values. 2024-12-16 16:09:37 -08:00
Andrew Trick
dc4e4665f9 AddressUtils: handle address initialization inside _modify.
We can assume that memory is already initialized at the point of a 'yield'; a
yield use does not need to invalidate the single-initialization property for
temporary stack allocations.
2024-12-16 15:28:00 -08:00
Andrew Trick
eab9a3599f [NFC] Add LifetimeDependence.Scope.ignoreBorrowScope utility.
Skip borrow scopes that do not actually require lifetime dependence. This
will improve LifetimeDependenceScopeFixup.
2024-12-16 15:28:00 -08:00
Andrew Trick
7811a63233 Extend LifetimeDependence to allow borrowed value base. 2024-12-16 15:28:00 -08:00
Andrew Trick
f5e16b698e [NFC] LifeDependenceUtils: fix a merge redundancy. 2024-12-16 15:27:25 -08:00
Andrew Trick
c4a2200669 Fix lifetime_dependence_borrow_fail diagnostic.
Report the correct variable name.
2024-12-14 22:46:55 -08:00
Andrew Trick
05c501a8f6 Add AddressOwnershipLiveRangeTest 2024-12-14 22:46:55 -08:00
Andrew Trick
46db7ce24e LifetimeDependenceDefUseWalker: recognize ConvertPointerToPointer
Look through a call to the ConvertPointerToPointerArgument compiler intrinsic
just like it is a copy of the pointer. At the source level, that's all it is:

Treat this like a direct use of the argument 'p' rather than the result of the
invisible pointer conversion call:

func foo(_: UnsafeRawPointer)

func bar(p: UnsafePointer<T>) {
  foo(p)
}
2024-12-14 22:46:54 -08:00
Andrew Trick
f5fc17804e [NFC] SwiftCompilerSources: Add VariableScopeInstruction utility
To diagnose dependence on trivial variables.
2024-12-14 22:46:54 -08:00
Erik Eckstein
6990a195a3 Optimizer: rename GuaranteedPhiUpdater -> PhiUpdater
Because it now has the replacePhisWithIncomingValues utility, which works for all kind of phis.
2024-12-12 09:09:11 +01:00
Erik Eckstein
09a5a4487a Optimizer: add a utility to replaces phis with the unique incoming values if all incoming values are the same
This is needed after running the SSAUpdater for an existing OSSA value, because the updater can
insert unnecessary phis in the middle of the original liverange which breaks up the original
liverange into smaller ones:

```
   %1 = def_of_owned_value
   %2 = begin_borrow %1
   ...
   br bb2(%1)
 bb2(%3 : @owned $T): // inserted by SSAUpdater
   ...
   end_borrow %2      // use after end-of-lifetime!
   destroy_value %3
```

It's not needed to run this utility if SSAUpdater is used to create a _new_ OSSA liverange.
2024-12-12 08:57:57 +01:00
Erik Eckstein
5be781a9a0 Optimizer: add a new destroy-hoisting optimization
It hoists `destroy_value` instructions  without shrinking an object's lifetime.
This is done if it can be proved that another copy of a value (either in an SSA value or in memory) keeps the referenced object(s) alive until the original position of the `destroy_value`.
```
  %1 = copy_value %0
  ...
  last_use_of %0
  // other instructions
  destroy_value %0       // %1 is still alive here
```
->
```
  %1 = copy_value %0
  ...
  last_use_of %0
  destroy_value %0
  // other instructions
```

The benefit of this optimization is that it can enable copy-propagation by moving destroys above deinit barries and access scopes.
2024-12-10 16:28:11 +01:00
Erik Eckstein
4b462004e7 Optimizer: Fix InstructionRange's begin instruction
Just don't store the begin instruction.
This led to problem if the "begin" was not actually an instruction but a block argument.
Using the first instruction of that block is not correct in case the range ends immediately at the first instruction, e.g.

```
  bb0(%0 : @owned $C):
    destroy_value %0
```
2024-12-10 16:28:10 +01:00
eeckstein
b63365c9d3 Merge pull request #77858 from eeckstein/fix-deinit-devirtualizer
Devirtualization: make sure to de-serialize the body of shared deinit functions
2024-12-02 07:11:56 +01:00
Erik Eckstein
9279a2c0d6 Devirtualization: make sure to de-serialize the body of shared deinit functions.
Sometimes it can happen that a deinit function, which is imported from another module, has shared linkage.
In this case it is important to de-serialize the function body. Otherwise it would be illegal SIL.

Unfortunately I don't have a test case for this.
2024-11-27 18:05:36 +01:00
Erik Eckstein
53bb72c3e4 EscapeUtils: don't follow the result of a partial_apply for not captured address operands.
A partial_apply copies the values from indirect-in arguments, but does not capture the address.
2024-11-27 12:16:43 +01:00
Erik Eckstein
2519c343e6 Optimizer: fix simplification of unchecked_enum_data
When replacing an `enum` - `unchecked_enum_data` pair and the enum's operand is another non-trivial enum which is constructed with a trivial payload, and this happens in different basic blocks, we need to insert a compensating `destroy_value`.

Fixes a verifier crash
rdar://139787167
2024-11-14 07:21:10 +01:00
eeckstein
53f772cd43 Merge pull request #77582 from eeckstein/fix-comment
GuaranteedPhiUpdater: fix a comment
2024-11-13 14:44:47 +01:00
Andrew Trick
eb8d9f4960 Merge pull request #77575 from atrick/preserve_extend_lifetime
Preserve extend_lifetime during dead instruction code elimination.
2024-11-13 05:22:30 -08:00
Erik Eckstein
2182f44d54 GuaranteedPhiUpdater: fix a comment 2024-11-13 10:49:58 +01:00
Andrew Trick
c5ab1287f3 Preserve extend_lifetime during dead instruction code elimination. 2024-11-12 23:53:56 -08:00
Erik Eckstein
51e3e5ed80 Optimizer: rename BorrowArgumentsUpdater -> GuaranteedPhiUpdater
NFC
2024-11-12 09:26:59 +01:00
Erik Eckstein
68d0f6120a SIL Verifier: verify the re-borrow flags for guaranteed phi arguments 2024-11-12 09:26:59 +01:00
Erik Eckstein
8462459f07 Optimizer: re-compute the re-borrow flags of phi arguments in updateAllBorrowArguments and updateBorrowArguments 2024-11-12 09:26:59 +01:00
Erik Eckstein
e934bb1321 BorrowUtils: support guaranteed forwarding phis without forwarding instructions when computing enclosing values
For example:

```
  %1 = begin_borrow %0
  %2 = br bb1(%1, %1)
bb1(%3 : @reborrow @guaranteed, %4: @guaranteed):
  // %4 is a guaranteed forwarding phi without any forwarding instructions between the begin_borrow and the incoming value.
```

Also improve the comments
2024-11-12 09:26:58 +01:00
Erik Eckstein
6b8c6a3c3b SIL: rename updateBorrowedFrom to updateBorrowArguments
NFC
2024-11-12 09:26:58 +01:00
Erik Eckstein
1c5e185e89 Simplification: don't delete trivially dead borrowed-from instruction
A dead borrowed-from can only be removed if the argument (= operand) is also removed.

Fixes a compiler crash.
2024-11-11 14:06:16 +01:00
Arnold Schwaighofer
9093e1b0d2 Add comments to SwiftCompilerSources for shouldExpand() 2024-11-05 09:05:24 -08:00
Erik Eckstein
f0633d5638 AccessUtils: support computing "constant" access paths
Add `Value.constantAccessPath`. It is like `accessPath`, but ensures that the projectionPath only contains "constant" elements.
This means: if the access contains an `index_addr` projection with a non-constant index, the `projectionPath` does _not_ contain the `index_addr`.
Instead, the `base` is an `AccessBase.index` which refers to the `index_addr`.
2024-11-04 19:26:44 +01:00
Arnold Schwaighofer
787c996394 LargeTypesReg2Mem: Add a new heuristic that trys harder to keep large
values on the stack

This heuristic can be enabled by passing -Xfrontend
-enable-aggressive-reg2mem.

rdar://123916109
2024-10-31 13:22:06 -07:00
Erik Eckstein
b8a91c95aa embedded: make sure to de-serialize vtable/wtable methods before trying to specialize them
Fixes a compiler crash.
rdar://138341211
2024-10-24 10:29:50 +02:00
Erik Eckstein
44a9919308 Revert "Revert "SIL Verifier: implement load-borrow-immutability checkin in the swift verifier""
This reverts commit d7810450fe.
2024-10-22 08:40:18 +02:00
Erik Eckstein
d7810450fe Revert "SIL Verifier: implement load-borrow-immutability checkin in the swift verifier"
This reverts commit b01e703ff3.
2024-10-18 11:13:33 +02:00
Erik Eckstein
709dfc2d21 MandatoryPerformanceOptimization: don't let not-inlinable functions to be inlined
Also refactor canInline.

Fixes a compiler crash.
rdar://137544788
2024-10-15 12:19:50 +02:00
Erik Eckstein
5c8fe55449 embedded: fix several issues with vtable specialization
* fix a false error if a derived class has different generic parameters than its base class
* fix a similar problem if a non-generic class derives from a generic class
* fix a compiler crash for calling a class method on a class metatype

rdar://137692055
2024-10-14 14:43:11 +02:00
Erik Eckstein
f249fa7289 OwnershipLiveness: add a flag visitInnerUses to enable constructing incomplete liveranges
If `visitInnerUses ` is true, it's not assumed that inner scopes are linear.
It forces to visit all interior uses if inner scopes.
2024-10-11 09:41:37 +02:00
Erik Eckstein
b01e703ff3 SIL Verifier: implement load-borrow-immutability checkin in the swift verifier 2024-10-11 09:41:37 +02:00
Erik Eckstein
f7aaf7e5a5 MandatoryPerformanceOptimizations: handle all kind of witness-table entries when specializing witness-tables
Support associated-type and associated-conformance entries.
This enable existentials with associated types.
2024-10-07 09:00:32 +02:00
Erik Eckstein
a67d9c5bed embedded: Support class existentials with inherited protocols
For example:
```
protocol Base: AnyObject {}
protocol Derived: Base {}

class C: Derived {}

let e: Derived = C()
```
2024-10-07 08:49:56 +02:00
Erik Eckstein
25728853b8 GenericSpecialization: change how new specialized witness tables are added to MandatoryPerformanceOptimization's worklist
Do it by passing a closure instead of returning the new witness table.
This allows to add more than one new witness table to the worklist
2024-10-07 08:49:56 +02:00