2019-03-27 05:45:38 +08:00
|
|
|
//===- OperationSupportTest.cpp - Operation support unit tests ------------===//
|
2019-03-06 05:37:48 +08:00
|
|
|
//
|
2020-01-26 11:58:30 +08:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
2019-12-24 01:35:36 +08:00
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2019-03-06 05:37:48 +08:00
|
|
|
//
|
2019-12-24 01:35:36 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2019-03-06 05:37:48 +08:00
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
#include "mlir/IR/OperationSupport.h"
|
2019-03-06 05:37:48 +08:00
|
|
|
#include "mlir/IR/Builders.h"
|
|
|
|
#include "mlir/IR/StandardTypes.h"
|
|
|
|
#include "gtest/gtest.h"
|
|
|
|
|
|
|
|
using namespace mlir;
|
|
|
|
using namespace mlir::detail;
|
|
|
|
|
2020-04-27 12:28:22 +08:00
|
|
|
static Operation *createOp(MLIRContext *context,
|
2020-03-30 06:35:38 +08:00
|
|
|
ArrayRef<Value> operands = llvm::None,
|
|
|
|
ArrayRef<Type> resultTypes = llvm::None) {
|
|
|
|
context->allowUnregisteredDialects();
|
2020-04-27 12:28:22 +08:00
|
|
|
return Operation::create(UnknownLoc::get(context),
|
|
|
|
OperationName("foo.bar", context), resultTypes,
|
|
|
|
operands, llvm::None, llvm::None, 0);
|
2019-03-06 05:37:48 +08:00
|
|
|
}
|
|
|
|
|
2020-03-30 06:35:38 +08:00
|
|
|
namespace {
|
2019-03-06 05:37:48 +08:00
|
|
|
TEST(OperandStorageTest, NonResizable) {
|
|
|
|
MLIRContext context;
|
|
|
|
Builder builder(&context);
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
Operation *useOp =
|
2020-04-27 12:28:22 +08:00
|
|
|
createOp(&context, /*operands=*/llvm::None, builder.getIntegerType(16));
|
2019-12-24 06:45:01 +08:00
|
|
|
Value operand = useOp->getResult(0);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// Create a non-resizable operation with one operand.
|
2020-04-30 07:09:11 +08:00
|
|
|
Operation *user = createOp(&context, operand);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
|
|
|
// The same number of operands is okay.
|
|
|
|
user->setOperands(operand);
|
2019-05-21 22:07:17 +08:00
|
|
|
EXPECT_EQ(user->getNumOperands(), 1u);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
|
|
|
// Removing is okay.
|
|
|
|
user->setOperands(llvm::None);
|
2019-05-21 22:07:17 +08:00
|
|
|
EXPECT_EQ(user->getNumOperands(), 0u);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// Destroy the operations.
|
2019-03-06 05:37:48 +08:00
|
|
|
user->destroy();
|
2019-03-27 05:45:38 +08:00
|
|
|
useOp->destroy();
|
2019-03-06 05:37:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OperandStorageTest, Resizable) {
|
|
|
|
MLIRContext context;
|
|
|
|
Builder builder(&context);
|
|
|
|
|
2019-03-27 05:45:38 +08:00
|
|
|
Operation *useOp =
|
2020-04-27 12:28:22 +08:00
|
|
|
createOp(&context, /*operands=*/llvm::None, builder.getIntegerType(16));
|
2019-12-24 06:45:01 +08:00
|
|
|
Value operand = useOp->getResult(0);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// Create a resizable operation with one operand.
|
2020-04-30 07:09:11 +08:00
|
|
|
Operation *user = createOp(&context, operand);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
|
|
|
// The same number of operands is okay.
|
|
|
|
user->setOperands(operand);
|
2019-05-21 22:07:17 +08:00
|
|
|
EXPECT_EQ(user->getNumOperands(), 1u);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
|
|
|
// Removing is okay.
|
|
|
|
user->setOperands(llvm::None);
|
2019-05-21 22:07:17 +08:00
|
|
|
EXPECT_EQ(user->getNumOperands(), 0u);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
|
|
|
// Adding more operands is okay.
|
|
|
|
user->setOperands({operand, operand, operand});
|
2019-05-21 22:07:17 +08:00
|
|
|
EXPECT_EQ(user->getNumOperands(), 3u);
|
2019-03-06 05:37:48 +08:00
|
|
|
|
2019-03-28 23:24:38 +08:00
|
|
|
// Destroy the operations.
|
2019-03-06 05:37:48 +08:00
|
|
|
user->destroy();
|
2019-03-27 05:45:38 +08:00
|
|
|
useOp->destroy();
|
2019-03-06 05:37:48 +08:00
|
|
|
}
|
|
|
|
|
2020-04-30 07:09:11 +08:00
|
|
|
TEST(OperandStorageTest, RangeReplace) {
|
|
|
|
MLIRContext context;
|
|
|
|
Builder builder(&context);
|
|
|
|
|
|
|
|
Operation *useOp =
|
|
|
|
createOp(&context, /*operands=*/llvm::None, builder.getIntegerType(16));
|
|
|
|
Value operand = useOp->getResult(0);
|
|
|
|
|
|
|
|
// Create a resizable operation with one operand.
|
|
|
|
Operation *user = createOp(&context, operand);
|
|
|
|
|
|
|
|
// Check setting with the same number of operands.
|
|
|
|
user->setOperands(/*start=*/0, /*length=*/1, operand);
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 1u);
|
|
|
|
|
|
|
|
// Check setting with more operands.
|
|
|
|
user->setOperands(/*start=*/0, /*length=*/1, {operand, operand, operand});
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 3u);
|
|
|
|
|
|
|
|
// Check setting with less operands.
|
|
|
|
user->setOperands(/*start=*/1, /*length=*/2, {operand});
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 2u);
|
|
|
|
|
|
|
|
// Check inserting without replacing operands.
|
|
|
|
user->setOperands(/*start=*/2, /*length=*/0, {operand});
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 3u);
|
|
|
|
|
|
|
|
// Check erasing operands.
|
|
|
|
user->setOperands(/*start=*/0, /*length=*/3, {});
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 0u);
|
|
|
|
|
|
|
|
// Destroy the operations.
|
|
|
|
user->destroy();
|
|
|
|
useOp->destroy();
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(OperandStorageTest, MutableRange) {
|
|
|
|
MLIRContext context;
|
|
|
|
Builder builder(&context);
|
|
|
|
|
|
|
|
Operation *useOp =
|
|
|
|
createOp(&context, /*operands=*/llvm::None, builder.getIntegerType(16));
|
|
|
|
Value operand = useOp->getResult(0);
|
|
|
|
|
|
|
|
// Create a resizable operation with one operand.
|
|
|
|
Operation *user = createOp(&context, operand);
|
|
|
|
|
|
|
|
// Check setting with the same number of operands.
|
|
|
|
MutableOperandRange mutableOperands(user);
|
|
|
|
mutableOperands.assign(operand);
|
|
|
|
EXPECT_EQ(mutableOperands.size(), 1u);
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 1u);
|
|
|
|
|
|
|
|
// Check setting with more operands.
|
|
|
|
mutableOperands.assign({operand, operand, operand});
|
|
|
|
EXPECT_EQ(mutableOperands.size(), 3u);
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 3u);
|
|
|
|
|
|
|
|
// Check with inserting a new operand.
|
|
|
|
mutableOperands.append({operand, operand});
|
|
|
|
EXPECT_EQ(mutableOperands.size(), 5u);
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 5u);
|
|
|
|
|
|
|
|
// Check erasing operands.
|
|
|
|
mutableOperands.clear();
|
|
|
|
EXPECT_EQ(mutableOperands.size(), 0u);
|
|
|
|
EXPECT_EQ(user->getNumOperands(), 0u);
|
|
|
|
|
|
|
|
// Destroy the operations.
|
|
|
|
user->destroy();
|
|
|
|
useOp->destroy();
|
|
|
|
}
|
|
|
|
|
2019-03-06 05:37:48 +08:00
|
|
|
} // end namespace
|