Skip to content

Prevent unsafe view-copy replacement around mutations - #23279

Open
JacobSzwejbka wants to merge 1 commit into
mainfrom
codex/view-copy-mutation-safety
Open

JacobSzwejbka wants to merge 1 commit into
mainfrom
codex/view-copy-mutation-safety

Conversation

@JacobSzwejbka

@JacobSzwejbka JacobSzwejbka commented Sep 30, 2026 •

Copy link
Copy Markdown
Contributor

Summary

ReplaceViewCopyWithViewPass currently runs after reinplacement. A reinplaced mutation of the copied view can therefore become a mutation of the base after view_copy is changed to memory.view, even though the original copy kept the base unchanged. The inverse interaction is possible when the base is mutated while the copied view remains live.

Add a reusable is_copy_to_view_safe check that follows existing view aliases, schema-declared mutation aliases, and reinplace allocation-sharing annotations. It rejects a substitution only when a write to either prospective storage family precedes a read from the other family. Mutations after the other value's last read remain eligible for view replacement.

The helper accepts the set of existing aliasing operators so other copy-to-view passes, including slice-copy lowering, can share the same safety analysis.

Authored with Codex.

Test plan

  • Added regression coverage for mutation through a view while its base remains live.
  • Added regression coverage for mutation of the base while the view remains live.
  • Added coverage showing both directions still replace the copy when the other value is dead.
  • Ran the focused test_remove_view_copy cases and the existing test_replace_view_copy_with_view_pass case.
  • Ran Black, Flake8, compileall, and git diff --check on the changed Python files.

Replace view_copy with a storage alias only when writes to the prospective
base and view storage cannot be observed through the other value. Preserve
the optimization when the other value is already dead.

Authored with Codex.
@pytorch-bot

pytorch-bot Bot commented Sep 30, 2026 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/23279

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 Unclassified Failure

As of commit 8674068 with merge base 21f770c (image):

UNCLASSIFIED FAILURE - DrCI could not classify the following job because the workflow did not run on the merge base. The failure may be pre-existing on trunk or introduced by this PR:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 30, 2026
@github-actions

Copy link
Copy Markdown

This PR needs a release notes: label

If your change should be included in the release notes (i.e. would users of this library care about this change?), please use a label starting with release notes:. This helps us keep track and include your important work in the next release notes.

To add a label, you can comment to pytorchbot, for example
@pytorchbot label "release notes: none"

For more information, see
https://github.com/pytorch/pytorch/wiki/PyTorch-AutoLabel-Bot#why-categorize-for-release-notes-and-how-does-it-work.

@metascroy metascroy left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM!

This branch was successfully deployed

1 active deployment
cadence — 86740686 Deployed Sep 30, 2026 by JacobSzwejbka via hifi-op-test / hifi4 #30599
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants