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