Skip to content

SIL: add the borrowed-from instruction #71176

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 10, 2024

Conversation

eeckstein
Copy link
Contributor

@eeckstein eeckstein commented Jan 26, 2024

The borrowed-from instruction declares from which enclosing values a guaranteed phi argument is borrowed from. For example:

    %2 = begin_borrow %1 : $T
    br bb1(%2 : $T, %1 : $T)
  bb1(%3: @guaranteed $T, %4 : @owned $T):
    %5 = borrowed %3 : $T from (%4 : $T)

An enclosing value is either a dominating borrow introducer of the borrowed operand (%3) or an adjacent phi-argument in the same block (%4).

So far, this information was computed dynamically by ownership utilities (e.g. gatherEnclosingValues). With the borrowed-from instructions those utilities get dramatically simpler.

The borrowed-from instruction is similar in spirit to phi-nodes in plain SSA: it "caches" information from predecessor blocks so that optimization passes can easily access this information. If an optimization pass introduces a new guaranteed phi argument it can use the BorrowedFromUpdater to generate the required borrowed-from instructions. This is similar to SSAUpdater which inserts required phi-nodes.

Beside simplifying the ownership utilities, it's also useful that the adjacent phi relations are now explicitly visible in SIL.

Another nice thing is that this change is (almost) a pure addition to SIL. This means that the existing C++ ownership utilities still work - with and without inserted borrowed-from instructions. The only exception is that they have to look through borrowed-from instructions in some places.

@eeckstein eeckstein marked this pull request as draft January 26, 2024 08:26
@eeckstein eeckstein force-pushed the borrowed-from-instruction branch 2 times, most recently from 898c0c0 to fad94c6 Compare February 12, 2024 17:22
@eeckstein eeckstein force-pushed the borrowed-from-instruction branch from fad94c6 to daa7bc7 Compare February 13, 2024 18:05
@eeckstein eeckstein force-pushed the borrowed-from-instruction branch 3 times, most recently from a2f9dc7 to 237bd40 Compare March 1, 2024 11:35
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein marked this pull request as ready for review March 1, 2024 15:21
@eeckstein eeckstein marked this pull request as draft March 1, 2024 15:21
@eeckstein
Copy link
Contributor Author

@swift-ci test

@eeckstein eeckstein force-pushed the borrowed-from-instruction branch 2 times, most recently from 2955436 to 973467f Compare March 4, 2024 09:51
@eeckstein
Copy link
Contributor Author

@swift-ci benchmark

@eeckstein eeckstein force-pushed the borrowed-from-instruction branch from 973467f to a1d9657 Compare March 25, 2024 11:16
@eeckstein eeckstein force-pushed the borrowed-from-instruction branch from a1d9657 to 9ddaf33 Compare April 9, 2024 11:58
@eeckstein eeckstein marked this pull request as ready for review April 9, 2024 11:59
@eeckstein
Copy link
Contributor Author

@swift-ci test

(``%1``).
In case of an adjacent phi, all incoming values of the adjacent phi must be
borrow introducers for the corresponding incoming value of the borrowed
operand in all predecessor blocks.
Copy link
Contributor

@meg-gupta meg-gupta Apr 9, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

While working on guaranteed phis, I think I encountered some cases where enclosing value could be adjacent or dominating. Does the borrowed from insertion pass handle when this can happen ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure what you mean exactly. Yes, the enclosing values (= the operands after from) can be an adjacent phis or dominating borrow introducers.
This is computed iteratively (until convergence) from incoming values in predcessors

It declares from which enclosing values a guaranteed phi argument is borrowed from.
Compute, update and handle borrowed-from instruction in various utilities and passes.
Also, used borrowed-from to simplify `gatherBorrowIntroducers` and `gatherEnclosingValues`.
Replace those utilities by `Value.getBorrowIntroducers` and `Value.getEnclosingValues`, which return a lazily computed Sequence of borrowed/enclosing values.
@eeckstein eeckstein force-pushed the borrowed-from-instruction branch from 9ddaf33 to e14c1d1 Compare April 10, 2024 11:39
@eeckstein
Copy link
Contributor Author

@swift-ci smoke test

@eeckstein eeckstein merged commit e871ae4 into swiftlang:main Apr 10, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants