Extend DotGeneralSimplify to handle broadcasted scalars#2637
Open
binoygeorge97 wants to merge 3 commits into
Open
Extend DotGeneralSimplify to handle broadcasted scalars#2637binoygeorge97 wants to merge 3 commits into
binoygeorge97 wants to merge 3 commits into
Conversation
…roadcasted runtime scalar)
DotGeneralSimplify rewrites dot_general(splat, X) into a reduce over the contracting dims scaled by the splat value. Detection only matched splat constants via m_Constant, so a broadcast_in_dim of a rank-0 runtime scalar (which no canonicalizer can fold to a constant) was left as a full dot_general. Add an extractBroadcastScalar helper that looks through a rank-0 broadcast_in_dim to its scalar operand (mirroring extractSplatInt's empty-broadcast_dimensions look-through, but returning the Value since the runtime case has no compile-time attribute). Detection now accepts either a splat constant or such a broadcast, on either operand. The constant path is unchanged; the runtime path scales the reduce by a broadcast of the scalar Value and always emits the multiply, relying on the existing canonicalizer to drop it when the value is 1. Fixes EnzymeAD#1475
Covers three cases: broadcast of a constant scalar (already folded by the pipeline), and broadcast of a runtime scalar on the LHS and RHS operands, both of which now simplify away the dot_general.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #2637 +/- ##
==========================================
- Coverage 31.73% 31.11% -0.63%
==========================================
Files 227 227
Lines 44908 44875 -33
==========================================
- Hits 14253 13963 -290
- Misses 30655 30912 +257 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
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.
Fixes #1475.
Problem
DotGeneralSimplifyrewritesdot_general(splat, X)into areduceover thecontracting dims, scaled by the splat value and broadcast to the result shape.
Detection matched a splat operand only via
m_Constant, so abroadcast_in_dimof a rank-0 runtime scalar (e.g. a function argument)was missed — no canonicalizer can fold it into a splat constant, so the full
dot_generalsurvived even though it is stills * reduce(X).Change
extractBroadcastScalar, which looks through a rank-0broadcast_in_dim(empty
broadcast_dimensions) to its scalar operandValue. Mirrors thelook-through in
extractSplatInt, but returns aValuesince the runtimecase has no compile-time attribute.
operand. The constant path is unchanged; the runtime path scales the reduce
by a broadcast of the scalar
Valueand always emits the multiply, relyingon the existing downstream canonicalizer to drop it when the value is 1.
dot_generalelement-type constraint guarantees the scalar's typematches the reduce result, so no
convertis needed.Tests
test/lit_tests/dot_general_bcast_scalar.mlircovers a broadcast of a constantscalar (already folded by the pipeline) plus runtime scalars on the LHS and RHS
operands, both of which now simplify away the
dot_general.Before (runtime scalar, LHS):
After: