Skip to content

PR #46314: [XLA] Fix asymmetric copy elision in conditional branches - #124167

Merged
copybara-service[bot] merged 1 commit into
masterfrom
exported_pr_955182971
Jul 29, 2026
Merged

copybara-service[bot] merged 1 commit into
masterfrom
exported_pr_955182971

Conversation

@copybara-service

@copybara-service copybara-service Bot commented Jul 28, 2026 •

Copy link
Copy Markdown

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):

%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 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

@copybara-service
copybara-service Bot force-pushed the exported_pr_955182971 branch 6 times, most recently from 97f0a30 to 0888445 Compare July 28, 2026 14:07
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
copybara-service Bot force-pushed the exported_pr_955182971 branch from 0888445 to ee1a2b6 Compare July 29, 2026 09:37
@copybara-service copybara-service Bot closed this Jul 29, 2026
@copybara-service
copybara-service Bot merged commit ee1a2b6 into master Jul 29, 2026
3 checks passed
@copybara-service
copybara-service Bot deleted the exported_pr_955182971 branch July 29, 2026 09:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment