Commit Graph

169 Commits

Author SHA1 Message Date
Andrew Trick
4796e55256 SwiftCompilerSources CopyingInstruction
And UnmanagedRetain/Release.
2024-01-30 08:38:57 -08:00
Andrew Trick
9950784adb SwiftCompilerSources API for SourceDestAddrInstructions. 2024-01-30 08:38:57 -08:00
Arnold Schwaighofer
edd7eaa0d7 OSLog: os_log strings should be in the __oslogstring section on machO
rdar://121384422
2024-01-26 08:30:56 -08:00
Andrew Trick
d9a8a0eb60 Added SourceDestAddrInstruction 2024-01-22 23:42:54 -08:00
Andrew Trick
e7a3ceb0d5 SwiftCompilerSources: add VarDecl.sourceLoc 2024-01-22 08:29:07 -08:00
Andrew Trick
a80686b485 [SIL] bridge MoveValue and BeginBorrow .isFromVarDecl 2024-01-22 08:29:07 -08:00
Andrew Trick
d8b901052a Add Swift SIL instructions needed by AddressUseVisitor 2024-01-17 08:37:14 -08:00
Erik Eckstein
bc99986cf9 SIL: add a dependency token operand to global_addr
Optionally, the dependency to the initialization of the global can be specified with a dependency token `depends_on <token>`.
This is usually a `builtin "once"` which calls the initializer for the global variable.
2024-01-10 09:33:58 +01:00
nate-chandler
7365f9f36b Merge pull request #70774 from nate-chandler/nfc/20240108/1/deinit-barrier-component-predicates
[NFC] SIL: Clarified deinit barrier APIs.
2024-01-08 23:11:38 -08:00
Nate Chandler
77fd37be99 [NFC] SIL: Renamed maySynchronize.
Dropped the NotConsideringSideEffects suffix.
2024-01-08 13:35:20 -08:00
Andrew Trick
264cbaea42 Add mark_dependence [nonescaping] flag.
The dependent 'value' may be marked 'nonescaping', which guarantees that the
lifetime dependence is statically enforceable. In this case, the compiler
must be able to follow all values forwarded from the dependent 'value', and
recognize all final (non-forwarded, non-escaping) use points. This implies
that `findPointerEscape` is false. A diagnostic pass checks that the
incoming SIL to verify that these use points are all initially within the
'base' lifetime. Regular 'mark_dependence' semantics ensure that
optimizations cannot violate the lifetime dependence after diagnostics.
2024-01-04 14:47:35 -08:00
Andrew Trick
2128c21106 Migrate SwiftCompilerSources to FunctionConvention.
Layers:
- FunctionConvention: AST FunctionType: results, parameters
- ArgumentConventions: SIL function arguments
- ApplyOperandConventions: applied operands

The meaning of an integer index is determined by the collection
type. All the mapping between the various indices (results,
parameters, SIL argument, applied arguments) is restricted to the
collection type that owns that mapping. Remove the concept of a
"caller argument index".
2024-01-03 12:24:50 -08:00
Andrew Trick
7dc3d72483 Merge pull request #70485 from atrick/bridge-util
[NFC] Bridging and utilities for SwiftCompilerSources required by lifetime dependence utilities.
2023-12-19 13:07:45 -08:00
Andrew Trick
2a9aef9eb8 [SIL] add BeginApplyInst.token 2023-12-19 02:05:16 -08:00
Andrew Trick
b637f06acd [SIL] Bridge findPointerEscape() and fix OnoneSimplifyable
Do not bridge the hasPointerEscape flag until it is implemented.
2023-12-18 09:16:55 -08:00
Erik Eckstein
84e06322b9 SwiftCompilerSources: bridge_object_to_word is not a ForwardingInstruction
Fix this mismatch between C++ SIL and swift SIL.
Fixes a crash in begin_borrow simplification
Also add an assert to catch such cases.

rdar://119763595
2023-12-18 11:08:22 +01:00
Erik Eckstein
e652f2c92e SIL: add the alloc_vector and vector instructions
* `alloc_vector`: allocates an uninitialized vector of elements on the stack or in a statically initialized global
* `vector`: creates an initialized vector in a statically initialized global
2023-12-09 18:49:55 +01: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
Erik Eckstein
37c715c58c SwiftCompilerSources: add EndLifetimeInst 2023-11-27 09:21:33 +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
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
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
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
ae5ad5557d Add Swift BeginApplyInst::yieldedValues 2023-10-12 14:33:37 -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
bce9817162 SwiftCompilerSources: add ForwardingInstruction 2023-10-03 23:54:57 -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
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
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
Michael Gottesman
37d60a08bb [move-only] Rename mark_must_check -> mark_unresolved_non_copyable_value.
I was originally hoping to reuse mark_must_check for multiple types of checkers.
In practice, this is not what happened... so giving it a name specifically to do
with non copyable types makes more sense and makes the code clearer.

Just a pure rename.
2023-08-30 22:29:30 -07:00
Erik Eckstein
89d3326ff4 Swift Optimizer: add a simplification for pointer_to_address
For a redundant pair of pointer-address conversions, e.g.

   %2 = address_to_pointer %1
   %3 = pointer_to_address %2 [strict]

replace all uses of %3 with %1.
2023-08-10 20:49:20 +02:00
Erik Eckstein
80778dedd6 AccessUtils: use the begin_apply result instead of the begin_apply instruction for a yield access base
A begin_apply can yield multiple addresses. We need to store the result of the apply in order to distinguish between two AccessBases with different results from the same begin_apply.
2023-07-21 07:19:55 +02:00
Erik Eckstein
efcd90af7d Swift SIL: rename ownership enums and properties in LoadInst and StoreInst
`ownership` is a bad name in `LoadInst`, because it hides `Value.ownership`.
Therefore rename it to `loadOwnership`.
Do the same for ownership in StoreInst to be consistent.
2023-07-11 22:33:02 +02:00
Erik Eckstein
ab915c308d Swift SIL: add the Deallocation.allocatedValue instruction API 2023-07-05 21:33:25 +02:00
Erik Eckstein
ff88efc1f6 - walk utils 2023-07-05 21:33:25 +02:00
Erik Eckstein
625619ee17 SIL: add a bare attribute to global_value
The `bare` attribute indicates that the object header is not used throughout the lifetime of the value.
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
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
Erik Eckstein
9474b9d1f0 Optimizer: add simplifications for checked_cast_br and unchecked_ref_cast
Look through `upcast` and `init_existential_ref` instructions and replace the operand of this cast instruction with the original value.
For example:
```
  %2 = upcast %1 : $Derived to $Base
  %3 = init_existential_ref %2 : $Base : $Base, $AnyObject
  checked_cast_br %3 : $AnyObject to Derived, bb1, bb2
```

This makes it more likely that the cast can be constant folded because the source operand's type is more accurate.
In the example above, the cast reduces to
```
  checked_cast_br %1 : $Derived to Derived, bb1, bb2
```
which can be trivially folded to always-succeeds.

Found while looking at `_SwiftDeferredNSDictionary.bridgeValues()`
2023-06-20 16:45:58 +02:00
Erik Eckstein
86f2ced581 StackProtection: treat source-operands of memcpy and memmove intrinsics as read-only
This makes `UnsafeMutableRawPointer.storeBytes` not triggering stack protection

rdar://110738333
2023-06-19 13:56:44 +02:00
Erik Eckstein
5325a4fe21 Swift SIL: add some instruction classes and APIs
* add `UnownedRetainInst` and `UnownedReleaseInst`
* add `var value` to `RetainValueInst` and `ReleaseValueInst`
* make the protocol `UnaryInstruction` be an `Instruction`
* add `var Type.isValueTypeWithDeinit`
* add `var Type.isUnownedStorageType`
* add `var OperandArray.values`
2023-06-07 12:55:56 +02:00
Erik Eckstein
d02765fd54 Swift SIL: some new APIs and some refactoring 2023-05-25 16:28:41 +02:00
Erik Eckstein
5c0ae940eb Optimizer: add simplification for the value_to_bridge_object instruction
This comes up in the code for constructing an empty string literal.
With this optimization it's possible to statically initialize empty string global variables.
2023-05-25 16:28:41 +02:00
Erik Eckstein
a4225a9088 Swift SIL: add a few SIL instructions and instruction APIs 2023-05-22 15:34:26 +02:00
Erik Eckstein
b9169064c6 Swift SIL: add some APIs
* `Options.assertConfiguration`
* `Argument.isIndirectResult`
* in `Function`: `selfArgument`, `isTransparent`, `performanceConstraints` and `inlineStrategy`
* `BuiltinInst.substitutionMap`
* `SubstitutionMap.replacementTypes`
* `Type.canBeClass`
2023-05-11 08:11:44 +02:00