1//===- HLFIRToolsTest.cpp -- HLFIR tools 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 "flang/Optimizer/Builder/HLFIRTools.h"
10#include "gtest/gtest.h"
11#include "flang/Optimizer/Builder/BoxValue.h"
12#include "flang/Optimizer/Builder/FIRBuilder.h"
13#include "flang/Optimizer/Dialect/Support/KindMapping.h"
14#include "flang/Optimizer/Support/InitFIR.h"
15
16struct HLFIRToolsTest : public testing::Test {
17public:
18 void SetUp() override {
19 fir::support::loadDialects(context);
20
21 llvm::ArrayRef<fir::KindTy> defs;
22 fir::KindMapping kindMap(&context, defs);
23 mlir::OpBuilder builder(&context);
24 auto loc = builder.getUnknownLoc();
25
26 // Set up a Module with a dummy function operation inside.
27 // Set the insertion point in the function entry block.
28 mlir::ModuleOp mod = builder.create<mlir::ModuleOp>(loc);
29 mlir::func::FuncOp func = mlir::func::FuncOp::create(
30 loc, "func1", builder.getFunctionType(std::nullopt, std::nullopt));
31 auto *entryBlock = func.addEntryBlock();
32 mod.push_back(mod);
33 builder.setInsertionPointToStart(entryBlock);
34
35 firBuilder = std::make_unique<fir::FirOpBuilder>(mod, kindMap);
36 }
37
38 mlir::Value createDeclare(fir::ExtendedValue exv) {
39 return hlfir::genDeclare(getLoc(), *firBuilder, exv,
40 "x" + std::to_string(varCounter++), fir::FortranVariableFlagsAttr{})
41 .getBase();
42 }
43
44 mlir::Value createConstant(std::int64_t cst) {
45 mlir::Type indexType = firBuilder->getIndexType();
46 return firBuilder->create<mlir::arith::ConstantOp>(
47 getLoc(), indexType, firBuilder->getIntegerAttr(indexType, cst));
48 }
49
50 mlir::Location getLoc() { return firBuilder->getUnknownLoc(); }
51 fir::FirOpBuilder &getBuilder() { return *firBuilder; }
52
53 int varCounter = 0;
54 mlir::MLIRContext context;
55 std::unique_ptr<fir::FirOpBuilder> firBuilder;
56};
57
58TEST_F(HLFIRToolsTest, testScalarRoundTrip) {
59 auto &builder = getBuilder();
60 mlir::Location loc = getLoc();
61 mlir::Type f32Type = mlir::FloatType::getF32(&context);
62 mlir::Type scalarf32Type = builder.getRefType(f32Type);
63 mlir::Value scalarf32Addr = builder.create<fir::UndefOp>(loc, scalarf32Type);
64 fir::ExtendedValue scalarf32{scalarf32Addr};
65 hlfir::EntityWithAttributes scalarf32Entity(createDeclare(scalarf32));
66 auto [scalarf32Result, cleanup] =
67 hlfir::translateToExtendedValue(loc, builder, scalarf32Entity);
68 auto *unboxed = scalarf32Result.getUnboxed();
69 EXPECT_FALSE(cleanup.has_value());
70 ASSERT_NE(unboxed, nullptr);
71 EXPECT_TRUE(*unboxed == scalarf32Entity.getFirBase());
72 EXPECT_TRUE(scalarf32Entity.isVariable());
73 EXPECT_FALSE(scalarf32Entity.isValue());
74}
75
76TEST_F(HLFIRToolsTest, testArrayRoundTrip) {
77 auto &builder = getBuilder();
78 mlir::Location loc = getLoc();
79 llvm::SmallVector<mlir::Value> extents{
80 createConstant(20), createConstant(30)};
81 llvm::SmallVector<mlir::Value> lbounds{
82 createConstant(-1), createConstant(-2)};
83
84 mlir::Type f32Type = mlir::FloatType::getF32(&context);
85 mlir::Type seqf32Type = builder.getVarLenSeqTy(f32Type, 2);
86 mlir::Type arrayf32Type = builder.getRefType(seqf32Type);
87 mlir::Value arrayf32Addr = builder.create<fir::UndefOp>(loc, arrayf32Type);
88 fir::ArrayBoxValue arrayf32{arrayf32Addr, extents, lbounds};
89 hlfir::EntityWithAttributes arrayf32Entity(createDeclare(arrayf32));
90 auto [arrayf32Result, cleanup] =
91 hlfir::translateToExtendedValue(loc, builder, arrayf32Entity);
92 auto *res = arrayf32Result.getBoxOf<fir::ArrayBoxValue>();
93 EXPECT_FALSE(cleanup.has_value());
94 ASSERT_NE(res, nullptr);
95 // gtest has a terrible time printing mlir::Value in case of failing
96 // EXPECT_EQ(mlir::Value, mlir::Value). So use EXPECT_TRUE instead.
97 EXPECT_TRUE(fir::getBase(*res) == arrayf32Entity.getFirBase());
98 ASSERT_EQ(res->getExtents().size(), arrayf32.getExtents().size());
99 for (unsigned i = 0; i < arrayf32.getExtents().size(); ++i)
100 EXPECT_TRUE(res->getExtents()[i] == arrayf32.getExtents()[i]);
101 ASSERT_EQ(res->getLBounds().size(), arrayf32.getLBounds().size());
102 for (unsigned i = 0; i < arrayf32.getLBounds().size(); ++i)
103 EXPECT_TRUE(res->getLBounds()[i] == arrayf32.getLBounds()[i]);
104 EXPECT_TRUE(arrayf32Entity.isVariable());
105 EXPECT_FALSE(arrayf32Entity.isValue());
106}
107
108TEST_F(HLFIRToolsTest, testScalarCharRoundTrip) {
109 auto &builder = getBuilder();
110 mlir::Location loc = getLoc();
111 mlir::Value len = createConstant(42);
112 mlir::Type charType = fir::CharacterType::getUnknownLen(&context, 1);
113 mlir::Type scalarCharType = builder.getRefType(charType);
114 mlir::Value scalarCharAddr =
115 builder.create<fir::UndefOp>(loc, scalarCharType);
116 fir::CharBoxValue scalarChar{scalarCharAddr, len};
117 hlfir::EntityWithAttributes scalarCharEntity(createDeclare(scalarChar));
118 auto [scalarCharResult, cleanup] =
119 hlfir::translateToExtendedValue(loc, builder, scalarCharEntity);
120 auto *res = scalarCharResult.getBoxOf<fir::CharBoxValue>();
121 EXPECT_FALSE(cleanup.has_value());
122 ASSERT_NE(res, nullptr);
123 EXPECT_TRUE(fir::getBase(*res) == scalarCharEntity.getFirBase());
124 EXPECT_TRUE(res->getLen() == scalarChar.getLen());
125 EXPECT_TRUE(scalarCharEntity.isVariable());
126 EXPECT_FALSE(scalarCharEntity.isValue());
127}
128
129TEST_F(HLFIRToolsTest, testArrayCharRoundTrip) {
130 auto &builder = getBuilder();
131 mlir::Location loc = getLoc();
132 llvm::SmallVector<mlir::Value> extents{
133 createConstant(20), createConstant(30)};
134 llvm::SmallVector<mlir::Value> lbounds{
135 createConstant(-1), createConstant(-2)};
136 mlir::Value len = createConstant(42);
137 mlir::Type charType = fir::CharacterType::getUnknownLen(&context, 1);
138 mlir::Type seqCharType = builder.getVarLenSeqTy(charType, 2);
139 mlir::Type arrayCharType = builder.getRefType(seqCharType);
140 mlir::Value arrayCharAddr = builder.create<fir::UndefOp>(loc, arrayCharType);
141 fir::CharArrayBoxValue arrayChar{arrayCharAddr, len, extents, lbounds};
142 hlfir::EntityWithAttributes arrayCharEntity(createDeclare(arrayChar));
143 auto [arrayCharResult, cleanup] =
144 hlfir::translateToExtendedValue(loc, builder, arrayCharEntity);
145 auto *res = arrayCharResult.getBoxOf<fir::CharArrayBoxValue>();
146 EXPECT_FALSE(cleanup.has_value());
147 ASSERT_NE(res, nullptr);
148 // gtest has a terrible time printing mlir::Value in case of failing
149 // EXPECT_EQ(mlir::Value, mlir::Value). So use EXPECT_TRUE instead.
150 EXPECT_TRUE(fir::getBase(*res) == arrayCharEntity.getFirBase());
151 EXPECT_TRUE(res->getLen() == arrayChar.getLen());
152 ASSERT_EQ(res->getExtents().size(), arrayChar.getExtents().size());
153 for (unsigned i = 0; i < arrayChar.getExtents().size(); ++i)
154 EXPECT_TRUE(res->getExtents()[i] == arrayChar.getExtents()[i]);
155 ASSERT_EQ(res->getLBounds().size(), arrayChar.getLBounds().size());
156 for (unsigned i = 0; i < arrayChar.getLBounds().size(); ++i)
157 EXPECT_TRUE(res->getLBounds()[i] == arrayChar.getLBounds()[i]);
158 EXPECT_TRUE(arrayCharEntity.isVariable());
159 EXPECT_FALSE(arrayCharEntity.isValue());
160}
161
162TEST_F(HLFIRToolsTest, testArrayCharBoxRoundTrip) {
163 auto &builder = getBuilder();
164 mlir::Location loc = getLoc();
165 llvm::SmallVector<mlir::Value> lbounds{
166 createConstant(-1), createConstant(-2)};
167 mlir::Value len = createConstant(42);
168 mlir::Type charType = fir::CharacterType::getUnknownLen(&context, 1);
169 mlir::Type seqCharType = builder.getVarLenSeqTy(charType, 2);
170 mlir::Type arrayCharBoxType = fir::BoxType::get(seqCharType);
171 mlir::Value arrayCharAddr =
172 builder.create<fir::UndefOp>(loc, arrayCharBoxType);
173 llvm::SmallVector<mlir::Value> explicitTypeParams{len};
174 fir::BoxValue arrayChar{arrayCharAddr, lbounds, explicitTypeParams};
175 hlfir::EntityWithAttributes arrayCharEntity(createDeclare(arrayChar));
176 auto [arrayCharResult, cleanup] =
177 hlfir::translateToExtendedValue(loc, builder, arrayCharEntity);
178 auto *res = arrayCharResult.getBoxOf<fir::BoxValue>();
179 EXPECT_FALSE(cleanup.has_value());
180 ASSERT_NE(res, nullptr);
181 // gtest has a terrible time printing mlir::Value in case of failing
182 // EXPECT_EQ(mlir::Value, mlir::Value). So use EXPECT_TRUE instead.
183 EXPECT_TRUE(fir::getBase(*res) == arrayCharEntity.getFirBase());
184 ASSERT_EQ(res->getExplicitParameters().size(),
185 arrayChar.getExplicitParameters().size());
186 for (unsigned i = 0; i < arrayChar.getExplicitParameters().size(); ++i)
187 EXPECT_TRUE(res->getExplicitParameters()[i] ==
188 arrayChar.getExplicitParameters()[i]);
189 ASSERT_EQ(res->getLBounds().size(), arrayChar.getLBounds().size());
190 for (unsigned i = 0; i < arrayChar.getLBounds().size(); ++i)
191 EXPECT_TRUE(res->getLBounds()[i] == arrayChar.getLBounds()[i]);
192 EXPECT_TRUE(arrayCharEntity.isVariable());
193 EXPECT_FALSE(arrayCharEntity.isValue());
194}
195

source code of flang/unittests/Optimizer/Builder/HLFIRToolsTest.cpp