1 //===-- Command.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/Command.h"
10 #include "flang/Optimizer/Builder/FIRBuilder.h"
11 #include "flang/Optimizer/Builder/Runtime/RTBuilder.h"
12 #include "flang/Runtime/command.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 
genCommandArgumentCount(fir::FirOpBuilder & builder,mlir::Location loc)28 mlir::Value fir::runtime::genCommandArgumentCount(fir::FirOpBuilder &builder,
29                                                   mlir::Location loc) {
30   auto argumentCountFunc =
31       fir::runtime::getRuntimeFunc<mkRTKey(ArgumentCount)>(loc, builder);
32   return builder.create<fir::CallOp>(loc, argumentCountFunc).getResult(0);
33 }
34 
genArgumentValue(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value number,mlir::Value value,mlir::Value errmsg)35 mlir::Value fir::runtime::genArgumentValue(fir::FirOpBuilder &builder,
36                                            mlir::Location loc,
37                                            mlir::Value number,
38                                            mlir::Value value,
39                                            mlir::Value errmsg) {
40   auto argumentValueFunc =
41       fir::runtime::getRuntimeFunc<mkRTKey(ArgumentValue)>(loc, builder);
42   llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
43       builder, loc, argumentValueFunc.getFunctionType(), number, value, errmsg);
44   return builder.create<fir::CallOp>(loc, argumentValueFunc, args).getResult(0);
45 }
46 
genArgumentLength(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value number)47 mlir::Value fir::runtime::genArgumentLength(fir::FirOpBuilder &builder,
48                                             mlir::Location loc,
49                                             mlir::Value number) {
50   auto argumentLengthFunc =
51       fir::runtime::getRuntimeFunc<mkRTKey(ArgumentLength)>(loc, builder);
52   llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
53       builder, loc, argumentLengthFunc.getFunctionType(), number);
54   return builder.create<fir::CallOp>(loc, argumentLengthFunc, args)
55       .getResult(0);
56 }
57 
genEnvVariableValue(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value name,mlir::Value value,mlir::Value trimName,mlir::Value errmsg)58 mlir::Value fir::runtime::genEnvVariableValue(
59     fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value name,
60     mlir::Value value, mlir::Value trimName, mlir::Value errmsg) {
61   auto valueFunc =
62       fir::runtime::getRuntimeFunc<mkRTKey(EnvVariableValue)>(loc, builder);
63   mlir::FunctionType valueFuncTy = valueFunc.getFunctionType();
64   mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
65   mlir::Value sourceLine =
66       fir::factory::locationToLineNo(builder, loc, valueFuncTy.getInput(5));
67   llvm::SmallVector<mlir::Value> args =
68       fir::runtime::createArguments(builder, loc, valueFuncTy, name, value,
69                                     trimName, errmsg, sourceFile, sourceLine);
70   return builder.create<fir::CallOp>(loc, valueFunc, args).getResult(0);
71 }
72 
genEnvVariableLength(fir::FirOpBuilder & builder,mlir::Location loc,mlir::Value name,mlir::Value trimName)73 mlir::Value fir::runtime::genEnvVariableLength(fir::FirOpBuilder &builder,
74                                                mlir::Location loc,
75                                                mlir::Value name,
76                                                mlir::Value trimName) {
77   auto lengthFunc =
78       fir::runtime::getRuntimeFunc<mkRTKey(EnvVariableLength)>(loc, builder);
79   mlir::FunctionType lengthFuncTy = lengthFunc.getFunctionType();
80   mlir::Value sourceFile = fir::factory::locationToFilename(builder, loc);
81   mlir::Value sourceLine =
82       fir::factory::locationToLineNo(builder, loc, lengthFuncTy.getInput(3));
83   llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
84       builder, loc, lengthFuncTy, name, trimName, sourceFile, sourceLine);
85   return builder.create<fir::CallOp>(loc, lengthFunc, args).getResult(0);
86 }
87