<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/58610>58610</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [MLIR] tensor.from_elements cannot be folded for complex tensors
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          dime10
      </td>
    </tr>
</table>

<pre>
    Minimal example, executed with `mlir-opt -canonicalize`:
```mlir
func.func public @test() -> tensor<complex<f64>> {

    %c1 = complex.constant [0.1, -1.0] : complex<f64>
    %0 = tensor.from_elements %c1 : tensor<complex<f64>>

    return %0 : tensor<complex<f64>>
}
```

Error message:
```
mlir-opt: llvm-project/mlir/lib/IR/BuiltinAttributes.cpp:1004: static mlir::DenseElementsAttr mlir::DenseElementsAttr::get(mlir::ShapedType, ArrayRef<mlir::Attribute>): Assertion `attr.isa<StringAttr>() && "expected string value for non integer/index/float element"' failed.
```

The reason appears to be the creation of a `DenseElementsAttr` here: 
https://github.com/llvm/llvm-project/blob/779a45515134a040006c17d8b85f91a4d9f810ed/mlir/lib/Dialect/Tensor/IR/TensorOps.cpp#L859-L862

Which I think contains the actual problem, as the type-agnostic version of the `DenseElementsAttr::get` method assumes the tensor is either int, float, index, or string:
https://github.com/llvm/llvm-project/blob/779a45515134a040006c17d8b85f91a4d9f810ed/mlir/lib/IR/BuiltinAttributes.cpp#L996-L1004

Even though a version for the method exists for complex types:
https://github.com/llvm/llvm-project/blob/779a45515134a040006c17d8b85f91a4d9f810ed/mlir/lib/IR/BuiltinAttributes.cpp#L1083-L1084

LLVM-version: 51effa57818bf937fb7c889b8f567c146bb4eefe
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJzNVU1v4zYQ_TXyhbBAUd8HH5w4BhZwUGA3aI8FRQ0lthQlkFQ26a_vUFISb9IG7aWoIUi0hj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