Commit Graph

462 Commits

Author SHA1 Message Date
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
Erik Eckstein
83754fa085 Swift SIL: add Builder.createEndCOWMutation 2023-09-19 15:10:30 +02:00
Kuba Mracek
50f6430a27 [embedded] Convert thick->thin metatypes on array builtins (copy, take, assign) in mandatory optimizations 2023-09-14 18:09:08 -07:00
zoecarver
48513f3c5b [embedded] Introduce isGenericFunction and addAllNonGenericFunctions. 2023-09-06 10:48:17 -07: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
afc0f617e0 Optimizer: support statically initialized globals which contain pointers to other globals
For example:
```
  var p = Point(x: 10, y: 20)
  let o = UnsafePointer(&p)
```

Also support outlined arrays with pointers to other globals. For example:
```
var g1 = 1
var g2 = 2

func f() -> [UnsafePointer<Int>] {
  return [UnsafePointer(&g1), UnsafePointer(&g2)]
}
```
2023-08-10 20:50:36 +02:00
Erik Eckstein
7e33e554ef SIL: a few changes regarding access to a GlobalVariable's static initializer instructions
* add `GlobalVariable.staticInitializerInstructions` to access all initializer instructions of a global
* implement `GlobalVariable.staticInitValue` with `GlobalVariable.staticInitializerInstructions`
* this requires that `InstructionList.reversed()` works without accessing the parent block of the iterator instruction
* allow `Context.erase(instruction:)` to delete instructions from a global's initializer list, which means to handle the case where a deleted instruction has no parent function.
2023-08-10 20:49:20 +02: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
ee3d702019 Swift SIL: add NominalTypeDecl.isStructWithUnreferenceableStorage 2023-07-21 07:19:12 +02:00
Erik Eckstein
2baaf4890e Swift SIL: add a few new create functions in Builder 2023-07-21 07:19:12 +02:00
Kuba (Brecka) Mracek
e34c20fdf5 Merge pull request #67243 from kubamracek/section-ump
Allow forced constant-folding of global initializers to work on UnsafeMutablePointer types
2023-07-13 20:07:05 -07:00
Kuba Mracek
d4d5228534 Allow constant folding IntToPtr builtin (make it isValidInStaticInitializerOfGlobal) 2023-07-12 20:51:40 -07:00
Erik Eckstein
9cb83c33eb DeadStoreElimination: some refactoring and improvements
Addresses review feedback of https://github.com/apple/swift/pull/67122
2023-07-11 22:33:03 +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
Kuba (Brecka) Mracek
2961cafb05 Merge pull request #66844 from kubamracek/static-init-structs
Allow using structs with trivial initializers in globals that require static initialization (e.g. @_section attribute)
2023-07-10 15:11:55 -07:00
Kuba Mracek
145f12f6a3 Allow using structs with trivial initializers in globals that require static initialization (e.g. @_section attribute)
Before this change, if a global variable is required to be statically initialized (e.g. due to @_section attribute), we don't allow its type to be a struct, only a scalar type works. This change improves on that by teaching MandatoryPerformanceOptimizations pass to inline struct initializer calls into initializer of globals, as long as they are simple enough so that we can be sure that we don't trigger recursive/infinite inlining.
2023-07-08 19:26:59 -07:00
Erik Eckstein
0494795ab4 Swift SIL: make BasicBlock Equatable 2023-07-05 21:33:25 +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
205c841ed8 Swift SIL: add some Builder APIs
* `createStructExtract`
* `createStructElementAddr`
* `createTupleExtract`
* `createTupleElementAddr`
2023-07-05 21:33:24 +02:00
Erik Eckstein
0d3fe6382e SmallProjectionPath: model existential projections and indexed elements
add field types:
  `x`:    existential address projection, like `open_existential_addr`
  `i<n>`: indexed element with constant index, like `index_addr` with an integer literal as index
  `i*`:   indexed element with an unknown index
2023-07-05 21:33:24 +02:00
John McCall
65e2e8c2fe Fix some SILArgument infrastructure for pack results.
Getting this right convinces the memory verifier to not complain
about untouched empty pack result arguments, which should be
innocuous.
2023-06-30 02:08:57 -04: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
fe367e8013 Optimizer/IRGen: allow enums in static initializers of globals
The main change here is in IRGen which needs to be able to emit constant enum values.
Use `emitValueInjection` to create the enum constant.
Usually this method creates code in the current function.
But if all arguments to the enum are constant, the builder never has to emit an instruction.
Instead it can constant fold everything and just returns the final constant.

Also, create statically initialized let-globals as constant global (`constant` instead of `global`).
2023-05-25 16:28:41 +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
38de5b1ab5 Swift SIL/Optimizer: implement cloning of static init values of globals in Swift
* add the StaticInitCloner utility
* remove bridging of `copyStaticInitializer` and `createStaticInitializer`
* add `Context.mangleOutlinedVariable` and `Context.createGlobalVariable`
2023-05-22 15:34:26 +02:00
Erik Eckstein
ee1c52bc77 Swift SIL: add some APIs 2023-05-22 15:34:26 +02:00
Erik Eckstein
b707b5a595 Swift SIL: improve the Builder
* add new create-functions for instructions
* allow the Builder to build static initializer instructions for global variables
* some refactoring to simplify the implementation
2023-05-22 15:34:26 +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
7bbf66ab5b ReadOnlyGlobalVariables: don't convert external global vars to lets
Because we don't know if external variables will be written

Fixes a miscompile
rdar://109476745
2023-05-19 18:28:22 +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
Erik Eckstein
4dcd91af7b Swift SIL: add all deallocation instructions and let them conform to the Deallocation protocol 2023-05-11 08:03:19 +02:00
Erik Eckstein
5e2e7e3884 SIL Bridging: use bool for boolean properties
This was a leftover from the time we didn't use C++ interop
2023-05-11 08:03:19 +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
Erik Eckstein
e95c6425f2 Swift SIL: add some APIs for global variables 2023-05-08 21:23:36 +02:00
Erik Eckstein
9b51e69dac Swift Optimizer: constant fold builtins in the simplification passes 2023-05-08 21:23:36 +02:00
Erik Eckstein
8a8a895239 alias analysis: compute more precise memory effects of builtin "once"
* Check if the address in question is even visible from outside the function
* Return the memory effects of the called function

Also, add a new API `Instruction.memoryEffects`, which is internally used by `mayReadFromMemory` et al.
2023-05-08 21:23:36 +02:00
Erik Eckstein
5992e68d23 EscapeUtils: better handling of noescape closures and convert_function instructions
* Assume that a noescape closure argument cannot escape a called function
* Handle convert_function instructions

rdar://108837480
2023-05-03 19:53:19 +02:00
Erik Eckstein
7d70a70acf SIL: add the drop_deinit instruction
his instruction is a marker for a following destroy instruction to suppress the call of the move-only type's deinitializer.
2023-04-11 10:25:48 +02:00
Slava Pestov
e24cbbc42f SILOptimizer: Bridge new Pack_Inout, Pack_Owned, Pack_Guaranteed ownership kinds 2023-03-30 14:10:27 -04:00
Erik Eckstein
010efc1ca6 Swift Bridging: use C++ instead of C bridging for the optimizer 2023-03-21 15:33:09 +01:00
Erik Eckstein
04c90166c4 Swift Bridging: use C++ instead of C bridging for BridgedBuilder 2023-03-21 15:33:09 +01:00