MLIR  20.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/StringExtras.h"
16 #include "llvm/ADT/TypeSwitch.h"
17 
18 using namespace mlir;
19 
20 #include "mlir/Dialect/Complex/IR/ComplexOpsDialect.cpp.inc"
21 
22 namespace {
23 /// This class defines the interface for handling inlining for complex
24 /// dialect operations.
25 struct ComplexInlinerInterface : public DialectInlinerInterface {
27  /// All complex dialect ops can be inlined.
28  bool isLegalToInline(Operation *, Region *, bool, IRMapping &) const final {
29  return true;
30  }
31 };
32 } // namespace
33 
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>();
45 }
46 
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));
54  }
55  return arith::ConstantOp::materialize(builder, value, type, loc);
56 }
57 
58 #define GET_ATTRDEF_CLASSES
59 #include "mlir/Dialect/Complex/IR/ComplexAttributes.cpp.inc"
60 
61 LogicalResult complex::NumberAttr::verify(
63  ::llvm::APFloat real, ::llvm::APFloat imag, ::mlir::Type type) {
64 
65  if (!llvm::isa<ComplexType>(type))
66  return emitError() << "complex attribute must be a complex type.";
67 
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.";
72 
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";
81 
82  return success();
83 }
84 
85 void complex::NumberAttr::print(AsmPrinter &printer) const {
86  printer << "<:" << llvm::cast<ComplexType>(getType()).getElementType() << " "
87  << getReal() << ", " << getImag() << ">";
88 }
89 
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 {};
97 
98  return NumberAttr::get(ComplexType::get(type), real, imag);
99 }
static Operation * materializeConstant(Dialect *dialect, OpBuilder &builder, Attribute value, Type type, Location loc)
A utility function used to materialize a constant for a given attribute and type.
Definition: FoldUtils.cpp:50
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.
static void print(spirv::VerCapExtAttr triple, DialectAsmPrinter &printer)
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.
Definition: InliningUtils.h:44
DialectInlinerInterface(Dialect *dialect)
Definition: InliningUtils.h:46
This is a utility class for mapping one set of IR entities to another.
Definition: IRMapping.h:26
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:314
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:66
This class helps build Operations.
Definition: Builders.h:215
Operation * create(const OperationState &state)
Creates an operation given the fields represented as an OperationState.
Definition: Builders.cpp:497
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition: Region.h:26
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
QueryRef parse(llvm::StringRef line, const QuerySession &qs)
Definition: Query.cpp:20
Include the generated interface declarations.
Type getType(OpFoldResult ofr)
Returns the int type of the integer in ofr.
Definition: Utils.cpp:305
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...
LogicalResult verify(Operation *op, bool verifyRecursively=true)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs,...
Definition: Verifier.cpp:426