Skip to content

Conversation

@newling
Copy link
Contributor

@newling newling commented Jul 21, 2025

The motivation is that it would have prevented the issue detected in #147691.

DenseElementsAttr DenseElementsAttr::reshape(ArrayRef<int64_t> newShape) {

ShapedType curType = getType();
auto newType = curType.cloneWith(newShape, curType.getElementType());

Choose a reason for hiding this comment

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

I think if result type conversion is intended then we should keep the element type of converted result type and use for subsequent lowering. Clone with newType.getElementType instead of curType.getElementType.

Choose a reason for hiding this comment

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

Otherwise the converted information like fp8->i8 will lost.
For example,

func.func @canonicalize_extract_shapecast_different_element_type() -> vector<1x192xf8E4M3FN> {
      %0 = arith.constant dense<1.000000e+00> : vector<192xf8E4M3FN>
      %1 = vector.shape_cast %0 : vector<192xf8E4M3FN> to vector<1x192xf8E4M3FN>
      return %1 : vector<1x192xf8E4M3FN>
}

will be converted to as below after -convert-to-llvm -canonicalize="test-convergence"

module {
  llvm.func @canonicalize_extract_shapecast_different_element_type() -> !llvm.array<1 x vector<192xf8E4M3FN>> {
    %0 = llvm.mlir.constant(dense<1.000000e+00> : vector<1x192xf8E4M3FN>) : !llvm.array<1 x vector<192xf8E4M3FN>>
    llvm.return %0 : !llvm.array<1 x vector<192xf8E4M3FN>>
  }
}

However, it should be

module {
  llvm.func @canonicalize_extract_shapecast_different_element_type() -> !llvm.array<1 x vector<192xi8>> {
    %0 = llvm.mlir.constant(dense<1.000000e+00> : vector<1x192xf8E4M3FN>) : !llvm.array<1 x vector<192xi8>>
    llvm.return %0 : !llvm.array<1 x vector<192xi8>>
  }
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Hi @MengmSun -- I just tried your example above on this branch with mlir-opt -convert-to-llvm --canonicalize test.mlir and it gives

llvm.func @canonicalize_extract_shapecast_different_element_type() -> !llvm.array<1 x vector<192xi8>> {
  %0 = llvm.mlir.constant(dense<1.000000e+00> : vector<1x192xf8E4M3FN>) : !llvm.array<1 x vector<192xi8>>
  llvm.return %0 : !llvm.array<1 x vector<192xi8>>
}

which I think is what we want

@newling newling changed the title [WIP][mlir] Make DenseElementsAttr::reshape(...) take a shape instead of a type [WIP][mlir] DenseElementsAttr::reshape(arg): make arg a shape, not a type Jul 22, 2025
@newling newling closed this Oct 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants