Skip to content

[MLIR][Vector]Add constraints to vector.shape_cast(constant) -> constant #147691

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
9 changes: 6 additions & 3 deletions mlir/lib/Dialect/Vector/IR/VectorOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5922,10 +5922,13 @@ OpFoldResult ShapeCastOp::fold(FoldAdaptor adaptor) {
return bcastOp.getSource();
}

// shape_cast(constant) -> constant
// shape_cast(constant) -> constant,
// if element type of the source and result are the same
if (auto splatAttr =
llvm::dyn_cast_if_present<SplatElementsAttr>(adaptor.getSource()))
return splatAttr.reshape(getType());
llvm::dyn_cast_if_present<SplatElementsAttr>(adaptor.getSource())) {
if (splatAttr.getElementType() == resultType.getElementType())
return splatAttr.reshape(getType());
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The code below will fold, but use the correct element type:

  // shape_cast(constant) -> constant
  if (auto splatAttr =
          llvm::dyn_cast_if_present<SplatElementsAttr>(adaptor.getSource())) {

    // The shape and 'scalable dims' of the new attribute must match the result
    // of the shape_cast:
    auto newShape = resultType.getShape();
    auto newScalableDims = resultType.getScalableDims();

    // The element type must be retained. Note that this is to handle currently
    // valid IR like
    //
    // ```
    // %0 = llvm.mlir.constant(dense<0.> : vector<1xf8E4M3FN>) : vector<1xi8>
    // %1 = vector.shape_cast %0 : vector<1xi8> to vector<1x1xi8>
    // ```
    //
    // where the element types of the attribute and result do not match.
    auto newElementType = splatAttr.getElementType();

    auto newAttr = VectorType::get(newShape, newElementType, newScalableDims);

    return DenseElementsAttr::get(newAttr,
                                  splatAttr.getSplatValue<Attribute>());
  }


// shape_cast(poison) -> poison
if (llvm::dyn_cast_if_present<ub::PoisonAttr>(adaptor.getSource())) {
Expand Down
12 changes: 12 additions & 0 deletions mlir/test/Dialect/Vector/canonicalize.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -1002,6 +1002,18 @@ func.func @fold_broadcast_shapecast(%arg0: vector<4xf32>) -> vector<4xf32> {

// -----

// CHECK-LABEL: func @canonicalize_extract_shapecast_different_element_type
Copy link
Contributor

@newling newling Jul 14, 2025

Choose a reason for hiding this comment

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

Something along the lines of:

// CHECK: [...] = llvm.mlir.constant
// CHECK-NEXT: return %[[CONST]] 

will be more explicit.

Wondering if it is ok to mix llvm dialect here, I guess so?

Copy link
Author

Choose a reason for hiding this comment

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

Yes it's OK. Updated.

func.func @canonicalize_extract_shapecast_different_element_type()->vector<12xi8> {
%0 = llvm.mlir.constant(dense<0.000000e+00> : vector<12xf8E4M3FN>) : vector<12xi8>
// CHECK-NOT: vector.shape_cast
%1 = vector.shape_cast %0 : vector<12xi8> to vector<1x12xi8>
// CHECK-NOT: vector.extract
%2 = vector.extract %1[0] : vector<12xi8> from vector<1x12xi8>
return %2 : vector<12xi8>
}

// -----

// CHECK-LABEL: func @canonicalize_broadcast_shapecast_scalar
// CHECK: vector.broadcast
// CHECK-NOT: vector.shape_cast
Expand Down