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::genRandomInit(fir::FirOpBuilder &builder, mlir::Location loc, |
110 | mlir::Value repeatable, |
111 | mlir::Value imageDistinct) { |
112 | mlir::func::FuncOp func = |
113 | fir::runtime::getRuntimeFunc<mkRTKey(RandomInit)>(loc, builder); |
114 | llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments( |
115 | builder, loc, func.getFunctionType(), repeatable, imageDistinct); |
116 | builder.create<fir::CallOp>(loc, func, args); |
117 | } |
118 | |
119 | void fir::runtime::genRandomNumber(fir::FirOpBuilder &builder, |
120 | mlir::Location loc, mlir::Value harvest) { |
121 | mlir::func::FuncOp func; |
122 | auto boxEleTy = fir::dyn_cast_ptrOrBoxEleTy(harvest.getType()); |
123 | auto eleTy = fir::unwrapSequenceType(boxEleTy); |
124 | if (eleTy.isF128()) { |
125 | func = fir::runtime::getRuntimeFunc<ForcedRandomNumberReal16>(loc, builder); |
126 | } else { |
127 | func = fir::runtime::getRuntimeFunc<mkRTKey(RandomNumber)>(loc, builder); |
128 | } |
129 | |
130 | mlir::FunctionType funcTy = func.getFunctionType(); |
131 | mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
132 | mlir::Value sourceLine = |
133 | fir::factory::locationToLineNo(builder, loc, funcTy.getInput(2)); |
134 | llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments( |
135 | builder, loc, funcTy, harvest, sourceFile, sourceLine); |
136 | builder.create<fir::CallOp>(loc, func, args); |
137 | } |
138 | |
139 | void fir::runtime::genRandomSeed(fir::FirOpBuilder &builder, mlir::Location loc, |
140 | mlir::Value size, mlir::Value put, |
141 | mlir::Value get) { |
142 | bool sizeIsPresent = |
143 | !mlir::isa_and_nonnull<fir::AbsentOp>(size.getDefiningOp()); |
144 | bool putIsPresent = |
145 | !mlir::isa_and_nonnull<fir::AbsentOp>(put.getDefiningOp()); |
146 | bool getIsPresent = |
147 | !mlir::isa_and_nonnull<fir::AbsentOp>(get.getDefiningOp()); |
148 | mlir::func::FuncOp func; |
149 | int staticArgCount = sizeIsPresent + putIsPresent + getIsPresent; |
150 | if (staticArgCount == 0) { |
151 | func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedDefaultPut)>(loc, |
152 | builder); |
153 | builder.create<fir::CallOp>(loc, func); |
154 | return; |
155 | } |
156 | mlir::FunctionType funcTy; |
157 | mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
158 | mlir::Value sourceLine; |
159 | mlir::Value argBox; |
160 | llvm::SmallVector<mlir::Value> args; |
161 | if (staticArgCount > 1) { |
162 | func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeed)>(loc, builder); |
163 | funcTy = func.getFunctionType(); |
164 | sourceLine = |
165 | fir::factory::locationToLineNo(builder, loc, funcTy.getInput(4)); |
166 | args = fir::runtime::createArguments(builder, loc, funcTy, size, put, get, |
167 | sourceFile, sourceLine); |
168 | builder.create<fir::CallOp>(loc, func, args); |
169 | return; |
170 | } |
171 | if (sizeIsPresent) { |
172 | func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedSize)>(loc, builder); |
173 | argBox = size; |
174 | } else if (putIsPresent) { |
175 | func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedPut)>(loc, builder); |
176 | argBox = put; |
177 | } else { |
178 | func = fir::runtime::getRuntimeFunc<mkRTKey(RandomSeedGet)>(loc, builder); |
179 | argBox = get; |
180 | } |
181 | funcTy = func.getFunctionType(); |
182 | sourceLine = fir::factory::locationToLineNo(builder, loc, funcTy.getInput(2)); |
183 | args = fir::runtime::createArguments(builder, loc, funcTy, argBox, sourceFile, |
184 | sourceLine); |
185 | builder.create<fir::CallOp>(loc, func, args); |
186 | } |
187 | |
188 | /// generate runtime call to transfer intrinsic with no size argument |
189 | void fir::runtime::genTransfer(fir::FirOpBuilder &builder, mlir::Location loc, |
190 | mlir::Value resultBox, mlir::Value sourceBox, |
191 | mlir::Value moldBox) { |
192 | |
193 | mlir::func::FuncOp func = |
194 | fir::runtime::getRuntimeFunc<mkRTKey(Transfer)>(loc, builder); |
195 | mlir::FunctionType fTy = func.getFunctionType(); |
196 | mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
197 | mlir::Value sourceLine = |
198 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(4)); |
199 | llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments( |
200 | builder, loc, fTy, resultBox, sourceBox, moldBox, sourceFile, sourceLine); |
201 | builder.create<fir::CallOp>(loc, func, args); |
202 | } |
203 | |
204 | /// generate runtime call to transfer intrinsic with size argument |
205 | void fir::runtime::genTransferSize(fir::FirOpBuilder &builder, |
206 | mlir::Location loc, mlir::Value resultBox, |
207 | mlir::Value sourceBox, mlir::Value moldBox, |
208 | mlir::Value size) { |
209 | mlir::func::FuncOp func = |
210 | fir::runtime::getRuntimeFunc<mkRTKey(TransferSize)>(loc, builder); |
211 | mlir::FunctionType fTy = func.getFunctionType(); |
212 | mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc); |
213 | mlir::Value sourceLine = |
214 | fir::factory::locationToLineNo(builder, loc, fTy.getInput(4)); |
215 | llvm::SmallVector<mlir::Value> args = |
216 | fir::runtime::createArguments(builder, loc, fTy, resultBox, sourceBox, |
217 | moldBox, sourceFile, sourceLine, size); |
218 | builder.create<fir::CallOp>(loc, func, args); |
219 | } |
220 | |
221 | /// generate system_clock runtime call/s |
222 | /// all intrinsic arguments are optional and may appear here as mlir::Value{} |
223 | void fir::runtime::genSystemClock(fir::FirOpBuilder &builder, |
224 | mlir::Location loc, mlir::Value count, |
225 | mlir::Value rate, mlir::Value max) { |
226 | auto makeCall = [&](mlir::func::FuncOp func, mlir::Value arg) { |
227 | mlir::Type type = arg.getType(); |
228 | fir::IfOp ifOp{}; |
229 | const bool isOptionalArg = |
230 | fir::valueHasFirAttribute(arg, fir::getOptionalAttrName()); |
231 | if (type.dyn_cast<fir::PointerType>() || type.dyn_cast<fir::HeapType>()) { |
232 | // Check for a disassociated pointer or an unallocated allocatable. |
233 | assert(!isOptionalArg && "invalid optional argument" ); |
234 | ifOp = builder.create<fir::IfOp>(loc, builder.genIsNotNullAddr(loc, arg), |
235 | /*withElseRegion=*/false); |
236 | } else if (isOptionalArg) { |
237 | ifOp = builder.create<fir::IfOp>( |
238 | loc, builder.create<fir::IsPresentOp>(loc, builder.getI1Type(), arg), |
239 | /*withElseRegion=*/false); |
240 | } |
241 | if (ifOp) |
242 | builder.setInsertionPointToStart(&ifOp.getThenRegion().front()); |
243 | mlir::Type kindTy = func.getFunctionType().getInput(0); |
244 | int integerKind = 8; |
245 | if (auto intType = fir::unwrapRefType(type).dyn_cast<mlir::IntegerType>()) |
246 | integerKind = intType.getWidth() / 8; |
247 | mlir::Value kind = builder.createIntegerConstant(loc, kindTy, integerKind); |
248 | mlir::Value res = |
249 | builder.create<fir::CallOp>(loc, func, mlir::ValueRange{kind}) |
250 | .getResult(0); |
251 | mlir::Value castRes = |
252 | builder.createConvert(loc, fir::dyn_cast_ptrEleTy(type), res); |
253 | builder.create<fir::StoreOp>(loc, castRes, arg); |
254 | if (ifOp) |
255 | builder.setInsertionPointAfter(ifOp); |
256 | }; |
257 | using fir::runtime::getRuntimeFunc; |
258 | if (count) |
259 | makeCall(getRuntimeFunc<mkRTKey(SystemClockCount)>(loc, builder), count); |
260 | if (rate) |
261 | makeCall(getRuntimeFunc<mkRTKey(SystemClockCountRate)>(loc, builder), rate); |
262 | if (max) |
263 | makeCall(getRuntimeFunc<mkRTKey(SystemClockCountMax)>(loc, builder), max); |
264 | } |
265 | |
266 | // CALL SIGNAL(NUMBER, HANDLER [, STATUS]) |
267 | // The definition of the SIGNAL intrinsic allows HANDLER to be a function |
268 | // pointer or an integer. STATUS can be dynamically optional |
269 | void fir::runtime::genSignal(fir::FirOpBuilder &builder, mlir::Location loc, |
270 | mlir::Value number, mlir::Value handler, |
271 | mlir::Value status) { |
272 | assert(mlir::isa<mlir::IntegerType>(number.getType())); |
273 | mlir::Type int64 = builder.getIntegerType(64); |
274 | number = builder.create<fir::ConvertOp>(loc, int64, number); |
275 | |
276 | mlir::Type handlerUnwrappedTy = fir::unwrapRefType(handler.getType()); |
277 | if (mlir::isa_and_nonnull<mlir::IntegerType>(handlerUnwrappedTy)) { |
278 | // pass the integer as a function pointer like one would to signal(2) |
279 | handler = builder.create<fir::LoadOp>(loc, handler); |
280 | mlir::Type fnPtrTy = fir::LLVMPointerType::get( |
281 | mlir::FunctionType::get(handler.getContext(), {}, {})); |
282 | handler = builder.create<fir::ConvertOp>(loc, fnPtrTy, handler); |
283 | } else { |
284 | assert(mlir::isa<fir::BoxProcType>(handler.getType())); |
285 | handler = builder.create<fir::BoxAddrOp>(loc, handler); |
286 | } |
287 | |
288 | mlir::func::FuncOp func{ |
289 | fir::runtime::getRuntimeFunc<mkRTKey(Signal)>(loc, builder)}; |
290 | mlir::Value stat = |
291 | builder.create<fir::CallOp>(loc, func, mlir::ValueRange{number, handler}) |
292 | ->getResult(0); |
293 | |
294 | // return status code via status argument (if present) |
295 | if (status) { |
296 | assert(mlir::isa<mlir::IntegerType>(fir::unwrapRefType(status.getType()))); |
297 | // status might be dynamically optional, so test if it is present |
298 | mlir::Value isPresent = |
299 | builder.create<IsPresentOp>(loc, builder.getI1Type(), status); |
300 | builder.genIfOp(loc, /*results=*/{}, isPresent, /*withElseRegion=*/false) |
301 | .genThen([&]() { |
302 | stat = builder.create<fir::ConvertOp>( |
303 | loc, fir::unwrapRefType(status.getType()), stat); |
304 | builder.create<fir::StoreOp>(loc, stat, status); |
305 | }) |
306 | .end(); |
307 | } |
308 | } |
309 | |
310 | void fir::runtime::genSleep(fir::FirOpBuilder &builder, mlir::Location loc, |
311 | mlir::Value seconds) { |
312 | mlir::Type int64 = builder.getIntegerType(64); |
313 | seconds = builder.create<fir::ConvertOp>(loc, int64, seconds); |
314 | mlir::func::FuncOp func{ |
315 | fir::runtime::getRuntimeFunc<mkRTKey(Sleep)>(loc, builder)}; |
316 | builder.create<fir::CallOp>(loc, func, seconds); |
317 | } |
318 | |