blob: 09bfa89d0c73d7ee099bcfef0fc70ef7ca75e473 [file]
//===- FortranObjectStoreOpInterfaceTest.cpp ------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "gtest/gtest.h"
#include "mlir/Dialect/Arith/IR/Arith.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinOps.h"
#include "flang/Optimizer/Dialect/CUF/CUFOps.h"
#include "flang/Optimizer/Dialect/FIROps.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/HLFIR/HLFIROps.h"
#include "flang/Optimizer/Support/InitFIR.h"
namespace {
struct FortranObjectStoreOpInterfaceTest : public testing::Test {
void SetUp() override {
fir::support::loadDialects(context);
module = mlir::ModuleOp::create(mlir::UnknownLoc::get(&context));
}
mlir::Block *addFunc(llvm::StringRef name, mlir::TypeRange inputs) {
mlir::OpBuilder builder(module->getBodyRegion());
auto func = mlir::func::FuncOp::create(
builder, module->getLoc(), name, builder.getFunctionType(inputs, {}));
return func.addEntryBlock();
}
llvm::SmallVector<fir::StoredValue> getStoredValues(mlir::Operation *op) {
llvm::SmallVector<fir::StoredValue> stored;
auto iface = mlir::dyn_cast<fir::FortranObjectStoreOpInterface>(op);
EXPECT_TRUE(iface);
if (iface)
iface.getStoredValues(stored);
return stored;
}
mlir::Type i32() { return mlir::IntegerType::get(&context, 32); }
mlir::Type i1() { return mlir::IntegerType::get(&context, 1); }
mlir::Type arrayRef() {
return fir::ReferenceType::get(fir::SequenceType::get({10}, i32()));
}
mlir::Type dynamicArray() {
return fir::SequenceType::get(
{fir::SequenceType::getUnknownExtent()}, i32());
}
mlir::Type arrayBox() { return fir::BoxType::get(dynamicArray()); }
mlir::Type allocatableRef() {
return fir::ReferenceType::get(
fir::BoxType::get(fir::HeapType::get(dynamicArray())));
}
mlir::Type record() {
auto recTy = fir::RecordType::get(&context, "t");
if (!recTy.isFinalized())
recTy.finalize({}, {{"c", i32()}});
return recTy;
}
mlir::Type recordRef() { return fir::ReferenceType::get(record()); }
mlir::MLIRContext context;
mlir::OwningOpRef<mlir::ModuleOp> module;
};
void expectStored(const fir::StoredValue &stored, fir::StoredSourceKind kind,
mlir::OpOperand *operand, mlir::OpOperand *destination) {
EXPECT_EQ(stored.source.kind, kind);
EXPECT_EQ(stored.source.operand, operand);
EXPECT_FALSE(stored.source.symbol);
ASSERT_EQ(stored.locations.size(), 1u);
EXPECT_EQ(stored.locations[0].destination, destination);
}
TEST_F(FortranObjectStoreOpInterfaceTest, Store) {
// fir.store writes the value of %v through %p.
//
// fir.store %v to %p : !fir.ref<i32>
// {Operand, %v} -> {%p}
mlir::Block *entry =
addFunc("store", {i32(), fir::ReferenceType::get(i32())});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto store = fir::StoreOp::create(
builder, module->getLoc(), entry->getArgument(0), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(store);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Operand,
&store.getValueMutable(), &store.getMemrefMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, Copy) {
// fir.copy writes the contents of %x through %y, not the address %x.
//
// fir.copy %x to %y : !fir.ref<!fir.array<10xi32>>,
// !fir.ref<!fir.array<10xi32>>
// {Operand | Contents, %x} -> {%y}
mlir::Block *entry = addFunc("copy", {arrayRef(), arrayRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto copy = fir::CopyOp::create(
builder, module->getLoc(), entry->getArgument(0), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(copy);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&copy.getSourceMutable(), &copy.getDestinationMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignValue) {
// A right-hand side that is not a variable is stored as a value.
//
// hlfir.assign %v to %y : i32, !fir.ref<i32>
// {Operand, %v} -> {%y}
mlir::Block *entry =
addFunc("assign_value", {i32(), fir::ReferenceType::get(i32())});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(
builder, module->getLoc(), entry->getArgument(0), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Operand,
&assign.getRhsMutable(), &assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignVariable) {
// The contents of a variable on the right-hand side are stored, not its
// address.
//
// hlfir.assign %x to %y : !fir.ref<!fir.array<10xi32>>,
// !fir.ref<!fir.array<10xi32>>
// {Operand | Contents, %x} -> {%y}
mlir::Block *entry = addFunc("assign_variable", {arrayRef(), arrayRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(
builder, module->getLoc(), entry->getArgument(0), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&assign.getRhsMutable(), &assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignExpr) {
// An expression has no address: its value is stored.
//
// %e = hlfir.as_expr %x : (!fir.ref<!fir.array<10xi32>>)
// -> !hlfir.expr<10xi32>
// hlfir.assign %e to %y : !hlfir.expr<10xi32>, !fir.ref<!fir.array<10xi32>>
// {Operand, %e} -> {%y}
mlir::Block *entry = addFunc("assign_expr", {arrayRef(), arrayRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto expr =
hlfir::AsExprOp::create(builder, module->getLoc(), entry->getArgument(0));
auto assign = hlfir::AssignOp::create(
builder, module->getLoc(), expr.getResult(), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Operand,
&assign.getRhsMutable(), &assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignRealloc) {
// An assignment to an allocatable may also store the address of new data
// into its descriptor. The assignment allocates that data, so it cannot
// name the source.
//
// hlfir.assign %x to %a realloc
// : !fir.box<!fir.array<?xi32>>,
// !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
// {Operand | Contents, %x} -> {%a}
// {Unknown} -> {%a}
mlir::Block *entry =
addFunc("assign_realloc", {arrayBox(), allocatableRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*realloc=*/true);
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&assign.getRhsMutable(), &assign.getLhsMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr,
&assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignDerivedType) {
// A derived type assignment may also store the addresses of the new data
// of allocatable components. It may call a final subroutine or a defined
// assignment, which may store anything that it can reach from either side,
// or anything else, anywhere.
//
// hlfir.assign %x to %y : !fir.ref<!fir.type<t{c:i32}>>,
// !fir.ref<!fir.type<t{c:i32}>>
// {Operand | Contents, %x} -> {%y}
// {Unknown} -> {%y}
// {Operand, %x} -> {unknown place}
// {Operand | Contents, %x} -> {unknown place}
// {Operand, %y} -> {unknown place}
// {Operand | Contents, %y} -> {unknown place}
// {Unknown} -> {unknown place}
mlir::Block *entry =
addFunc("assign_derived_type", {recordRef(), recordRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(
builder, module->getLoc(), entry->getArgument(0), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 7u);
mlir::OpOperand *rhs = &assign.getRhsMutable();
mlir::OpOperand *lhs = &assign.getLhsMutable();
fir::StoredSourceKind contents =
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents;
expectStored(stored[0], contents, rhs, lhs);
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr, lhs);
expectStored(stored[2], fir::StoredSourceKind::Operand, rhs, nullptr);
expectStored(stored[3], contents, rhs, nullptr);
expectStored(stored[4], fir::StoredSourceKind::Operand, lhs, nullptr);
expectStored(stored[5], contents, lhs, nullptr);
expectStored(stored[6], fir::StoredSourceKind::Unknown, nullptr, nullptr);
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignDerivedTypeToTemporary) {
// An assignment to a temporary calls no final subroutine and no defined
// assignment.
//
// hlfir.assign %x to %y temporary_lhs : !fir.ref<!fir.type<t{c:i32}>>,
// !fir.ref<!fir.type<t{c:i32}>>
// {Operand | Contents, %x} -> {%y}
// {Unknown} -> {%y}
mlir::Block *entry =
addFunc("assign_derived_type_to_temporary", {recordRef(), recordRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*realloc=*/false,
/*keep_lhs_length_if_realloc=*/false, /*temporary_lhs=*/true);
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&assign.getRhsMutable(), &assign.getLhsMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr,
&assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignUnlimitedPolymorphic) {
// The dynamic type of an unlimited polymorphic entity may have allocatable
// components.
//
// hlfir.assign %x to %y temporary_lhs : !fir.class<none>, !fir.class<none>
// {Operand | Contents, %x} -> {%y}
// {Unknown} -> {%y}
mlir::Type classNone = fir::ClassType::get(mlir::NoneType::get(&context));
mlir::Block *entry = addFunc("assign_unlimited", {classNone, classNone});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*realloc=*/false,
/*keep_lhs_length_if_realloc=*/false, /*temporary_lhs=*/true);
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&assign.getRhsMutable(), &assign.getLhsMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr,
&assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, AssignDescriptorInMemory) {
// The data of an allocatable on the right-hand side is two dereferences of
// the address of its descriptor, so its source is unknown.
//
// hlfir.assign %a to %y : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>,
// !fir.box<!fir.array<?xi32>>
// {Unknown} -> {%y}
mlir::Block *entry =
addFunc("assign_descriptor_in_memory", {allocatableRef(), arrayBox()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto assign = hlfir::AssignOp::create(
builder, module->getLoc(), entry->getArgument(0), entry->getArgument(1));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(assign);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Unknown, nullptr,
&assign.getLhsMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, SaveResult) {
// fir.save_result stores the value of a function result.
//
// fir.save_result %v to %p : !fir.box<!fir.heap<i32>>,
// !fir.ref<!fir.box<!fir.heap<i32>>>
// {Operand, %v} -> {%p}
mlir::Type boxTy = fir::BoxType::get(fir::HeapType::get(i32()));
mlir::Block *entry =
addFunc("save_result", {boxTy, fir::ReferenceType::get(boxTy)});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto save = fir::SaveResultOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*shape=*/mlir::Value{},
/*typeparams=*/mlir::ValueRange{});
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(save);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Operand,
&save.getValueMutable(), &save.getMemrefMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, CharConvert) {
// fir.char_convert stores characters converted from the contents of %x.
//
// fir.char_convert %x for %n to %y : !fir.ref<!fir.char<1,?>>, i32,
// !fir.ref<!fir.char<2,?>>
// {Operand | Contents, %x} -> {%y}
mlir::Type from = fir::ReferenceType::get(
fir::CharacterType::getUnknownLen(&context, /*kind=*/1));
mlir::Type to = fir::ReferenceType::get(
fir::CharacterType::getUnknownLen(&context, /*kind=*/2));
mlir::Block *entry = addFunc("char_convert", {from, i32(), to});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto convert = fir::CharConvertOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), entry->getArgument(2));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(convert);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&convert.getFromMutable(), &convert.getToMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, CopyIn) {
// A copy stores the data of %x into a temporary that hlfir.copy_in
// allocates, and the descriptor of the temporary into %t.
//
// hlfir.copy_in %x to %t
// : (!fir.box<!fir.array<?xi32>>,
// !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>)
// -> (!fir.box<!fir.array<?xi32>>, i1, i1)
// {Operand | Contents, %x} -> {%t}
// {Unknown} -> {%t}
mlir::Block *entry = addFunc("copy_in", {arrayBox(), allocatableRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto copyIn = hlfir::CopyInOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1),
/*var_is_present=*/mlir::Value{});
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(copyIn);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&copyIn.getVarMutable(), &copyIn.getTempBoxMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr,
&copyIn.getTempBoxMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, CopyOut) {
// The copy back stores the data of the temporary, which is two
// dereferences of %t. Without a variable, hlfir.copy_out stores nothing.
//
// hlfir.copy_out %t, %copied, %free to %x
// : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, i1, i1,
// !fir.box<!fir.array<?xi32>>) -> ()
// {Unknown} -> {%x}
// hlfir.copy_out %t, %copied, %free
// : (!fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>, i1, i1) -> ()
mlir::Block *entry =
addFunc("copy_out", {allocatableRef(), i1(), i1(), arrayBox()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto copyOut =
hlfir::CopyOutOp::create(builder, module->getLoc(), entry->getArgument(0),
entry->getArgument(1), entry->getArgument(2), entry->getArgument(3));
auto freeOnly = hlfir::CopyOutOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), entry->getArgument(2),
/*var=*/mlir::Value{});
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(copyOut);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Unknown, nullptr,
&copyOut.getVarMutable()[0]);
EXPECT_TRUE(getStoredValues(freeOnly).empty());
}
TEST_F(FortranObjectStoreOpInterfaceTest, DataTransferConstant) {
// A constant source is stored as a value.
//
// %c = arith.constant 1 : i32
// cuf.data_transfer %c to %p transfer_kind(host_device) : i32,
// !fir.ref<i32>
// {Operand, %c} -> {%p}
mlir::Block *entry =
addFunc("data_transfer_constant", {fir::ReferenceType::get(i32())});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto constant = mlir::arith::ConstantOp::create(
builder, module->getLoc(), builder.getI32IntegerAttr(1));
auto transfer = cuf::DataTransferOp::create(builder, module->getLoc(),
constant, entry->getArgument(0), /*shape=*/mlir::Value{},
cuf::DataTransferKindAttr::get(
&context, cuf::DataTransferKind::HostDevice));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(transfer);
ASSERT_EQ(stored.size(), 1u);
expectStored(stored[0], fir::StoredSourceKind::Operand,
&transfer.getSrcMutable(), &transfer.getDstMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, DataTransferToDescriptorValue) {
// The transfer stores the contents of %x. It may also allocate the data of
// the descriptor that %b holds, which is in memory that no operand
// identifies.
//
// cuf.data_transfer %x to %b transfer_kind(host_device)
// : !fir.ref<!fir.array<10xi32>>, !fir.box<!fir.array<?xi32>>
// {Operand | Contents, %x} -> {%b}
// {Unknown} -> {unknown place}
mlir::Block *entry =
addFunc("data_transfer_to_descriptor_value", {arrayRef(), arrayBox()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto transfer = cuf::DataTransferOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*shape=*/mlir::Value{},
cuf::DataTransferKindAttr::get(
&context, cuf::DataTransferKind::HostDevice));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(transfer);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&transfer.getSrcMutable(), &transfer.getDstMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr, nullptr);
}
TEST_F(FortranObjectStoreOpInterfaceTest, DataTransferDerivedType) {
// A transfer of a derived type may also store the addresses of the new
// data of allocatable components.
//
// cuf.data_transfer %b to %r transfer_kind(host_device)
// : !fir.box<!fir.type<t{c:i32}>>, !fir.ref<!fir.type<t{c:i32}>>
// {Operand | Contents, %b} -> {%r}
// {Unknown} -> {%r}
mlir::Block *entry = addFunc(
"data_transfer_derived_type", {fir::BoxType::get(record()), recordRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto transfer = cuf::DataTransferOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*shape=*/mlir::Value{},
cuf::DataTransferKindAttr::get(
&context, cuf::DataTransferKind::HostDevice));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(transfer);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0],
fir::StoredSourceKind::Operand | fir::StoredSourceKind::Contents,
&transfer.getSrcMutable(), &transfer.getDstMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr,
&transfer.getDstMutable());
}
TEST_F(FortranObjectStoreOpInterfaceTest, DataTransferDescriptorsInMemory) {
// The data of %a is two dereferences of %a, so its source is unknown. The
// transfer may also allocate the data of the descriptor at %d.
//
// cuf.data_transfer %a to %d transfer_kind(device_device)
// : !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>,
// !fir.ref<!fir.box<!fir.heap<!fir.array<?xi32>>>>
// {Unknown} -> {%d}
// {Unknown} -> {%d}
mlir::Block *entry = addFunc("data_transfer_descriptors_in_memory",
{allocatableRef(), allocatableRef()});
auto builder = mlir::OpBuilder::atBlockBegin(entry);
auto transfer = cuf::DataTransferOp::create(builder, module->getLoc(),
entry->getArgument(0), entry->getArgument(1), /*shape=*/mlir::Value{},
cuf::DataTransferKindAttr::get(
&context, cuf::DataTransferKind::DeviceDevice));
llvm::SmallVector<fir::StoredValue> stored = getStoredValues(transfer);
ASSERT_EQ(stored.size(), 2u);
expectStored(stored[0], fir::StoredSourceKind::Unknown, nullptr,
&transfer.getDstMutable());
expectStored(stored[1], fir::StoredSourceKind::Unknown, nullptr,
&transfer.getDstMutable());
}
} // namespace