Skip to content

Commit 4e1f546

Browse files
committed
getOperationName -> getInterfaceName, add HasInterfaceParent trait, TestDialect tests
1 parent e0ad57e commit 4e1f546

File tree

7 files changed

+55
-2
lines changed

7 files changed

+55
-2
lines changed

mlir/include/mlir/IR/OpBase.td

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,11 @@ class ParentOneOf<list<string> ops>
141141
: ParamNativeOpTrait<"HasParent", !interleave(ops, ", ")>,
142142
StructuralOpTrait;
143143

144+
// Op's parent operation implements the provided interface.
145+
class HasInterfaceParent<string interface>
146+
: ParamNativeOpTrait<"HasInterfaceParent", interface>,
147+
StructuralOpTrait;
148+
144149
// Op result type is derived from the first attribute. If the attribute is an
145150
// subclass of `TypeAttrBase`, its value is used, otherwise, the type of the
146151
// attribute content is used.

mlir/include/mlir/IR/OpDefinition.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,6 +1334,24 @@ struct HasParent {
13341334
};
13351335
};
13361336

1337+
/// This class provides a verifier for ops that are expecting their parent to
1338+
/// implement a specific interface.
1339+
template <typename ParentInterfaceType>
1340+
struct HasInterfaceParent {
1341+
template <typename ConcreteType>
1342+
class Impl : public TraitBase<ConcreteType, Impl> {
1343+
public:
1344+
static LogicalResult verifyTrait(Operation *op) {
1345+
if (llvm::isa_and_nonnull<ParentInterfaceType>(op->getParentOp()))
1346+
return success();
1347+
1348+
return op->emitOpError()
1349+
<< "expects parent op to implement interface '"
1350+
<< ParentInterfaceType::getInterfaceName() << "'";
1351+
}
1352+
};
1353+
};
1354+
13371355
/// A trait for operations that have an attribute specifying operand segments.
13381356
///
13391357
/// Certain operations can have multiple variadic operands and their size

mlir/test/IR/traits.mlir

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -668,3 +668,21 @@ func.func @failed_attr_traits() {
668668
"test.attr_with_trait"() {attr = 42 : i32} : () -> ()
669669
return
670670
}
671+
672+
// -----
673+
674+
func.func @has_interface_parent_trait() {
675+
// CHECK: "test.interface_parent"() ({
676+
// CHECK: "test.interface_child"() : () -> ()
677+
"test.interface_parent"() ({
678+
"test.interface_child"() : () -> ()
679+
}) : () -> ()
680+
return
681+
}
682+
683+
// -----
684+
685+
func.func @illegal_interface_parent_trait() {
686+
// expected-error@+1 {{'test.interface_child' op expects parent op to implement interface 'TestInterfaceParentInterface'}}
687+
"test.interface_child"() : () -> ()
688+
}

mlir/test/lib/Dialect/Test/TestInterfaces.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,4 +147,8 @@ def TestOptionallyImplementedOpInterface
147147
}];
148148
}
149149

150+
def TestInterfaceParentInterface : OpInterface<"TestInterfaceParentInterface"> {
151+
let cppNamespace = "::mlir";
152+
}
153+
150154
#endif // MLIR_TEST_DIALECT_TEST_INTERFACES

mlir/test/lib/Dialect/Test/TestOps.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -750,6 +750,14 @@ def ParentOp1 : TEST_Op<"parent1"> {
750750
def ChildWithParentOneOf : TEST_Op<"child_with_parent_one_of",
751751
[ParentOneOf<["ParentOp", "ParentOp1"]>]>;
752752

753+
// HasInterfaceParent trait
754+
def InterfaceParentOp : TEST_Op<"interface_parent",
755+
[SingleBlock, NoTerminator, TestInterfaceParentInterface]> {
756+
let regions = (region SizedRegion<1>:$region);
757+
}
758+
def InterfaceChildOp : TEST_Op<"interface_child",
759+
[HasInterfaceParent<"mlir::TestInterfaceParentInterface">]>;
760+
753761
def TerminatorOp : TEST_Op<"finish", [Terminator]>;
754762
def SingleBlockImplicitTerminatorOp : TEST_Op<"SingleBlockImplicitTerminator",
755763
[SingleBlockImplicitTerminator<"TerminatorOp">]> {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ def DeclareMethodsWithDefaultOp : Op<TestDialect, "declare_methods_op",
172172
// DECL: class TestOpInterface : public ::mlir::OpInterface<TestOpInterface, detail::TestOpInterfaceInterfaceTraits>
173173

174174
// DECL: /// Returns the name of this interface.
175-
// DECL: static ::llvm::StringLiteral getOperationName() { return ::llvm::StringLiteral( "TestOpInterface"); }
175+
// DECL: static ::llvm::StringLiteral getInterfaceName() { return ::llvm::StringLiteral( "TestOpInterface"); }
176176

177177
// DECL: /// some function comment
178178
// DECL: int foo(int input);

mlir/tools/mlir-tblgen/OpInterfacesGen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -499,7 +499,7 @@ static void emitInterfaceNameGetter(const Interface &interface,
499499
if (!isa<OpInterface>(interface))
500500
return;
501501
os << " /// Returns the name of this interface.\n"
502-
<< " static ::llvm::StringLiteral getOperationName() { return "
502+
<< " static ::llvm::StringLiteral getInterfaceName() { return "
503503
"::llvm::StringLiteral( \""
504504
<< interface.getName() << "\"); }\n";
505505
}

0 commit comments

Comments
 (0)