Commit Graph

954 Commits

Author SHA1 Message Date
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
Erik Eckstein
30ea104085 make InstructionRange.contains more efficient
Instead of trying to find an instruction by iterating through all instruction in the list, use an InstructionSet.
2023-09-19 15:10:30 +02:00
Kuba Mracek
51792d8a31 [embedded] Simplify optimizeArrayBuiltin, avoid needing a single use of the metatype inst 2023-09-15 10:40:19 -07:00
Kuba Mracek
350107e153 [embedded] Also cover DestroyArray builtin in embedded Swift mode 2023-09-15 06:56:11 -07: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
Kuba Mracek
c0750cdd33 [embedded] Fix accidentally reversed logic in isGenericFunction + addAllNonGenericFunctions 2023-09-12 21:27:29 -07:00
Erik Eckstein
d9826f6043 SimplifyBranch: don't leave an empty block after merging two basic blocks
Although empty blocks are cleaned up at the end of every Simplification pass, it's not legal SIL to have empty blocks and subsequent simplification passes may crash because of this.

rdar://115169880
2023-09-11 14:11:46 +02:00
zoecarver
520305332a [embedded] Don't add functions twice; fix a typo. 2023-09-06 10:59:49 -07:00
zoecarver
48513f3c5b [embedded] Introduce isGenericFunction and addAllNonGenericFunctions. 2023-09-06 10:48:17 -07:00
zoecarver
52cea4250e [embedded] 🚀 add Embedded expiremental feature.
Optimize all functions in embedded mode. Diagnose any uses of existentials or witness tables.
2023-09-06 10:48:17 -07:00
Erik Eckstein
f6f9e75173 AliasAnalysis: use a complexity limit for the isObjReleased function
We already use a complexity limit for other functions in AliasAnalysis.
This is a workaround for quadratic complexity in ARCSequenceOpts.

Fixes a compile time problem
rdar://114352817
2023-09-04 19:52:57 +02:00
Manu
02b5fa2c8e Fix some typos in the codebase 2023-08-31 18:50:10 -03:00
Michael Gottesman
ba06693745 Merge pull request #68228 from gottesmm/pr-088dba5d9c0854c0e00da0603eb99b770d99709c
[move-only] Rename mark_must_check -> mark_unresolved_non_copyable_value
2023-08-31 09:50:55 -07:00
Erik Eckstein
51cb7575ea RedundantLoadElimination: don't crash when loading a non-trivial enum which was stored as trivial non-payload case.
rdar://114648847
2023-08-31 09:16:17 +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
Andrew Trick
8a71844456 Cleanup SIL ComputeSideEffects for partial_apply arguments 2023-08-29 16:58:56 -07:00
Meghana Gupta
9f93ba003e Don't inline non-ossa function into an ossa function in mandatory inlining
Update the inliner with this check.
Inlining non-ossa into ossa can causes unexpected consequences.

Fixes rdar://114466605
2023-08-25 21:46:50 -07:00
Andrew Trick
4ce6fcf18f Fix SIL function side effects to handle unapplied escaping
Fixes rdar://113339972 DeadStoreElimination causes uninitialized closure context

Before this fix, the recently enabled function side effect implementation
would return no side effects for a partial apply that is not applied
in the same function. This resulted in DeadStoreElimination
incorrectly eliminating the initialization of the closure context.

The fix is to model the effects of capturing the arguments for the
closure context. The effects of running the closure body will be
considered later, at the point that the closure is
applied. Running the closure does, however, depend on the captured
values to be valid. If the value being captured is addressible,
then we need to model the effect of reading from that memory. In
this case, the capture reads from a local stack slot:

    %stack = alloc_stack $Klass
    store %ref to %stack : $*Klass
    %closure = partial_apply [callee_guaranteed] %f(%stack)
      : $@convention(thin) (@in_guaranteed Klass) -> ()

Later, when the closure is applied, we won't have any reference back
to the original stack slot. The application may not even happen in a caller.

Note that, even if the closure will be applied in the current
function, the side effects of the application are insufficient to
cover the side effects of the capture. For example, the closure
body itself may not read from an argument, but the context must
still be valid in case it is copied or if the capture itself was
not a bitwise-move.

As an optimization, we ignore the effect of captures for on-stack
partial applies. Such captures are always either a bitwise-move
or, more commonly, capture the source value by address. In these
cases, the side effects of applying the closure are sufficient to
cover the effects of the captures. And, if an on-stack closure is
not invoked in the current function (or passed to a callee) then
it will never be invoked, so the captures never have effects.
2023-08-16 11:58:11 -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
538d7306bd MandatoryPerformanceOptimizations: better handling of statically initialized globals which contain a pointer to another global
E.g.
```
  var p = Point(x: 10, y: 20)
  let o = UnsafePointer(&p)
```

MandatoryPerformanceOptimization must inline the accessor to the referenced global.
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
e45b82f4db AccessUtils: add AccessBase.hasLocalOwnershipLifetime 2023-08-10 20:49:20 +02:00
Erik Eckstein
8223b3e210 MandatoryPerformanceOptimizations: fix some problems with inilning
* don't inline functions if it's not possible - by checking `SILInliner::canInlineApplySite`
* fix stack nesting after inlining a `begin_apply`
2023-08-10 15:01:47 +02:00
Erik Eckstein
799bd0a5f5 Swift Optimizer: some reformatting in the optimization passes
Use tail closures for all optimization passes.
NFC.
2023-08-04 10:33:52 +02:00
Kuba (Brecka) Mracek
8510bdd7f0 Merge pull request #67587 from kubamracek/move-target-const-folding-to-swift-simplification
Move target const folding to swift simplification
2023-08-01 15:19:26 -07:00
Kuba Mracek
5dac59ce71 Move TargetConstantFolding pass to the simplification passes in Swift, enable using MemoryLayout's .size, .stride, .alignment fields in forced-const global initializers 2023-07-31 10:54:07 -07:00
Nate Chandler
e5d87f75a8 [SIL] Add source formal type to checked_cast_br.
It is necessary for opaque values where for casts that will newly start
out as checked_cast_brs and be lowered to checked_cast_addr_brs, since
the latter has the source formal type, IRGen relies on being able to
access it, and there's no way in general to obtain the source formal
type from the source lowered type.
2023-07-27 15:04:15 -07:00
Erik Eckstein
64255651f5 MandatoryPerformanceOptimizations: refine the inlining decisions in global initializers
We inline a function (e.g. a struct initializer) into a global init function if the result is part of the initialized global.
Now, also handle functions with indirect return values. Such function can result from not-reabstracted generic specializations.
Handle cases where the result is stored into a temporary alloc_stack or directly stored to (a part) of the global variable.
2023-07-26 11:06:50 +02:00
Erik Eckstein
393711d5f4 SimplifyBuiltin: ignore debug_step and debug_value when checking for side-effects in the callee of a builtin.once 2023-07-26 11:06:50 +02:00
Erik Eckstein
c567167b66 InitializeStaticGlobals: add a peephole to merge element stores to a single store.
Sometimes structs are not stored in one piece, but as individual elements. Merge such individual stores to a single store of the whole struct.
This enables generating a statically initialized global.
2023-07-26 11:06:50 +02:00
Erik Eckstein
29246fd80b AliasAnalysis: add complexity budget for the getMemEffectsFunction 2023-07-21 07:19:56 +02:00
Erik Eckstein
4d20423e00 Optimizer: re-implement the RedundantLoadElimination pass in Swift
The new implementation has several benefits compared to the old C++ implementation:

* It is significantly simpler. It optimizes each load separately instead of all at once with bit-field based dataflow.
* It's using alias analysis more accurately which enables more loads to be optimized
* It avoids inserting additional copies in OSSA

The algorithm is a data flow analysis which starts at the original load and searches for preceding stores or loads by following the control flow in backward direction.
The preceding stores and loads provide the "available values" with which the original load can be replaced.
2023-07-21 07:19:56 +02:00
Erik Eckstein
86771468fc Swift Optimizer: move StoreInst.split into OpUtils.swift
To make it available in other optimizations as well.
Also, a few problems:
* Use destructre instructions when in OSSA
* Don't split the store if it's nominal type has unreferenceable stoarge
* rename it to `trySplit` because it's not guaranteed to work

Also, add the counterpart for load instructions: `LoadInst.trySplit()`
2023-07-21 07:19:56 +02:00
Erik Eckstein
8739befa92 AccessUtils: let two tail-access bases with the same reference root be equivalent
This is possible now because projection path now models index-addressing correctly.
2023-07-21 07:19:56 +02:00