Debian Codegraph @

flang/unittests/Optimizer/FortranVariableTest.cpp

raw
1 //===- FortranVariableTest.cpp --------------------------------------------===//
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 "gtest/gtest.h"
10 #include "flang/Optimizer/Dialect/FIROps.h"
11 #include "flang/Optimizer/Support/InitFIR.h"
12
13 struct FortranVariableTest : public testing::Test {
14 public:
15 void SetUp() {
16 fir::support::loadDialects(context);
17 builder = std::make_unique<mlir::OpBuilder>(&context);
18 mlir::Location loc = builder->getUnknownLoc();
19
20 // Set up a Module with a dummy function operation inside.
21 // Set the insertion point in the function entry block.
22 mlir::ModuleOp mod = builder->create<mlir::ModuleOp>(loc);
23 mlir::func::FuncOp func =
24 mlir::func::FuncOp::create(loc, "fortran_variable_tests",
25 builder->getFunctionType(std::nullopt, std::nullopt));
26 auto *entryBlock = func.addEntryBlock();
27 mod.push_back(mod);
28 builder->setInsertionPointToStart(entryBlock);
29 }
30
31 mlir::Location getLoc() { return builder->getUnknownLoc(); }
32 mlir::Value createConstant(std::int64_t cst) {
33 mlir::Type indexType = builder->getIndexType();
34 return builder->create<mlir::arith::ConstantOp>(
35 getLoc(), indexType, builder->getIntegerAttr(indexType, cst));
36 }
37
38 mlir::Value createShape(llvm::ArrayRef<mlir::Value> extents) {
39 return builder->create<fir::ShapeOp>(getLoc(), extents);
40 }
41 mlir::MLIRContext context;
42 std::unique_ptr<mlir::OpBuilder> builder;
43 };
44
45 TEST_F(FortranVariableTest, SimpleScalar) {
46 mlir::Location loc = getLoc();
47 mlir::Type eleType = mlir::FloatType::getF32(&context);
48 mlir::Value addr = builder->create<fir::AllocaOp>(loc, eleType);
49 auto name = mlir::StringAttr::get(&context, "x");
50 auto declare = builder->create<fir::DeclareOp>(loc, addr.getType(), addr,
51 /*shape=*/mlir::Value{}, /*typeParams=*/std::nullopt,
52 /*dummy_scope=*/nullptr, name,
53 /*fortran_attrs=*/fir::FortranVariableFlagsAttr{},
54 /*data_attr=*/cuf::DataAttributeAttr{});
55
56 fir::FortranVariableOpInterface fortranVariable = declare;
57 EXPECT_FALSE(fortranVariable.isArray());
58 EXPECT_FALSE(fortranVariable.isCharacter());
59 EXPECT_FALSE(fortranVariable.isPointer());
60 EXPECT_FALSE(fortranVariable.isAllocatable());
61 EXPECT_FALSE(fortranVariable.hasExplicitCharLen());
62 EXPECT_EQ(fortranVariable.getElementType(), eleType);
63 EXPECT_EQ(fortranVariable.getElementOrSequenceType(),
64 fortranVariable.getElementType());
65 EXPECT_NE(fortranVariable.getBase(), addr);
66 EXPECT_EQ(fortranVariable.getBase().getType(), addr.getType());
67 }
68
69 TEST_F(FortranVariableTest, CharacterScalar) {
70 mlir::Location loc = getLoc();
71 mlir::Type eleType = fir::CharacterType::getUnknownLen(&context, 4);
72 mlir::Value len = createConstant(42);
73 llvm::SmallVector<mlir::Value> typeParams{len};
74 mlir::Value addr = builder->create<fir::AllocaOp>(
75 loc, eleType, /*pinned=*/false, typeParams);
76 auto name = mlir::StringAttr::get(&context, "x");
77 auto declare = builder->create<fir::DeclareOp>(loc, addr.getType(), addr,
78 /*shape=*/mlir::Value{}, typeParams, /*dummy_scope=*/nullptr, name,
79 /*fortran_attrs=*/fir::FortranVariableFlagsAttr{},
80 /*data_attr=*/cuf::DataAttributeAttr{});
81
82 fir::FortranVariableOpInterface fortranVariable = declare;
83 EXPECT_FALSE(fortranVariable.isArray());
84 EXPECT_TRUE(fortranVariable.isCharacter());
85 EXPECT_FALSE(fortranVariable.isPointer());
86 EXPECT_FALSE(fortranVariable.isAllocatable());
87 EXPECT_TRUE(fortranVariable.hasExplicitCharLen());
88 EXPECT_EQ(fortranVariable.getElementType(), eleType);
89 EXPECT_EQ(fortranVariable.getElementOrSequenceType(),
90 fortranVariable.getElementType());
91 EXPECT_NE(fortranVariable.getBase(), addr);
92 EXPECT_EQ(fortranVariable.getBase().getType(), addr.getType());
93 EXPECT_EQ(fortranVariable.getExplicitCharLen(), len);
94 }
95
96 TEST_F(FortranVariableTest, SimpleArray) {
97 mlir::Location loc = getLoc();
98 mlir::Type eleType = mlir::FloatType::getF32(&context);
99 llvm::SmallVector<mlir::Value> extents{
100 createConstant(10), createConstant(20), createConstant(30)};
101 fir::SequenceType::Shape typeShape(
102 extents.size(), fir::SequenceType::getUnknownExtent());
103 mlir::Type seqTy = fir::SequenceType::get(typeShape, eleType);
104 mlir::Value addr = builder->create<fir::AllocaOp>(
105 loc, seqTy, /*pinned=*/false, /*typeParams=*/std::nullopt, extents);
106 mlir::Value shape = createShape(extents);
107 auto name = mlir::StringAttr::get(&context, "x");
108 auto declare = builder->create<fir::DeclareOp>(loc, addr.getType(), addr,
109 shape, /*typeParams*/ std::nullopt, /*dummy_scope=*/nullptr, name,
110 /*fortran_attrs=*/fir::FortranVariableFlagsAttr{},
111 /*data_attr=*/cuf::DataAttributeAttr{});
112
113 fir::FortranVariableOpInterface fortranVariable = declare;
114 EXPECT_TRUE(fortranVariable.isArray());
115 EXPECT_FALSE(fortranVariable.isCharacter());
116 EXPECT_FALSE(fortranVariable.isPointer());
117 EXPECT_FALSE(fortranVariable.isAllocatable());
118 EXPECT_FALSE(fortranVariable.hasExplicitCharLen());
119 EXPECT_EQ(fortranVariable.getElementType(), eleType);
120 EXPECT_EQ(fortranVariable.getElementOrSequenceType(), seqTy);
121 EXPECT_NE(fortranVariable.getBase(), addr);
122 EXPECT_EQ(fortranVariable.getBase().getType(), addr.getType());
123 }
124
125 TEST_F(FortranVariableTest, CharacterArray) {
126 mlir::Location loc = getLoc();
127 mlir::Type eleType = fir::CharacterType::getUnknownLen(&context, 4);
128 mlir::Value len = createConstant(42);
129 llvm::SmallVector<mlir::Value> typeParams{len};
130 llvm::SmallVector<mlir::Value> extents{
131 createConstant(10), createConstant(20), createConstant(30)};
132 fir::SequenceType::Shape typeShape(
133 extents.size(), fir::SequenceType::getUnknownExtent());
134 mlir::Type seqTy = fir::SequenceType::get(typeShape, eleType);
135 mlir::Value addr = builder->create<fir::AllocaOp>(
136 loc, seqTy, /*pinned=*/false, typeParams, extents);
137 mlir::Value shape = createShape(extents);
138 auto name = mlir::StringAttr::get(&context, "x");
139 auto declare = builder->create<fir::DeclareOp>(loc, addr.getType(), addr,
140 shape, typeParams, /*dummy_scope=*/nullptr, name,
141 /*fortran_attrs=*/fir::FortranVariableFlagsAttr{},
142 /*data_attr=*/cuf::DataAttributeAttr{});
143
144 fir::FortranVariableOpInterface fortranVariable = declare;
145 EXPECT_TRUE(fortranVariable.isArray());
146 EXPECT_TRUE(fortranVariable.isCharacter());
147 EXPECT_FALSE(fortranVariable.isPointer());
148 EXPECT_FALSE(fortranVariable.isAllocatable());
149 EXPECT_TRUE(fortranVariable.hasExplicitCharLen());
150 EXPECT_EQ(fortranVariable.getElementType(), eleType);
151 EXPECT_EQ(fortranVariable.getElementOrSequenceType(), seqTy);
152 EXPECT_NE(fortranVariable.getBase(), addr);
153 EXPECT_EQ(fortranVariable.getBase().getType(), addr.getType());
154 EXPECT_EQ(fortranVariable.getExplicitCharLen(), len);
155 }