Skip to content

Fix CSE isEqual and HashVisitor for escaped values #63791

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 1 commit into from
Feb 23, 2023

Conversation

meg-gupta
Copy link
Contributor

@meg-gupta meg-gupta commented Feb 20, 2023

CSE would look through ownership instructions while matching operands, this isn't valid in the case of escaped values. CSE relies on OSSA RAUW for replacing the redundant instruction which extends lifetimes when necessary for replacement. For escaped values, OSSA RAUW does not extend the base value lifetime. For all such instructions, this PR changes CSE to not look through ownership instructions.

This PR also fixes non-determinism in CSE for ref_to_unmanaged instruction, before the changes in the PR, llvm::DenseMapInfo<SimpleValue>::isEqual was looking through ownership instructions while matching operands, but HashVisitor::visit_ref_to_unmanaged did not.

Fixes rdar://105627656

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci benchmark

@meg-gupta
Copy link
Contributor Author

@swift-ci Please Apple Silicon benchmark

bool isIdenticalTo(
const SILInstruction *RHS,
std::function<bool(const Operand *, const Operand *)> opEqual) const {
// Quick check if both instructions have the same kind, number of operands,
Copy link
Contributor Author

@meg-gupta meg-gupta Feb 20, 2023

Choose a reason for hiding this comment

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

TODO: Refactor code common to bool isIdenticalTo(const SILInstruction *RHS, std::function<bool(SILValue, SILValue)> opEqual) const

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci benchmark

auto opOwnership = op->getOperandOwnership();

if (opOwnership == OperandOwnership::PointerEscape ||
opOwnership == OperandOwnership::BitwiseEscape ||
Copy link
Contributor

Choose a reason for hiding this comment

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

I think we need a comment here because we don't normally consider BitwiseEscape to mean we can't optimize the lifetime. This looks like the usual check for "can we optimize this lifetime" but I think it's actually different. Here you're trying to catch all the cases that RAUW doesn't handle correctly, right? Can we just check exactly the same condition that RAUW checks and comment that they need to be in-sync?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Unfortunately, there isn't any specific function that OSSA RAUW checks for. It doesn't bailout for none, unowned values, it replaces all uses with the replacement value without checking for lifetime.

@meg-gupta
Copy link
Contributor Author

@swift-ci benchmark

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta meg-gupta force-pushed the csebug branch 2 times, most recently from 608fc81 to 5d5773e Compare February 21, 2023 18:41
@meg-gupta
Copy link
Contributor Author

@swift-ci benchmark

@meg-gupta
Copy link
Contributor Author

@swift-ci apple silicon benchmark

@meg-gupta meg-gupta changed the title Fix CSE isEqual and HashVisitor for non-guaranteed, non-owned values Fix CSE isEqual and HashVisitor for escaped values Feb 21, 2023
@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test

@meg-gupta
Copy link
Contributor Author

@swift-ci test

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.

Awesome! Thanks.

@meg-gupta
Copy link
Contributor Author

@swift-ci test linux platform

1 similar comment
@meg-gupta
Copy link
Contributor Author

@swift-ci test linux platform

CSE relies on OSSA RAUW for lifetime extension when replacing a redundant instruction.
OSSA RAUW however does not handle lifetime extension for escaped base values.
Values escape from ownership via pointer escape, bitwise escape, forwarding unowned operations
and have none or unowned ownership. For all such values do not look through ownership instructions
while determining equality. It is possible to CSE such values with equivalent operands, because the
operand use guarantees lifetime of the base operand.
@meg-gupta
Copy link
Contributor Author

@swift-ci test and merge

@swift-ci swift-ci merged commit 4ab5024 into swiftlang:main Feb 23, 2023
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