MLIR  16.0.0git
Dialect.cpp
Go to the documentation of this file.
1 //===- Dialect.cpp - Dialect wrapper class --------------------------------===//
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 //
9 // Dialect wrapper to simplify using TableGen Record defining a MLIR dialect.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/TableGen/Dialect.h"
14 #include "llvm/TableGen/Error.h"
15 #include "llvm/TableGen/Record.h"
16 
17 using namespace mlir;
18 using namespace mlir::tblgen;
19 Dialect::Dialect(const llvm::Record *def) : def(def) {
20  if (def == nullptr)
21  return;
22  for (StringRef dialect : def->getValueAsListOfStrings("dependentDialects"))
23  dependentDialects.push_back(dialect);
24 }
25 
26 StringRef Dialect::getName() const { return def->getValueAsString("name"); }
27 
28 StringRef Dialect::getCppNamespace() const {
29  return def->getValueAsString("cppNamespace");
30 }
31 
32 std::string Dialect::getCppClassName() const {
33  // Simply use the name and remove any '_' tokens.
34  std::string cppName = def->getName().str();
35  llvm::erase_value(cppName, '_');
36  return cppName;
37 }
38 
39 static StringRef getAsStringOrEmpty(const llvm::Record &record,
40  StringRef fieldName) {
41  if (auto *valueInit = record.getValueInit(fieldName)) {
42  if (llvm::isa<llvm::StringInit>(valueInit))
43  return record.getValueAsString(fieldName);
44  }
45  return "";
46 }
47 
48 StringRef Dialect::getSummary() const {
49  return getAsStringOrEmpty(*def, "summary");
50 }
51 
52 StringRef Dialect::getDescription() const {
53  return getAsStringOrEmpty(*def, "description");
54 }
55 
57  return dependentDialects;
58 }
59 
61  auto value = def->getValueAsString("extraClassDeclaration");
62  return value.empty() ? llvm::Optional<StringRef>() : value;
63 }
64 
66  return def->getValueAsBit("hasCanonicalizer");
67 }
68 
70  return def->getValueAsBit("hasConstantMaterializer");
71 }
72 
74  return def->getValueAsBit("hasNonDefaultDestructor");
75 }
76 
78  return def->getValueAsBit("hasOperationAttrVerify");
79 }
80 
82  return def->getValueAsBit("hasRegionArgAttrVerify");
83 }
84 
86  return def->getValueAsBit("hasRegionResultAttrVerify");
87 }
88 
90  return def->getValueAsBit("hasOperationInterfaceFallback");
91 }
92 
94  return def->getValueAsBit("useDefaultAttributePrinterParser");
95 }
96 
98  return def->getValueAsBit("useDefaultTypePrinterParser");
99 }
100 
102  int prefix = def->getValueAsInt("emitAccessorPrefix");
103  if (prefix < 0 || prefix > static_cast<int>(EmitPrefix::Both))
104  PrintFatalError(def->getLoc(), "Invalid accessor prefix value");
105 
106  return static_cast<EmitPrefix>(prefix);
107 }
108 
109 bool Dialect::isExtensible() const {
110  return def->getValueAsBit("isExtensible");
111 }
112 
113 bool Dialect::operator==(const Dialect &other) const {
114  return def == other.def;
115 }
116 
117 bool Dialect::operator<(const Dialect &other) const {
118  return getName() < other.getName();
119 }
Include the generated interface declarations.
StringRef getName() const
StringRef getSummary() const
Definition: Dialect.cpp:48
ArrayRef< StringRef > getDependentDialects() const
Definition: Dialect.cpp:56
static constexpr const bool value
bool hasRegionResultAttrVerify() const
Returns true if this dialect has a region result attribute verifier.
Definition: Dialect.cpp:85
bool useDefaultAttributePrinterParser() const
Returns true if this dialect should generate the default dispatch for attribute printing/parsing.
Definition: Dialect.cpp:93
bool hasRegionArgAttrVerify() const
Returns true if this dialect has a region argument attribute verifier.
Definition: Dialect.cpp:81
bool hasConstantMaterializer() const
Returns true if this dialect has a constant materializer.
Definition: Dialect.cpp:69
bool hasCanonicalizer() const
Returns true if this dialect has a canonicalizer.
Definition: Dialect.cpp:65
StringRef getCppNamespace() const
Definition: Dialect.cpp:28
bool hasNonDefaultDestructor() const
Returns true if the destructor definition is provided explicitly or false if a default should be gene...
Definition: Dialect.cpp:73
bool operator<(const Dialect &other) const
Definition: Dialect.cpp:117
llvm::Optional< StringRef > getExtraClassDeclaration() const
Definition: Dialect.cpp:60
std::string getCppClassName() const
Definition: Dialect.cpp:32
bool useDefaultTypePrinterParser() const
Returns true if this dialect should generate the default dispatch for type printing/parsing.
Definition: Dialect.cpp:97
bool hasOperationInterfaceFallback() const
Returns true if this dialect has fallback interfaces for its operations.
Definition: Dialect.cpp:89
StringRef getDescription() const
Definition: Dialect.cpp:52
static StringRef getAsStringOrEmpty(const llvm::Record &record, StringRef fieldName)
Definition: Dialect.cpp:39
bool isExtensible() const
Returns true if this dialect can be extended at runtime with new operations or types.
Definition: Dialect.cpp:109
EmitPrefix getEmitAccessorPrefix() const
Definition: Dialect.cpp:101
bool hasOperationAttrVerify() const
Returns true if this dialect has an operation attribute verifier.
Definition: Dialect.cpp:77
Dialect(const llvm::Record *def)
Definition: Dialect.cpp:19
bool operator==(const Dialect &other) const
Definition: Dialect.cpp:113