Skip to content

Commit 8937b61

Browse files
authored
[mlir][vector] Fix cast incompatible type bug in ShuffleOp::fold (#150037)
This PR uses `dyn_cast` instead of `cast` to avoid a crash when the constant attribute is not a `DenseElementsAttr`. Fixes #149325.
1 parent 01b47eb commit 8937b61

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

mlir/lib/Dialect/Vector/IR/VectorOps.cpp

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3057,13 +3057,17 @@ OpFoldResult vector::ShuffleOp::fold(FoldAdaptor adaptor) {
30573057
SmallVector<Attribute> v1Elements, v2Elements;
30583058
Attribute poisonElement;
30593059
if (!isV2Poison) {
3060-
v2Elements =
3061-
to_vector(cast<DenseElementsAttr>(v2Attr).getValues<Attribute>());
3060+
auto v2DenseAttr = dyn_cast<DenseElementsAttr>(v2Attr);
3061+
if (!v2DenseAttr)
3062+
return {};
3063+
v2Elements = to_vector(v2DenseAttr.getValues<Attribute>());
30623064
poisonElement = v2Elements[0];
30633065
}
30643066
if (!isV1Poison) {
3065-
v1Elements =
3066-
to_vector(cast<DenseElementsAttr>(v1Attr).getValues<Attribute>());
3067+
auto v1DenseAttr = dyn_cast<DenseElementsAttr>(v1Attr);
3068+
if (!v1DenseAttr)
3069+
return {};
3070+
v1Elements = to_vector(v1DenseAttr.getValues<Attribute>());
30673071
poisonElement = v1Elements[0];
30683072
}
30693073

mlir/test/Dialect/Vector/canonicalize.mlir

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2347,6 +2347,19 @@ func.func @shuffle_1d() -> vector<4xi32> {
23472347

23482348
// -----
23492349

2350+
// Ensure shuffle dense resource elements not crash.
2351+
2352+
// CHECK-LABEL: func.func @shuffle_1d_dense_resource
2353+
// CHECK: vector.shuffle
2354+
func.func @shuffle_1d_dense_resource() -> vector<4xi32> {
2355+
%v0 = arith.constant dense_resource<__elided__> : vector<3xi32>
2356+
%v1 = arith.constant dense_resource<__elided__> : vector<3xi32>
2357+
%shuffle = vector.shuffle %v0, %v1 [3, 2, 5, 1] : vector<3xi32>, vector<3xi32>
2358+
return %shuffle : vector<4xi32>
2359+
}
2360+
2361+
// -----
2362+
23502363
// Check that poison indices pick the first element of the first non-poison
23512364
// input vector. That is, %v[0] (i.e., 5) in this test.
23522365

0 commit comments

Comments
 (0)