Skip to content

[ownership] Add a new class BorrowScopeIntroducer that enables code t… #27129

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

Conversation

gottesmm
Copy link
Contributor

…o work abstractly with values that introduce a new borrow scope.

The idea is that this can be used to work with things like load_borrow,
begin_borrow, SILFunctionArgument, BeginApply (in the future) and the like in a
generic way using exhaustive switches to make sure this code stays up to date.

I refactored code in SemanticARCOpts and some utilities in OwnershipUtils.cpp to
use these new APIs. The code looks a lot nicer and should be quite easy to
expand to handle new borrow introducers (e.x.: end_apply).

Should be a pure refactor so: NFCI.

@gottesmm gottesmm requested a review from atrick September 11, 2019 22:26
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test

@gottesmm gottesmm force-pushed the pr-8fee81e00841b8044b3368fabdcd84dd5fbd5f68 branch from d51b6a6 to eb7b5da Compare September 12, 2019 06:09
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test

@gottesmm
Copy link
Contributor Author

@swift-ci smoke test and merge

2 similar comments
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test and merge

@gottesmm
Copy link
Contributor Author

@swift-ci smoke test and merge

Copy link
Contributor

@atrick atrick left a comment

Choose a reason for hiding this comment

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

noice!

llvm_unreachable("Covered switch isn't covered?!");
}

bool BorrowScopeIntroducer::visitScopeEndingInstructions(
Copy link
Contributor

Choose a reason for hiding this comment

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

I've been making a lot of use of visitor patterns that return false as soon as a visitor returns false. In general, this allows the visitor to bail out, early exit, and return that information to the client. In that pattern, when no visitors are called, true must be returned to indicate that everything is handled. I think returning false here to mean something specific in this context does not respect the more general expectation. It would be better to either add hasLocalScope method or return a more descriptive enum result to indicate that function arguments are special here.

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 am fine with returning a more descriptive enum.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@atrick was thinking about this. The key thing to note here is that by construction we know that we will always be able to visit everything. So perhaps the way to make this clearer is as you said do the hasLocalScope, put in an assert that we only call this if we have a local scope.

Copy link
Contributor

Choose a reason for hiding this comment

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

That sounds great.

return os;
}

bool BorrowScopeIntroducer::areInstructionsWithinScope(
Copy link
Contributor

Choose a reason for hiding this comment

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

I'll be happy when BranchPropagatedUser can go away, and valueHasLinearLifetime is a object that holds its own state. Until then, this is fine.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. I am with you. I just wanted to abstract this a way since it is a general operation we have been performing. I can abstract it out.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@atrick for some cases I don't think it will go away, but in a subsequent commit, I have a small helper that in the case where we /know/ that we are never going to pass a cond_br we can just convert the ArrayRef<SILInstruction *> to ArrayRef. The reason why this is safe is BranchPropagatedUser is just a pointer int pair internally and if it is not storing a cond_br, then it is exactly bitwise the same as the underlying SILInstruction *. I can make the helper safe to use by performing a dynamic check if we have SILInstruction and a compile time check if we have an ArrayRef.

Copy link
Contributor

Choose a reason for hiding this comment

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

That fact that cond_br still has arguments is dumb but it's unrelated to this PR. I'm just anxious for that to go away so code like this can be vastly simpler.

Copy link
Contributor Author

@gottesmm gottesmm left a comment

Choose a reason for hiding this comment

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

Just responding to your comments here.

FYI: In a forthcoming commit, I am going to rename this class to BorrowScopeIntroducingValue. The reason why is that we also need to consider operands as borrow scope introducer concepts since we are allowing for the use of an instruction to introduce a borrow scope. Consider begin_apply. So I need to think about how to layer these with an isa relationship.

return os;
}

bool BorrowScopeIntroducer::areInstructionsWithinScope(
Copy link
Contributor Author

Choose a reason for hiding this comment

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

yes. I am with you. I just wanted to abstract this a way since it is a general operation we have been performing. I can abstract it out.

llvm_unreachable("Covered switch isn't covered?!");
}

bool BorrowScopeIntroducer::visitScopeEndingInstructions(
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 am fine with returning a more descriptive enum.

…code to work abstractly with values that introduce a new borrow scope.

The idea is that this can be used to work with things like load_borrow,
begin_borrow, SILFunctionArgument, results of begin_apply(in the future) and the like in a
generic way using exhaustive switches to make sure this code stays up to date.

I refactored code in SemanticARCOpts and some utilities in OwnershipUtils.cpp to
use these new APIs. The code looks a lot nicer and should be quite easy to
expand to handle new borrow introducers (e.x.: end_apply).
@gottesmm gottesmm force-pushed the pr-8fee81e00841b8044b3368fabdcd84dd5fbd5f68 branch from eb7b5da to bb4df03 Compare September 16, 2019 16:34
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test and merge

1 similar comment
@gottesmm
Copy link
Contributor Author

@swift-ci smoke test and merge

@swift-ci swift-ci merged commit 520527d into swiftlang:master Sep 16, 2019
@gottesmm gottesmm deleted the pr-8fee81e00841b8044b3368fabdcd84dd5fbd5f68 branch September 16, 2019 17:45
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.

3 participants