Commit Graph

27 Commits

Author SHA1 Message Date
Jakub Florek
e3140e0ae0 Add new generalized cloner. 2025-08-28 20:57:57 +01:00
Doug Gregor
c91a4822d3 Serialize the "markedAsUsed" bit for SILGlobalVariable 2025-08-17 15:25:50 -07:00
Andrew Trick
d9dd93560d Support mark_dependence_addr in SIL passes. 2025-03-25 23:02:45 -07: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
Erik Eckstein
b4ba750481 Optimizer: move Context.erase(instructions:) from Outliner to Context 2024-12-09 19:59:05 +01:00
Erik Eckstein
6a0b7d1f8c ObjectOutliner: create outlined arrays as let variables
This will allow load-simplification to replace a load of such an array.
2024-11-28 09:40:12 +01:00
Erik Eckstein
10782cf42b SwiftCompilerSources: introduce the AST module
As the optimizer uses more and more AST stuff, it's now time to create an "AST" module.
Initially it defines following AST datastructures:
* declarations: `Decl` + derived classes
* `Conformance`
* `SubstitutionMap`
* `Type` and `CanonicalType`

Some of those were already defined in the SIL module and are now moved to the AST module.
This change also cleans up a few things:
* proper definition of `NominalTypeDecl`-related APIs in `SIL.Type`
* rename `ProtocolConformance` to `Conformance`
* use `AST.Type`/`AST.CanonicalType` instead of `BridgedASTType` in SIL and the Optimizer
2024-10-02 07:10:29 +02:00
Erik Eckstein
ccc691cc42 ObjectOutliner: try outline alloc_refs in multiple iterations to handle multi-dimensional arrays
This allows to completely statically allocate multi-dimensional global arrays, like
```
var x = [[1, 2], [3, 4], [5, 6]]
```
2024-05-17 17:03:07 +02:00
Erik Eckstein
af68435d90 Optimizer: support static initialization of global arrays
The buffer of global arrays could already be statically initialized.
The missing piece was the array itself, which is basically a reference to the array buffer.
For example:
```
var a = [1, 2, 3]
```
ends up in two statically initialized globals:
1. the array buffer, which contains the elements
2. the variable `a` which is a single reference (= pointer) of the array buffer

This optimization removes the need for lazy initialization of such variables.

rdar://127757554
2024-05-16 21:34:36 +02:00
Erik Eckstein
eb626c2814 ObjectOutliner: don't outline objects with tail allocations
An object with tail allocated elements is in risk of being passed to malloc_size, which does not work for non-heap allocated objects.
Conservatively, disable objects with tail allocations.

rdar://121886093
2024-01-31 16:25:21 +01:00
Meghana Gupta
5d2454f3e3 Merge pull request #68180 from meg-gupta/lowerb4inline
Lower OwnershipModelEliminator to just before inlining
2024-01-10 13:53:34 -08:00
Erik Eckstein
fa3a959524 ObjectOutliner: support outlining of classes in global let variables
```
  let c = SomeClass()
```

is turned into

```
  private let outlinedVariable = SomeClass()  // statically initialized and allocated in the data section
  let c = outlinedVariable
```

rdar://111021230
rdar://115502043

Also, make the ObjectOutliner work for OSSA. Though, it currently doesn't run in the OSSA pipeline.
2024-01-10 09:34:01 +01: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
Meghana Gupta
df3468fd6c Follow up of #68180
Remove unnecessary cases
2024-01-05 13:20:52 -08:00
Erik Eckstein
ded20d3c11 SwiftCompilerSources: add Value.isValidGlobalInitValue and FullApplySite.isSemanticCall
`Value.isValidGlobalInitValue` was privately used by the ObjectOutliner. Make it a generally usable utility.
2023-12-09 18:49:58 +01:00
Meghana Gupta
86b651330b Revert "Merge pull request #69807 from apple/revert-69450-uninarrayfix"
This reverts commit cabb5e109f, reversing
changes made to 09688abb02.
2023-12-06 08:29:50 -08:00
Meghana Gupta
a228a8859a Handle unchecked_ref_cast in ObjectOutliner 2023-12-05 13:06:36 -08: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
Mishal Shah
e8de333daf Revert "Add a mark_dependence while emitting SIL for uninitialized array allocation " 2023-11-12 09:43:13 -08: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
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
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
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
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
4284dc10d0 Optimizer: implement the ObjectOutliner pass in Swift 2023-05-22 15:34:26 +02:00