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

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