Debian Codegraph @

flang/lib/Optimizer/Builder/Runtime/Intrinsics.cpp

raw
1 //===-- Intrinsics.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 "flang/Optimizer/Builder/Runtime/Intrinsics.h"
10 #include "flang/Optimizer/Builder/BoxValue.h"
11 #include "flang/Optimizer/Builder/FIRBuilder.h"
12 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
13 #include "flang/Optimizer/Dialect/FIROpsSupport.h"
14 #include "flang/Parser/parse-tree.h"
15 #include "flang/Runtime/extensions.h"
16 #include "flang/Runtime/misc-intrinsic.h"
17 #include "flang/Runtime/pointer.h"
18 #include "flang/Runtime/random.h"
19 #include "flang/Runtime/stop.h"
20 #include "flang/Runtime/time-intrinsic.h"
21 #include "flang/Semantics/tools.h"
22 #include "llvm/Support/Debug.h"
23 #include <optional>
24 #include <signal.h>
25
26 #define DEBUG_TYPE "flang-lower-runtime"
27
28 using namespace Fortran::runtime;
29
30 namespace {
31 /// Placeholder for real*16 version of RandomNumber Intrinsic
32 struct ForcedRandomNumberReal16 {
33 static constexpr const char *name = ExpandAndQuoteKey(RTNAME(RandomNumber16));
34 static constexpr fir::runtime::FuncTypeBuilderFunc getTypeModel() {
35 return [](mlir::MLIRContext *ctx) {
36 auto boxTy =
37 fir::runtime::getModel<const Fortran::runtime::Descriptor &>()(ctx);
38 auto strTy = fir::runtime::getModel<const char *>()(ctx);
39 auto intTy = fir::runtime::getModel<int>()(ctx);
40 ;
41 return mlir::FunctionType::get(ctx, {boxTy, strTy, intTy},
42 mlir::NoneType::get(ctx));
43 };
44 }
45 };
46 } // namespace
47
48 mlir::Value fir::runtime::genAssociated(fir::FirOpBuilder &builder,
49 mlir::Location loc, mlir::Value pointer,
50 mlir::Value target) {
51 mlir::func::FuncOp func =
52 fir::runtime::getRuntimeFunc<mkRTKey(PointerIsAssociatedWith)>(loc,
53 builder);
54 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
55 builder, loc, func.getFunctionType(), pointer, target);
56 return builder.create<fir::CallOp>(loc, func, args).getResult(0);
57 }
58
59 mlir::Value fir::runtime::genCpuTime(fir::FirOpBuilder &builder,
60 mlir::Location loc) {
61 mlir::func::FuncOp func =
62 fir::runtime::getRuntimeFunc<mkRTKey(CpuTime)>(loc, builder);
63 return builder.create<fir::CallOp>(loc, func, std::nullopt).getResult(0);
64 }
65
66 void fir::runtime::genDateAndTime(fir::FirOpBuilder &builder,
67 mlir::Location loc,
68 std::optional<fir::CharBoxValue> date,
69 std::optional<fir::CharBoxValue> time,
70 std::optional<fir::CharBoxValue> zone,
71 mlir::Value values) {
72 mlir::func::FuncOp callee =
73 fir::runtime::getRuntimeFunc<mkRTKey(DateAndTime)>(loc, builder);
74 mlir::FunctionType funcTy = callee.getFunctionType();
75 mlir::Type idxTy = builder.getIndexType();
76 mlir::Value zero;
77 auto splitArg = [&](std::optional<fir::CharBoxValue> arg, mlir::Value &buffer,
78 mlir::Value &len) {
79 if (arg) {
80 buffer = arg->getBuffer();
81 len = arg->getLen();
82 } else {
83 if (!zero)
84 zero = builder.createIntegerConstant(loc, idxTy, 0);
85 buffer = zero;
86 len = zero;
87 }
88 };
89 mlir::Value dateBuffer;
90 mlir::Value dateLen;
91 splitArg(date, dateBuffer, dateLen);
92 mlir::Value timeBuffer;
93 mlir::Value timeLen;
94 splitArg(time, timeBuffer, timeLen);
95 mlir::Value zoneBuffer;
96 mlir::Value zoneLen;
97 splitArg(zone, zoneBuffer, zoneLen);
98
99 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
100 mlir::Value sourceLine =
101 fir::factory::locationToLineNo(builder, loc, funcTy.getInput(7));
102
103 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
104 builder, loc, funcTy, dateBuffer, dateLen, timeBuffer, timeLen,
105 zoneBuffer, zoneLen, sourceFile, sourceLine, values);
106 builder.create<fir::CallOp>(loc, callee, args);
107 }
108
109 void fir::runtime::genEtime(fir::FirOpBuilder &builder, mlir::Location loc,
110 mlir::Value values, mlir::Value time) {
111 auto runtimeFunc = fir::runtime::getRuntimeFunc<mkRTKey(Etime)>(loc, builder);
112 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
113
114 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
115 mlir::Value sourceLine =
116 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(3));
117
118 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
119 builder, loc, runtimeFuncTy, values, time, sourceFile, sourceLine);
120 builder.create<fir::CallOp>(loc, runtimeFunc, args);
121 }
122
123 void fir::runtime::genRandomInit(fir::FirOpBuilder &builder, mlir::Location loc,
124 mlir::Value repeatable,
125 mlir::Value imageDistinct) {
126 mlir::func::FuncOp func =
127 fir::runtime::getRuntimeFunc<mkRTKey(RandomInit)>(loc, builder);
128 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
129 builder, loc, func.getFunctionType(), repeatable, imageDistinct);
130 builder.create<fir::CallOp>(loc, func, args);
131 }
132
133 void fir::runtime::genRandomNumber(fir::FirOpBuilder &builder,
134 mlir::Location loc, mlir::Value harvest) {
135 mlir::func::FuncOp func;
136 auto boxEleTy = fir::dyn_cast_ptrOrBoxEleTy(harvest.getType());
137 auto eleTy = fir::unwrapSequenceType(boxEleTy);
138 if (eleTy.isF128()) {
139 func = fir::runtime::getRuntimeFunc<ForcedRandomNumberReal16>(loc, builder);
140 } else {
141 func = fir::runtime::getRuntimeFunc<mkRTKey(RandomNumber)>(loc, builder);
142 }
143
144 mlir::FunctionType funcTy = func.getFunctionType();
145 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
146 mlir::Value sourceLine =
147 fir::factory::locationToLineNo(builder, loc, funcTy.getInput(2));
148 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
149 builder, loc, funcTy, harvest, sourceFile, sourceLine);
150 builder.create<fir::CallOp>(loc, func, args);
151 }
152
153 void fir::runtime::genRandomSeed(fir::FirOpBuilder &builder, mlir::Location loc,
154 mlir::Value size, mlir::Value put,
155 mlir::Value get) {
156 bool sizeIsPresent =
157 !mlir::isa_and_nonnull<fir::AbsentOp>(size.getDefiningOp());
158 bool putIsPresent =
159 !mlir::isa_and_nonnull<fir::AbsentOp>(put.getDefiningOp());
160 bool getIsPresent =
161 !mlir::isa_and_nonnull<fir::AbsentOp>(get.getDefiningOp());
162 mlir::func::FuncOp func;
163 int staticArgCount = sizeIsPresent + putIsPresent + getIsPresent;
164 if (staticArgCount == 0) {
165 func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedDefaultPut)>(loc,
166 builder);
167 builder.create<fir::CallOp>(loc, func);
168 return;
169 }
170 mlir::FunctionType funcTy;
171 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
172 mlir::Value sourceLine;
173 mlir::Value argBox;
174 llvm::SmallVector<mlir::Value> args;
175 if (staticArgCount > 1) {
176 func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeed)>(loc, builder);
177 funcTy = func.getFunctionType();
178 sourceLine =
179 fir::factory::locationToLineNo(builder, loc, funcTy.getInput(4));
180 args = fir::runtime::createArguments(builder, loc, funcTy, size, put, get,
181 sourceFile, sourceLine);
182 builder.create<fir::CallOp>(loc, func, args);
183 return;
184 }
185 if (sizeIsPresent) {
186 func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedSize)>(loc, builder);
187 argBox = size;
188 } else if (putIsPresent) {
189 func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedPut)>(loc, builder);
190 argBox = put;
191 } else {
192 func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedGet)>(loc, builder);
193 argBox = get;
194 }
195 funcTy = func.getFunctionType();
196 sourceLine = fir::factory::locationToLineNo(builder, loc, funcTy.getInput(2));
197 args = fir::runtime::createArguments(builder, loc, funcTy, argBox, sourceFile,
198 sourceLine);
199 builder.create<fir::CallOp>(loc, func, args);
200 }
201
202 /// generate rename runtime call
203 void fir::runtime::genRename(fir::FirOpBuilder &builder, mlir::Location loc,
204 mlir::Value path1, mlir::Value path2,
205 mlir::Value status) {
206 auto runtimeFunc =
207 fir::runtime::getRuntimeFunc<mkRTKey(Rename)>(loc, builder);
208 mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
209
210 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
211 mlir::Value sourceLine =
212 fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(4));
213
214 llvm::SmallVector<mlir::Value> args =
215 fir::runtime::createArguments(builder, loc, runtimeFuncTy, path1, path2,
216 status, sourceFile, sourceLine);
217 builder.create<fir::CallOp>(loc, runtimeFunc, args);
218 }
219
220 /// generate runtime call to transfer intrinsic with no size argument
221 void fir::runtime::genTransfer(fir::FirOpBuilder &builder, mlir::Location loc,
222 mlir::Value resultBox, mlir::Value sourceBox,
223 mlir::Value moldBox) {
224
225 mlir::func::FuncOp func =
226 fir::runtime::getRuntimeFunc<mkRTKey(Transfer)>(loc, builder);
227 mlir::FunctionType fTy = func.getFunctionType();
228 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
229 mlir::Value sourceLine =
230 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
231 llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
232 builder, loc, fTy, resultBox, sourceBox, moldBox, sourceFile, sourceLine);
233 builder.create<fir::CallOp>(loc, func, args);
234 }
235
236 /// generate runtime call to transfer intrinsic with size argument
237 void fir::runtime::genTransferSize(fir::FirOpBuilder &builder,
238 mlir::Location loc, mlir::Value resultBox,
239 mlir::Value sourceBox, mlir::Value moldBox,
240 mlir::Value size) {
241 mlir::func::FuncOp func =
242 fir::runtime::getRuntimeFunc<mkRTKey(TransferSize)>(loc, builder);
243 mlir::FunctionType fTy = func.getFunctionType();
244 mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
245 mlir::Value sourceLine =
246 fir::factory::locationToLineNo(builder, loc, fTy.getInput(4));
247 llvm::SmallVector<mlir::Value> args =
248 fir::runtime::createArguments(builder, loc, fTy, resultBox, sourceBox,
249 moldBox, sourceFile, sourceLine, size);
250 builder.create<fir::CallOp>(loc, func, args);
251 }
252
253 /// generate system_clock runtime call/s
254 /// all intrinsic arguments are optional and may appear here as mlir::Value{}
255 void fir::runtime::genSystemClock(fir::FirOpBuilder &builder,
256 mlir::Location loc, mlir::Value count,
257 mlir::Value rate, mlir::Value max) {
258 auto makeCall = [&](mlir::func::FuncOp func, mlir::Value arg) {
259 mlir::Type type = arg.getType();
260 fir::IfOp ifOp{};
261 const bool isOptionalArg =
262 fir::valueHasFirAttribute(arg, fir::getOptionalAttrName());
263 if (mlir::dyn_cast<fir::PointerType>(type) ||
264 mlir::dyn_cast<fir::HeapType>(type)) {
265 // Check for a disassociated pointer or an unallocated allocatable.
266 assert(!isOptionalArg && "invalid optional argument");
267 ifOp = builder.create<fir::IfOp>(loc, builder.genIsNotNullAddr(loc, arg),
268 /*withElseRegion=*/false);
269 } else if (isOptionalArg) {
270 ifOp = builder.create<fir::IfOp>(
271 loc, builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), arg),
272 /*withElseRegion=*/false);
273 }
274 if (ifOp)
275 builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
276 mlir::Type kindTy = func.getFunctionType().getInput(0);
277 int integerKind = 8;
278 if (auto intType =
279 mlir::dyn_cast<mlir::IntegerType>(fir::unwrapRefType(type)))
280 integerKind = intType.getWidth() / 8;
281 mlir::Value kind = builder.createIntegerConstant(loc, kindTy, integerKind);
282 mlir::Value res =
283 builder.create<fir::CallOp>(loc, func, mlir::ValueRange{kind})
284 .getResult(0);
285 mlir::Value castRes =
286 builder.createConvert(loc, fir::dyn_cast_ptrEleTy(type), res);
287 builder.create<fir::StoreOp>(loc, castRes, arg);
288 if (ifOp)
289 builder.setInsertionPointAfter(ifOp);
290 };
291 using fir::runtime::getRuntimeFunc;
292 if (count)
293 makeCall(getRuntimeFunc<mkRTKey(SystemClockCount)>(loc, builder), count);
294 if (rate)
295 makeCall(getRuntimeFunc<mkRTKey(SystemClockCountRate)>(loc, builder), rate);
296 if (max)
297 makeCall(getRuntimeFunc<mkRTKey(SystemClockCountMax)>(loc, builder), max);
298 }
299
300 // CALL SIGNAL(NUMBER, HANDLER [, STATUS])
301 // The definition of the SIGNAL intrinsic allows HANDLER to be a function
302 // pointer or an integer. STATUS can be dynamically optional
303 void fir::runtime::genSignal(fir::FirOpBuilder &builder, mlir::Location loc,
304 mlir::Value number, mlir::Value handler,
305 mlir::Value status) {
306 assert(mlir::isa<mlir::IntegerType>(number.getType()));
307 mlir::Type int64 = builder.getIntegerType(64);
308 number = builder.create<fir::ConvertOp>(loc, int64, number);
309
310 mlir::Type handlerUnwrappedTy = fir::unwrapRefType(handler.getType());
311 if (mlir::isa_and_nonnull<mlir::IntegerType>(handlerUnwrappedTy)) {
312 // pass the integer as a function pointer like one would to signal(2)
313 handler = builder.create<fir::LoadOp>(loc, handler);
314 mlir::Type fnPtrTy = fir::LLVMPointerType::get(
315 mlir::FunctionType::get(handler.getContext(), {}, {}));
316 handler = builder.create<fir::ConvertOp>(loc, fnPtrTy, handler);
317 } else {
318 assert(mlir::isa<fir::BoxProcType>(handler.getType()));
319 handler = builder.create<fir::BoxAddrOp>(loc, handler);
320 }
321
322 mlir::func::FuncOp func{
323 fir::runtime::getRuntimeFunc<mkRTKey(Signal)>(loc, builder)};
324 mlir::Value stat =
325 builder.create<fir::CallOp>(loc, func, mlir::ValueRange{number, handler})
326 ->getResult(0);
327
328 // return status code via status argument (if present)
329 if (status) {
330 assert(mlir::isa<mlir::IntegerType>(fir::unwrapRefType(status.getType())));
331 // status might be dynamically optional, so test if it is present
332 mlir::Value isPresent =
333 builder.create<IsPresentOp>(loc, builder.getI1Type(), status);
334 builder.genIfOp(loc, /*results=*/{}, isPresent, /*withElseRegion=*/false)
335 .genThen([&]() {
336 stat = builder.create<fir::ConvertOp>(
337 loc, fir::unwrapRefType(status.getType()), stat);
338 builder.create<fir::StoreOp>(loc, stat, status);
339 })
340 .end();
341 }
342 }
343
344 void fir::runtime::genSleep(fir::FirOpBuilder &builder, mlir::Location loc,
345 mlir::Value seconds) {
346 mlir::Type int64 = builder.getIntegerType(64);
347 seconds = builder.create<fir::ConvertOp>(loc, int64, seconds);
348 mlir::func::FuncOp func{
349 fir::runtime::getRuntimeFunc<mkRTKey(Sleep)>(loc, builder)};
350 builder.create<fir::CallOp>(loc, func, seconds);
351 }