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