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/TestTypes.h"
19 
20 using namespace mlir;
21 using namespace test;
22 
23 TEST(InterfaceTest, OpInterfaceDenseMapKey) {
24   MLIRContext context;
25   context.loadDialect<test::TestDialect>();
26 
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;
36   opSet.insert(op1);
37   opSet.insert(op2);
38   opSet.erase(op1);
39   EXPECT_FALSE(opSet.contains(op1));
40   EXPECT_TRUE(opSet.contains(op2));
41   EXPECT_FALSE(opSet.contains(op3));
42 }
43 
44 TEST(InterfaceTest, AttrInterfaceDenseMapKey) {
45   MLIRContext context;
46   context.loadDialect<test::TestDialect>();
47 
48   OpBuilder builder(&context);
49 
50   DenseSet<SubElementAttrInterface> attrSet;
51   auto attr1 = builder.getArrayAttr({});
52   auto attr2 = builder.getI32ArrayAttr({0});
53   auto attr3 = builder.getI32ArrayAttr({1});
54   attrSet.insert(attr1);
55   attrSet.insert(attr2);
56   attrSet.erase(attr1);
57   EXPECT_FALSE(attrSet.contains(attr1));
58   EXPECT_TRUE(attrSet.contains(attr2));
59   EXPECT_FALSE(attrSet.contains(attr3));
60 }
61 
62 TEST(InterfaceTest, TypeInterfaceDenseMapKey) {
63   MLIRContext context;
64   context.loadDialect<test::TestDialect>();
65 
66   OpBuilder builder(&context);
67   DenseSet<DataLayoutTypeInterface> typeSet;
68   auto type1 = builder.getType<test::TestTypeWithLayoutType>(1);
69   auto type2 = builder.getType<test::TestTypeWithLayoutType>(2);
70   auto type3 = builder.getType<test::TestTypeWithLayoutType>(3);
71   typeSet.insert(type1);
72   typeSet.insert(type2);
73   typeSet.erase(type1);
74   EXPECT_FALSE(typeSet.contains(type1));
75   EXPECT_TRUE(typeSet.contains(type2));
76   EXPECT_FALSE(typeSet.contains(type3));
77 }
78