@@ -97,18 +97,16 @@ static bool extractUIntBitwidth(StringRef uintType, int64_t &bitwidth) {
97
97
// / Emits an attribute builder for the given enum attribute to support automatic
98
98
// / conversion between enum values and attributes in Python. Returns
99
99
// / `false` on success, `true` on failure.
100
- static bool emitAttributeBuilder (const EnumAttr &enumAttr, raw_ostream &os) {
100
+ static bool emitAttributeBuilderRegistration (const EnumAttr &enumAttr,
101
+ raw_ostream &os) {
101
102
int64_t bitwidth;
102
103
if (extractUIntBitwidth (enumAttr.getUnderlyingType (), bitwidth)) {
103
104
llvm::errs () << " failed to identify bitwidth of "
104
105
<< enumAttr.getUnderlyingType ();
105
106
return true ;
106
107
}
107
108
108
- llvm::SmallVector<StringRef> namespaces;
109
- enumAttr.getStorageType ().ltrim (" ::" ).split (namespaces, " ::" );
110
- namespaces = llvm::SmallVector<StringRef>{llvm::drop_end (namespaces)};
111
- std::string namespace_ = getAttributeNameSpace (namespaces);
109
+ std::string namespace_ = getEnumAttributeNameSpace (enumAttr);
112
110
if (!namespace_.empty ())
113
111
namespace_ += " _" ;
114
112
@@ -127,8 +125,9 @@ static bool emitAttributeBuilder(const EnumAttr &enumAttr, raw_ostream &os) {
127
125
// / Emits an attribute builder for the given dialect enum attribute to support
128
126
// / automatic conversion between enum values and attributes in Python. Returns
129
127
// / `false` on success, `true` on failure.
130
- static bool emitDialectEnumAttributeBuilder (const AttrOrTypeDef &attr,
131
- raw_ostream &os) {
128
+ static bool emitDialectEnumAttributeBuilderRegistration (const llvm::Record &def,
129
+ raw_ostream &os) {
130
+ const AttrOrTypeDef attr (&def);
132
131
StringRef mnemonic = attr.getMnemonic ().value ();
133
132
std::optional<StringRef> assemblyFormat = attr.getAssemblyFormat ();
134
133
StringRef dialect = attr.getDialect ().getName ();
@@ -145,15 +144,15 @@ static bool emitDialectEnumAttributeBuilder(const AttrOrTypeDef &attr,
145
144
return true ;
146
145
}
147
146
148
- llvm::SmallVector<StringRef> namespaces;
149
- attr.getStorageNamespace ().ltrim (" ::" ).split (namespaces, " ::" );
150
- std::string namespace_ = getAttributeNameSpace (namespaces);
147
+ EnumAttr enumAttr (def);
148
+ std::string namespace_ = getEnumAttributeNameSpace (enumAttr);
151
149
if (!namespace_.empty ())
152
150
namespace_ += " _" ;
153
151
154
152
os << llvm::formatv (" @register_attribute_builder(\" {0}{1}\" )\n " , namespace_,
155
- attr.getName ());
156
- os << llvm::formatv (" def _{0}(x, context):\n " , attr.getName ().lower ());
153
+ enumAttr.getAttrDefName ());
154
+ os << llvm::formatv (" def _{0}(x, context):\n " ,
155
+ enumAttr.getAttrDefName ().lower ());
157
156
os << llvm::formatv (" return "
158
157
" _ods_ir.Attribute.parse(f'{0}', context=context)\n\n " ,
159
158
formatString);
@@ -164,17 +163,32 @@ static bool emitDialectEnumAttributeBuilder(const AttrOrTypeDef &attr,
164
163
// / `false` on success, `true` on failure.
165
164
static bool emitPythonEnums (const llvm::RecordKeeper &recordKeeper,
166
165
raw_ostream &os) {
166
+ llvm::SmallDenseSet<StringRef> alreadyEmitted;
167
167
os << fileHeader;
168
+ for (const auto &it :
169
+ recordKeeper.getAllDerivedDefinitionsIfDefined (" EnumAttr" )) {
170
+ EnumAttr *enumAttr;
171
+ for (const auto &value : it->getValues ())
172
+ if (value.getType ()->getAsString () == " EnumAttrInfo" )
173
+ enumAttr = new EnumAttr (value.getValue ()->getRecordKeeper ().getDef (
174
+ value.getValue ()->getAsString ()));
175
+ if (enumAttr) {
176
+ emitEnumClass (*enumAttr, os);
177
+ alreadyEmitted.insert (enumAttr->getEnumClassName ());
178
+ }
179
+ }
168
180
for (auto &it :
169
181
recordKeeper.getAllDerivedDefinitionsIfDefined (" EnumAttrInfo" )) {
170
182
EnumAttr enumAttr (*it);
171
- emitEnumClass (enumAttr, os);
172
- emitAttributeBuilder (enumAttr, os);
183
+ if (!alreadyEmitted.contains (enumAttr.getEnumClassName ()))
184
+ emitEnumClass (enumAttr, os);
185
+ if (emitAttributeBuilderRegistration (enumAttr, os))
186
+ return true ;
173
187
}
174
188
for (const auto &it :
175
189
recordKeeper.getAllDerivedDefinitionsIfDefined (" EnumAttr" )) {
176
- const AttrOrTypeDef attr (& *it);
177
- return emitDialectEnumAttributeBuilder (attr, os) ;
190
+ if ( emitDialectEnumAttributeBuilderRegistration ( *it, os))
191
+ return true ;
178
192
}
179
193
180
194
return false ;
0 commit comments