Skip to content

[mlir][arith] Only fold splats for static shape result types #93102

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion mlir/include/mlir/Dialect/CommonFolders.h
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ Attribute constFoldCastOp(ArrayRef<Attribute> operands, Type resType,
calculate(op.getSplatValue<ElementValueT>(), castStatus);
if (!castStatus)
return {};
return DenseElementsAttr::get(cast<ShapedType>(resType), elementResult);
auto shapedResType = cast<ShapedType>(resType);
if (!shapedResType.hasStaticShape())
return {};
return DenseElementsAttr::get(shapedResType, elementResult);
}
if (auto op = dyn_cast<ElementsAttr>(operands[0])) {
// Operand is ElementsAttr-derived; perform an element-wise fold by
Expand Down
8 changes: 8 additions & 0 deletions mlir/test/Dialect/Arith/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -2950,6 +2950,14 @@ func.func @unsignedExtendConstantResource() -> tensor<i16> {
return %ext : tensor<i16>
}

// Just checks that this doesn't crash.
// CHECK-LABEL: @signedExtendSplatAsDynamicShape
func.func @signedExtendSplatAsDynamicShape() -> tensor<?xi64> {
%splat = arith.constant dense<5> : tensor<2xi16>
%extsplat = arith.extsi %splat : tensor<2xi16> to tensor<?xi64>
Copy link
Member

Choose a reason for hiding this comment

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

I'm surprised this is even allowed at the level of artith. Shouldn't we change the verifier to reject this extend + 'shape cast' pattern instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Makes sense. I'll rewrite it so that it's rejected during verification.

Copy link
Contributor

Choose a reason for hiding this comment

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

That might be a different can of worms. This is an obvious things to fix for now. Maybe we should land this first.

return %extsplat : tensor<?xi64>
}

// CHECK-LABEL: @extsi_i0
// CHECK: %[[ZERO:.*]] = arith.constant 0 : i16
// CHECK: return %[[ZERO]] : i16
Expand Down
Loading