MLIR  21.0.0git
Attribute.cpp
Go to the documentation of this file.
1 //===- Attribute.cpp - Attribute 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 // Attribute wrapper to simplify using TableGen Record defining a MLIR
10 // Attribute.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #include "mlir/TableGen/Operator.h"
15 #include "llvm/TableGen/Record.h"
16 
17 using namespace mlir;
18 using namespace mlir::tblgen;
19 
20 using llvm::DefInit;
21 using llvm::Init;
22 using llvm::Record;
23 using llvm::StringInit;
24 
25 // Returns the initializer's value as string if the given TableGen initializer
26 // is a code or string initializer. Returns the empty StringRef otherwise.
27 static StringRef getValueAsString(const Init *init) {
28  if (const auto *str = dyn_cast<StringInit>(init))
29  return str->getValue().trim();
30  return {};
31 }
32 
33 bool AttrConstraint::isSubClassOf(StringRef className) const {
34  return def->isSubClassOf(className);
35 }
36 
37 Attribute::Attribute(const Record *record) : AttrConstraint(record) {
38  assert(record->isSubClassOf("Attr") &&
39  "must be subclass of TableGen 'Attr' class");
40 }
41 
42 Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {}
43 
44 bool Attribute::isDerivedAttr() const { return isSubClassOf("DerivedAttr"); }
45 
46 bool Attribute::isTypeAttr() const { return isSubClassOf("TypeAttrBase"); }
47 
49  StringRef defName = def->getName();
50  if (defName == "SymbolRefAttr" || defName == "FlatSymbolRefAttr")
51  return true;
52  return isSubClassOf("SymbolRefAttr") || isSubClassOf("FlatSymbolRefAttr");
53 }
54 
55 bool Attribute::isEnumAttr() const { return isSubClassOf("EnumAttrInfo"); }
56 
57 StringRef Attribute::getStorageType() const {
58  const auto *init = def->getValueInit("storageType");
59  auto type = getValueAsString(init);
60  if (type.empty())
61  return "::mlir::Attribute";
62  return type;
63 }
64 
65 StringRef Attribute::getReturnType() const {
66  const auto *init = def->getValueInit("returnType");
67  return getValueAsString(init);
68 }
69 
70 // Return the type constraint corresponding to the type of this attribute, or
71 // std::nullopt if this is not a TypedAttr.
72 std::optional<Type> Attribute::getValueType() const {
73  if (const auto *defInit = dyn_cast<DefInit>(def->getValueInit("valueType")))
74  return Type(defInit->getDef());
75  return std::nullopt;
76 }
77 
79  const auto *init = def->getValueInit("convertFromStorage");
80  return getValueAsString(init);
81 }
82 
84  const auto *init = def->getValueInit("constBuilderCall");
85  return !getValueAsString(init).empty();
86 }
87 
89  const auto *init = def->getValueInit("constBuilderCall");
90  return getValueAsString(init);
91 }
92 
94  if (const auto *defInit = dyn_cast<DefInit>(def->getValueInit("baseAttr"))) {
95  return Attribute(defInit).getBaseAttr();
96  }
97  return *this;
98 }
99 
101  const auto *init = def->getValueInit("defaultValue");
102  return !getValueAsString(init).empty();
103 }
104 
105 StringRef Attribute::getDefaultValue() const {
106  const auto *init = def->getValueInit("defaultValue");
107  return getValueAsString(init);
108 }
109 
110 bool Attribute::isOptional() const { return def->getValueAsBit("isOptional"); }
111 
112 StringRef Attribute::getAttrDefName() const {
113  if (def->isAnonymous()) {
114  return getBaseAttr().def->getName();
115  }
116  return def->getName();
117 }
118 
119 StringRef Attribute::getDerivedCodeBody() const {
120  assert(isDerivedAttr() && "only derived attribute has 'body' field");
121  return def->getValueAsString("body");
122 }
123 
125  const llvm::RecordVal *record = def->getValue("dialect");
126  if (record && record->getValue()) {
127  if (const DefInit *init = dyn_cast<DefInit>(record->getValue()))
128  return Dialect(init->getDef());
129  }
130  return Dialect(nullptr);
131 }
132 
133 const Record &Attribute::getDef() const { return *def; }
134 
135 ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) {
136  assert(def->isSubClassOf("ConstantAttr") &&
137  "must be subclass of TableGen 'ConstantAttr' class");
138 }
139 
141  return Attribute(def->getValueAsDef("attr"));
142 }
143 
145  return def->getValueAsString("value");
146 }
147 
148 const char * ::mlir::tblgen::inferTypeOpInterface = "InferTypeOpInterface";
static StringRef getValueAsString(const Init *init)
Definition: Attribute.cpp:27
Attributes are known-constant values of operations.
Definition: Attributes.h:25
Dialects are groups of MLIR operations, types and attributes, as well as behavior associated with the...
Definition: Dialect.h:38
bool isSubClassOf(StringRef className) const
Definition: Attribute.cpp:33
StringRef getConstBuilderTemplate() const
Definition: Attribute.cpp:88
bool isConstBuildable() const
Definition: Attribute.cpp:83
StringRef getConvertFromStorageCall() const
Definition: Attribute.cpp:78
StringRef getStorageType() const
Definition: Attribute.cpp:57
bool hasDefaultValue() const
Definition: Attribute.cpp:100
StringRef getDefaultValue() const
Definition: Attribute.cpp:105
Attribute(const llvm::Record *record)
const llvm::Record & getDef() const
Definition: Attribute.cpp:133
StringRef getAttrDefName() const
Definition: Attribute.cpp:112
StringRef getDerivedCodeBody() const
Definition: Attribute.cpp:119
StringRef getReturnType() const
Definition: Attribute.cpp:65
bool isDerivedAttr() const
Definition: Attribute.cpp:44
std::optional< Type > getValueType() const
Definition: Attribute.cpp:72
bool isSymbolRefAttr() const
Definition: Attribute.cpp:48
Dialect getDialect() const
Attribute getBaseAttr() const
Definition: Attribute.cpp:93
bool isEnumAttr() const
Definition: Attribute.cpp:55
bool isTypeAttr() const
Definition: Attribute.cpp:46
ConstantAttr(const llvm::DefInit *init)
Definition: Attribute.cpp:135
StringRef getConstantValue() const
Definition: Attribute.cpp:144
Attribute getAttribute() const
Definition: Attribute.cpp:140
const llvm::Record * def
Definition: Constraint.h:90
const char * inferTypeOpInterface
Definition: Attribute.cpp:148
Include the generated interface declarations.