Commit Graph

387 Commits

Author SHA1 Message Date
Erik Eckstein
7b8f433fed EscapeUtils: add EscapeVisitorWithResult.cleanupOnAbort()
Useful to destroy the result if it's not returned
2023-11-08 17:31:10 +01:00
Meghana Gupta
192bb2eed2 Merge pull request #69450 from meg-gupta/uninarrayfix
Add a mark_dependence while emitting SIL for uninitialized array allocation
2023-10-31 22:07:14 -07:00
Meghana Gupta
25d1e53241 Handle mark_dependence in ObjectOutliner 2023-10-31 11:07:40 -07:00
Meghana Gupta
f707d8a7de ObjectOutliner: isValidUseOfObject(Instruction) -> isValidUseOfObject(Operand) 2023-10-31 11:07:40 -07:00
Hamish Knight
ce23252a3f [Basic] Improve some bridging APIs
Improve APIs for BridgedStringRef,
BridgedOwnedString, and BridgedSourceLoc.
2023-10-30 23:49:53 +00:00
Hamish Knight
3e3fb584c3 [SwiftCompilerSources] NFC: Workaround a compiler bug 2023-10-30 23:49:52 +00: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
Erik Eckstein
b6938475b9 Optimizer: add simplification for the convert_escape_to_noescape instruction
Including the required bridging stuff.

  %2 = thin_to_thick_function %1 to $() -> ()
  %3 = convert_escape_to_noescape %2 : $() -> () to $@noescape () -> ()
->
  %3 = thin_to_thick_function %1 to $@noescape () -> ()
2023-10-27 10:47:07 +02:00
Erik Eckstein
8b9f2a73d0 SimplifyApply: optimize thick calls where the callee is a thin_to_thick_function
```
   %2 = thin_to_thick_function %1
   %3 = apply %2(...) : @callee_guaranteed
 ->
   %2 = thin_to_thick_function %1
   %3 = apply %1(...): @convention(thin)
```
2023-10-27 10:47:07 +02:00
Erik Eckstein
bd022b4051 InitializeStaticGlobals: fix a SIL verifier crash
When merging stores in a global initializer, it's possible that the merged store is inserted at the wrong location, causing a SIL verifier error.
This is hard to reproduce, but can happen.
The merged store must be inserted _after_ all other stores. Instead it's inserted after the store of the last property. Now, if properties are _not_ initialized in the order they are declared, this problem can show up.

rdar://117189962
2023-10-19 20:29:30 +02:00
Erik Eckstein
54d254f100 Optimizer: better handling of the complexity budget in redundant-load-elimination and dead-store-elimination.
Instead of having a budget for each optimized load or store, provide a budget for the whole function.
Fixes a build time problem.

rdar://116877696
2023-10-16 14:44:34 +02:00
Andrew Trick
e2333cdcfc SwiftCompilerSources: diagnostics bridging. 2023-10-12 14:33:37 -07:00
Andrew Trick
13142e3957 ForwardingUtils.swift improvements
For diagnostics in the presence of dead values.
2023-10-12 09:39:34 -07:00
Andrew Trick
bd157b3be4 ForwardingUtils.swift unit tests 2023-10-10 13:42:48 -07:00
Andrew Trick
75ea3821ed Fix initial ForwardingInstruction implementation and handle phis 2023-10-10 13:40:23 -07:00
Andrew Trick
12ea57919a Test.swift cleanup for usability 2023-10-10 13:39:59 -07:00
Nate Chandler
9ca6b9ac1f [Test] Print to stdout.
In the C++ sources it is slightly more convenient to dump to stderr than
to print to stdout, but it is rather more unsightly to print to stderr
from the Swift sources.  Switch to stdout.  Also allows the dump
functions to be marked debug only.
2023-10-10 08:19:44 -07:00
Nate Chandler
99576dc261 [SwiftCompilerSources] NFC: Renamed two helper fns 2023-10-09 07:40:18 -07:00
Erik Eckstein
2dbd6cc56b SwiftCompilerSources: rework bridging
Introduce two modes of bridging:
* inline mode: this is basically how it worked so far. Using full C++ interop which allows bridging functions to be inlined.
* pure mode: bridging functions are not inlined but compiled in a cpp file. This allows to reduce the C++ interop requirements to a minimum. No std/llvm/swift headers are imported.

This change requires a major refactoring of bridging sources. The implementation of bridging functions go to two separate files: SILBridgingImpl.h and OptimizerBridgingImpl.h.
Depending on the mode, those files are either included in the corresponding header files (inline mode), or included in the c++ file (pure mode).

The mode can be selected with the BRIDGING_MODE cmake variable. By default it is set to the inline mode (= existing behavior). The pure mode is only selected in certain configurations to work around C++ interop issues:
* In debug builds, to workaround a problem with LLDB's `po` command (rdar://115770255).
* On windows to workaround a build problem.
2023-10-09 09:52:52 +02:00
Nate Chandler
e65e6678e5 [Gardening] Updated comment.
Mention that running FunctionTests written in swift requires
swift-in-compiler and fixed the name of the register function.
2023-10-08 09:42:24 -07:00
Nate Chandler
ae1f950315 [SwiftCompilerSources] Moved Test into Optimizer.
And changed the type of the context argument to FunctionPassContext.
2023-10-07 21:23:13 -07:00
Kuba Mracek
5d8c55eacb [embedded] Initial Swift Concurrency for embedded Swift 2023-10-06 20:04:03 -07:00
Andrew Trick
eccf94d57b Merge pull request #68885 from atrick/bridge-forwarding
SwiftCompilerSources: added OwnershipUtils for ForwardingInstruction.
2023-10-05 10:22:30 -07:00
Andrew Trick
e27b308781 SwiftCompilerSources: added OwnershipUtils for ForwardingInstruction.
This could be combined with ValueUseDefWalker if the latter is
refactored to classsify instructions by projections and aggegation
(which always forward) vs. other arbitrary hard-coded instruction
types. It would also need to limit the walk to real operands (which
are always forwarded). Then this walker can call into the default walk
for projections and track the projection path. The current
implementation is however simpler and more efficient.
2023-10-03 23:54:57 -07:00
Ikko Eltociear Ashimine
680c6f3ae2 Fix typo in ComputeSideEffects.swift
mutliple -> multiple
2023-10-03 17:39:39 +09:00
Kuba (Brecka) Mracek
f4a0397407 Merge pull request #68883 from kubamracek/embedded-release-devirt
[embedded] Re-enable ReleaseDevirtualizer and teach it to look for specialized destructors
2023-10-02 13:15:03 -07:00
Kuba Mracek
28a3d583c8 [embedded] Re-enable ReleaseDevirtualizer and teach it to look for specialized destructors 2023-09-29 16:28:59 -07:00
Andrew Trick
2a604e2ab0 Merge pull request #68814 from atrick/blockarg
SwiftCompilerSources: Replace BlockArgument with Phi and TermResult.
2023-09-29 15:51:00 -07:00
Nate Chandler
dab8c146a6 [SwiftCompilerSources] Bridged in-IR testing.
Added the bridging types involved and the basic functionality.
2023-09-28 11:33:50 -07:00
Andrew Trick
a5d8aafb23 SwiftCompilerSources: Replace BlockArgument with Phi and TermResult.
All SILArgument types are "block arguments". There are three kinds:
1. Function arguments
2. Phis
3. Terminator results

In every situation where the source of the block argument matters, we
need to distinguish between these three. Accidentally failing to
handle one of the cases is an perpetual source of compiler
bugs. Attempting to handle both phis and terminator results uniformly
is *always* a bug, especially once OSSA has phi flags. Even when all
cases are handled correctly, the code that deals with data flow across
blocks is incomprehensible without giving each case a type. This
continues to be a massive waste of time literally every time I review
code that involves cross-block control flow.

Unfortunately, we don't have these C++ types yet (nothing big is
blocking that, it just wasn't done). That's manageable because we can
use wrapper types on the Swift side for now. Wrapper types don't
create any more complexity than protocols, but they do sacrifice some
usability in switch cases.

There is no reason for a BlockArgument type. First, a function
argument is a block argument just as much as any other. BlockArgument
provides no useful information beyond Argument. And it is nearly
always a mistake to care about whether a value is a function argument
and not care whether it is a phi or terminator result.
2023-09-27 18:47:46 -07:00
Kavon Farvardin
b688a1f4a1 [SILOpt] experimental async demotion pass
For chains of async functions where suspensions can be statically
proven to never be required, this pass removes all suspensions and
turns the functions into synchronous functions.

For example, this function does not actually require any suspensions,
once the correct executor is acquired upon initial entry:

```
func fib(_ n: Int) async -> Int {
  if n <= 1 { return n }
  return await fib(n-1) + fib(n-2)
}
```

So we can turn the above into this for better performance:

```
func fib() async -> Int {
  return fib_sync()
}

func fib_sync(_ n: Int) -> Int {
  if n <= 1 { return n }
  return fib(n-1) + fib(n-2)
}
```

while rewriting callers of `fib` to use the `sync` entry-point
when we can prove that it will be invoked on a compatible executor.

This pass is currently experimental and under development. Thus, it
is disabled by default and you must use
`-enable-experimental-async-demotion` to try it.
2023-09-21 12:21:02 -07:00
Kavon Farvardin
e6a8ec9d01 [SwiftCompilerSources] Handle .anyValueFields use-def walks through enums
The walker was not treating an EnumInst with zero payload, such as `Optional.none` as a root.
It seems the best way to fix that is to implement the handling of .anyValueFields for enums, as
they're documented in a comment to mean "follow anything", unlike .enumCase which expects
to find a specific case (though perhaps if it matches and there's no payload, it should still be a root?)
2023-09-21 12:20:24 -07:00
eeckstein
57c242bbd4 Merge pull request #68652 from eeckstein/fix-dead-store-elimination
AccessUtils: fix handling of indexing in overlap checks
2023-09-21 19:19:43 +02:00
Kuba (Brecka) Mracek
140caf63be Merge pull request #68659 from kubamracek/embedded-ispod
[embedded] Handle thick->thin metatype conversion on IsPOD builtin too
2023-09-21 06:50:29 -07:00
Erik Eckstein
14c2e180b1 AccessUtils: fix handling of indexing in overlap checks
Indexing is not a projection where the base overlaps the "projected" address.
Fixes a miscompile.

rdar://115747816
2023-09-21 08:39:33 +02:00
Kuba Mracek
6a0bfa267d [embedded] Handle thick->thin metatype conversion on IsPOD builtin too 2023-09-20 15:49:13 -07:00
Kuba Mracek
a3d15b12bd [embedded] No need to pass isEmbeddedSwift as an explicit argument, use context directly 2023-09-20 11:14:36 -07:00
Kuba Mracek
f67c660eb6 [embedded] Move isEmbedded check to MandatoryPerformanceOptimizations from VTableSpecializer 2023-09-20 11:03:48 -07:00
Kuba Mracek
efd8f08ea0 [embedded] Add notifyCallsChanged() to specializeClassMethodInst 2023-09-20 09:49:57 -07:00
Kuba Mracek
f0c8a59486 [embedded] Drop notifyInstructionsChanged() from specializeVTable() as we're not changing any instructions 2023-09-20 09:48:31 -07:00
Kuba Mracek
89df0426e0 [embedded] Specialize vtables when processing instructions, not bulk 2023-09-20 09:41:09 -07:00
Kuba Mracek
03f927eca1 [embedded] Perform VTable specialization iteratively as part of MandatoryPerformanceOptimizations 2023-09-20 09:41:09 -07:00
Erik Eckstein
614a635d5d StripObjectHeaders: make sure that a dealloc_ref comes from the original root allocation.
Fixes a miscompile
2023-09-19 15:10:30 +02:00
Erik Eckstein
5bc036661c SIL optimizer: add the LetPropertyLowering pass
It lowers let property accesses of classes.
Lowering consists of two tasks:

* In class initializers, insert `end_init_let_ref` instructions at places where all let-fields are initialized.
  This strictly separates the life-range of the class into a region where let fields are still written during
  initialization and a region where let fields are truly immutable.

* Add the `[immutable]` flag to all `ref_element_addr` instructions (for let-fields) which are in the "immutable"
  region. This includes the region after an inserted `end_init_let_ref` in an class initializer, but also all
  let-field accesses in other functions than the initializer and the destructor.

This pass should run after DefiniteInitialization but before RawSILInstLowering (because it relies on `mark_uninitialized` still present in the class initializer).
Note that it's not mandatory to run this pass. If it doesn't run, SIL is still correct.

Simplified example (after lowering):

  bb0(%0 : @owned C):                           // = self of the class initializer
    %1 = mark_uninitialized %0
    %2 = ref_element_addr %1, #C.l              // a let-field
    store %init_value to %2
    %3 = end_init_let_ref %1                    // inserted by lowering
    %4 = ref_element_addr [immutable] %3, #C.l  // set to immutable by lowering
    %5 = load %4
2023-09-19 15:10:30 +02:00
Erik Eckstein
ad594f2713 Swift SIL: add some APIs
* `AssignInst`
* `Function.isDestructor`
* `MarkUninitializedInst.kind`
* `Type.isMoveOnly`
* `RefElementAddrInst.isImmutable` and `RefElementAddrInst.set(isImmutable:)`
* `BeginBorrowInst.endBorrows`
* `Context.hadError` and `Context.silStage`
2023-09-19 15:10:30 +02:00
Erik Eckstein
0312804a4e WalkUtils: fix indentation 2023-09-19 15:10:30 +02:00
Erik Eckstein
88a7ee0cd9 WalkUtils: avoid dynamic protocol lookup when handling enum instructions
It's simpler and faster
2023-09-19 15:10:30 +02:00
Erik Eckstein
f0b811c45f SIL: add the end_init_let_ref instruction
This instructions marks the point where all let-fields of a class are initialized.
This is important to ensure the correctness of ``ref_element_addr [immutable]`` for let-fields,
because in the initializer of a class, its let-fields are not immutable, yet.
2023-09-19 15:10:30 +02:00
Erik Eckstein
e5eb15dcbe Swift SIL: replace the set_deallocating instruction with begin_dealloc_ref
Codegen is the same, but `begin_dealloc_ref` consumes the operand and produces a new SSA value.
This cleanly splits the liferange to the region before and within the destructor of a class.
2023-09-19 15:10:30 +02:00
Erik Eckstein
d457368014 SIL Optimizer: move some projection path utilities from RedundantLoadElimination and AccessUtils into OptUtils
NFC
2023-09-19 15:10:30 +02:00