[MLIR][Vector] Broadcast folding creates invalid scalar-result vector.contract
Summary
CombineContractBroadcastMask, registered by populateVectorReductionToContractPatterns, produces invalid IR when folding a broadcast-like vector.shape_cast on one operand of a scalar-result vector.contract.
The input passes verification. Running only the Vector reduction-to-contract patterns with standalone mlir-opt removes unit reduction dimensions from the left-hand operand while retaining them in the right-hand operand and iterator list. The resulting contraction fails verification with invalid accumulator/result vector shape.
No contraction-lowering patterns are needed to reproduce the failure.
Reproducer
repro.mlir:
func.func @repro(%a: vector<2xbf16>, %b: bf16) -> bf16 {
%zero = arith.constant 0.0 : bf16
%lhs = vector.shape_cast %a : vector<2xbf16> to vector<1x1x2xbf16>
%rhs = vector.broadcast %b : bf16 to vector<1x1x2xbf16>
%out = vector.contract {
indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> ()>],
iterator_types = ["reduction", "reduction", "reduction"],
kind = #vector.kind<add>
} %lhs, %rhs, %zero : vector<1x1x2xbf16>, vector<1x1x2xbf16> into bf16
return %out : bf16
}
Commands:
# Verify the original input: succeeds with exit code 0.
mlir-opt repro.mlir -o /dev/null
# Apply only the reduction-to-contract patterns: fails with exit code 1.
mlir-opt repro.mlir \
--test-vector-reduction-to-contract-patterns \
--verify-each=true \
--mlir-disable-threading \
--mlir-print-ir-after-failure
The test pass invokes populateVectorReductionToContractPatterns followed by applyPatternsGreedily. The reproducer uses only Func, Arith, and Vector operations in an implicit Builtin module.
Why the input IR is valid
The shape cast preserves the element type and element count, adding only two leading unit dimensions. The broadcast legally expands a scalar to vector<1x1x2xbf16>.
Both contraction inputs have shape (1, 1, 2) and identity indexing maps. All three reduction dimensions appear in both operands with matching extents. There are no free or batch dimensions, so the scalar accumulator and result are valid, as described in the Vector dialect documentation.
The asymmetric contraction below is generated by the rewrite, rather than supplied as input.
Actual behavior
mlir-opt exits with code 1 after the pass produces an invalid contraction:
repro.mlir:5:10: error: 'vector.contract' op invalid accumulator/result vector shape
%out = vector.contract {
^
The IR dump after failure contains the following contraction, shown in custom syntax for readability:
%out = vector.contract {
indexing_maps = [affine_map<(d0, d1, d2) -> (d2)>,
affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> ()>],
iterator_types = ["reduction", "reduction", "reduction"],
kind = #vector.kind<add>
} %a, %rhs, %zero : vector<2xbf16>, vector<1x1x2xbf16> into bf16
The left-hand shape cast has been removed. Dimensions d0 and d1 remain reduction iterators but now occur only in the right-hand operand. Only d2 still forms a contracting dimension pair.
The verifier consequently treats the unpaired right-hand dimensions as free dimensions when checking the result shape, which is incompatible with the unchanged scalar result.
Expected behavior
The pattern should preserve valid contraction IR. If a fold would leave a reduction iterator present on only one operand, it should retain the original operation.
Folding unit reduction dimensions from both operands together remains valid when the dimensions can also be removed from the iterator list and at least one contracting dimension pair remains.
Likely root cause
In combineContractAndBroadcast in VectorTransforms.cpp, the left-hand shape cast is folded because it adds leading unit dimensions. The right-hand broadcast is retained because its source is scalar and contraction operands must be vectors.
The removed dimensions remain used by the right-hand map, so unused-dimension compression retains their iterators. The subsequent hasReductionIteratorApplyingOnBothSides check requires only one paired reduction and stops at the first match. It accepts d2 without rejecting the unpaired d0 and d1.
The later unused-dimension check also passes: every iterator is still used by at least one operand. Neither check requires every retained reduction to occur in both operand maps.
Candidate fix
After compressing unused dimensions, reject the fold if any retained reduction iterator is missing from either operand map. Keep the existing requirement that at least one reduction pair remains:
bool hasReductionIteratorApplyingOnBothSides = false;
for (unsigned i = 0; i < iterators.size(); ++i) {
if (!isReductionIterator(iterators[i]))
continue;
if (!getResultIndex(maps[0], i) || !getResultIndex(maps[1], i))
return failure();
hasReductionIteratorApplyingOnBothSides = true;
}
if (!hasReductionIteratorApplyingOnBothSides)
return failure();
This is a candidate fix, not a claim of verification with a rebuilt compiler.
Regression coverage in mlir/test/Dialect/Vector/vector-reduce-to-contract.mlir should include one-sided unit-reduction folding with another paired reduction still present, as in this reproducer. It should also retain coverage for legal two-sided unit-reduction folding and rejection of folds that remove all reduction pairs.
Environment
- Tool: standalone LLVM
mlir-opt. - Tool version output:
LLVM version 24.0.0git,Optimized build with assertions. - Original input verification: exit code
0. - Reduction-to-contract pattern pass with verification enabled: exit code
1. - The reproducer was verified using an existing compiler build; no compiler source modification, rebuild, or runtime execution was performed.
@llvm/issue-subscribers-mlir
Author: Cerulime
CombineContractBroadcastMask, registered by populateVectorReductionToContractPatterns, produces invalid IR when folding a broadcast-like vector.shape_cast on one operand of a scalar-result vector.contract.
The input passes verification. Running only the Vector reduction-to-contract patterns with standalone mlir-opt removes unit reduction dimensions from the left-hand operand while retaining them in the right-hand operand and iterator list. The resulting contraction fails verification with invalid accumulator/result vector shape.
No contraction-lowering patterns are needed to reproduce the failure.
Reproducer
repro.mlir:
func.func @<!-- -->repro(%a: vector<2xbf16>, %b: bf16) -> bf16 {
%zero = arith.constant 0.0 : bf16
%lhs = vector.shape_cast %a : vector<2xbf16> to vector<1x1x2xbf16>
%rhs = vector.broadcast %b : bf16 to vector<1x1x2xbf16>
%out = vector.contract {
indexing_maps = [affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> ()>],
iterator_types = ["reduction", "reduction", "reduction"],
kind = #vector.kind<add>
} %lhs, %rhs, %zero : vector<1x1x2xbf16>, vector<1x1x2xbf16> into bf16
return %out : bf16
}
Commands:
# Verify the original input: succeeds with exit code 0.
mlir-opt repro.mlir -o /dev/null
# Apply only the reduction-to-contract patterns: fails with exit code 1.
mlir-opt repro.mlir \
--test-vector-reduction-to-contract-patterns \
--verify-each=true \
--mlir-disable-threading \
--mlir-print-ir-after-failure
The test pass invokes populateVectorReductionToContractPatterns followed by applyPatternsGreedily. The reproducer uses only Func, Arith, and Vector operations in an implicit Builtin module.
Why the input IR is valid
The shape cast preserves the element type and element count, adding only two leading unit dimensions. The broadcast legally expands a scalar to vector<1x1x2xbf16>.
Both contraction inputs have shape (1, 1, 2) and identity indexing maps. All three reduction dimensions appear in both operands with matching extents. There are no free or batch dimensions, so the scalar accumulator and result are valid, as described in the Vector dialect documentation.
The asymmetric contraction below is generated by the rewrite, rather than supplied as input.
Actual behavior
mlir-opt exits with code 1 after the pass produces an invalid contraction:
repro.mlir:5:10: error: 'vector.contract' op invalid accumulator/result vector shape
%out = vector.contract {
^
The IR dump after failure contains the following contraction, shown in custom syntax for readability:
%out = vector.contract {
indexing_maps = [affine_map<(d0, d1, d2) -> (d2)>,
affine_map<(d0, d1, d2) -> (d0, d1, d2)>,
affine_map<(d0, d1, d2) -> ()>],
iterator_types = ["reduction", "reduction", "reduction"],
kind = #vector.kind<add>
} %a, %rhs, %zero : vector<2xbf16>, vector<1x1x2xbf16> into bf16
The left-hand shape cast has been removed. Dimensions d0 and d1 remain reduction iterators but now occur only in the right-hand operand. Only d2 still forms a contracting dimension pair.
The verifier consequently treats the unpaired right-hand dimensions as free dimensions when checking the result shape, which is incompatible with the unchanged scalar result.
Expected behavior
The pattern should preserve valid contraction IR. If a fold would leave a reduction iterator present on only one operand, it should retain the original operation.
Folding unit reduction dimensions from both operands together remains valid when the dimensions can also be removed from the iterator list and at least one contracting dimension pair remains.
Likely root cause
In combineContractAndBroadcast in VectorTransforms.cpp, the left-hand shape cast is folded because it adds leading unit dimensions. The right-hand broadcast is retained because its source is scalar and contraction operands must be vectors.
The removed dimensions remain used by the right-hand map, so unused-dimension compression retains their iterators. The subsequent hasReductionIteratorApplyingOnBothSides check requires only one paired reduction and stops at the first match. It accepts d2 without rejecting the unpaired d0 and d1.
The later unused-dimension check also passes: every iterator is still used by at least one operand. Neither check requires every retained reduction to occur in both operand maps.
Candidate fix
After compressing unused dimensions, reject the fold if any retained reduction iterator is missing from either operand map. Keep the existing requirement that at least one reduction pair remains:
bool hasReductionIteratorApplyingOnBothSides = false;
for (unsigned i = 0; i < iterators.size(); ++i) {
if (!isReductionIterator(iterators[i]))
continue;
if (!getResultIndex(maps[0], i) || !getResultIndex(maps[1], i))
return failure();
hasReductionIteratorApplyingOnBothSides = true;
}
if (!hasReductionIteratorApplyingOnBothSides)
return failure();
This is a candidate fix, not a claim of verification with a rebuilt compiler.
Regression coverage in mlir/test/Dialect/Vector/vector-reduce-to-contract.mlir should include one-sided unit-reduction folding with another paired reduction still present, as in this reproducer. It should also retain coverage for legal two-sided unit-reduction folding and rejection of folds that remove all reduction pairs.
Environment
- Tool: standalone LLVM
mlir-opt. - Tool version output:
LLVM version 24.0.0git,Optimized build with assertions. - Original input verification: exit code
0. - Reduction-to-contract pattern pass with verification enabled: exit code
1. - The reproducer was verified using an existing compiler build; no compiler source modification, rebuild, or runtime execution was performed.
bing bing, i remember seeing a very similar repro somewhere.
yes! i'd like to work there (again but deeper) :o
#228971
You may want to say this. Yes I first trigger the crash, then AI help me to find this bug because they are in the same file.
That is amazing! what i meant by my comment is:
i have worked on a very related issue before hence, i would love to work on this too.