Commit Graph

462 Commits

Author SHA1 Message Date
Erik Eckstein
9514b3450c SwiftCompilerSources: add some context APIs
* `Context.moduleIsSerialized`
* `Context.getBuiltinIntegerType`
* `Instruction.move(before:)`
2023-12-09 18:48:50 +01:00
Erik Eckstein
0bd6034270 SwiftCompilerSources: add some Builder APIs
* `createAddressToPointer`
* `createLoadBorrow`
2023-12-09 18:48:50 +01:00
Erik Eckstein
bfeb2128cb remove default value for the keepUnique argument of Builder.createEndCOWMutation
This avoids bugs because the default value false is sometimes not the right choice.
NFC
2023-12-06 15:40:08 +01:00
eeckstein
10ff55b2b3 Merge pull request #70155 from eeckstein/refactoring
MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis
2023-12-04 08:53:59 +01:00
Erik Eckstein
0897d8a720 MemoryLifetimeVerifier: use CalleeCache instead of AliasAnalysis
To verify if a function may read from an indirect argument, don't use AliasAnalysis.
Instead use the CalleeCache to get the list of callees of an apply instruction.
Then use a simple call-back into the swift Function to check if a callee has any relevant memory effect set.

This avoids a dependency from SIL to the Optimizer.
It fixes a linker error when building some unit tests in debug.
2023-12-01 19:20:18 +01:00
Erik Eckstein
80ace58104 SwiftCompilerSources: split SIL/Utils.swift into multiple files 2023-12-01 19:20:13 +01:00
Erik Eckstein
ec4c815506 SwiftCompilerSources: move WalkUtils and AccessUtils from the Optimizer module to the SIL module
Those utilities are conceptually part of the SIL definition.
2023-12-01 19:20:13 +01:00
zoecarver
69498e2f2e [opt] Add three new perf annotations: @_noRuntime, @_noExistential, and @_noObjCBridging. 2023-12-01 09:13:24 -07:00
Erik Eckstein
6aaf2005d3 SwiftCompilerSources: remove the BeginBorrowInst.endBorrows API
Using this API almost certainly indicates a bug, because it neglects re-borrows.
2023-11-27 16:20:47 +01:00
Erik Eckstein
e4d227a5fb SwiftCompilerSources: add StoreBorrowInst and BorrowIntroducingInstruction
`BorrowIntroducingInstruction` is a protocol to which all instructions conform which start a borrow-scope which must be ended by `EndBorrowInst` instructions: `begin_borrow`, `load_borrow` and `store_borrow`
2023-11-27 16:20:47 +01:00
eeckstein
67ebbee39e Merge pull request #69955 from eeckstein/deinit-devirtualizer
Optimizer: de-virtualize deinits of non-copyable types
2023-11-27 12:41:49 +01:00
Erik Eckstein
37c715c58c SwiftCompilerSources: add EndLifetimeInst 2023-11-27 09:21:33 +01:00
Erik Eckstein
6f6c0a5608 SwiftCompilerSources: add a few enum related APIs in Type and Builder
* `Type.getEnumCases`
* `Builder.createUncheckedTakeEnumDataAddr`
* `Builder.createSwitchEnumAddr`
2023-11-27 09:21:33 +01:00
Erik Eckstein
fc534e1c28 SwiftCompilerSources: better APIs for handling resilient nominal types
* add `NominalTypeDecl.isResilient`

* make the return type of `Type.getNominalFields` optional and return nil in case the nominal type is resilient.
This forces users of this API to think about what to do in case the nominal type is resilient.
2023-11-27 09:21:33 +01:00
Erik Eckstein
7752fbfdf9 SIL: add Type.selfOrAnyFieldHasValueDeinit
Implemented by adding a recursive property in TypeLowering
2023-11-27 09:21:32 +01:00
Erik Eckstein
9bbee4411b SwiftCompilerSources: consider indirect error results in Function.numArguments 2023-11-27 08:51:12 +01:00
Erik Eckstein
3bfabf6e8a SimplifyBeginBorrow: don't remove begin_borrow instructions which have the [point_escape] flag set. 2023-11-14 12:26:23 +01:00
Erik Eckstein
95c83e9899 Swift SIL: add BeginBorrowInst.isLexical 2023-11-13 20:18:07 +01:00
Erik Eckstein
0509a056fb SwiftCompilerSources: improve APIs for UseList
Make filter APIs for UseList chainable by adding them to Sequence where Element == Operand

For example, it allows to write:
```
let singleUse = value.uses.ignoreDebugUses.ignoreUsers(ofType: EndAccessInst.self).singleUse
```

Also, add `UseList.getSingleUser(notOfType:)`
2023-11-13 20:18:07 +01:00
Erik Eckstein
eb597cb6d8 Swift SIL: add a few ownership APIs to ForwardingInstruction
* `canForwardGuaranteedValues`
* `canForwardOwnedValues`
* `setForwardingOwnership`
* `Operand.canAccept(ownership:)`
2023-11-13 20:18:07 +01:00
Slava Pestov
05ccd9734c SIL: Introduce ThrowAddrInst 2023-10-31 16:58:54 -04:00
Hamish Knight
2ac0860716 [AST] Remove BridgedVarDecl + BridgedNominalTypeDecl duplicates
These are now stamped out by the ASTBridgingWrappers.
2023-10-30 23:49:56 +00:00
Hamish Knight
cae5d33368 [AST] Move BridgedNominalTypeDecl + BridgedVarDecl to ASTBridging
And renaming OptionalBridgedVarDecl to
BridgedNullableVarDecl for consistency with the
existing nullable AST node wrappers.
2023-10-30 23:49:55 +00:00
Hamish Knight
ce23252a3f [Basic] Improve some bridging APIs
Improve APIs for BridgedStringRef,
BridgedOwnedString, and BridgedSourceLoc.
2023-10-30 23:49:53 +00: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
Andrew Trick
ca7253a25c Add the @_unsafeNonEscapableResult function
This will allow NonEscapable types to be returned from a function.
2023-10-17 12:44:31 -07:00
Andrew Trick
045253e614 Add SILType::isEscapable 2023-10-17 12:44:31 -07:00
Andrew Trick
972a69f17e SwiftCompilerSources bridging review feedback 2023-10-12 14:33:37 -07:00
Andrew Trick
d7c1c50d9b SwiftCompilerSources: VarDecl bridging for SIL. 2023-10-12 14:33:37 -07:00
Andrew Trick
e2333cdcfc SwiftCompilerSources: diagnostics bridging. 2023-10-12 14:33:37 -07:00
Andrew Trick
ae5ad5557d Add Swift BeginApplyInst::yieldedValues 2023-10-12 14:33:37 -07:00
Andrew Trick
75ea3821ed Fix initial ForwardingInstruction implementation and handle phis 2023-10-10 13:40:23 -07:00
Andrew Trick
e04abee2e9 Merge pull request #69049 from apple/fix-select-enum
Fix ownership of select_enum instruction
2023-10-09 10:02:34 -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
Andrew Trick
69a884565a Fix ownership of select_enum instruction
This instruction was given forwarding ownership in the original OSSA
implementation. That will obviously lead to memory leaks. Remove
ownership from this instruction and verify that it is never used for
non-trivial types.
2023-10-08 01:34:48 -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
nate-chandler
18ce3ab465 Merge pull request #69007 from nate-chandler/swiftcompilersources/test-bridging-registration-cleanup
[SwiftCompilerSources] Simplified test registration.
2023-10-07 21:22:04 -07:00
Nate Chandler
ce4ba6ce2a [Test] Simplified registration.
At the cost of adding an unsafe bitcast implementation detail,
simplified the code involved to register a new FunctionTest when adding
one.

Also simplifies how Swift native `FunctionTest`s are registered with the
C++ registry.

Now, the to-be-executed thin closure for native Swift `FunctionTest`s is
stored under within the swift::test::FunctionTest instance corresponding
to it.  Because its type isn't representable in C++, `void *` is used
instead.  When the FunctionTest is invoked, a thunk is called which
takes the actual test function and bridged versions of the arguments.
That thunk unwraps the arguments, casts the stored function to the
appropriate type, and invokes it.

Thanks to Andrew Trick for the idea.
2023-10-05 18:02:35 -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
Nate Chandler
149412a62a [Test] NFC: Link out to SIL.rst.
Help readers find out what the specify_test instruction allows right
away as they peruse the documentation.
2023-10-04 11:10:46 -07:00
Andrew Trick
bce9817162 SwiftCompilerSources: add ForwardingInstruction 2023-10-03 23:54:57 -07:00
Nate Chandler
68ca2dc418 [Gardening] Fixed comment.
Refer to a source file in the relevant language.
2023-10-02 16:57:47 -07:00
Andrew Trick
139c573920 SwiftCompilerSources: add OperandOwnership enum. 2023-10-02 09:41:12 -07:00
Andrew Trick
1f7bfd8f12 SwiftCompilerSources: expose Swift Operand.endsLifetime 2023-10-02 09:41:12 -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
36805c8a41 [Test] Moved test-spec parsing test to Swift.
To prove out that the bridging works.
2023-09-28 15:15:29 -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
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