1 //===- InterfaceTest.cpp - Test interfaces --------------------------------===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "mlir/IR/BuiltinAttributes.h"
10 #include "mlir/IR/BuiltinDialect.h"
11 #include "mlir/IR/BuiltinOps.h"
12 #include "mlir/IR/BuiltinTypes.h"
13 #include "mlir/IR/OwningOpRef.h"
14 #include "gtest/gtest.h"
16 #include "../../test/lib/Dialect/Test/TestAttributes.h"
17 #include "../../test/lib/Dialect/Test/TestDialect.h"
18 #include "../../test/lib/Dialect/Test/TestOps.h"
19 #include "../../test/lib/Dialect/Test/TestTypes.h"
24 TEST(InterfaceTest
, OpInterfaceDenseMapKey
) {
26 context
.loadDialect
<test::TestDialect
>();
28 OwningOpRef
<ModuleOp
> module
= ModuleOp::create(UnknownLoc::get(&context
));
29 OpBuilder
builder(module
->getBody(), module
->getBody()->begin());
30 auto op1
= builder
.create
<test::SideEffectOp
>(builder
.getUnknownLoc(),
31 builder
.getI32Type());
32 auto op2
= builder
.create
<test::SideEffectOp
>(builder
.getUnknownLoc(),
33 builder
.getI32Type());
34 auto op3
= builder
.create
<test::SideEffectOp
>(builder
.getUnknownLoc(),
35 builder
.getI32Type());
36 DenseSet
<MemoryEffectOpInterface
> opSet
;
40 EXPECT_FALSE(opSet
.contains(op1
));
41 EXPECT_TRUE(opSet
.contains(op2
));
42 EXPECT_FALSE(opSet
.contains(op3
));
45 TEST(InterfaceTest
, TypeInterfaceDenseMapKey
) {
47 context
.loadDialect
<test::TestDialect
>();
49 OpBuilder
builder(&context
);
50 DenseSet
<DataLayoutTypeInterface
> typeSet
;
51 auto type1
= builder
.getType
<test::TestTypeWithLayoutType
>(1);
52 auto type2
= builder
.getType
<test::TestTypeWithLayoutType
>(2);
53 auto type3
= builder
.getType
<test::TestTypeWithLayoutType
>(3);
54 typeSet
.insert(type1
);
55 typeSet
.insert(type2
);
57 EXPECT_FALSE(typeSet
.contains(type1
));
58 EXPECT_TRUE(typeSet
.contains(type2
));
59 EXPECT_FALSE(typeSet
.contains(type3
));
62 TEST(InterfaceTest
, TestCustomClassOf
) {
64 context
.loadDialect
<test::TestDialect
>();
66 OpBuilder
builder(&context
);
67 auto op
= builder
.create
<TestOpOptionallyImplementingInterface
>(
68 builder
.getUnknownLoc(), /*implementsInterface=*/true);
69 EXPECT_TRUE(isa
<TestOptionallyImplementedOpInterface
>(*op
));
70 op
.setImplementsInterface(false);
71 EXPECT_FALSE(isa
<TestOptionallyImplementedOpInterface
>(*op
));
75 TEST(InterfaceTest
, TestImplicitConversion
) {
77 context
.loadDialect
<test::TestDialect
>();
79 TestBaseTypeInterfacePrintTypeB typeB
;
80 TestBaseTypeInterfacePrintTypeA typeA
= typeB
;
81 EXPECT_EQ(typeA
, nullptr);
83 typeB
= TestType::get(&context
);
85 EXPECT_EQ(typeA
, typeB
);