• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/Record.h"
15 
16 using namespace mlir;
17 using namespace mlir::tblgen;
Dialect(const llvm::Record * def)18 Dialect::Dialect(const llvm::Record *def) : def(def) {
19   if (def == nullptr)
20     return;
21   for (StringRef dialect : def->getValueAsListOfStrings("dependentDialects"))
22     dependentDialects.push_back(dialect);
23 }
24 
getName() const25 StringRef Dialect::getName() const { return def->getValueAsString("name"); }
26 
getCppNamespace() const27 StringRef Dialect::getCppNamespace() const {
28   return def->getValueAsString("cppNamespace");
29 }
30 
getCppClassName() const31 std::string Dialect::getCppClassName() const {
32   // Simply use the name and remove any '_' tokens.
33   std::string cppName = def->getName().str();
34   llvm::erase_if(cppName, [](char c) { return c == '_'; });
35   return cppName;
36 }
37 
getAsStringOrEmpty(const llvm::Record & record,StringRef fieldName)38 static StringRef getAsStringOrEmpty(const llvm::Record &record,
39                                     StringRef fieldName) {
40   if (auto valueInit = record.getValueInit(fieldName)) {
41     if (llvm::isa<llvm::StringInit>(valueInit))
42       return record.getValueAsString(fieldName);
43   }
44   return "";
45 }
46 
getSummary() const47 StringRef Dialect::getSummary() const {
48   return getAsStringOrEmpty(*def, "summary");
49 }
50 
getDescription() const51 StringRef Dialect::getDescription() const {
52   return getAsStringOrEmpty(*def, "description");
53 }
54 
getDependentDialects() const55 ArrayRef<StringRef> Dialect::getDependentDialects() const {
56   return dependentDialects;
57 }
58 
getExtraClassDeclaration() const59 llvm::Optional<StringRef> Dialect::getExtraClassDeclaration() const {
60   auto value = def->getValueAsString("extraClassDeclaration");
61   return value.empty() ? llvm::Optional<StringRef>() : value;
62 }
63 
hasConstantMaterializer() const64 bool Dialect::hasConstantMaterializer() const {
65   return def->getValueAsBit("hasConstantMaterializer");
66 }
67 
hasOperationAttrVerify() const68 bool Dialect::hasOperationAttrVerify() const {
69   return def->getValueAsBit("hasOperationAttrVerify");
70 }
71 
hasRegionArgAttrVerify() const72 bool Dialect::hasRegionArgAttrVerify() const {
73   return def->getValueAsBit("hasRegionArgAttrVerify");
74 }
75 
hasRegionResultAttrVerify() const76 bool Dialect::hasRegionResultAttrVerify() const {
77   return def->getValueAsBit("hasRegionResultAttrVerify");
78 }
79 
operator ==(const Dialect & other) const80 bool Dialect::operator==(const Dialect &other) const {
81   return def == other.def;
82 }
83 
operator <(const Dialect & other) const84 bool Dialect::operator<(const Dialect &other) const {
85   return getName() < other.getName();
86 }
87