Skip to content

Add rewrite broadcasts for fuse_reduce - #5146

Open
pfultz2 wants to merge 4 commits into
developfrom
fuse-reduce-rewrite-broadcasts
Open

Add rewrite broadcasts for fuse_reduce#5146
pfultz2 wants to merge 4 commits into
developfrom
fuse-reduce-rewrite-broadcasts

Conversation

@pfultz2

@pfultz2 pfultz2 commented Aug 17, 2026

Copy link
Copy Markdown
Collaborator

Motivation

This PR introduces a reusable “rewrite broadcasts onto pointwise inputs” transformation and wires it into the fuse_reduce pipeline (and associated tests) to enable additional fusion opportunities when broadcasts sit between pointwise ops and reductions.

Technical Details

Changelog Category

Add a CHANGELOG.md entry for any option other than Not Applicable

    • Added: New functionality.
    • Changed: Changes to existing functionality.
    • Removed: Functionality or support that has been removed. (Compared to a previous release)
    • Optimized: Component performance that has been optimized or improved.
    • Resolved Issues: Known issues from a previous version that have been resolved.
    • Not Applicable: This PR is not to be included in the changelog.

Follow the LLVM AI Tool Use Policy for contributions using AI.

@pfultz2
pfultz2 requested a review from causten as a code owner August 17, 2026 22:37
@pfultz2
pfultz2 requested review from bdevorem, eddieliao and shivadbhavsar and a lite review from Copilot August 17, 2026 22:37

Copilot AI 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.

Pull request overview

This PR introduces a reusable “rewrite broadcasts onto pointwise inputs” transformation and wires it into the fuse_reduce pipeline (and associated tests) to enable additional fusion opportunities when broadcasts sit between pointwise ops and reductions.

Changes:

  • Added a shared rewrite_broadcasts(module_pass_manager&, op_name) utility (moved out of fuse_pointwise) and integrated it into both fuse_pointwise and fuse_reduce.
  • Extended fuse_reduce to optionally rewrite/hoist broadcasts to unlock fusions, and enabled this path in fuse_pointwise_reduce.
  • Added new unit tests covering fusion cases enabled by broadcast rewriting/hoisting.

Reviewed changes

Copilot reviewed 9 out of 9 changed files in this pull request and generated 1 comment.

Show a summary per file
File Description
src/rewrite_broadcasts.cpp New shared implementation of broadcast-rewrite matcher and cleanup passes.
src/include/migraphx/rewrite_broadcasts.hpp Public declaration for the new rewrite helper.
src/fuse_pointwise.cpp Replaces the local broadcast rewrite logic with the shared helper.
src/include/migraphx/fuse_reduce.hpp Adds enable_rewrite_broadcasts flag to control the new behavior.
src/fuse_reduce.cpp Adds optional broadcast rewriting/hoisting logic for reduce fusion, plus broader broadcast matching.
src/fuse_pointwise_reduce.cpp Runs an additional fuse_reduce pass with broadcast rewriting enabled.
test/fuse_pointwise.cpp Adds a test validating pointwise fusion enabled by broadcast rewrite.
test/fuse_reduce.cpp Adds multiple tests validating reduce fusion enabled by broadcast rewrite/hoist.
src/CMakeLists.txt Adds the new rewrite_broadcasts.cpp to the library build.
Suppressed comments (1)

src/fuse_reduce.cpp:520

  • In reduce_reshape, converting broadcast to multibroadcast can change semantics for the 2-input broadcast form (dynamic broadcasting), where out_lens is unused and may be unset. multibroadcast with 2+ inputs ignores the out_lens attribute entirely, so this rewrite can miscompile graphs that contain 2-input broadcasts inside the fused submodule.
            if(contains({"multibroadcast", "broadcast"}, sop.name()))
                return make_op("multibroadcast", {{"out_lens", dims}});
            assert(sop.name() == "pointwise");

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment thread src/fuse_reduce.cpp
Comment on lines +324 to +332
if(contains({"multibroadcast", "broadcast"}, op.name()))
{
auto out_lens =
expand_dims(op.to_value().at("out_lens").to_vector<std::size_t>(),
unreduced_axes,
blens);
return rmm.insert_instruction(
pos, make_op("multibroadcast", {{"out_lens", out_lens}}), inputs);
}

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.

n the reduce-module rebuild callback, treating broadcast and multibroadcast uniformly and unconditionally reading op.to_value().at("out_lens") is unsafe for 2-input broadcast instructions (used for dynamic broadcasting), which typically don't set out_lens at all (e.g. src/onnx/onnx_parser.cpp:155). This can lead to an empty lens vector and out-of-range access in expand_dims, and converting a 2-input broadcast into a 2-input multibroadcast changes semantics (multibroadcast ignores out_lens when it has 2+ inputs).

I think this is valid, multi-input broadcasts need to be handled differently or just dont apply this matcher for that case for now

@codecov

codecov Bot commented Aug 18, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 92.04545% with 7 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
src/fuse_reduce.cpp 89.29% 6 Missing ⚠️
src/fuse_pointwise_reduce.cpp 0.00% 1 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop    #5146      +/-   ##
===========================================
- Coverage    93.13%   93.12%   -0.01%     
===========================================
  Files          625      626       +1     
  Lines        33252    33309      +57     
===========================================
+ Hits         30967    31017      +50     
- Misses        2285     2292       +7     
Files with missing lines Coverage Δ
src/fuse_pointwise.cpp 97.50% <100.00%> (-0.27%) ⬇️
src/include/migraphx/fuse_reduce.hpp 100.00% <ø> (ø)
src/rewrite_broadcasts.cpp 100.00% <100.00%> (ø)
src/fuse_pointwise_reduce.cpp 0.00% <0.00%> (ø)
src/fuse_reduce.cpp 96.06% <89.29%> (-1.71%) ⬇️
🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@bdevorem bdevorem left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

lgtm. Copilot's comment about 2-input broadcast seems meaningful though

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.

4 participants