MLIR 22.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
15#include "llvm/TableGen/Record.h"
16
17using namespace mlir;
18using namespace mlir::tblgen;
19
20using llvm::DefInit;
21using llvm::Init;
22using llvm::Record;
23using 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.
27static StringRef getValueAsString(const Init *init) {
28 if (const auto *str = dyn_cast<StringInit>(init))
29 return str->getValue().trim();
30 return {};
31}
32
33bool AttrConstraint::isSubClassOf(StringRef className) const {
34 return def->isSubClassOf(className);
35}
36
37Attribute::Attribute(const Record *record) : AttrConstraint(record) {
38 assert(record->isSubClassOf("Attr") &&
39 "must be subclass of TableGen 'Attr' class");
40}
41
42Attribute::Attribute(const DefInit *init) : Attribute(init->getDef()) {}
43
44bool Attribute::isDerivedAttr() const { return isSubClassOf("DerivedAttr"); }
45
46bool 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
55bool Attribute::isEnumAttr() const { return isSubClassOf("EnumAttrInfo"); }
56
57StringRef 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
65StringRef 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.
72std::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
105StringRef Attribute::getDefaultValue() const {
106 const auto *init = def->getValueInit("defaultValue");
107 return getValueAsString(init);
108}
109
110bool Attribute::isOptional() const { return def->getValueAsBit("isOptional"); }
111
112StringRef Attribute::getAttrDefName() const {
113 if (def->isAnonymous()) {
114 return getBaseAttr().def->getName();
115 }
116 return def->getName();
117}
118
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
133const Record &Attribute::getDef() const { return *def; }
134
135ConstantAttr::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
148const 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
constexpr Attribute()=default
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
StringRef getDefaultValue() const
Attribute(const llvm::Record *record)
const llvm::Record & getDef() const
StringRef getAttrDefName() const
StringRef getDerivedCodeBody() const
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)
StringRef getConstantValue() const
Attribute getAttribute() const
const llvm::Record * def
Definition Constraint.h:90
const char * inferTypeOpInterface
Include the generated interface declarations.