[mlir] Refactor ComplexOps.td [NFC]
authorAdrian Kuegel <akuegel@google.com>
Fri, 11 Jun 2021 07:37:01 +0000 (09:37 +0200)
committerAdrian Kuegel <akuegel@google.com>
Fri, 11 Jun 2021 08:53:29 +0000 (10:53 +0200)
Create a ComplexUnaryOp base class and use it for AbsOp, ReOp and ImOp.
Sort all ops in lexicographic order.

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

mlir/include/mlir/Dialect/Complex/IR/ComplexOps.td

index 8b3ea0c..7e22ebf 100644 (file)
@@ -29,47 +29,52 @@ class ComplexArithmeticOp<string mnemonic, list<OpTrait> traits = []> :
   let verifier = ?;
 }
 
+// Base class for standard unary operations on complex numbers with a
+// floating-point element type. These operations take one operand and return
+// one result; the operand must be a complex number.
+class ComplexUnaryOp<string mnemonic, list<OpTrait> traits = []> :
+    Complex_Op<mnemonic, traits # [NoSideEffect]> {
+  let arguments = (ins Complex<AnyFloat>:$complex);
+  let assemblyFormat = "$complex attr-dict `:` type($complex)";
+  let verifier = ?;
+}
+
 //===----------------------------------------------------------------------===//
-// AddOp
+// AbsOp
 //===----------------------------------------------------------------------===//
 
-def AddOp : ComplexArithmeticOp<"add"> {
-  let summary = "complex addition";
+def AbsOp : ComplexUnaryOp<"abs",
+    [TypesMatchWith<"complex element type matches result type",
+                    "complex", "result",
+                    "$_self.cast<ComplexType>().getElementType()">]> {
+  let summary = "computes absolute value of a complex number";
   let description = [{
-    The `add` operation takes two complex numbers and returns their sum.
+    The `abs` op takes a single complex number and computes its absolute value.
 
     Example:
 
     ```mlir
-    %a = complex.add %b, %c : complex<f32>
+    %a = complex.abs %b : complex<f32>
     ```
   }];
+  let results = (outs AnyFloat:$result);
 }
 
 //===----------------------------------------------------------------------===//
-// AbsOp
+// AddOp
 //===----------------------------------------------------------------------===//
 
-def AbsOp : Complex_Op<"abs",
-    [NoSideEffect,
-     TypesMatchWith<"complex element type matches result type",
-                    "complex", "result",
-                    "$_self.cast<ComplexType>().getElementType()">]> {
-  let summary = "computes absolute value of a complex number";
+def AddOp : ComplexArithmeticOp<"add"> {
+  let summary = "complex addition";
   let description = [{
-    The `abs` op takes a single complex number and computes its absolute value.
+    The `add` operation takes two complex numbers and returns their sum.
 
     Example:
 
     ```mlir
-    %a = complex.abs %b : complex<f32>
+    %a = complex.add %b, %c : complex<f32>
     ```
   }];
-
-  let arguments = (ins Complex<AnyFloat>:$complex);
-  let results = (outs AnyFloat:$result);
-
-  let assemblyFormat = "$complex attr-dict `:` type($complex)";
 }
 
 //===----------------------------------------------------------------------===//
@@ -122,33 +127,6 @@ def DivOp : ComplexArithmeticOp<"div"> {
 }
 
 //===----------------------------------------------------------------------===//
-// ImOp
-//===----------------------------------------------------------------------===//
-
-def ImOp : Complex_Op<"im",
-    [NoSideEffect,
-     TypesMatchWith<"complex element type matches result type",
-                    "complex", "imaginary",
-                    "$_self.cast<ComplexType>().getElementType()">]> {
-  let summary = "extracts the imaginary part of a complex number";
-  let description = [{
-    The `im` op takes a single complex number and extracts the imaginary part.
-
-    Example:
-
-    ```mlir
-    %a = complex.im %b : complex<f32>
-    ```
-  }];
-
-  let arguments = (ins Complex<AnyFloat>:$complex);
-  let results = (outs AnyFloat:$imaginary);
-
-  let assemblyFormat = "$complex attr-dict `:` type($complex)";
-  let hasFolder = 1;
-}
-
-//===----------------------------------------------------------------------===//
 // EqualOp
 //===----------------------------------------------------------------------===//
 
@@ -178,6 +156,29 @@ def EqualOp : Complex_Op<"eq",
 }
 
 //===----------------------------------------------------------------------===//
+// ImOp
+//===----------------------------------------------------------------------===//
+
+def ImOp : ComplexUnaryOp<"im",
+    [TypesMatchWith<"complex element type matches result type",
+                    "complex", "imaginary",
+                    "$_self.cast<ComplexType>().getElementType()">]> {
+  let summary = "extracts the imaginary part of a complex number";
+  let description = [{
+    The `im` op takes a single complex number and extracts the imaginary part.
+
+    Example:
+
+    ```mlir
+    %a = complex.im %b : complex<f32>
+    ```
+  }];
+
+  let results = (outs AnyFloat:$imaginary);
+  let hasFolder = 1;
+}
+
+//===----------------------------------------------------------------------===//
 // MulOp
 //===----------------------------------------------------------------------===//
 
@@ -226,9 +227,8 @@ def NotEqualOp : Complex_Op<"neq",
 // ReOp
 //===----------------------------------------------------------------------===//
 
-def ReOp : Complex_Op<"re",
-    [NoSideEffect,
-     TypesMatchWith<"complex element type matches result type",
+def ReOp : ComplexUnaryOp<"re",
+    [TypesMatchWith<"complex element type matches result type",
                     "complex", "real",
                     "$_self.cast<ComplexType>().getElementType()">]> {
   let summary = "extracts the real part of a complex number";
@@ -242,10 +242,7 @@ def ReOp : Complex_Op<"re",
     ```
   }];
 
-  let arguments = (ins Complex<AnyFloat>:$complex);
   let results = (outs AnyFloat:$real);
-
-  let assemblyFormat = "$complex attr-dict `:` type($complex)";
   let hasFolder = 1;
 }