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