[Mlir-commits] [mlir] e8d5009 - [mlir] Fix parsing of empty complex tensors (#134322)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Fri Apr 4 09:29:54 PDT 2025


Author: Kevin Gleason
Date: 2025-04-04T09:29:51-07:00
New Revision: e8d50097849081168f0285418ce8a36733eb7154

URL: https://github.com/llvm/llvm-project/commit/e8d50097849081168f0285418ce8a36733eb7154
DIFF: https://github.com/llvm/llvm-project/commit/e8d50097849081168f0285418ce8a36733eb7154.diff

LOG: [mlir] Fix parsing of empty complex tensors (#134322)

After https://github.com/llvm/llvm-project/pull/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.

Added: 
    

Modified: 
    mlir/lib/AsmParser/AttributeParser.cpp
    mlir/test/IR/parser.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/AsmParser/AttributeParser.cpp b/mlir/lib/AsmParser/AttributeParser.cpp
index 93a24dee29ad2..2474e88373e04 100644
--- a/mlir/lib/AsmParser/AttributeParser.cpp
+++ b/mlir/lib/AsmParser/AttributeParser.cpp
@@ -566,8 +566,10 @@ DenseElementsAttr TensorLiteralParser::getAttr(SMLoc loc, ShapedType type) {
   if (ComplexType complexTy = dyn_cast<ComplexType>(eltType)) {
     eltType = complexTy.getElementType();
     isComplex = true;
-    // Complex types have 2 elements.
-    if (shape.empty() && storage.size() != 2) {
+    // Complex types have N*2 elements or complex splat.
+    // Empty shape may mean a splat or empty literal, only validate splats.
+    bool isSplat = shape.empty() && type.getNumElements() != 0;
+    if (isSplat && storage.size() != 2) {
       p.emitError(loc) << "parsed " << storage.size() << " elements, but type ("
                        << complexTy << ") expected 2 elements";
       return nullptr;

diff  --git a/mlir/test/IR/parser.mlir b/mlir/test/IR/parser.mlir
index cace1fefa43d6..8b192ff11d573 100644
--- a/mlir/test/IR/parser.mlir
+++ b/mlir/test/IR/parser.mlir
@@ -730,6 +730,10 @@ func.func @densetensorattr() -> () {
   "complex_attr"(){bar = dense<(1.000000e+00,0.000000e+00)> : tensor<complex<f32>>} : () -> ()
   // CHECK: dense<[(1.000000e+00,0.000000e+00), (2.000000e+00,2.000000e+00)]> : tensor<2xcomplex<f32>>
   "complex_attr"(){bar = dense<[(1.000000e+00,0.000000e+00), (2.000000e+00,2.000000e+00)]> : tensor<2xcomplex<f32>>} : () -> ()
+  // CHECK: dense<> : tensor<0xcomplex<i64>>
+  "complex_attr"(){bar = dense<> : tensor<0xcomplex<i64>>} : () -> ()
+  // CHECK: dense<> : tensor<2x0xcomplex<i64>>
+  "complex_attr"(){bar = dense<> : tensor<2x0xcomplex<i64>>} : () -> ()
   return
 }
 


        


More information about the Mlir-commits mailing list