xref: /llvm-project/flang/lib/Optimizer/Builder/Runtime/Execute.cpp (revision e2b896aa640fec25f68d283948c1b44711087f0f)
1 //===-- Execute.cpp -- generate command line runtime API calls ------------===//
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/Execute.h"
10 #include "flang/Optimizer/Builder/FIRBuilder.h"
11 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12 #include "flang/Runtime/execute.h"
13 
14 using namespace Fortran::runtime;
15 
16 // Certain runtime intrinsics should only be run when select parameters of the
17 // intrisic are supplied. In certain cases one of these parameters may not be
18 // given, however the intrinsic needs to be run due to another required
19 // parameter being supplied. In this case the missing parameter is assigned to
20 // have an "absent" value. This typically happens in IntrinsicCall.cpp. For this
21 // reason the extra indirection with `isAbsent` is needed for testing whether a
22 // given parameter is actually present (so that parameters with "value" absent
23 // are not considered as present).
isAbsent(mlir::Value val)24 inline bool isAbsent(mlir::Value val) {
25   return mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp());
26 }
27 
genExecuteCommandLine(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value command,mlir::Value wait,mlir::Value exitstat,mlir::Value cmdstat,mlir::Value cmdmsg)28 void fir::runtime::genExecuteCommandLine(fir::FirOpBuilder &builder,
29                                          mlir::Location loc,
30                                          mlir::Value command, mlir::Value wait,
31                                          mlir::Value exitstat,
32                                          mlir::Value cmdstat,
33                                          mlir::Value cmdmsg) {
34   auto runtimeFunc =
35       fir::runtime::getRuntimeFunc<mkRTKey(ExecuteCommandLine)>(loc, builder);
36   mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
37   mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
38   mlir::Value sourceLine =
39       fir::factory::locationToLineNo(builder, loc, runtimeFuncTy.getInput(6));
40   llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
41       builder, loc, runtimeFuncTy, command, wait, exitstat, cmdstat, cmdmsg,
42       sourceFile, sourceLine);
43   builder.create<fir::CallOp>(loc, runtimeFunc, args);
44 }
45