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 declarePromisedInterface<ConvertToLLVMPatternInterface, ComplexDialect>();
33 addInterfaces<ComplexInlinerInterface>();
34}
35
36Operation *complex::ComplexDialect::materializeConstant(OpBuilder &builder,
37 Attribute value,
38 Type type,
39 Location loc) {
40 if (complex::ConstantOp::isBuildableWith(value, type)) {
41 return complex::ConstantOp::create(builder, loc, type,
42 llvm::cast<ArrayAttr>(value));
43 }
44 return arith::ConstantOp::materialize(builder, value, type, loc);
45}
46
47#define GET_ATTRDEF_CLASSES
48#include "mlir/Dialect/Complex/IR/ComplexAttributes.cpp.inc"
49
50LogicalResult complex::NumberAttr::verify(
52 ::llvm::APFloat real, ::llvm::APFloat imag, ::mlir::Type type) {
53
54 if (!llvm::isa<ComplexType>(type))
55 return emitError() << "complex attribute must be a complex type.";
56
57 Type elementType = llvm::cast<ComplexType>(type).getElementType();
58 if (!llvm::isa<FloatType>(elementType))
59 return emitError()
60 << "element type of the complex attribute must be float like type.";
61
62 const auto &typeFloatSemantics =
63 llvm::cast<FloatType>(elementType).getFloatSemantics();
64 if (&real.getSemantics() != &typeFloatSemantics)
65 return emitError()
66 << "type doesn't match the type implied by its `real` value";
67 if (&imag.getSemantics() != &typeFloatSemantics)
68 return emitError()
69 << "type doesn't match the type implied by its `imag` value";
70
71 return success();
72}
73
74void complex::NumberAttr::print(AsmPrinter &printer) const {
75 printer << "<:" << llvm::cast<ComplexType>(getType()).getElementType() << " "
76 << getReal() << ", " << getImag() << ">";
77}
78
79Attribute complex::NumberAttr::parse(AsmParser &parser, Type odsType) {
80 Type type;
81 double real, imag;
82 if (parser.parseLess() || parser.parseColon() || parser.parseType(type) ||
83 parser.parseFloat(real) || parser.parseComma() ||
84 parser.parseFloat(imag) || parser.parseGreater())
85 return {};
86
87 return NumberAttr::get(ComplexType::get(type), real, imag);
88}
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