diff --git a/mlir/lib/IR/BuiltinAttributes.cpp b/mlir/lib/IR/BuiltinAttributes.cpp --- a/mlir/lib/IR/BuiltinAttributes.cpp +++ b/mlir/lib/IR/BuiltinAttributes.cpp @@ -1589,6 +1589,18 @@ if (eltType.isa()) return FloatAttr::get(eltType, 0); + // Handle complex elements. + if (auto complexTy = eltType.dyn_cast()) { + auto eltType = complexTy.getElementType(); + Attribute zero; + if (eltType.isa()) + zero = FloatAttr::get(eltType, 0); + else // must be integer + zero = IntegerAttr::get(eltType, 0); + return ArrayAttr::get(complexTy.getContext(), + ArrayRef{zero, zero}); + } + // Handle string type. if (getValues().isa()) return StringAttr::get("", eltType); diff --git a/mlir/test/Integration/Dialect/LLVMIR/CPU/test-complex-sparse-constant.mlir b/mlir/test/Integration/Dialect/LLVMIR/CPU/test-complex-sparse-constant.mlir new file mode 100644 --- /dev/null +++ b/mlir/test/Integration/Dialect/LLVMIR/CPU/test-complex-sparse-constant.mlir @@ -0,0 +1,16 @@ +// RUN: mlir-opt %s --convert-memref-to-llvm | \ +// RUN: mlir-cpu-runner -e entry -entry-point-result=void + +// +// Code should not crash on the complex32 sparse constant. +// +module attributes {llvm.data_layout = ""} { + memref.global "private" constant @"__constant_32xcomplex_0" : memref<32xcomplex> = + sparse<[[1], [28], [31]], + [(1.000000e+00,0.000000e+00), (2.000000e+00,0.000000e+00), (3.000000e+00,0.000000e+00)] + > {alignment = 128 : i64} + llvm.func @entry() { + %0 = memref.get_global @"__constant_32xcomplex_0" : memref<32xcomplex> + llvm.return + } +}