Commit Graph

2610 Commits

Author SHA1 Message Date
Andrew Trick
5f7f6e464f Rename SILBuilder::createUncheckedBitCast
to SILBuilder::createUncheckedForwardingCast

It would be disastrous to confuse this utility with a bit cast. A bit
cast always produces an Unowned value which must immediately be copied
to be used. This utility always forwards ownership. It cannot be used
to truncate values.

Also, be careful not to convert "reinterpret cast"
(e.g. Builtin.reinterpretCast) into a "value cast" since ownership
will be incorrect and the reinterpreted types might not have
equivalent layout.
2022-10-03 10:09:07 -07:00
nate-chandler
4dec19388b Merge pull request #61344 from nate-chandler/copy-propagation/remove-poison
[CopyPropagation] Removed poison mode.
2022-09-29 10:15:07 -07:00
Nate Chandler
129dadbe70 [CopyPropagation] Removed poison mode.
The Onone strategy will be to shorten lifetimes as little as possible
while eliminating as many copies as are eliminated at -O.
2022-09-28 17:13:24 -07:00
Nate Chandler
5d14610043 [SILOptimizer] Preserve arg attrs at cloning.
Arguments are copied into new cloned functions in a number of places.
Wherever that happens, be sure to transfer the attributes as well.
2022-09-26 16:55:50 -07:00
Dario Rexin
210c68d8aa [SILOptimizer] Add prespecialization for arbitray reference types (#58846)
* [SILOptimizer] Add prespecialization for arbitray reference types

* Fix benchmark Package.swift

* Move SimpleArray to utils

* Fix multiple indirect result case

* Remove leftover code from previous attempt

* Fix test after rebase

* Move code to compute type replacements to SpecializedFunction

* Fix ownership when OSSA is enabled

* Fixes after rebase

* Changes after rebasing

* Add feature flag for layout pre-specialization

* Fix pre_specialize-macos.swift

* Add compiler flag to benchmark build

* Fix benchmark SwiftPM flags
2022-09-22 16:29:01 -07:00
Adrian Prantl
1f8a5d6045 Merge pull request #61168 from adrian-prantl/99874371
Disable SROA debug info for variables with expressions
2022-09-21 08:43:38 -07:00
Adrian Prantl
90bbe517f9 Disable SROA debug info for variables with expressions.
Currently the SROA just overwrites already-existing expressions on
variables. When SROA is recursively run on a data structure this leads to
nonsensical expressions such as

  type $*Outer, expr op_fragment:#Inner.x

instead of

  type $*Outer, expr op_fragment:#Outer.inner op_fragment:#Inner.x

The (nonsensical) LLVM IR generated from this violates some assumptions in LLVM
for example, if a struct has multiple members of the same type, you can end up
with multiple dbg.declare intrinsics claiming to describe the same variable). As
a quick fix, this patch detects this situation and drops the debug info. A
proper fix shouldn't be too difficult to implement though.

rdar://99874371
2022-09-19 16:10:18 -07:00
Meghana Gupta
a1bcf17b26 Avoid optimizing lexical borrow scopes in DCE 2022-09-15 16:42:23 -07:00
Meghana Gupta
6c0c3dd1e1 Rename hasEscaped -> hasPointerEscape, and don't consider OperandOwnership::BitwiseEscape as a pointer escape 2022-09-15 16:40:39 -07:00
Meghana Gupta
804e3f0968 Merge pull request #61126 from meg-gupta/fixdce
Don't shrink escaped borrow scopes in DCE
2022-09-15 15:47:20 -07:00
Meghana Gupta
88782ac939 Don't shrink escaped borrow scopes in DCE 2022-09-14 21:02:02 -07:00
Nate Chandler
75608f0d69 [Outliner] Replace assertion with bail.
Previously, when attempting to pattern-match bridged ObjC properties,
there was an assertion about the location of destroy_value instructions
in the code that we were trying to match.  Instead, bail in the face of
an unexpected pattern.

rdar://99873905
2022-09-14 16:45:37 -07:00
Michael Gottesman
467b742a5c [move-only] Update the non-address move checker part of the optimizer to handle mark_must_check on addresses. 2022-09-09 13:38:18 -07:00
Meghana Gupta
b47230135a Fix Mem2Reg check on end_borrows of store_borrow
Instead of checking if the end_borrow is ending the lifetime of the store_borrow of the asi under consideration,
this code was checking if the store_borrow source is the runningValue which is incorrect in cases where a store_borrow
src to another destination gets replaced during mem2reg. This PR fixes the issue.
2022-09-08 12:00:02 -07:00
Erik Eckstein
97b2354be6 SIL: add needsStackProtection flags for address_to_pointer and index_addr instructions.
Also add new "unprotected" variants of the `addressof` builtins:
* `Builtin.unprotectedAddressOf`
* `Builtin.unprotectedAddressOfBorrow`
2022-09-08 08:42:22 +02:00
Hamish Knight
b12015c343 [SIL] Introduce the increment_profiler_counter instruction
This is a dedicated instruction for incrementing a
profiler counter, which lowers to the
`llvm.instrprof.increment` intrinsic. This
replaces the builtin instruction that was
previously used, and ensures that its arguments
are statically known. This ensures that SIL
optimization passes do not invalidate the
instruction, fixing some code coverage cases in
`-O`.

rdar://39146527
2022-09-07 17:55:13 +01:00
Michael Gottesman
4dc6e6ecc4 [move-keyword] Remove old implementation.
By using the keyword instead of the function, we actually get a much simpler
implementation since we avoid all of the machinery of SILGenApply. Given that we
are going down that path, I am removing the old builtin implementation since it
is dead code.

The reason why I am removing this now is that in a subsequent commit, I want to
move all of the ownership checking passes to run /before/ mandatory inlining. I
originally placed the passes after mandatory inlining since the function version
of the move keyword was transparent and needing to be inlined before we could
process it. Since we use the keyword now, that is no longer an issue.
2022-09-04 01:19:01 -07:00
Erik Eckstein
bd1651169c TempRValueOpt: fix a wrong assert.
The assert was too restrictive.
Fixes a crash.

rdar://99386994
2022-09-01 15:51:40 +02:00
Meghana Gupta
17664c0c60 Fix store_borrow multi block handling in mem2reg 2022-08-30 12:48:37 -07:00
Meghana Gupta
e0f8711b23 Fix SILMem2Reg while marking end_borrow of store_borrow as deinit point
Fixes rdar://99229461
2022-08-27 21:01:16 -07:00
Meghana Gupta
9a4a21d7eb Merge pull request #59541 from meg-gupta/mem2regstoreborrowpr
Handle store_borrow in SILMem2Reg and some other fixes
2022-08-26 09:36:34 -07:00
Meghana Gupta
ed7788fff1 Get rid of isStorageValid flag on lastStoreInst 2022-08-25 13:50:08 -07:00
Meghana Gupta
8e9117dda3 Handle store_borrow in SILMem2Reg 2022-08-25 13:25:15 -07:00
Slava Pestov
7a16b0275b AST: Allow one-element tuple types to be constructed
These will never appear in the source language, but can arise
after substitution when the original type is a tuple type with
a pack expansion type.

Two examples:
- original type: (Int, T...), substitution T := {}
- original type: (T...), substitution T := {Int}

We need to model these correctly to maintain invariants.

Callers that previously used to rely on TupleType::get()
returning a ParenType now explicitly check for the one-element
case instead.
2022-08-23 11:12:00 -04:00
Nate Chandler
8d55e09bf3 [SSADestroyHoisting] Lifetimes alter arg hoisting.
Arguments whose lifetimes are not lexical should be hoisted without
respect to deinit barriers.  On the other hand, inout arguments
explicitly annotated @_lexical should respect deinit barriers.
2022-08-22 15:27:59 -07:00
Meghana Gupta
13223f6edf Remove fake use of lexical copy 2022-08-22 15:27:52 -07:00
Meghana Gupta
0c2fd6b419 Remove unnecessary edge case 2022-08-22 15:27:52 -07:00
Meghana Gupta
7f6fd0bcf8 Reorganize if to reduce indentation 2022-08-22 15:27:47 -07:00
Michael Gottesman
3e52007562 [builtin] Remove "unsafeGuaranteed" and related code since Unmanaged now has an Ownership SSA based implementation that works completely in SILGen.
This isn't used in the stdlib anymore as well.
2022-08-21 01:22:36 -07:00
Meghana Gupta
8769cbec07 Simplify SILMem2Reg by using computeDominatedBoundaryBlocks api instead of computing in-place 2022-08-19 09:46:53 -07:00
Nate Chandler
7ed618fb5e [TempRValueOpts] Opt some lexical alloc_stacks.
Previously, whenever an alloc_stack [lexical] was seen, optimization
bailed conservatively.

In the fullness of time, we should optimize an alloc_stack [lexical] so
long as:
(1) the root of the source of the store/copy_addr is lexical
(2) the range in which the alloc_stack [lexical] contains the value
    store'd/copy_addr'd into it (i.e. the range ending at
    destroy_addr/etc) is contained within the live range of that lexical
    root

Here, an incremental step in that direction is taken: if the base of the
accessed storage of the source of a copy_addr is a guaranteed function
argument, then we know
(1) the base is lexical--guaranteed function arguments are always
    lexical
(2) the range in which the alloc_stack contains the value copy_addr'd in
    is trivially contained within the range of that lexical root--the
    live range of a guaranteed function argument is the whole function

Added TODOs for the full optimization.
2022-08-12 11:37:19 -07:00
Nate Chandler
896a464ced [AllocBoxToStack] Lexical borrowees get lexical alloc_stacks.
Previously, whenever an alloc_box that was promoted to an alloc_stack,
the new alloc_stack would be lexical.  The result was that alloc_boxes
which didn't need (or explicitly didn't want, in the case of eager move
vars) received lexical alloc_stacks.

Here, only add the lexical flag to the new alloc_stack instruction if
any of the box's uses are `begin_borrow [lexical]`.  That way,
alloc_boxes end up having lexical alloc_stacks only if they were
"lexical alloc_boxes".
2022-08-12 11:37:19 -07:00
Nate Chandler
80ba5b588b [SSADestroyHoisting] Only lexical alloc_stacks respect deinit barriers. 2022-08-10 19:27:57 -07:00
Ben Barham
6de34f37e5 [NFC] Revert SmallVector<T> -> SmallVector<T, N> fixes
With the change to include `SmallVector.h` directly in `LLVM.h` rather
than forward declaring in the only case it matters (ie. Clang <= 5),
these fixes are no longer needed. Since defaulted version is preferred
when there's no better choice (which is presumably the case if that's
how they were originally added), use it instead. Some uses were instead
changed to add `llvm::` so remove that too.
2022-08-05 21:25:55 -07:00
Michael Gottesman
1e6187c4f4 [sil] Update all usages of old API SILValue::getOwnershipKind() in favor of new ValueBase::getOwnershipKind().
Andy some time ago already created the new API but didn't go through and update
the old occurences. I did that in this PR and then deprecated the old API. The
tree is clean, so I could just remove it, but I decided to be nicer to
downstream people by deprecating it first.
2022-07-26 11:46:23 -07:00
nate-chandler
dcea14716c Merge pull request #41943 from nate-chandler/copy_propagation/canonicalize-all
[CopyPropagation] Canonicalize all values.
2022-07-22 16:54:04 -07:00
Nate Chandler
a9038057b9 [Outliner] Add consuming bridged property pattern.
Previously, for the BridgedProperty pattern, there were two flavors of
outlined function that would be formed, varying on the ownership of the
instance whose field is being access:

(1) guaranteed_in -- for use by addresses
(2) unowned -- for use by values which were not destroyed until some
              time after the pattern of interest is matched

Now that the lifetime of the instance may be shortened to just after the
call to the objc method, the latter of these is not appropriate for
values.  We would have to re-extend the lifetime until after the method
returns.

Instead, here, we add a new variant

(3) owned -- for use by values which were destroyed just after the objc
             method call

Doing so requires new mangling.
2022-07-22 08:17:14 -07:00
Nate Chandler
9b290cdfb6 [Outliner] Defer bridged property mangling.
Put off calculating the outlined function name until the point at which
we will have all the inputs for the name.
2022-07-22 08:17:14 -07:00
Nate Chandler
b527649b15 [NFC] [Outliner] Removed unused fields. 2022-07-22 08:17:14 -07:00
Nate Chandler
50a7080c9c [Outliner] Bridged property expects hoisted destroy.
Thanks to CopyPropagation canonicalizing all values, not just those for
which there is a copy_value instruction, the lifetime of the value that
is loaded in the BridgedProperty pattern is shortened and the
destroy_value appears right after its use rather than after the full CFG
for the bridged property.  Updated the BridgedProperty pattern to match
that newly hoisted instruction.
2022-07-22 08:17:14 -07:00
Nate Chandler
50a0cfa406 [CopyPropagation] Canonicalize all values.
Previously, the lifetimes of only values which were copied were
canonicalized during the non-mandatory CopyPropagation pass.  The
mandatory pass (i.e. MandatoryCopyPropagation) has been disabled,
meaning that non-copied values lifetimes were  never canonicalized.  So
enable canonicalization of all lifetimes during regular CopyPropagation.

rdar://54335055
2022-07-22 08:17:13 -07:00
Nate Chandler
fe9920ef7b [Outliner] Fixed bridged property match in OSSA.
Previously, the match failed to find the larger sequence that began with
load [copy] and ended with destroy_value because the iterator advanced
after finding the load [copy].  Advanced the iterator here.

Enables reverting the test outlining test changes introduced in
a52b8966c6.
2022-07-22 08:17:13 -07:00
Erik Eckstein
dc42c4c17d AllocStackToBox: fix a bug which results in a too early released captured variable
In case of a borrowed `alloc_box`, the optimization didn't look through the `begin_borrow` when calculating the final release of the box.
This resulted in inserting the destroy of the inserted `alloc_stack` too early.

rdar://97087762
2022-07-18 17:13:12 +02:00
Erik Eckstein
4818a4605f AccessEnforcementWMO: notify analysis if functions have changed.
This was missing. It's not a big problem, but should be done correctly.
I noticed this because changes didn't get printed when I used `-sil-print-function`.
2022-07-14 18:42:35 +02:00
Erik Eckstein
777b42180d SILOptimizer: use the new set data structures in various optimizations 2022-07-13 14:27:50 +02:00
nate-chandler
aff4e799d9 Merge pull request #59726 from nate-chandler/freestanding/task-to-thread-model
[Freestanding] Disable unstructured tasks.
2022-07-11 07:40:31 -07:00
Nate Chandler
34c08b8344 [TaskToThread] Add Task.runInline.
The new intrinsic, exposed via static functions on Task<T, Never> and
Task<T, Error> (rethrowing), begins an asynchronous context within a
synchronous caller's context.  This is only available for use under the
task-to-thread concurrency model, and even then only under SPI.
2022-07-08 08:44:18 -07:00
Erik Eckstein
da53141ca2 MandatoryGenericSpecializer: always inline co-routines
Co-routines (called by `begin_apply`) may allocate and therefore it’s important to always inline them.

Also, refactor the code a bit to fix the if-changed-then-return-true logic.

rdar://94833845
2022-07-07 08:34:54 +02:00
Erik Eckstein
9ef9c154f6 MandatoryGenericSpecializer: run optimizations in a loop
To avoid phase ordering problems of the involved optimizations, iterate until we reach a fixed point.
2022-07-07 08:34:53 +02:00
Meghana Gupta
cf6c177117 Fix mem2reg assert to allow phi arguments (#59796)
rdar://96171577
2022-06-30 19:11:44 -07:00