1//===- ComplexDialect.cpp - MLIR Complex Dialect --------------------------===//
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#include "mlir/Conversion/ConvertToLLVM/ToLLVMInterface.h"
10#include "mlir/Dialect/Arith/IR/Arith.h"
11#include "mlir/Dialect/Complex/IR/Complex.h"
12#include "mlir/IR/Builders.h"
13#include "mlir/IR/DialectImplementation.h"
14#include "mlir/Transforms/InliningUtils.h"
15#include "llvm/ADT/TypeSwitch.h"
16
17using namespace mlir;
18
19#include "mlir/Dialect/Complex/IR/ComplexOpsDialect.cpp.inc"
20
21namespace {
22/// This class defines the interface for handling inlining for complex
23/// dialect operations.
24struct ComplexInlinerInterface : public DialectInlinerInterface {
25 using DialectInlinerInterface::DialectInlinerInterface;
26 /// All complex dialect ops can be inlined.
27 bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
28 return true;
29 }
30};
31} // namespace
32
33void complex::ComplexDialect::initialize() {
34 addOperations<
35#define GET_OP_LIST
36#include "mlir/Dialect/Complex/IR/ComplexOps.cpp.inc"
37 >();
38 addAttributes<
39#define GET_ATTRDEF_LIST
40#include "mlir/Dialect/Complex/IR/ComplexAttributes.cpp.inc"
41 >();
42 declarePromisedInterface<ConvertToLLVMPatternInterface, ComplexDialect>();
43 addInterfaces<ComplexInlinerInterface>();
44}
45
46Operation *complex::ComplexDialect::materializeConstant(OpBuilder &builder,
47 Attribute value,
48 Type type,
49 Location loc) {
50 if (complex::ConstantOp::isBuildableWith(value, type)) {
51 return builder.create<complex::ConstantOp>(location: loc, args&: type,
52 args: llvm::cast<ArrayAttr>(Val&: value));
53 }
54 return arith::ConstantOp::materialize(builder, value, type, loc);
55}
56
57#define GET_ATTRDEF_CLASSES
58#include "mlir/Dialect/Complex/IR/ComplexAttributes.cpp.inc"
59
60LogicalResult complex::NumberAttr::verify(
61 ::llvm::function_ref<::mlir::InFlightDiagnostic()> emitError,
62 ::llvm::APFloat real, ::llvm::APFloat imag, ::mlir::Type type) {
63
64 if (!llvm::isa<ComplexType>(Val: type))
65 return emitError() << "complex attribute must be a complex type.";
66
67 Type elementType = llvm::cast<ComplexType>(Val&: type).getElementType();
68 if (!llvm::isa<FloatType>(Val: elementType))
69 return emitError()
70 << "element type of the complex attribute must be float like type.";
71
72 const auto &typeFloatSemantics =
73 llvm::cast<FloatType>(Val&: elementType).getFloatSemantics();
74 if (&real.getSemantics() != &typeFloatSemantics)
75 return emitError()
76 << "type doesn't match the type implied by its `real` value";
77 if (&imag.getSemantics() != &typeFloatSemantics)
78 return emitError()
79 << "type doesn't match the type implied by its `imag` value";
80
81 return success();
82}
83
84void complex::NumberAttr::print(AsmPrinter &printer) const {
85 printer << "<:" << llvm::cast<ComplexType>(Val: getType()).getElementType() << " "
86 << getReal() << ", " << getImag() << ">";
87}
88
89Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
90 Type type;
91 double real, imag;
92 if (parser.parseLess() || parser.parseColon() || parser.parseType(result&: type) ||
93 parser.parseFloat(result&: real) || parser.parseComma() ||
94 parser.parseFloat(result&: imag) || parser.parseGreater())
95 return {};
96
97 return NumberAttr::get(type: ComplexType::get(elementType: type), real, imag);
98}
99

source code of mlir/lib/Dialect/Complex/IR/ComplexDialect.cpp