1 | //===- mlir/unittest/IR/ValueTest.cpp - Value unit tests ------------------===// |
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/Value.h" |
10 | #include "../../test/lib/Dialect/Test/TestDialect.h" |
11 | #include "../../test/lib/Dialect/Test/TestOps.h" |
12 | #include "mlir/IR/Builders.h" |
13 | #include "mlir/IR/BuiltinTypes.h" |
14 | #include "mlir/IR/OperationSupport.h" |
15 | #include "gtest/gtest.h" |
16 | |
17 | using namespace mlir; |
18 | |
19 | static Operation *createOp(MLIRContext *context, |
20 | ArrayRef<Value> operands = std::nullopt, |
21 | ArrayRef<Type> resultTypes = std::nullopt, |
22 | unsigned int numRegions = 0) { |
23 | context->allowUnregisteredDialects(); |
24 | return Operation::create( |
25 | UnknownLoc::get(context), OperationName("foo.bar" , context), resultTypes, |
26 | operands, std::nullopt, nullptr, std::nullopt, numRegions); |
27 | } |
28 | |
29 | namespace { |
30 | |
31 | TEST(ValueTest, getNumUses) { |
32 | MLIRContext context; |
33 | Builder builder(&context); |
34 | |
35 | Operation *op0 = |
36 | createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16)); |
37 | |
38 | Value v0 = op0->getResult(idx: 0); |
39 | EXPECT_EQ(v0.getNumUses(), (unsigned)0); |
40 | |
41 | Operation *op1 = createOp(&context, {v0}, builder.getIntegerType(16)); |
42 | EXPECT_EQ(v0.getNumUses(), (unsigned)1); |
43 | |
44 | Operation *op2 = createOp(&context, {v0, v0}, builder.getIntegerType(16)); |
45 | EXPECT_EQ(v0.getNumUses(), (unsigned)3); |
46 | |
47 | op2->destroy(); |
48 | op1->destroy(); |
49 | op0->destroy(); |
50 | } |
51 | |
52 | TEST(ValueTest, hasNUses) { |
53 | MLIRContext context; |
54 | Builder builder(&context); |
55 | |
56 | Operation *op0 = |
57 | createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16)); |
58 | Value v0 = op0->getResult(idx: 0); |
59 | EXPECT_TRUE(v0.hasNUses(0)); |
60 | EXPECT_FALSE(v0.hasNUses(1)); |
61 | |
62 | Operation *op1 = createOp(&context, {v0}, builder.getIntegerType(16)); |
63 | EXPECT_FALSE(v0.hasNUses(0)); |
64 | EXPECT_TRUE(v0.hasNUses(1)); |
65 | |
66 | Operation *op2 = createOp(&context, {v0, v0}, builder.getIntegerType(16)); |
67 | EXPECT_FALSE(v0.hasNUses(0)); |
68 | EXPECT_FALSE(v0.hasNUses(1)); |
69 | EXPECT_TRUE(v0.hasNUses(3)); |
70 | |
71 | op2->destroy(); |
72 | op1->destroy(); |
73 | op0->destroy(); |
74 | } |
75 | |
76 | TEST(ValueTest, hasNUsesOrMore) { |
77 | MLIRContext context; |
78 | Builder builder(&context); |
79 | |
80 | Operation *op0 = |
81 | createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16)); |
82 | Value v0 = op0->getResult(idx: 0); |
83 | EXPECT_TRUE(v0.hasNUsesOrMore(0)); |
84 | EXPECT_FALSE(v0.hasNUsesOrMore(1)); |
85 | |
86 | Operation *op1 = createOp(&context, {v0}, builder.getIntegerType(16)); |
87 | EXPECT_TRUE(v0.hasNUsesOrMore(0)); |
88 | EXPECT_TRUE(v0.hasNUsesOrMore(1)); |
89 | EXPECT_FALSE(v0.hasNUsesOrMore(2)); |
90 | |
91 | Operation *op2 = createOp(&context, {v0, v0}, builder.getIntegerType(16)); |
92 | EXPECT_TRUE(v0.hasNUsesOrMore(0)); |
93 | EXPECT_TRUE(v0.hasNUsesOrMore(1)); |
94 | EXPECT_TRUE(v0.hasNUsesOrMore(3)); |
95 | EXPECT_FALSE(v0.hasNUsesOrMore(4)); |
96 | |
97 | op2->destroy(); |
98 | op1->destroy(); |
99 | op0->destroy(); |
100 | } |
101 | |
102 | } // end anonymous namespace |
103 | |