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/TestTypes.h"
23 TEST(InterfaceTest
, OpInterfaceDenseMapKey
) {
25 context
.loadDialect
<test::TestDialect
>();
27 OwningOpRef
<ModuleOp
> module
= ModuleOp::create(UnknownLoc::get(&context
));
28 OpBuilder
builder(module
->getBody(), module
->getBody()->begin());
29 auto op1
= builder
.create
<test::SideEffectOp
>(builder
.getUnknownLoc(),
30 builder
.getI32Type());
31 auto op2
= builder
.create
<test::SideEffectOp
>(builder
.getUnknownLoc(),
32 builder
.getI32Type());
33 auto op3
= builder
.create
<test::SideEffectOp
>(builder
.getUnknownLoc(),
34 builder
.getI32Type());
35 DenseSet
<MemoryEffectOpInterface
> opSet
;
39 EXPECT_FALSE(opSet
.contains(op1
));
40 EXPECT_TRUE(opSet
.contains(op2
));
41 EXPECT_FALSE(opSet
.contains(op3
));
44 TEST(InterfaceTest
, TypeInterfaceDenseMapKey
) {
46 context
.loadDialect
<test::TestDialect
>();
48 OpBuilder
builder(&context
);
49 DenseSet
<DataLayoutTypeInterface
> typeSet
;
50 auto type1
= builder
.getType
<test::TestTypeWithLayoutType
>(1);
51 auto type2
= builder
.getType
<test::TestTypeWithLayoutType
>(2);
52 auto type3
= builder
.getType
<test::TestTypeWithLayoutType
>(3);
53 typeSet
.insert(type1
);
54 typeSet
.insert(type2
);
56 EXPECT_FALSE(typeSet
.contains(type1
));
57 EXPECT_TRUE(typeSet
.contains(type2
));
58 EXPECT_FALSE(typeSet
.contains(type3
));
61 TEST(InterfaceTest
, TestCustomClassOf
) {
63 context
.loadDialect
<test::TestDialect
>();
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
));
74 TEST(InterfaceTest
, TestImplicitConversion
) {
76 context
.loadDialect
<test::TestDialect
>();
78 TestBaseTypeInterfacePrintTypeB typeB
;
79 TestBaseTypeInterfacePrintTypeA typeA
= typeB
;
80 EXPECT_EQ(typeA
, nullptr);
82 typeB
= TestType::get(&context
);
84 EXPECT_EQ(typeA
, typeB
);