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