MLIR 24.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
12using namespace mlir;
13using namespace mlir::pdl;
14
15//===----------------------------------------------------------------------===//
16// TableGen'd type method definitions
17//===----------------------------------------------------------------------===//
18
19#define GET_TYPEDEF_CLASSES
20#include "mlir/Dialect/PDL/IR/PDLOpsTypes.cpp.inc"
21
22static Type parsePDLType(AsmParser &parser) {
23 StringRef typeTag;
24 {
25 Type genType;
26 auto parseResult = generatedTypeParser(parser, &typeTag, genType);
27 if (parseResult.has_value())
28 return genType;
29 }
30
31 // FIXME: This ends up with a double error being emitted if `RangeType` also
32 // emits an error. We should rework the `generatedTypeParser` to better
33 // support when the keyword is valid but the individual type parser itself
34 // emits an error.
35 parser.emitError(parser.getNameLoc(), "invalid 'pdl' type: `")
36 << typeTag << "'";
37 return Type();
38}
39
40//===----------------------------------------------------------------------===//
41// PDL Types
42//===----------------------------------------------------------------------===//
43
45 return llvm::isa<PDLDialect>(type.getDialect());
46}
47
49 if (auto rangeType = llvm::dyn_cast<RangeType>(type))
50 return rangeType.getElementType();
51 return type;
52}
53
54//===----------------------------------------------------------------------===//
55// RangeType
56//===----------------------------------------------------------------------===//
57
58Type RangeType::parse(AsmParser &parser) {
59 if (parser.parseLess())
60 return Type();
61
62 SMLoc elementLoc = parser.getCurrentLocation();
63 Type elementType = parsePDLType(parser);
64 if (!elementType || parser.parseGreater())
65 return Type();
66
67 if (llvm::isa<RangeType>(elementType)) {
68 parser.emitError(elementLoc)
69 << "element of pdl.range cannot be another range, but got"
70 << elementType;
71 return Type();
72 }
73 return RangeType::get(elementType);
74}
75
76void RangeType::print(AsmPrinter &printer) const {
77 printer << "<";
78 (void)generatedTypePrinter(getElementType(), printer);
79 printer << ">";
80}
81
82LogicalResult RangeType::verify(function_ref<InFlightDiagnostic()> emitError,
83 Type elementType) {
84 if (!llvm::isa<PDLType>(elementType) || llvm::isa<RangeType>(elementType)) {
85 return emitError()
86 << "expected element of pdl.range to be one of [!pdl.attribute, "
87 "!pdl.operation, !pdl.type, !pdl.value], but got "
88 << elementType;
89 }
90 return success();
91}
return success()
static OptionalParseResult generatedTypeParser(AsmParser &parser, StringRef *mnemonic, Type &value)
These are unused for now.
static LogicalResult generatedTypePrinter(Type def, AsmPrinter &printer)
static Type parsePDLType(AsmParser &parser)
Definition PDLTypes.cpp:22
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:230
This base class exposes generic asm parser hooks, usable across the various derived parsers.
virtual InFlightDiagnostic emitError(SMLoc loc, const Twine &message={})=0
Emit a diagnostic at the specified location and return failure.
virtual ParseResult parseLess()=0
Parse a '<' token.
virtual SMLoc getCurrentLocation()=0
Get the location of the next token and store it into the argument.
virtual SMLoc getNameLoc() const =0
Return the location of the original name token.
virtual ParseResult parseGreater()=0
Parse a '>' token.
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition Types.h:74
Dialect & getDialect() const
Get the dialect this type is registered to.
Definition Types.h:107
constexpr Type()=default
static bool classof(Type type)
Definition PDLTypes.cpp:44
Type getRangeElementTypeOrSelf(Type type)
If the given type is a range, return its element type, otherwise return the type itself.
Definition PDLTypes.cpp:48
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref
Definition LLVM.h:155