MLIR 24.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
56 return isSubClassOf("EnumAttrInfo") || isSubClassOf("EnumAttr");
57}
58
59StringRef Attribute::getStorageType() const {
60 const auto *init = def->getValueInit("storageType");
61 auto type = getValueAsString(init);
62 if (type.empty())
63 return "::mlir::Attribute";
64 return type;
65}
66
67StringRef Attribute::getReturnType() const {
68 const auto *init = def->getValueInit("returnType");
69 return getValueAsString(init);
70}
71
72// Return the type constraint corresponding to the type of this attribute, or
73// std::nullopt if this is not a TypedAttr.
74std::optional<Type> Attribute::getValueType() const {
75 if (const auto *defInit = dyn_cast<DefInit>(def->getValueInit("valueType")))
76 return Type(defInit->getDef());
77 return std::nullopt;
78}
79
81 const auto *init = def->getValueInit("convertFromStorage");
82 return getValueAsString(init);
83}
84
86 const auto *init = def->getValueInit("constBuilderCall");
87 return !getValueAsString(init).empty();
88}
89
91 const auto *init = def->getValueInit("constBuilderCall");
92 return getValueAsString(init);
93}
94
96 if (const auto *defInit = dyn_cast<DefInit>(def->getValueInit("baseAttr"))) {
97 return Attribute(defInit).getBaseAttr();
98 }
99 return *this;
100}
101
103 const auto *init = def->getValueInit("defaultValue");
104 return !getValueAsString(init).empty();
105}
106
107StringRef Attribute::getDefaultValue() const {
108 const auto *init = def->getValueInit("defaultValue");
109 return getValueAsString(init);
110}
111
112bool Attribute::isOptional() const { return def->getValueAsBit("isOptional"); }
113
114StringRef Attribute::getAttrDefName() const {
115 if (def->isAnonymous()) {
116 return getBaseAttr().def->getName();
117 }
118 return def->getName();
119}
120
122 assert(isDerivedAttr() && "only derived attribute has 'body' field");
123 return def->getValueAsString("body");
124}
125
127 const llvm::RecordVal *record = def->getValue("dialect");
128 if (record && record->getValue()) {
129 if (const DefInit *init = dyn_cast<DefInit>(record->getValue()))
130 return Dialect(init->getDef());
131 }
132 return Dialect(nullptr);
133}
134
135const Record &Attribute::getDef() const { return *def; }
136
137ConstantAttr::ConstantAttr(const DefInit *init) : def(init->getDef()) {
138 assert(def->isSubClassOf("ConstantAttr") &&
139 "must be subclass of TableGen 'ConstantAttr' class");
140}
141
143 return Attribute(def->getValueAsDef("attr"));
144}
145
147 return def->getValueAsString("value");
148}
149
150const 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:90
bool isConstBuildable() const
Definition Attribute.cpp:85
StringRef getConvertFromStorageCall() const
Definition Attribute.cpp:80
StringRef getStorageType() const
Definition Attribute.cpp:59
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:67
bool isDerivedAttr() const
Definition Attribute.cpp:44
std::optional< Type > getValueType() const
Definition Attribute.cpp:74
bool isSymbolRefAttr() const
Definition Attribute.cpp:48
Dialect getDialect() const
Attribute getBaseAttr() const
Definition Attribute.cpp:95
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.