Debian Codegraph @

flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h

raw
1 //===- RuntimeCallTestBase.cpp -- Base for runtime call generation 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 #ifndef FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RUNTIMECALLTESTBASE_H
10 #define FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RUNTIMECALLTESTBASE_H
11
12 #include "gtest/gtest.h"
13 #include "flang/Optimizer/Builder/FIRBuilder.h"
14 #include "flang/Optimizer/Dialect/Support/KindMapping.h"
15 #include "flang/Optimizer/Support/InitFIR.h"
16
17 struct RuntimeCallTest : public testing::Test {
18 public:
19 void SetUp() override {
20 fir::support::loadDialects(context);
21
22 mlir::OpBuilder builder(&context);
23 auto loc = builder.getUnknownLoc();
24
25 // Set up a Module with a dummy function operation inside.
26 // Set the insertion point in the function entry block.
27 mlir::ModuleOp mod = builder.create<mlir::ModuleOp>(loc);
28 mlir::func::FuncOp func =
29 mlir::func::FuncOp::create(loc, "runtime_unit_tests_func",
30 builder.getFunctionType(std::nullopt, std::nullopt));
31 auto *entryBlock = func.addEntryBlock();
32 mod.push_back(mod);
33 builder.setInsertionPointToStart(entryBlock);
34
35 kindMap = std::make_unique<fir::KindMapping>(&context);
36 firBuilder = std::make_unique<fir::FirOpBuilder>(mod, *kindMap);
37
38 i1Ty = firBuilder->getI1Type();
39 i8Ty = firBuilder->getI8Type();
40 i16Ty = firBuilder->getIntegerType(16);
41 i32Ty = firBuilder->getI32Type();
42 i64Ty = firBuilder->getI64Type();
43 i128Ty = firBuilder->getIntegerType(128);
44
45 f32Ty = firBuilder->getF32Type();
46 f64Ty = firBuilder->getF64Type();
47 f80Ty = firBuilder->getF80Type();
48 f128Ty = firBuilder->getF128Type();
49
50 c4Ty = fir::ComplexType::get(firBuilder->getContext(), 4);
51 c8Ty = fir::ComplexType::get(firBuilder->getContext(), 8);
52 c10Ty = fir::ComplexType::get(firBuilder->getContext(), 10);
53 c16Ty = fir::ComplexType::get(firBuilder->getContext(), 16);
54
55 seqTy10 = fir::SequenceType::get(fir::SequenceType::Shape(1, 10), i32Ty);
56 boxTy = fir::BoxType::get(mlir::NoneType::get(firBuilder->getContext()));
57
58 char1Ty = fir::CharacterType::getSingleton(builder.getContext(), 1);
59 char2Ty = fir::CharacterType::getSingleton(builder.getContext(), 2);
60 char4Ty = fir::CharacterType::getSingleton(builder.getContext(), 4);
61
62 logical1Ty = fir::LogicalType::get(builder.getContext(), 1);
63 logical2Ty = fir::LogicalType::get(builder.getContext(), 2);
64 logical4Ty = fir::LogicalType::get(builder.getContext(), 4);
65 logical8Ty = fir::LogicalType::get(builder.getContext(), 8);
66 }
67
68 mlir::MLIRContext context;
69 std::unique_ptr<fir::KindMapping> kindMap;
70 std::unique_ptr<fir::FirOpBuilder> firBuilder;
71
72 // Commonly used types
73 mlir::Type i1Ty;
74 mlir::Type i8Ty;
75 mlir::Type i16Ty;
76 mlir::Type i32Ty;
77 mlir::Type i64Ty;
78 mlir::Type i128Ty;
79 mlir::Type f32Ty;
80 mlir::Type f64Ty;
81 mlir::Type f80Ty;
82 mlir::Type f128Ty;
83 mlir::Type c4Ty;
84 mlir::Type c8Ty;
85 mlir::Type c10Ty;
86 mlir::Type c16Ty;
87 mlir::Type seqTy10;
88 mlir::Type boxTy;
89 mlir::Type char1Ty;
90 mlir::Type char2Ty;
91 mlir::Type char4Ty;
92 mlir::Type logical1Ty;
93 mlir::Type logical2Ty;
94 mlir::Type logical4Ty;
95 mlir::Type logical8Ty;
96 };
97
98 /// Check that the \p op is a `fir::CallOp` operation and its name matches
99 /// \p fctName and the number of arguments is equal to \p nbArgs.
100 /// Most runtime calls have two additional location arguments added. These are
101 /// added in this check when \p addLocArgs is true.
102 static inline void checkCallOp(mlir::Operation *op, llvm::StringRef fctName,
103 unsigned nbArgs, bool addLocArgs = true) {
104 EXPECT_TRUE(mlir::isa<fir::CallOp>(*op));
105 auto callOp = mlir::dyn_cast<fir::CallOp>(*op);
106 EXPECT_TRUE(callOp.getCallee().has_value());
107 mlir::SymbolRefAttr callee = *callOp.getCallee();
108 EXPECT_EQ(fctName, callee.getRootReference().getValue());
109 // sourceFile and sourceLine are added arguments.
110 if (addLocArgs)
111 nbArgs += 2;
112 EXPECT_EQ(nbArgs, callOp.getArgs().size());
113 }
114
115 /// Check the call operation from the \p result value. In some cases the
116 /// value is directly used in the call and sometimes there is an indirection
117 /// through a `fir.convert` operation. Once the `fir.call` operation is
118 /// retrieved the check is made by `checkCallOp`.
119 ///
120 /// Directly used in `fir.call`.
121 /// ```
122 /// %result = arith.constant 1 : i32
123 /// %0 = fir.call @foo(%result) : (i32) -> i1
124 /// ```
125 ///
126 /// Value used in `fir.call` through `fir.convert` indirection.
127 /// ```
128 /// %result = arith.constant 1 : i32
129 /// %arg = fir.convert %result : (i32) -> i16
130 /// %0 = fir.call @foo(%arg) : (i16) -> i1
131 /// ```
132 static inline void checkCallOpFromResultBox(mlir::Value result,
133 llvm::StringRef fctName, unsigned nbArgs, bool addLocArgs = true) {
134 EXPECT_TRUE(result.hasOneUse());
135 const auto &u = result.user_begin();
136 if (mlir::isa<fir::CallOp>(*u))
137 return checkCallOp(*u, fctName, nbArgs, addLocArgs);
138 auto convOp = mlir::dyn_cast<fir::ConvertOp>(*u);
139 EXPECT_NE(nullptr, convOp);
140 checkCallOpFromResultBox(convOp.getResult(), fctName, nbArgs, addLocArgs);
141 }
142
143 /// Check the operations in \p block for a `fir::CallOp` operation where the
144 /// function being called shares its function name with \p fctName and the
145 /// number of arguments is equal to \p nbArgs. Note that this check only cares
146 /// if the operation exists, and not the order in when the operation is called.
147 /// This results in exiting the test as soon as the first correct instance of
148 /// `fir::CallOp` is found).
149 static inline void checkBlockForCallOp(
150 mlir::Block *block, llvm::StringRef fctName, unsigned nbArgs) {
151 assert(block && "mlir::Block given is a nullptr");
152 for (auto &op : block->getOperations()) {
153 if (auto callOp = mlir::dyn_cast<fir::CallOp>(op)) {
154 if (fctName == callOp.getCallee()->getRootReference().getValue()) {
155 EXPECT_EQ(nbArgs, callOp.getArgs().size());
156 return;
157 }
158 }
159 }
160 FAIL() << "No calls to " << fctName << " were found!";
161 }
162
163 #endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_RUNTIMECALLTESTBASE_H