[Mlir-commits] [mlir] [WIP][mlir] Make `DenseElementsAttr::reshape(...)` take a shape instead of a type (PR #149947)
Mengmeng Sun
llvmlistbot at llvm.org
Mon Jul 21 20:58:26 PDT 2025
================
@@ -1241,8 +1241,10 @@ ArrayRef<StringRef> DenseElementsAttr::getRawStringData() const {
/// Return a new DenseElementsAttr that has the same data as the current
/// attribute, but has been reshaped to 'newType'. The new type must have the
/// same total number of elements as well as element type.
-DenseElementsAttr DenseElementsAttr::reshape(ShapedType newType) {
+DenseElementsAttr DenseElementsAttr::reshape(ArrayRef<int64_t> newShape) {
+
ShapedType curType = getType();
+ auto newType = curType.cloneWith(newShape, curType.getElementType());
----------------
MengmSun wrote:
Otherwise the converted information like fp8->i8 will lost.
For example,
```bash
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"`
```bash
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>>
}
}
```
https://github.com/llvm/llvm-project/pull/149947
More information about the Mlir-commits
mailing list