|
| 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(0); |
| 39 | + EXPECT_EQ(v0.getNumUses(), (unsigned)0); |
| 40 | + |
| 41 | + createOp(&context, {v0}, builder.getIntegerType(16)); |
| 42 | + EXPECT_EQ(v0.getNumUses(), (unsigned)1); |
| 43 | + |
| 44 | + createOp(&context, {v0, v0}, builder.getIntegerType(16)); |
| 45 | + EXPECT_EQ(v0.getNumUses(), (unsigned)3); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(ValueTest, hasNUses) { |
| 49 | + MLIRContext context; |
| 50 | + Builder builder(&context); |
| 51 | + |
| 52 | + Operation *op = |
| 53 | + createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16)); |
| 54 | + Value v0 = op->getResult(0); |
| 55 | + EXPECT_TRUE(v0.hasNUses(0)); |
| 56 | + EXPECT_FALSE(v0.hasNUses(1)); |
| 57 | + |
| 58 | + createOp(&context, {v0}, builder.getIntegerType(16)); |
| 59 | + EXPECT_FALSE(v0.hasNUses(0)); |
| 60 | + EXPECT_TRUE(v0.hasNUses(1)); |
| 61 | + |
| 62 | + createOp(&context, {v0, v0}, builder.getIntegerType(16)); |
| 63 | + EXPECT_FALSE(v0.hasNUses(0)); |
| 64 | + EXPECT_FALSE(v0.hasNUses(1)); |
| 65 | + EXPECT_TRUE(v0.hasNUses(3)); |
| 66 | +} |
| 67 | + |
| 68 | +TEST(ValueTest, hasNUsesOrMore) { |
| 69 | + MLIRContext context; |
| 70 | + Builder builder(&context); |
| 71 | + |
| 72 | + Operation *op = |
| 73 | + createOp(&context, /*operands=*/std::nullopt, builder.getIntegerType(16)); |
| 74 | + Value v0 = op->getResult(0); |
| 75 | + EXPECT_TRUE(v0.hasNUsesOrMore(0)); |
| 76 | + EXPECT_FALSE(v0.hasNUsesOrMore(1)); |
| 77 | + |
| 78 | + createOp(&context, {v0}, builder.getIntegerType(16)); |
| 79 | + EXPECT_TRUE(v0.hasNUsesOrMore(0)); |
| 80 | + EXPECT_TRUE(v0.hasNUsesOrMore(1)); |
| 81 | + EXPECT_FALSE(v0.hasNUsesOrMore(2)); |
| 82 | + |
| 83 | + createOp(&context, {v0, v0}, builder.getIntegerType(16)); |
| 84 | + EXPECT_TRUE(v0.hasNUsesOrMore(0)); |
| 85 | + EXPECT_TRUE(v0.hasNUsesOrMore(1)); |
| 86 | + EXPECT_TRUE(v0.hasNUsesOrMore(3)); |
| 87 | + EXPECT_FALSE(v0.hasNUsesOrMore(4)); |
| 88 | +} |
| 89 | + |
| 90 | +} // end anonymous namespace |
0 commit comments