Skip to content

Commit 91aa371

Browse files
committed
Add unit test compiling and exercising an interface method in extraClassOf
1 parent 8cc3353 commit 91aa371

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

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

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,4 +134,17 @@ class TestEffects<list<TestEffect> effects = []>
134134

135135
def TestConcreteEffect : TestEffect<"TestEffects::Concrete">;
136136

137+
def TestOptionallyImplementedOpInterface
138+
: OpInterface<"TestOptionallyImplementedOpInterface"> {
139+
let cppNamespace = "::mlir";
140+
141+
let methods = [
142+
InterfaceMethod<"", "bool", "getImplementsInterface", (ins)>,
143+
];
144+
145+
let extraClassOf = [{
146+
return $_op.getImplementsInterface();
147+
}];
148+
}
149+
137150
#endif // MLIR_TEST_DIALECT_TEST_INTERFACES

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2952,4 +2952,10 @@ def TestStoreWithARegionTerminator : TEST_Op<"store_with_a_region_terminator",
29522952
let assemblyFormat = "attr-dict";
29532953
}
29542954

2955+
def TestOpOptionallyImplementingInterface
2956+
: TEST_Op<"op_optionally_implementing_interface",
2957+
[TestOptionallyImplementedOpInterface]> {
2958+
let arguments = (ins BoolAttr:$implementsInterface);
2959+
}
2960+
29552961
#endif // TEST_OPS

mlir/unittests/IR/InterfaceTest.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,15 @@ TEST(InterfaceTest, TypeInterfaceDenseMapKey) {
5757
EXPECT_TRUE(typeSet.contains(type2));
5858
EXPECT_FALSE(typeSet.contains(type3));
5959
}
60+
61+
TEST(InterfaceTest, TestCustomClassOf) {
62+
MLIRContext context;
63+
context.loadDialect<test::TestDialect>();
64+
65+
OpBuilder builder(&context);
66+
auto op = builder.create<TestOpOptionallyImplementingInterface>(
67+
builder.getUnknownLoc(), /*implementsInterface=*/true);
68+
EXPECT_TRUE(isa<TestOptionallyImplementedOpInterface>(*op));
69+
op.setImplementsInterface(false);
70+
EXPECT_FALSE(isa<TestOptionallyImplementedOpInterface>(*op));
71+
}

0 commit comments

Comments
 (0)