Commit Graph

1615 Commits

Author SHA1 Message Date
Meghana Gupta
ecb5d65d6d Fix lifetime of intermediate phis created by RLE
We were adjusting the lifetime of the final phi created by the
SSAUpdater. The intermediate phi's lifetime needs to be adjusted as
well.
2021-02-02 21:54:09 -08:00
Meghana Gupta
f5d47c3ba4 Fix debug location while creating instructions in RLE
The insert point may not have RegularKind debug location.
So, use an auto-generated RegularKind debug location.
2021-02-02 21:40:48 -08:00
Michael Gottesman
b16f340595 Merge pull request #35684 from eeckstein/fix-find-jpds
SIL: fix problems in findJointPostDominatingSet and some refactoring
2021-02-02 14:18:22 -08:00
Andrew Trick
3cbf31c63b Merge pull request #35677 from atrick/add-phioperand
Add PhiOperand and PhiValue types.
2021-02-02 09:04:03 -08:00
Erik Eckstein
d33ea9f350 SIL: remove the JointPostDominanceSetComputer helper struct.
Instead make `findJointPostDominatingSet` a stand-alone function.
There is no need to keep the temporary SmallVector alive across multiple calls of findJointPostDominatingSet for the purpose of re-using malloc'ed memory. The worklist usually contains way less elements than its small size.
2021-02-02 10:20:35 +01:00
Andrew Trick
4324432c55 Merge pull request #35676 from atrick/fix-copyprop-reborrow
Fix CopyPropagation's reborrow handling.
2021-02-01 23:23:17 -08:00
Andrew Trick
0183c0d612 Merge pull request #35671 from atrick/ossa-borrow-utils
Centralize and document low-level OSSA utilities
2021-02-01 21:33:53 -08:00
Andrew Trick
17bcf54f2c Fix CopyPropagation's reborrow handling.
Call the new BorrowingOperand::visitExtendedScopeEndingUses() API in
two places where CanonicalizeOSSA was cutting borrowed lifetimes
short.

Fixes rdar://73802572 ([OSSA] CopyPropagation Crash (or miscompile):
assert(succBB->getSinglePredecessorBlock() == predBB && "value is
live-out on another predBB successor: critical edge?");)

This could actually have caused use-after-free miscompiles.

Example provided by Meghana:

    bb0:
      %alloc = alloc_ref $C
      %copy = copy_value %alloc : $C
      %borrow = begin_borrow %alloc : $C
      cond_br undef, bb1, bb2
    bb1:
      br bb3(%copy : $C, %borrow: $C)
    bb2:
      br bb3(%copy : $C, %borrow: $C)
    bb3(%phi : @owned $C, %borrowphi : @guaranteed $C):
      // %alloc must be kept alive until this end_borrow.
      end_borrow %borrowphi : $C
      // This destroy_value must not be hoisted.
      destroy_value %phi : $C
      destroy_value %alloc : $C
      %99 = tuple ()
      return %99 : $()
2021-02-01 20:19:46 -08:00
Andrew Trick
b3e76ae8e5 Add PhiOperand and PhiValue types.
This is necessitated by the SILArgument representation. It has the
tragic property that adding unrelated phis invalidates existing
phis. Therefore, the optimizer can't do book-keeping of phi values by
refering directly to SILValues or Operands. Instead, it must only
refer to SILBasicBlocks and argument indices.
2021-02-01 20:15:05 -08:00
Andrew Trick
2095c0cf04 OSSA: Use a public explicit constructor for BorrowedValue. 2021-02-01 18:03:24 -08:00
Andrew Trick
caefb9afaa Centralize and document low-level OSSA utilities
...for handling borrow scopes:

- find[Extended]TransitiveGuaranteedUses

- BorrowingOperand::visit[Extended]ScopeEndingUses

- BorrowedValue BorrowingOperand::getBorrowIntroducingUserResult()

And document the logic.

Mostly NFC in this commit, but more RAUW cases should be correctly
handled now.

Particularly, ensure that we can cleanly handle all manner of
reborrows. This is necessary to ensure both CanonicalizeOSSA and
replace-all-uses higher-level utilities handle all cases.

This generalizes some of the logic in CanonicalizeOSSA so it can be
shared by other high-level OSSA utilities.

These utilities extend the fundamental BorrowingOperand and
BorrowedValue functionality that has been designed recently. It builds
on and replaces a mix of piece-meal functionality that was needed
during bootstrapping. These APIs are now consistent with the more
recently designed code. It should be obvious what they mean and how to
use them, should be very hard to use them incorrectly, and should be
as efficient as possible, since they're fundamental to other
higher-level utilities.

Most importantly, there were several very subtle correctness issues
that were not handled cleanly in a centralized way. There are now a
mix of higher-level utilities trying to use this underlying
functionality, but it was hard to tell if all those higher-level
utilities were covering all the subtle cases:

- checking for PointerEscapes everywhere we need to

- the differences between uses of borrow introducers and other
  guaranteed values

- handling the uses of nested borrow scopes

- transitively handling reborrows

- the difference between nested and top-level reborrows

- forwarding destructures (which can cause exponential explosion)

In short, I was fundamentally confused and getting things wrong before
designing these utilities.
2021-02-01 18:03:24 -08:00
Adrian Prantl
a9f05c8c30 Merge remote-tracking branch 'origin/main' into rebranch
Conflicts:
	lib/IRGen/IRGenDebugInfo.cpp
2021-01-29 16:17:10 -08:00
Erik Eckstein
462e58d3cb SILLocation: a big refactoring and reducing its size from 3 to 2 words
My goal was to reduce the size of SILLocation. It now contains only of a storage union, which is basically a pointer and a bitfield containing the Kind, StorageKind and flags. By far, most locations are only single pointers to an AST node. For the few cases where more data needs to be stored, this data is allocated separately: with the SILModule's bump pointer allocator.

While working on this, I couldn't resist to do a major refactoring to simplify the code:

* removed unused stuff
* The term "DebugLoc" was used for 3 completely different things:
    - for `struct SILLocation::DebugLoc` -> renamed it to `FilePosition`
    - for `hasDebugLoc()`/`getDebugSourceLoc()` -> renamed it to `hasASTNodeForDebugging()`/`getSourceLocForDebugging()`
    - for `class SILDebugLocation` -> kept it as it is (though, `SILScopedLocation` would be a better name, IMO)
* made SILLocation more "functional", i.e. replaced some setters with corresponding constructors
* replaced the hand-written bitfield `KindData` with C bitfields
* updated and improved comments
2021-01-29 20:28:21 +01:00
swift-ci
c47946ab05 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-28 19:12:47 -08:00
Michael Gottesman
dbd66d8e2d [ownership] Change BorrowingOperand to no longer be convertable to Operand * and provide a conversion to bool instead.
A few notes:

1. We already have provide an operator* that does this and even better makes the
   conversion explicit in the source.

2. The bool operator checks both that kind is not invalid and that the operand
   is non-null. This ensures we can use an `if` to properly check for a valid
   BorrowingOperand.
2021-01-28 12:10:16 -08:00
Michael Gottesman
2fad943df0 [sil-combine] Update convert_function canonicalization for ownership.
Some notes:

1. I moved the identity round-trip case to InstSimplify since that is where
   optimizations like that are.

2. I did not update in this commit the code that eliminates convert_function
   when it is only destroyed. In a subsequent commit I am going to implement
   that in a general way and apply it to all forwarding instructions.

3. I implemented eliminating convert_function with ownership only uses in a
   utility so that I can reuse it for other similar optimizations in SILCombine.
2021-01-28 12:10:16 -08:00
Michael Gottesman
9f952714d3 [ownership] Allow for OwnershipRAUWHelper to RAUW in non-ossa mode to cleanup code.
This just makes it easier to write code that supports both
ownership/non-ownership code.
2021-01-28 12:10:16 -08:00
swift-ci
2af275246e Merge remote-tracking branch 'origin/main' into rebranch 2021-01-27 14:12:39 -08:00
Meghana Gupta
c7b6ec8e92 Merge pull request #35609 from meg-gupta/ossabugs3
Minor bug fixes in SILCombine, Canonicalization and LoadBorrowImmutabilityChecker
2021-01-27 13:55:24 -08:00
swift-ci
2f5c3d3e51 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-27 11:12:39 -08:00
Erik Eckstein
011358edd6 SIL: let SingleValueInstruction only inherit from a single SILNode.
This removes the ambiguity when casting from a SingleValueInstruction to SILNode, which makes the code simpler. E.g. the "isRepresentativeSILNode" logic is not needed anymore.
Also, it reduces the size of the most used instruction class - SingleValueInstruction - by one pointer.

Conceptually, SILInstruction is still a SILNode. But implementation-wise SILNode is not a base class of SILInstruction anymore.
Only the two sub-classes of SILInstruction - SingleValueInstruction and NonSingleValueInstruction - inherit from SILNode. SingleValueInstruction's SILNode is embedded into a ValueBase and its relative offset in the class is the same as in NonSingleValueInstruction (see SILNodeOffsetChecker).
This makes it possible to cast from a SILInstruction to a SILNode without knowing which SILInstruction sub-class it is.
Casting to SILNode cannot be done implicitly, but only with an LLVM `cast` or with SILInstruction::asSILNode(). But this is a rare case anyway.
2021-01-27 16:40:15 +01:00
Erik Eckstein
e98c527319 SILOptimizer: remove the unused findApplyFromDevirtualizedResult utility function 2021-01-27 16:40:14 +01:00
swift-ci
e349794517 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-27 04:12:41 -08:00
Erik Eckstein
f48191966c SILOptimizer: use BasicBlockSet instead of SmallPtrSet in various transformations.
It reduces compile time.
2021-01-27 10:31:17 +01:00
Erik Eckstein
ddd0f4d3a9 SIL: replace SmallPtrSet<SILBasicBlock *> with BasicBlockSet in LinearLifetimeChecker
This makes it unnecessary to share a single instance of visited blocks across multiple calls the the checker.
2021-01-27 10:31:17 +01:00
swift-ci
2d6dde9b00 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-27 00:32:36 -08:00
Meghana Gupta
78a6213080 Clear context in ownership rauw when we cannot fix ownership for rauw.
If not, subsequent unrelated call of OwnershipRAUWHelper::perform will have stale
entries for transitiveBorrowedUses etc
2021-01-26 20:34:24 -08:00
Meghana Gupta
3e330abf44 Fix use-after-free in ownership rauw
Instead of saving BorrowingOperand on the context save the SILBasicBlock
and index of the terminator operand.
This avoids the use-after-free in eliminateReborrowsOfRecursiveBorrows.
Previously, eliminateReborrowsOfRecursiveBorrows called helper
insertOwnedBaseValueAlongBranchEdge, which can delete a branch
instruction (reborrow) that could have been cached in
recursiveReborrows.
2021-01-26 20:21:16 -08:00
Meghana Gupta
ddf8b459ba Fix use-after-free of deleted debug instructions in Canonicalize 2021-01-26 19:56:30 -08:00
swift-ci
954dda5be8 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-26 17:52:28 -08:00
Michael Gottesman
fdbe872889 Merge pull request #35601 from gottesmm/ossa-sil-combine-3
[sil-combine] More ownership updates
2021-01-26 17:43:26 -08:00
swift-ci
6137579b93 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-26 10:12:52 -08:00
Eric Miotto
8e7f9c9cbd Revert "SIL: let SingleValueInstruction only inherit from a single SILNode." 2021-01-26 10:02:24 -08:00
Michael Gottesman
de58e7997b [ownership] When RAUWing an unowned value with a owned/guaranteed value only insert unchecked_ownership_conversion for terminator uses.
Already, we allow for owned/guaranteed values to be passed to values with
unowned ownership. This just cleans up the SIL a bit.

That being said, we still need unchecked_ownership_conversion for the terminator
case since any owned/guaranteed's value (ignoreing function parameters) will end
before the return meaning that we need some way of escaping it.
2021-01-25 18:44:00 -08:00
Michael Gottesman
44fdc746ea [ownership] Allow the user of OwnershipRAUWHelper to insert forwarding transforms of the new value at oldValue in between checking and RAUWing. 2021-01-25 17:18:31 -08:00
swift-ci
a45f6a4518 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-25 03:32:36 -08:00
Erik Eckstein
ff1991740a SIL: let SingleValueInstruction only inherit from a single SILNode.
This removes the ambiguity when casting from a SingleValueInstruction to SILNode, which makes the code simpler. E.g. the "isRepresentativeSILNode" logic is not needed anymore.
Also, it reduces the size of the most used instruction class - SingleValueInstruction - by one pointer.

Conceptually, SILInstruction is still a SILNode. But implementation-wise SILNode is not a base class of SILInstruction anymore.
Only the two sub-classes of SILInstruction - SingleValueInstruction and NonSingleValueInstruction - inherit from SILNode. SingleValueInstruction's SILNode is embedded into a ValueBase and its relative offset in the class is the same as in NonSingleValueInstruction (see SILNodeOffsetChecker).
This makes it possible to cast from a SILInstruction to a SILNode without knowing which SILInstruction sub-class it is.
Casting to SILNode cannot be done implicitly, but only with an LLVM `cast` or with SILInstruction::asSILNode(). But this is a rare case anyway.
2021-01-25 09:30:04 +01:00
Erik Eckstein
5ad332f6cd SILOptimizer: remove the unused findApplyFromDevirtualizedResult utility function 2021-01-25 09:30:04 +01:00
swift-ci
56123ac984 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-23 20:52:41 -08:00
swift-ci
f7f658cce7 Merge pull request #35571 from atrick/fix-rauw-borrowuse 2021-01-23 20:43:18 -08:00
Andrew Trick
a2fac95f9f OSSA ownership optimization RAUW utility fixes.
Verify that the OwnershipRAUWUtility always preserves the original
borrow scope by exhaustively switching over OperandOwnership.

And related cleanup.
2021-01-23 18:15:58 -08:00
swift-ci
d303fac16e Merge remote-tracking branch 'origin/main' into rebranch 2021-01-22 19:12:50 -08:00
Michael Gottesman
1e039cce38 Merge pull request #35563 from gottesmm/pr-cd7c9e97e46a0141e37f10fbfb74c094e0576ff3-followups
[ownership] cd7c9e97e4 followups
2021-01-22 19:05:12 -08:00
Michael Gottesman
f39df1d73b [sil-combine] Address comments from post-commit review of cd7c9e97e4. 2021-01-22 14:26:59 -08:00
swift-ci
a162b3936c Merge remote-tracking branch 'origin/main' into rebranch 2021-01-22 13:39:04 -08:00
Andrew Trick
1f1123f6c0 Merge pull request #35553 from atrick/fix-jumpthread-classmethod
Remove CheckedCastBrJumpThreading::Edit::modifyCFGForUnknownPreds
2021-01-22 08:43:01 -08:00
Andrew Trick
6eb574b646 Remove CheckedCastBrJumpThreading::Edit::modifyCFGForUnknownPreds
Remove some complexity from the simplify-cfg pass. This code was
obviously incorrect, untested, and is not needed for performance.
2021-01-22 01:40:33 -08:00
swift-ci
5dec637c83 Merge remote-tracking branch 'origin/main' into rebranch 2021-01-21 23:44:22 -08:00
eeckstein
a0884baa3c Merge pull request #35521 from eeckstein/sil-bitfields
SIL: add a utility which can manage per-block bitfields and flags efficiently.
2021-01-22 08:39:13 +01:00
swift-ci
81d5797f4e Merge remote-tracking branch 'origin/main' into rebranch 2021-01-21 20:13:27 -08:00