Skip to content

[linalg][transfrom] A transform extension for tiling and fusing dependent reductions. - #297

Merged
tkarna merged 7 commits into
llvm:mainfrom
charithaintc:linalg_reduction_fusion
Oct 9, 2026
Merged

tkarna merged 7 commits into
llvm:mainfrom
charithaintc:linalg_reduction_fusion

Conversation

@charithaintc

@charithaintc charithaintc commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

This PR introduces a new transform op fuse_dependent_reduction_ops that takes tiled reduction and an elementwise op followed by another reduction and, fuse them into a single tiled online reduction loop.

Main motivation is is to capture R1 -> E -> R2 linalg op sequence where,

  • R1 is a generic tiled reduction with at least one result.
  • E is an elementwise generic that consumes at least one output of R1 and uses it broadcasted over the same reduction axis as R1 (This recovers the rank of R1's input)
  • R2 is a generic add reduction that uses E's single output and produce single output.

And fuse them into a single tiled loop. This type of fusion is not always doable and legality is checked before applying the fusion.

PR does not integrate the op with any schedule yet and will be done in a follow-up PR.

This is part of landing #274

Assisted by: Claude, Codex

@charithaintc

Copy link
Copy Markdown
Contributor Author

cc @tkarna

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 Changes recommended

Accepted payloads can produce incorrect numerical results or invalid intermediate representation.

8 open findings
What changed in this PR

Introduces dependent-reduction fusion for tiled online reductions, advancing #274 without integrating it into existing schedules yet.

Changes:

  • Adds and exports the fusion transform.
  • Implements correction planning, loop rewriting, and legality checks.
  • Adds structural and rejection tests.
File Description
test/​transform/​test_fuse_dependent_reduction_ops.py Tests softmax, attention, mixed precision, and rejection behavior.
test/​transform/​test_dependent_reduction_legality.py Tests unsupported correction substitutions.
lighthouse/​dialects/​transform/​transform_ext/​utils/​ir_rewrite.py Adds cloning, hoisting, and type utilities.
lighthouse/​dialects/​transform/​transform_ext/​utils/​dependent_reduction_legality.py Adds correction-body preflight checks.
lighthouse/​dialects/​transform/​transform_ext/​utils/​dependent_reduction_fusion.py Implements correction factors and fused-loop construction.
lighthouse/​dialects/​transform/​transform_ext/​ops/​fuse_dependent_reduction_ops.py Defines the transform operation and handle contract.
lighthouse/​dialects/​transform/​transform_ext/​__init__.py Exports the transform and reduction-loop marker.

🧠 Review effort: Balanced


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

Comment on lines +280 to +284
if isinstance(operand, ir.BlockArgument) and operand.owner == body:
neutral = plan.neutral_by_op[op_index]
assert neutral is not None
value_map[operand] = arith.constant(compute_type, neutral)
temporary.append(operand)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

This is a real issue. currently I don't have good solution rather than assuming a already simplified form the IR (x+x -> 2x in this case). I imagine this can be solved by tracking provenance over the R1 input in E. only values that data-dependent on R1 needs such adjustment. I keep this for later improvements.

Adding a TODO:

operand
for consumer in (e, r2)
for operand in consumer.operands
if not irr.depends_on_op(operand, r1_loop)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I think this is a corner case for now (other users between E and R2). I would keep this for future work.

Comment on lines +379 to +383
to_hoist = [
operand
for consumer in (e, r2)
for operand in consumer.operands
if not irr.depends_on_op(operand, r1_loop)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

same as above. I think LLVM have stronger support for cleaner hoisting. harder to in python. leaving for future work.

@tkarna tkarna 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.

Thanks, LGTM

@tkarna
tkarna merged commit 7e4fa5c into llvm:main Oct 9, 2026
3 checks passed
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