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