Repository navigation
PR #46314: [XLA] Fix asymmetric copy elision in conditional branches - #124167
Merged
Merged
Conversation
copybara-service
Bot
force-pushed
the
exported_pr_955182971
branch
6 times, most recently
from
July 28, 2026 14:07
97f0a30 to
0888445
Compare
Imported from GitHub PR openxla/xla#46314 Copy elision around kConditional depends on which position a branch occupies: a branch that forwards its operand unchanged loses its aliasing (one copy in each branch plus one at the caller) unless it is branch 0. HloOrdering artificially orders branch computations so that buffers can be shared across branches, but the direction-dependent flag prevents sharing for branches in position > 0. Before (passthrough in branch 1): ``` %copy.15 = f32[512,512] copy(%get-tuple-element.94) ROOT %tuple = tuple(%loop_add_fusion, %copy.15, ...) ``` After: carry is forwarded directly -- 0 copies. Fix: gate the exclusive-branch cases on actual escape analysis (has_escaped_use_in_conditional) instead of the asymmetric flag. Teach TryElideCopy to elide a copy that connects two values already sharing a buffer when those uses are live-range before every value ordered between the copy's source and destination. Adds a parameterized test (CopyInsertionCondOrderTest) verifying zero copies in both branch orders, and a while-body test (WhileBodyWithPassthroughCond) for the scan + cond pattern. Closes jax-ml/jax#5986 (5.3 years - cond inside scan is slow) Closes jax-ml/jax#16661 (3 years - 1000x slowdown from branch order) Closes jax-ml/jax#38145 (per-iteration scan carry copies) Benchmark from jax-ml/jax#38145: 3 extra copies per iteration x 25K iterations = 75K extra kernel launches, 2.6x slowdown (251ms -> 651ms) on L40S. Fix restores the non-copying baseline. Copybara import of the project: -- d5bf8a4b1b365c659638881e9dd70e65621e64bc by SakshamKapoor2911 <[email protected]>: Fix asymmetric copy elision in conditional branches Gate UsesBeforeValueDefinition on has_escaped_use_in_conditional instead of the direction-dependent has_use_in_exclusive_branches. This makes copy elision symmetric regardless of which branch position a passthrough occupies. The NestedWhileAndConditional3 test now produces 5 copies instead of 4 because the symmetric gate is more conservative for defs escaping a conditional inside a while loop. The 5th copy does not appear in real CPU/GPU pipelines because ConditionalCanonicalizer runs first. -- e59ef1473b93e25564082b0014590eb74981db9e by SakshamKapoor2911 <[email protected]>: Support copy elision for same-buffer non-adjacent copies When a copy connects two values that already share a buffer but are non-adjacent in the list (because values from other conditional branches are ordered between them), removing the copy does not merge buffers -- it only transfers uses. This is safe if the uses are live-range before every intervening value. RemoveCopyValue now accepts an optional operand_node parameter for the case where the operand is not adjacent to the copy value node. -- 1f70f59679375a915b697505888ddc05f6fec04b by SakshamKapoor2911 <[email protected]>: Add parametrized regression test for conditional copy elision CopyInsertionCondOrderTest verifies that a two-branch conditional with a passthrough branch and an in-place-update branch compiles to zero copies, regardless of which position the passthrough occupies and with or without region-based live-range analysis. -- 40e0b32a4cda2f696a85510804d2f2e8b22bca4b by SakshamKapoor2911 <[email protected]>: Apply clang-format to changed files -- 3ac0e59905df3cd57b4baff633a5e346251d92a5 by SakshamKapoor2911 <[email protected]>: Add while-body with passthrough cond regression test WhileBodyWithPassthroughCond directly covers the lax.scan + lax.cond pattern from jax-ml/jax#38145 / #5986 at the HLO level, verifying that the while-body does not introduce extra copies beyond what while-loop semantics require. -- 6830a3d2189e2607a675e7f4664cbf3df732d68f by SakshamKapoor2911 <[email protected]>: Run git clang-format against upstream/main to fix include ordering gmock/gtest headers were placed before standard library includes, violating Google C++ style. -- 3c8e6a72beaf7c7a8c694acd607d8614aa2d99cc by SakshamKapoor2911 <[email protected]>: Fix clang-tidy warnings in copy_insertion_test.cc Replace deprecated TF_ASSERT_OK_AND_ASSIGN with ASSERT_OK_AND_ASSIGN. Add missing #include <tuple> for std::tuple usage. These warnings are only reported by clang-tidy-cuda for the new test code (lines 5139, 5154, 5218) that were added in this PR. Signed-off-by: SakshamKapoor2911 <[email protected]> Merging this change closes #46314 PiperOrigin-RevId: 955757644
copybara-service
Bot
force-pushed
the
exported_pr_955182971
branch
from
July 29, 2026 09:37
0888445 to
ee1a2b6
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
PR #46314: [XLA] Fix asymmetric copy elision in conditional branches
Imported from GitHub PR openxla/xla#46314
Copy elision around kConditional depends on which position a branch occupies: a branch that forwards its operand unchanged loses its aliasing (one copy in each branch plus one at the caller) unless it is branch 0. HloOrdering artificially orders branch computations so that buffers can be shared across branches, but the direction-dependent flag prevents sharing for branches in position > 0.
Before (passthrough in branch 1):
After: carry is forwarded directly -- 0 copies.
Fix: gate the exclusive-branch cases on actual escape analysis (has_escaped_use_in_conditional) instead of the asymmetric flag. Teach TryElideCopy to elide a copy that connects two values already sharing a buffer when those uses are live-range before every value ordered between the copy's source and destination.
Adds a parameterized test (CopyInsertionCondOrderTest) verifying zero copies in both branch orders, and a while-body test (WhileBodyWithPassthroughCond) for the scan + cond pattern.
Closes jax-ml/jax#5986 (5.3 years - cond inside scan is slow)
Closes jax-ml/jax#16661 (3 years - 1000x slowdown from branch order)
Closes jax-ml/jax#38145 (per-iteration scan carry copies)
Benchmark from jax-ml/jax#38145: 3 extra copies per iteration x 25K iterations = 75K extra kernel launches, 2.6x slowdown (251ms -> 651ms) on L40S. Fix restores the non-copying baseline.
Copybara import of the project:
--
d5bf8a4b1b365c659638881e9dd70e65621e64bc by SakshamKapoor2911 [email protected]:
Fix asymmetric copy elision in conditional branches
Gate UsesBeforeValueDefinition on has_escaped_use_in_conditional
instead of the direction-dependent has_use_in_exclusive_branches.
This makes copy elision symmetric regardless of which branch position
a passthrough occupies.
The NestedWhileAndConditional3 test now produces 5 copies instead of 4
because the symmetric gate is more conservative for defs escaping a
conditional inside a while loop. The 5th copy does not appear in real
CPU/GPU pipelines because ConditionalCanonicalizer runs first.
--
e59ef1473b93e25564082b0014590eb74981db9e by SakshamKapoor2911 [email protected]:
Support copy elision for same-buffer non-adjacent copies
When a copy connects two values that already share a buffer but are
non-adjacent in the list (because values from other conditional branches
are ordered between them), removing the copy does not merge buffers --
it only transfers uses. This is safe if the uses are live-range before
every intervening value.
RemoveCopyValue now accepts an optional operand_node parameter for
the case where the operand is not adjacent to the copy value node.
--
1f70f59679375a915b697505888ddc05f6fec04b by SakshamKapoor2911 [email protected]:
Add parametrized regression test for conditional copy elision
CopyInsertionCondOrderTest verifies that a two-branch conditional
with a passthrough branch and an in-place-update branch compiles to
zero copies, regardless of which position the passthrough occupies
and with or without region-based live-range analysis.
--
40e0b32a4cda2f696a85510804d2f2e8b22bca4b by SakshamKapoor2911 [email protected]:
Apply clang-format to changed files
--
3ac0e59905df3cd57b4baff633a5e346251d92a5 by SakshamKapoor2911 [email protected]:
Add while-body with passthrough cond regression test
WhileBodyWithPassthroughCond directly covers the lax.scan + lax.cond
pattern from jax-ml/jax#38145 / #5986 at the HLO level, verifying
that the while-body does not introduce extra copies beyond what
while-loop semantics require.
--
6830a3d2189e2607a675e7f4664cbf3df732d68f by SakshamKapoor2911 [email protected]:
Run git clang-format against upstream/main to fix include ordering
gmock/gtest headers were placed before standard library includes,
violating Google C++ style.
--
3c8e6a72beaf7c7a8c694acd607d8614aa2d99cc by SakshamKapoor2911 [email protected]:
Fix clang-tidy warnings in copy_insertion_test.cc
Replace deprecated TF_ASSERT_OK_AND_ASSIGN with ASSERT_OK_AND_ASSIGN.
Add missing #include for std::tuple usage.
These warnings are only reported by clang-tidy-cuda for the new
test code (lines 5139, 5154, 5218) that were added in this PR.
Signed-off-by: SakshamKapoor2911 [email protected]
Merging this change closes #46314
FUTURE_COPYBARA_INTEGRATE_REVIEW=openxla/xla#46314 from SakshamKapoor2911:fix/conditional-copy-elision 3c8e6a72beaf7c7a8c694acd607d8614aa2d99cc