MLIR  14.0.0git
PDLTypes.cpp
Go to the documentation of this file.
1 //===- PDLTypes.cpp - Pattern Descriptor Language Types -------------------===//
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 
11 #include "mlir/IR/Builders.h"
13 #include "llvm/ADT/TypeSwitch.h"
14 
15 using namespace mlir;
16 using namespace mlir::pdl;
17 
18 //===----------------------------------------------------------------------===//
19 // TableGen'd type method definitions
20 //===----------------------------------------------------------------------===//
21 
22 #define GET_TYPEDEF_CLASSES
23 #include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
24 
25 //===----------------------------------------------------------------------===//
26 // PDLDialect
27 //===----------------------------------------------------------------------===//
28 
29 void PDLDialect::registerTypes() {
30  addTypes<
31 #define GET_TYPEDEF_LIST
32 #include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
33  >();
34 }
35 
37  StringRef typeTag;
38  if (parser.parseKeyword(&typeTag))
39  return Type();
40  {
41  Type genType;
42  auto parseResult = generatedTypeParser(parser, typeTag, genType);
43  if (parseResult.hasValue())
44  return genType;
45  }
46 
47  // FIXME: This ends up with a double error being emitted if `RangeType` also
48  // emits an error. We should rework the `generatedTypeParser` to better
49  // support when the keyword is valid but the individual type parser itself
50  // emits an error.
51  parser.emitError(parser.getNameLoc(), "invalid 'pdl' type: `")
52  << typeTag << "'";
53  return Type();
54 }
55 
56 //===----------------------------------------------------------------------===//
57 // PDL Types
58 //===----------------------------------------------------------------------===//
59 
60 bool PDLType::classof(Type type) {
61  return llvm::isa<PDLDialect>(type.getDialect());
62 }
63 
64 //===----------------------------------------------------------------------===//
65 // RangeType
66 //===----------------------------------------------------------------------===//
67 
68 Type RangeType::parse(AsmParser &parser) {
69  if (parser.parseLess())
70  return Type();
71 
72  llvm::SMLoc elementLoc = parser.getCurrentLocation();
73  Type elementType = parsePDLType(parser);
74  if (!elementType || parser.parseGreater())
75  return Type();
76 
77  if (elementType.isa<RangeType>()) {
78  parser.emitError(elementLoc)
79  << "element of pdl.range cannot be another range, but got"
80  << elementType;
81  return Type();
82  }
83  return RangeType::get(elementType);
84 }
85 
86 void RangeType::print(AsmPrinter &printer) const {
87  printer << "<";
88  (void)generatedTypePrinter(getElementType(), printer);
89  printer << ">";
90 }
91 
93  Type elementType) {
94  if (!elementType.isa<PDLType>() || elementType.isa<RangeType>()) {
95  return emitError()
96  << "expected element of pdl.range to be one of [!pdl.attribute, "
97  "!pdl.operation, !pdl.type, !pdl.value], but got "
98  << elementType;
99  }
100  return success();
101 }
Include the generated interface declarations.
Dialect & getDialect() const
Get the dialect this type is registered to.
Definition: Types.h:114
constexpr Type()
Definition: Types.h:84
This class represents a diagnostic that is inflight and set to be reported.
Definition: Diagnostics.h:301
LogicalResult verify(Operation *op)
Perform (potentially expensive) checks of invariants, used to detect compiler bugs, on this operation and any nested operations.
Definition: Verifier.cpp:353
static Type getElementType(Type type, ArrayRef< int32_t > indices, function_ref< InFlightDiagnostic(StringRef)> emitErrorFn)
Walks the given type hierarchy with the given indices, potentially down to component granularity...
Definition: SPIRVOps.cpp:639
void print(OpAsmPrinter &p, FunctionLibraryOp op)
Definition: Shape.cpp:1112
This class represents the base class of all PDL types.
Definition: PDLTypes.h:25
virtual llvm::SMLoc getNameLoc() const =0
Return the location of the original name token.
static bool classof(Type type)
Definition: PDLTypes.cpp:60
LogicalResult success(bool isSuccess=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:56
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26
virtual ParseResult parseGreater()=0
Parse a &#39;>&#39; token.
virtual llvm::SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
ParseResult parseKeyword(StringRef keyword, const Twine &msg="")
Parse a given keyword.
virtual ParseResult parseLess()=0
Parse a &#39;<&#39; token.
This base class exposes generic asm parser hooks, usable across the various derived parsers...
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:72
static Type parsePDLType(AsmParser &parser)
Definition: PDLTypes.cpp:36
virtual InFlightDiagnostic emitError(llvm::SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
This base class exposes generic asm printer hooks, usable across the various derived printers...
bool isa() const
Definition: Types.h:234