Skip to content

Commit 8cc3353

Browse files
committed
[mlir] Make classof substitution in interface use an instance
The substitution supported by `extraClassOf` is currently limited to only the base instance, i.e. `Operation*`, `Type` or `Attribute`, which limits the kind of checks you can perform in the `classof` implementation. Since prior to the user code, the interface concept is fetched, we can use it to construct an instance of the interface, allowing use of its methods in the `classof` check. Since an instance of the interface allows access to the base class methods through the `->` operator, I've gone ahead and replaced the substitution of `$_op/$_type/$_attr` with an interface instance. This is also consistent with `extraSharedClassDeclaration` and other methods created in the interface class which do the same.
1 parent 5ccda55 commit 8cc3353

File tree

3 files changed

+9
-5
lines changed

3 files changed

+9
-5
lines changed

mlir/include/mlir/IR/Interfaces.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ class Interface<string name, list<Interface> baseInterfacesArg = []> {
114114
// be used to better enable "optional" interfaces, where an entity only
115115
// implements the interface if some dynamic characteristic holds.
116116
// `$_attr`/`$_op`/`$_type` may be used to refer to an instance of the
117-
// entity being checked.
117+
// interface instance being checked.
118118
code extraClassOf = "";
119119

120120
// An optional set of base interfaces that this interface

mlir/test/mlir-tblgen/op-interface.td

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,11 @@ def ExtraClassOfInterface : OpInterface<"ExtraClassOfInterface"> {
1111

1212
// DECL: class ExtraClassOfInterface
1313
// DECL: static bool classof(::mlir::Operation * base) {
14-
// DECL-NEXT: if (!getInterfaceFor(base))
14+
// DECL-NEXT: auto* concept = getInterfaceFor(base);
15+
// DECL-NEXT: if (!concept)
1516
// DECL-NEXT: return false;
16-
// DECL-NEXT: return base->someOtherMethod();
17+
// DECL-NEXT: ExtraClassOfInterface odsInterfaceInstance(base, concept);
18+
// DECL-NEXT: return odsInterfaceInstance->someOtherMethod();
1719
// DECL-NEXT: }
1820

1921
def ExtraShardDeclsInterface : OpInterface<"ExtraShardDeclsInterface"> {

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -582,10 +582,12 @@ void InterfaceGenerator::emitInterfaceDecl(const Interface &interface) {
582582
// Emit classof code if necessary.
583583
if (std::optional<StringRef> extraClassOf = interface.getExtraClassOf()) {
584584
auto extraClassOfFmt = tblgen::FmtContext();
585-
extraClassOfFmt.addSubst(substVar, "base");
585+
extraClassOfFmt.addSubst(substVar, "odsInterfaceInstance");
586586
os << " static bool classof(" << valueType << " base) {\n"
587-
<< " if (!getInterfaceFor(base))\n"
587+
<< " auto* concept = getInterfaceFor(base);\n"
588+
<< " if (!concept)\n"
588589
" return false;\n"
590+
" " << interfaceName << " odsInterfaceInstance(base, concept);\n"
589591
<< " " << tblgen::tgfmt(extraClassOf->trim(), &extraClassOfFmt)
590592
<< "\n }\n";
591593
}

0 commit comments

Comments
 (0)