MLIR 24.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"
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, loc, type,
50 llvm::cast<ArrayAttr>(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(
60 ::llvm::APFloat real, ::llvm::APFloat imag, ::mlir::Type type) {
61
62 if (!llvm::isa<ComplexType>(type))
63 return emitError() << "complex attribute must be a complex type.";
64
65 Type elementType = llvm::cast<ComplexType>(type).getElementType();
66 if (!llvm::isa<FloatType>(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>(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>(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(type) ||
91 parser.parseFloat(real) || parser.parseComma() ||
92 parser.parseFloat(imag) || parser.parseGreater())
93 return {};
94
95 return NumberAttr::get(ComplexType::get(type), real, imag);
96}
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 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:210
Operation is the basic unit of execution within MLIR.
Definition Operation.h:87
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:311
constexpr T real(const NonFloatComplex< T > &x)
Definition Complex.h:255
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
constexpr T imag(const NonFloatComplex< T > &x)
Definition Complex.h:260