xref: /llvm-project/mlir/include/mlir/Dialect/Complex/IR/ComplexAttributes.td (revision 5c8ce6d5761ed6a9a39ef5a77aa45d8b6095e0f5)
1//===- ComplexAttributes.td - Definitions for complex attributes -*- tablegen -*-=//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#ifndef COMPLEX_ATTRIBUTE
10#define COMPLEX_ATTRIBUTE
11
12include "mlir/IR/AttrTypeBase.td"
13include "mlir/IR/BuiltinAttributeInterfaces.td"
14include "mlir/Dialect/Complex/IR/ComplexBase.td"
15
16//===----------------------------------------------------------------------===//
17// Complex Attributes.
18//===----------------------------------------------------------------------===//
19
20class Complex_Attr<string attrName, string attrMnemonic,
21                   list<Trait> traits = []>
22    : AttrDef<Complex_Dialect, attrName, traits> {
23  let mnemonic = attrMnemonic;
24}
25
26def Complex_NumberAttr : Complex_Attr<"Number", "number",
27                                      [TypedAttrInterface]> {
28  let summary = "A complex number attribute";
29
30  let description = [{
31    A complex number attribute.
32
33    Example:
34
35    ```mlir
36    #complex.number<:f64 1.0, 2.0>
37    ```
38  }];
39
40  let parameters = (ins APFloatParameter<"">:$real,
41                        APFloatParameter<"">:$imag,
42                        AttributeSelfTypeParameter<"">:$type);
43  let builders = [
44    AttrBuilderWithInferredContext<(ins "mlir::ComplexType":$type,
45                                        "double":$real,
46                                        "double":$imag), [{
47      auto elementType = ::llvm::cast<FloatType>(type.getElementType());
48      APFloat realFloat(real);
49      bool unused;
50      realFloat.convert(elementType.getFloatSemantics(),
51                        APFloat::rmNearestTiesToEven, &unused);
52      APFloat imagFloat(imag);
53      imagFloat.convert(elementType.getFloatSemantics(),
54                        APFloat::rmNearestTiesToEven, &unused);
55      return $_get(type.getContext(), realFloat, imagFloat, type);
56    }]>
57  ];
58
59  let extraClassDeclaration = [{
60    std::complex<APFloat> getValue() {
61      return std::complex<APFloat>(getReal(), getImag());
62    }
63  }];
64
65  let genVerifyDecl = 1;
66  let hasCustomAssemblyFormat = 1;
67  let skipDefaultBuilders = 1;
68}
69
70#endif // COMPLEX_ATTRIBUTE
71