1//===- InterfaceTest.cpp - Test interfaces --------------------------------===//
2//
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
6//
7//===----------------------------------------------------------------------===//
8
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"
15
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"
20
21using namespace mlir;
22using namespace test;
23
24TEST(InterfaceTest, OpInterfaceDenseMapKey) {
25 MLIRContext context;
26 context.loadDialect<test::TestDialect>();
27
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;
37 opSet.insert(op1);
38 opSet.insert(op2);
39 opSet.erase(op1);
40 EXPECT_FALSE(opSet.contains(op1));
41 EXPECT_TRUE(opSet.contains(op2));
42 EXPECT_FALSE(opSet.contains(op3));
43}
44
45TEST(InterfaceTest, TypeInterfaceDenseMapKey) {
46 MLIRContext context;
47 context.loadDialect<test::TestDialect>();
48
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);
56 typeSet.erase(type1);
57 EXPECT_FALSE(typeSet.contains(type1));
58 EXPECT_TRUE(typeSet.contains(type2));
59 EXPECT_FALSE(typeSet.contains(type3));
60}
61
62TEST(InterfaceTest, TestCustomClassOf) {
63 MLIRContext context;
64 context.loadDialect<test::TestDialect>();
65
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));
72 op.erase();
73}
74
75TEST(InterfaceTest, TestImplicitConversion) {
76 MLIRContext context;
77 context.loadDialect<test::TestDialect>();
78
79 TestBaseTypeInterfacePrintTypeB typeB;
80 TestBaseTypeInterfacePrintTypeA typeA = typeB;
81 EXPECT_EQ(typeA, nullptr);
82
83 typeB = TestType::get(&context);
84 typeA = typeB;
85 EXPECT_EQ(typeA, typeB);
86}
87

source code of mlir/unittests/IR/InterfaceTest.cpp