MLIR 22.0.0git
ComplexDialect.cpp
Go to the documentation of this file.
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
12#include "mlir/IR/Builders.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 {
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 complex::ConstantOp::create(builder, loc, type,
52 llvm::cast<ArrayAttr>(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(
62 ::llvm::APFloat real, ::llvm::APFloat imag, ::mlir::Type type) {
63
64 if (!llvm::isa<ComplexType>(type))
65 return emitError() << "complex attribute must be a complex type.";
66
67 Type elementType = llvm::cast<ComplexType>(type).getElementType();
68 if (!llvm::isa<FloatType>(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>(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>(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(type) ||
93 parser.parseFloat(real) || parser.parseComma() ||
94 parser.parseFloat(imag) || parser.parseGreater())
95 return {};
96
97 return NumberAttr::get(ComplexType::get(type), real, imag);
98}
return success()
static bool isLegalToInline(InlinerInterface &interface, Region *src, Region *insertRegion, bool shouldCloneInlinedRegion, IRMapping &valueMapping)
Utility to check that all of the operations within 'src' can be inlined.
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual ParseResult parseColon()=0
Parse a : token.
virtual ParseResult parseGreater()=0
Parse a '>' token.
virtual ParseResult parseType(Type &result)=0
Parse a type.
virtual ParseResult parseComma()=0
Parse a , token.
virtual ParseResult parseFloat(double &result)=0
Parse a floating point value from the stream.
This base class exposes generic asm printer hooks, usable across the various derived printers.
Attributes are known-constant values of operations.
Definition Attributes.h:25
This is the interface that must be implemented by the dialects of operations to be inlined.
DialectInlinerInterface(Dialect *dialect)
This class represents a diagnostic that is inflight and set to be reported.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition Location.h:76
This class helps build Operations.
Definition Builders.h:207
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition Utils.cpp:304
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.