[Mlir-commits] [mlir] 23c3eb7 - [mlir][Complex] Change complex.number attribute type to ComplexType.

Alexander Belyaev llvmlistbot at llvm.org
Thu Jul 28 12:25:33 PDT 2022


Author: Adrian Kuegel
Date: 2022-07-28T21:25:12+02:00
New Revision: 23c3eb7cdf3478c9db86f6cb5115821a8f0f5f40

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

LOG: [mlir][Complex] Change complex.number attribute type to ComplexType.

It is more useful to use ComplexType as type of the attribute than to
use the element type as attribute type. This means when using this
attribute in complex::ConstantOp, we just need to check whether
the types match.

Reviewed By: pifon2a

Differential Revision: https://reviews.llvm.org/D130703

Added: 
    

Modified: 
    mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
    mlir/test/Dialect/Complex/attribute.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp b/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
index 697fe7057f218..8c5308929659d 100644
--- a/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
+++ b/mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp
@@ -48,11 +48,16 @@ LogicalResult complex::NumberAttr::verify(
     ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
     ::llvm::APFloat real, ::llvm::APFloat imag, ::mlir::Type type) {
 
-  if (!type.isa<FloatType>())
+  if (!type.isa<ComplexType>())
+    return emitError() << "complex attribute must be a complex type.";
+
+  Type elementType = type.cast<ComplexType>().getElementType();
+  if (!elementType.isa<FloatType>())
     return emitError()
-           << "element of the complex attribute must be float like type.";
+           << "element type of the complex attribute must be float like type.";
 
-  const auto &typeFloatSemantics = type.cast<FloatType>().getFloatSemantics();
+  const auto &typeFloatSemantics =
+      elementType.cast<FloatType>().getFloatSemantics();
   if (&real.getSemantics() != &typeFloatSemantics)
     return emitError()
            << "type doesn't match the type implied by its `real` value";
@@ -64,7 +69,8 @@ LogicalResult complex::NumberAttr::verify(
 }
 
 void complex::NumberAttr::print(AsmPrinter &printer) const {
-  printer << "<:" << getType() << " " << getReal() << ", " << getImag() << ">";
+  printer << "<:" << getType().cast<ComplexType>().getElementType() << " "
+          << getReal() << ", " << getImag() << ">";
 }
 
 Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
@@ -82,5 +88,6 @@ Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
   APFloat imagFloat(imag);
   imagFloat.convert(type.cast<FloatType>().getFloatSemantics(),
                     APFloat::rmNearestTiesToEven, &unused);
-  return NumberAttr::get(parser.getContext(), realFloat, imagFloat, type);
+  return NumberAttr::get(parser.getContext(), realFloat, imagFloat,
+                         ComplexType::get(type));
 }

diff  --git a/mlir/test/Dialect/Complex/attribute.mlir b/mlir/test/Dialect/Complex/attribute.mlir
index d2510d9a1e395..070d942fd87e2 100644
--- a/mlir/test/Dialect/Complex/attribute.mlir
+++ b/mlir/test/Dialect/Complex/attribute.mlir
@@ -2,7 +2,7 @@
 
 func.func @number_attr_f64() {
   "test.number_attr"() {
-    // CHECK: attr = #complex.number<:f64 1.000000e+00, 0.000000e+00> : f64
+    // CHECK: attr = #complex.number<:f64 1.000000e+00, 0.000000e+00> : complex<f64>
     attr = #complex.number<:f64 1.0, 0.0>
   } : () -> ()
 
@@ -11,7 +11,7 @@ func.func @number_attr_f64() {
 
 func.func @number_attr_f32() {
   "test.number_attr"() {
-    // CHECK: attr = #complex.number<:f32 1.000000e+00, 0.000000e+00> : f32
+    // CHECK: attr = #complex.number<:f32 1.000000e+00, 0.000000e+00> : complex<f32>
     attr = #complex.number<:f32 1.0, 0.0>
   } : () -> ()
 


        


More information about the Mlir-commits mailing list