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).
24 inline bool isAbsent(mlir::Value val) {
25   return mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp());
26 }
27 
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 
35 void fir::runtime::genGetCommandArgument(fir::FirOpBuilder &builder,
36                                          mlir::Location loc, mlir::Value number,
37                                          mlir::Value value, mlir::Value length,
38                                          mlir::Value status,
39                                          mlir::Value errmsg) {
40   auto argumentValueFunc =
41       fir::runtime::getRuntimeFunc<mkRTKey(ArgumentValue)>(loc, builder);
42   auto argumentLengthFunc =
43       fir::runtime::getRuntimeFunc<mkRTKey(ArgumentLength)>(loc, builder);
44 
45   mlir::Value valueResult;
46   // Run `ArgumentValue` intrinsic only if we have a "value" in either "VALUE",
47   // "STATUS" or "ERRMSG" parameters.
48   if (!isAbsent(value) || status || !isAbsent(errmsg)) {
49     llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
50         builder, loc, argumentValueFunc.getFunctionType(), number, value,
51         errmsg);
52     valueResult =
53         builder.create<fir::CallOp>(loc, argumentValueFunc, args).getResult(0);
54   }
55 
56   // Only save result of `ArgumentValue` if "STATUS" parameter has been given
57   if (status) {
58     const mlir::Value statusLoaded = builder.create<fir::LoadOp>(loc, status);
59     mlir::Value resultCast =
60         builder.createConvert(loc, statusLoaded.getType(), valueResult);
61     builder.create<fir::StoreOp>(loc, resultCast, status);
62   }
63 
64   // Only run `ArgumentLength` intrinsic if "LENGTH" parameter provided
65   if (length) {
66     llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
67         builder, loc, argumentLengthFunc.getFunctionType(), number);
68     mlir::Value result =
69         builder.create<fir::CallOp>(loc, argumentLengthFunc, args).getResult(0);
70     const mlir::Value valueLoaded = builder.create<fir::LoadOp>(loc, length);
71     mlir::Value resultCast =
72         builder.createConvert(loc, valueLoaded.getType(), result);
73     builder.create<fir::StoreOp>(loc, resultCast, length);
74   }
75 }
76 
77 void fir::runtime::genGetEnvironmentVariable(
78     fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value name,
79     mlir::Value value, mlir::Value length, mlir::Value status,
80     mlir::Value trimName, mlir::Value errmsg) {
81   auto valueFunc =
82       fir::runtime::getRuntimeFunc<mkRTKey(EnvVariableValue)>(loc, builder);
83   auto lengthFunc =
84       fir::runtime::getRuntimeFunc<mkRTKey(EnvVariableLength)>(loc, builder);
85 
86   mlir::Value sourceFile;
87   mlir::Value sourceLine;
88   // We only need `sourceFile` and `sourceLine` variables when calling either
89   // `EnvVariableValue` or `EnvVariableLength` below.
90   if (!isAbsent(value) || status || !isAbsent(errmsg) || length) {
91     sourceFile = fir::factory::locationToFilename(builder, loc);
92     sourceLine = fir::factory::locationToLineNo(
93         builder, loc, valueFunc.getFunctionType().getInput(5));
94   }
95 
96   mlir::Value valueResult;
97   // Run `EnvVariableValue` intrinsic only if we have a "value" in either
98   // "VALUE", "STATUS" or "ERRMSG" parameters.
99   if (!isAbsent(value) || status || !isAbsent(errmsg)) {
100     llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
101         builder, loc, valueFunc.getFunctionType(), name, value, trimName,
102         errmsg, sourceFile, sourceLine);
103     valueResult =
104         builder.create<fir::CallOp>(loc, valueFunc, args).getResult(0);
105   }
106 
107   // Only save result of `EnvVariableValue` if "STATUS" parameter provided
108   if (status) {
109     const mlir::Value statusLoaded = builder.create<fir::LoadOp>(loc, status);
110     mlir::Value resultCast =
111         builder.createConvert(loc, statusLoaded.getType(), valueResult);
112     builder.create<fir::StoreOp>(loc, resultCast, status);
113   }
114 
115   // Only run `EnvVariableLength` intrinsic if "LENGTH" parameter provided
116   if (length) {
117     llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
118         builder, loc, lengthFunc.getFunctionType(), name, trimName, sourceFile,
119         sourceLine);
120     mlir::Value result =
121         builder.create<fir::CallOp>(loc, lengthFunc, args).getResult(0);
122     const mlir::Value lengthLoaded = builder.create<fir::LoadOp>(loc, length);
123     mlir::Value resultCast =
124         builder.createConvert(loc, lengthLoaded.getType(), result);
125     builder.create<fir::StoreOp>(loc, resultCast, length);
126   }
127 }
128