Skip to content

Commit e8d5009

Browse files
authored
[mlir] Fix parsing of empty complex tensors (llvm#134322)
After llvm#133220 we had some empty complex literals (`tensor<0xcomplex<f32>>`) failing to parse. This was largely due to the ambiguity between `shape.empty()` meaning splat (`dense<1>`) or empty literal (`dense<>`). Used type's numel to disambiguate during verification.
1 parent 0d3f5ec commit e8d5009

File tree

2 files changed

+8
-2
lines changed

2 files changed

+8
-2
lines changed

mlir/lib/AsmParser/AttributeParser.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -566,8 +566,10 @@ DenseElementsAttr TensorLiteralParser::getAttr(SMLoc loc, ShapedType type) {
566566
if (ComplexType complexTy = dyn_cast<ComplexType>(eltType)) {
567567
eltType = complexTy.getElementType();
568568
isComplex = true;
569-
// Complex types have 2 elements.
570-
if (shape.empty() && storage.size() != 2) {
569+
// Complex types have N*2 elements or complex splat.
570+
// Empty shape may mean a splat or empty literal, only validate splats.
571+
bool isSplat = shape.empty() && type.getNumElements() != 0;
572+
if (isSplat && storage.size() != 2) {
571573
p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
572574
<< complexTy << ") expected 2 elements";
573575
return nullptr;

mlir/test/IR/parser.mlir

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,10 @@ func.func @densetensorattr() -> () {
730730
"complex_attr"(){bar = dense<(1.000000e+00,0.000000e+00)> : tensor<complex<f32>>} : () -> ()
731731
// CHECK: dense<[(1.000000e+00,0.000000e+00), (2.000000e+00,2.000000e+00)]> : tensor<2xcomplex<f32>>
732732
"complex_attr"(){bar = dense<[(1.000000e+00,0.000000e+00), (2.000000e+00,2.000000e+00)]> : tensor<2xcomplex<f32>>} : () -> ()
733+
// CHECK: dense<> : tensor<0xcomplex<i64>>
734+
"complex_attr"(){bar = dense<> : tensor<0xcomplex<i64>>} : () -> ()
735+
// CHECK: dense<> : tensor<2x0xcomplex<i64>>
736+
"complex_attr"(){bar = dense<> : tensor<2x0xcomplex<i64>>} : () -> ()
733737
return
734738
}
735739

0 commit comments

Comments
 (0)