Commit Graph

46 Commits

Author SHA1 Message Date
Erik Eckstein
87e7250750 Optimizer: fix an ownership violation when duplicating loops
If a guaranteed value is used in a dead-end exit block and the enclosing value is _not_ destroyed in this block, we end up missing the enclosing value as phi-argument after duplicating the loop.
TODO: once we have complete lifetimes we can remove this check again.

rdar://159125605
2025-08-28 18:40:54 +02:00
Meghana Gupta
2b072a56c1 Allow duplicating mark_dependence [nonescaping] on addresses 2025-02-28 17:11:59 -08:00
Andrew Trick
4612728581 [NFC] Add BeginApplyInst::getEndApplyUses() API.
A begin_apply token may be used by operands that do not end the coroutine:
mark_dependence.

We need an API that gives us only the coroutine-ending uses. This blocks
~Escapable accessors.

end_borrow is considered coroutine-ending even though it does not actually
terminate the coroutine.

We cannot simply ask isLifetimeEnding, because end_apply and abort_apply do not
end any lifetime.
2024-11-18 01:37:00 -08:00
Erik Eckstein
156f1fc759 Optimizer: prevent duplicating blocks with dead-end begin-borrow instructions.
In case the control flow ends in a dead-end block there can be begin-borrow instructions which have no corresponding end-borrow uses.
After duplicating such a block, the re-borrow flags cannot be recomputed correctly for inserted phi arguments.
Therefore just disable duplicating such blocks, e.g. in jump-threading.
2024-11-12 10:04:09 +01:00
Nate Chandler
71239d6357 [CoroutineAccessors] SIL represents callee alloc.
When its operand has coroutine kind `yield_once_2`, a `begin_apply`
instruction produces an additional value representing the storage
allocated by the callee.  This storage must be deallocated by a
`dealloc_stack` on every path out of the function.  Like any other stack
allocation, it must obey stack discipline.
2024-10-11 08:25:03 -07:00
Meghana Gupta
5282194682 Fix isTriviallyDuplicatable to handle open_pack_element
SILInstruction::clone doesn't know how to clone instructions that produce
the archetype uuid. SILCloner is equipped to handle such instructions.

Optimizations like LoopRotate use  SILInstruction::clone and will be
incorrect for such instructions.

rdar://130047619
2024-06-29 13:42:05 -07:00
Tim Kientzle
1d961ba22d Add #include "swift/Basic/Assertions.h" to a lot of source files
Although I don't plan to bring over new assertions wholesale
into the current qualification branch, it's entirely possible
that various minor changes in main will use the new assertions;
having this basic support in the release branch will simplify that.
(This is why I'm adding the includes as a separate pass from
rewriting the individual assertions)
2024-06-05 19:37:30 -07:00
Nate Chandler
5307ac666e [SIL] Permit end_borrow(begin_apply). 2024-04-25 17:03:41 -07:00
Andrew Trick
2e82db8e1b ClosureLifetimeFixup; emit mark_dependence [nonescaping].
It is safe to do so because the on_stack transformation already checks that the closure has a single known apply use.
2024-03-18 17:45:33 -07:00
Meghana Gupta
1369043c6c Don't duplicate builtin "once"
Fixes rdar://121485860
2024-01-24 14:40:43 -08:00
Andrew Trick
a4070e627c Prevent partial_apply cloning.
partial_apply cannot be cloned, even in OSSA. OSSA lowering does
not know how to allocate for multiple partial applies.

Fixes rdar://119768691 (OwnershipModelEliminator triggers assertion
when lowering certain [on_stack] partial_applys in certain
circumstances)
2024-01-03 10:31:01 -08:00
Doug Gregor
53c8e84a1f [Typed throws] Handle throw_addr in the same places as throw. 2023-11-03 19:02:58 -07:00
Meghana Gupta
2c6139dce4 Rename SILArgument::isEscaping -> SILArgument::hasPointerEscape 2023-05-26 15:58:30 -07:00
Meghana Gupta
1dc713e2f7 Add new flags "reborrow" and "escaping" to SILArgument.
"reborrow" flag on the SILArgument avoids transitive walk over the phi operandsi
to determine if it is a reborrow in multiple utilities.
SIL transforms must keep the flag up-to-date by calling SILArgument::setReborrow.
SILVerifier checks to ensure the flag is not invalidated.

Currently "escaping" is not used anywhere.
2023-05-11 12:31:37 -07:00
Erik Eckstein
edce513580 SIL: simplify replaceBranchTarget
Instead of re-creating all kind of terminator instructions, just re-assign the terminator's successor.
2023-01-16 18:55:35 +01:00
Meghana Gupta
1c05e768b9 Fix OwnershipKind of block argument while creating a preheader 2022-12-13 21:24:07 -08:00
Allan Shortlidge
0921480486 SILOptimizer: Break circular dependency with SIL library by moving canDuplicate().
Fixes a cycle introduced by https://github.com/apple/swift/pull/61051.
2022-10-27 16:12:05 -07:00
Nate Chandler
a886c7fd9d [LoopRotate] Use ownership for backedge block.
Previously, all phi arguments added to the backedge block were given
@owned ownership.  That was not correct for arguments that needed to
have @guaranteed ownership.  Here, that is corrected by using the
ownership of the original argument from the header block when adding the
phi argument to the backedge block.
2021-10-15 19:06:28 -07:00
swift_jenkins
d5be531a83 Merge remote-tracking branch 'origin/main' into next 2020-11-10 19:07:28 -08:00
Michael Gottesman
c026e95cce [ownership] Extract out SILOwnershipKind from ValueOwnershipKind into its own type and rename Invalid -> Any.
This makes it easier to understand conceptually why a ValueOwnershipKind with
Any ownership is invalid and also allowed me to explicitly document the lattice
that relates ownership constraints/value ownership kinds.
2020-11-10 14:29:11 -08:00
Arnold Schwaighofer
97e5e01fe2 Merge remote-tracking branch 'origin/main' into next 2020-11-04 12:20:35 -08:00
Andrew Trick
7c2c047116 LoopUtils: Remove useless canonicalization. 2020-11-03 01:40:00 -08:00
Gwen Mittertreiner
625881816b Update for rG89c1e35f3c50
https://reviews.llvm.org/D82895 changed empty to isInnermost
2020-09-30 11:29:35 -07:00
Andrew Trick
bddc69c8a6 Organize SILOptimizer/Utils headers. Remove Local.h.
The XXOptUtils.h convention is already established and parallels
the SIL/XXUtils convention.

New:
- InstOptUtils.h
- CFGOptUtils.h
- BasicBlockOptUtils.h
- ValueLifetime.h

Removed:
- Local.h
- Two conflicting CFG.h files

This reorganization is helpful before I introduce more
utilities for block cloning similar to SinkAddressProjections.

Move the control flow utilies out of Local.h, which was an
unreadable, unprincipled mess. Rename it to InstOptUtils.h, and
confine it to small APIs for working with individual instructions.
These are the optimizer's additions to /SIL/InstUtils.h.

Rename CFG.h to CFGOptUtils.h and remove the one in /Analysis. Now
there is only SIL/CFG.h, resolving the naming conflict within the
swift project (this has always been a problem for source tools). Limit
this header to low-level APIs for working with branches and CFG edges.

Add BasicBlockOptUtils.h for block level transforms (it makes me sad
that I can't use BBOptUtils.h, but SIL already has
BasicBlockUtils.h). These are larger APIs for cloning or removing
whole blocks.
2019-10-02 11:34:54 -07:00
Adrian Prantl
ff63eaea6f Remove \brief commands from doxygen comments.
We've been running doxygen with the autobrief option for a couple of
years now. This makes the \brief markers into our comments
redundant. Since they are a visual distraction and we don't want to
encourage more \brief markers in new code either, this patch removes
them all.

Patch produced by

      for i in $(git grep -l '\\brief'); do perl -pi -e 's/\\brief //g' $i & done
2018-12-04 15:45:04 -08:00
Andrew Trick
12bb49f57a Expose SIL/BasicBlockUtils for critical edge splitting.
The primary interfaces for CFG manipulation belong in SIL. This is
just what's necessary to fix SILCloner.
2018-10-22 08:34:57 -07:00
Michael Gottesman
d57a88af0d [gardening] Rename references to SILPHIArgument => SILPhiArgument. 2018-09-25 22:23:34 -07:00
John McCall
512e55683e Make it easy to create a SILBasicBlock immediately before a target block.
Also, make "after" requests explicit in the API.
2018-08-18 12:36:36 -04:00
Bob Wilson
8e330ee344 NFC: Fix indentation around the newly renamed LLVM_DEBUG macro.
Jordan used a sed command to rename DEBUG to LLVM_DEBUG. That caused some
lines to wrap and messed up indentiation for multi-line arguments.
2018-07-21 00:56:18 -07:00
Jordan Rose
cefb0b62ba Replace old DEBUG macro with new LLVM_DEBUG
...using a sed command provided by Vedant:

$ find . -name \*.cpp -print -exec sed -i "" -E "s/ DEBUG\(/ LLVM_DEBUG(/g" {} \;
2018-07-20 14:37:26 -07:00
Adrian Prantl
c74ae5f375 Remove the redundant SILLocation::getCompilerGenerated interface (NFC) 2018-01-17 11:09:23 -08:00
Vedant Kumar
d60ff05265 Rename getEmptyLocation to getCompilerGeneratedLocation 2018-01-09 12:38:45 -08:00
Vedant Kumar
e33f3b663d [DebugInfo] Add a free helper to get empty locations, NFC 2018-01-08 17:59:16 -08:00
Michael Gottesman
ea1f804207 [semantic-sil] Eliminate ValueOwnershipKind::Any from SILPHIArguments in Semantic SIL.
Most of this involved sprinkling ValueOwnershipKind::Owned in many places. In
some of these places, I am sure I was too cavalier and I expect some of them to
be trivial. The verifier will help me to track those down.

On the other hand, I do expect there to be some places where we are willing to
accept guaranteed+trivial or owned+trivial. In those cases, I am going to
provide an aggregate ValueOwnershipKind that will then tell SILArgument that it
should disambiguate using the type. This will eliminate the ackwardness from
such code.

I am going to use a verifier to fix such cases.

This commit also begins the serialization of ValueOwnershipKind of arguments,
but does not implement parsing of value ownership kinds. That and undef are the
last places that we still use ValueOwnershipKind::Any.

rdar://29791263
2017-01-10 20:05:23 -08:00
practicalswift
6d1ae2a39c [gardening] 2016 → 2017 2017-01-06 16:41:22 +01:00
Michael Gottesman
4e8ff35df5 [semantic-sil] Add ValueOwnershipKind field to SILPHIArgument and split Argument creation methods into one for SILPHIArgument and another for SILFunctionArgument.
We preserve the current behavior of assuming Any ownership always and use
default arguments to hide this change most of the time. There are asserts now in
the SILBasicBlock::{create,replace,insert}{PHI,Function}Argument to ensure that
the people can only create SILFunctionArguments in entry blocks and
SILPHIArguments in non-entry blocks. This will ensure that the code in tree
maintains the API distinction even if we are not using the full distinction in
between the two.

Once the verifier is finished being upstreamed, I am going to audit the
createPHIArgument cases for the proper ownership. This is b/c I will be able to
use the verifier to properly debug the code. At that point, I will also start
serializing/printing/parsing the ownershipkind of SILPHIArguments, but lets take
things one step at a time and move incrementally.

In the process, I also discovered a CSE bug. I am not sure how it ever worked.
Basically we replace an argument with a new argument type but return the uses of
the old argument to refer to the old argument instead of a new argument.

rdar://29671437
2016-12-18 14:48:35 -08:00
Michael Gottesman
38ec08f45f [gardening] Standardize SILBasicBlock successor/predecessor methods that deal with blocks rather than the full successor data structure to have the suffix 'Block'.
This was already done for getSuccessorBlocks() to distinguish getting successor
blocks from getting the full list of SILSuccessors via getSuccessors(). This
commit just makes all of the successor/predecessor code follow that naming
convention.

Some examples:

getSingleSuccessor() => getSingleSuccessorBlock().
isSuccessor() => isSuccessorBlock().
getPreds() => getPredecessorBlocks().

Really, IMO, we should consider renaming SILSuccessor to a more verbose name so
that it is clear that it is more of an internal detail of SILBasicBlock's
implementation rather than something that one should consider as apart of one's
mental model of the IR when one really wants to be thinking about predecessor
and successor blocks. But that is not what this commit is trying to change, it
is just trying to eliminate a bit of technical debt by making the naming
conventions here consistent.
2016-11-27 12:32:51 -08:00
Michael Gottesman
96837babda Merge pull request #5920 from gottesmm/vacation_gardening
Vacation gardening
2016-11-25 09:17:21 -06:00
Michael Gottesman
bf6920650c [gardening] Drop BB from all argument related code in SILBasicBlock.
Before this commit all code relating to handling arguments in SILBasicBlock had
somewhere in the name BB. This is redundant given that the class's name is
already SILBasicBlock. This commit drops those names.

Some examples:

getBBArg() => getArgument()
BBArgList => ArgumentList
bbarg_begin() => args_begin()
2016-11-25 01:14:36 -06:00
Michael Gottesman
e42bf07af4 [gardening] Always create SILBasicBlocks via SILFunction::createBasicBlock.
This eliminates all inline creation of SILBasicBlock via placement new.

There are a few reasons to do this:

1. A SILBasicBlock is always created with a parent function. This commit
formalizes this into the SILBasicBlock API by only allowing for SILFunctions to
create SILBasicBlocks. This is implemented via the type system by making all
SILBasicBlock constructors private. Since SILFunction is a friend of
SILBasicBlock, SILFunction can still create a SILBasicBlock without issue.

2. Since all SILBasicBlocks will be created in only a few functions, it becomes
very easy to determine using instruments the amount of memory being allocated
for SILBasicBlocks by simply inverting the call tree in Allocations.

With LTO+PGO, normal inlining can occur if profitable so there shouldn't be
overhead that we care about in shipping compilers.
2016-11-25 01:12:49 -06:00
practicalswift
797b80765f [gardening] Use the correct base URL (https://swift.org) in references to the Swift website
Remove all references to the old non-TLS enabled base URL (http://swift.org)
2016-11-20 17:36:03 +01:00
Michael Gottesman
e4914bd4f9 [loop-utils] We always create pre-headers now before this step of loop canonicalization, so change an early exit to an assert. 2016-02-25 15:32:16 -08:00
Adrian Prantl
40c7a1abee Separate underlying storage and location kind in SILLocation and
remove the mixed concept that was SILFileLocation.
Also add support for a third type of underlying storage that will be used
for deserialized debug lcoations from textual SIL.

NFC

<rdar://problem/22706994>
2016-02-19 11:16:48 -08:00
Zach Panzarino
e3a4147ac9 Update copyright date 2015-12-31 23:28:40 +00:00
Michael Gottesman
b235064c3f [loop-canonicalization] Change pre-header insertion so that it always occurs.
Previously, if a header had multiple predecessors or if the predecessor was not
a cond_br, we would not insert a pre-header. Now we always insert pre-headers.

This is important in terms of ensuring that ARC Sequence Opts is correct when it
inserts retains before loops.

Reviewed by Andy Trick.

rdar://23853221
SR-102
2015-12-17 19:18:45 -06:00
Andrew Trick
739b0e9c56 Reorganize SILOptimizer directories for better discoverability.
(libraries now)

It has been generally agreed that we need to do this reorg, and now
seems like the perfect time. Some major pass reorganization is in the
works.

This does not have to be the final word on the matter. The consensus
among those working on the code is that it's much better than what we
had and a better starting point for future bike shedding.

Note that the previous organization was designed to allow separate
analysis and optimization libraries. It turns out this is an
artificial distinction and not an important goal.
2015-12-11 15:14:23 -08:00