Skip to content

Commit 517ced8

Browse files
[MLIR][TableGen] Add genMnemonicAlias field for OpAsm{Type,Attr}Interface
1 parent 0689d23 commit 517ced8

File tree

5 files changed

+50
-0
lines changed

5 files changed

+50
-0
lines changed

mlir/include/mlir/IR/AttrTypeBase.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,9 @@ class AttrOrTypeDef<string valueType, string name, list<Trait> defTraits,
249249
// generated code is placed inside the class's C++ namespace. `$cppClass` is
250250
// replaced by the class name.
251251
code extraClassDefinition = [{}];
252+
253+
// Generate a default 'getAlias' method for OpAsm{Type,Attr}Interface.
254+
bit genMnemonicAlias = 0;
252255
}
253256

254257
// Define a new attribute, named `name`, belonging to `dialect` that inherits

mlir/include/mlir/TableGen/AttrOrTypeDef.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -212,6 +212,10 @@ class AttrOrTypeDef {
212212
/// Returns the def's extra class definition code.
213213
std::optional<StringRef> getExtraDefs() const;
214214

215+
/// Returns true if we need to generate a default 'getAlias' implementation
216+
/// using the mnemonic.
217+
bool genMnemonicAlias() const;
218+
215219
/// Get the code location (for error printing).
216220
ArrayRef<SMLoc> getLoc() const;
217221

mlir/lib/TableGen/AttrOrTypeDef.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,10 @@ std::optional<StringRef> AttrOrTypeDef::getExtraDefs() const {
205205
return value.empty() ? std::optional<StringRef>() : value;
206206
}
207207

208+
bool AttrOrTypeDef::genMnemonicAlias() const {
209+
return def->getValueAsBit("genMnemonicAlias");
210+
}
211+
208212
ArrayRef<SMLoc> AttrOrTypeDef::getLoc() const { return def->getLoc(); }
209213

210214
bool AttrOrTypeDef::skipDefaultBuilders() const {

mlir/test/mlir-tblgen/attrdefs.td

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,3 +172,16 @@ def H_TestExtraClassAttr : TestAttr<"TestExtraClass"> {
172172
// DEF-LABEL: int TestExtraClassAttr::getFoo(int i) {
173173
// DEF: return i+1;
174174
// DEF-NEXT: }
175+
176+
def I_TestGenMnemonicAliasAttr : TestAttr<"TestGenMnemonicAlias"> {
177+
let mnemonic = "test_gen_mnemonic_alias";
178+
let genMnemonicAlias = 1;
179+
}
180+
181+
// DECL-LABEL: class TestGenMnemonicAliasAttr : public ::mlir::Attribute
182+
// DECL: ::mlir::OpAsmAliasResult getAlias(::llvm::raw_ostream &os) const;
183+
184+
// DEF-LABEL: ::mlir::OpAsmAliasResult TestGenMnemonicAliasAttr::getAlias(::llvm::raw_ostream &os) const {
185+
// DEF-NEXT: os << "test_gen_mnemonic_alias";
186+
// DEF-NEXT: return ::mlir::OpAsmAliasResult::OverridableAlias;
187+
// DEF-NEXT: }

mlir/tools/mlir-tblgen/AttrOrTypeDefGen.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,12 @@ class DefGen {
130130
/// Emit a trait method.
131131
void emitTraitMethod(const InterfaceMethod &method);
132132

133+
//===--------------------------------------------------------------------===//
134+
// OpAsm{Type,Attr}Interface Default Method Emission
135+
136+
/// Emit 'getAlias' method using mnemonic as alias.
137+
void emitMnemonicAliasMethod();
138+
133139
//===--------------------------------------------------------------------===//
134140
// Storage Class Emission
135141
void emitStorageClass();
@@ -215,6 +221,9 @@ DefGen::DefGen(const AttrOrTypeDef &def)
215221
emitAccessors();
216222
// Emit trait interface methods
217223
emitInterfaceMethods();
224+
// Emit OpAsm{Type,Attr}Interface default methods
225+
if (def.genMnemonicAlias())
226+
emitMnemonicAliasMethod();
218227
defCls.finalize();
219228
// Emit a storage class if one is needed
220229
if (storageCls && def.genStorageClass())
@@ -576,6 +585,23 @@ void DefGen::emitTraitMethod(const InterfaceMethod &method) {
576585
std::move(params));
577586
}
578587

588+
//===----------------------------------------------------------------------===//
589+
// OpAsm{Type,Attr}Interface Default Method Emission
590+
591+
void DefGen::emitMnemonicAliasMethod() {
592+
// If the mnemonic is not set, there is nothing to do.
593+
if (!def.getMnemonic()) {
594+
return;
595+
}
596+
597+
// Emit the mnemonic alias method.
598+
SmallVector<MethodParameter> params{{"::llvm::raw_ostream &", "os"}};
599+
Method *m = defCls.addMethod<Method::Const>("::mlir::OpAsmAliasResult",
600+
"getAlias", std::move(params));
601+
m->body().indent() << strfmt("os << \"{0}\";\n", *def.getMnemonic())
602+
<< "return ::mlir::OpAsmAliasResult::OverridableAlias;\n";
603+
}
604+
579605
//===----------------------------------------------------------------------===//
580606
// Storage Class Emission
581607

0 commit comments

Comments
 (0)