Skip to content

Commit 5ebbcfa

Browse files
committed
[flang] Upstream partial lowering of GET_ENVIRONMENT_VARIABLE intrinsic
This patch adds partial lowering of the "GET_ENVIRONMENT_VARIABLE" intrinsic to the backend runtime hook implemented in patches D111394 and D112698. It also renames the `isPresent` lambda to `isAbsent` and moves it out to its own function in `Command.cpp`. Corresponding comment fixes for this are also modified. Lastly it adds the i1 type to `RuntimeCallTestBash.h`. Differential Revision: https://reviews.llvm.org/D118984
1 parent 56d6ccd commit 5ebbcfa

File tree

4 files changed

+101
-13
lines changed

4 files changed

+101
-13
lines changed

flang/include/flang/Optimizer/Builder/Runtime/Command.h

+9
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,14 @@ void genGetCommandArgument(fir::FirOpBuilder &, mlir::Location,
3232
mlir::Value length, mlir::Value status,
3333
mlir::Value errmsg);
3434

35+
/// Generate call to GET_ENVIRONMENT_VARIABLE intrinsic runtime routine.
36+
/// Note that GET_ENVIRONMENT_ARGUMENT intrinsic is split between 2 functions in
37+
/// implementation; EnvVariableValue and EnvVariableLength. So we handle each
38+
/// seperately.
39+
void genGetEnvironmentVariable(fir::FirOpBuilder &, mlir::Location,
40+
mlir::Value number, mlir::Value value,
41+
mlir::Value length, mlir::Value status,
42+
mlir::Value trimName, mlir::Value errmsg);
43+
3544
} // namespace fir::runtime
3645
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H

flang/lib/Optimizer/Builder/Runtime/Command.cpp

+69-13
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313

1414
using namespace Fortran::runtime;
1515

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+
1628
mlir::Value fir::runtime::genCommandArgumentCount(fir::FirOpBuilder &builder,
1729
mlir::Location loc) {
1830
auto argumentCountFunc =
@@ -30,33 +42,25 @@ void fir::runtime::genGetCommandArgument(fir::FirOpBuilder &builder,
3042
auto argumentLengthFunc =
3143
fir::runtime::getRuntimeFunc<mkRTKey(ArgumentLength)>(loc, builder);
3244

33-
auto isPresent = [&](mlir::Value val) -> bool {
34-
return !mlir::isa_and_nonnull<fir::AbsentOp>(val.getDefiningOp());
35-
};
36-
3745
mlir::Value valueResult;
38-
// Run `ArgumentValue` intrisc only if we have either "value", "status" or
39-
// "errmsg" `ArgumentValue` "requires" existing values for its arguments
40-
// "value" and "errmsg". So in the case they aren't given, but the user has
41-
// requested "status", we have to assign "absent" values to them before
42-
// calling `ArgumentValue`. This happens in IntrinsicCall.cpp. For this reason
43-
// we need extra indirection with `isPresent` for testing whether "value" or
44-
// "errmsg" is present.
45-
if (isPresent(value) || status || isPresent(errmsg)) {
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)) {
4649
llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
4750
builder, loc, argumentValueFunc.getType(), number, value, errmsg);
4851
valueResult =
4952
builder.create<fir::CallOp>(loc, argumentValueFunc, args).getResult(0);
5053
}
5154

52-
// Only save result of ArgumentValue if "status" parameter has been given
55+
// Only save result of `ArgumentValue` if "STATUS" parameter has been given
5356
if (status) {
5457
const mlir::Value statusLoaded = builder.create<fir::LoadOp>(loc, status);
5558
mlir::Value resultCast =
5659
builder.createConvert(loc, statusLoaded.getType(), valueResult);
5760
builder.create<fir::StoreOp>(loc, resultCast, status);
5861
}
5962

63+
// Only run `ArgumentLength` intrinsic if "LENGTH" parameter provided
6064
if (length) {
6165
llvm::SmallVector<mlir::Value> args = fir::runtime::createArguments(
6266
builder, loc, argumentLengthFunc.getType(), number);
@@ -68,3 +72,55 @@ void fir::runtime::genGetCommandArgument(fir::FirOpBuilder &builder,
6872
builder.create<fir::StoreOp>(loc, resultCast, length);
6973
}
7074
}
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+
}

flang/unittests/Optimizer/Builder/Runtime/CommandTest.cpp

+21
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,24 @@ TEST_F(RuntimeCallTest, genGetCommandArgument) {
3636
EXPECT_TRUE(block) << "Failed to retrieve the block!";
3737
checkBlockForCallOp(block, "_FortranAArgumentLength", /*nbArgs=*/1);
3838
}
39+
40+
TEST_F(RuntimeCallTest, genGetEnvironmentVariable) {
41+
mlir::Location loc = firBuilder->getUnknownLoc();
42+
mlir::Type intTy = firBuilder->getDefaultIntegerType();
43+
mlir::Type charTy = fir::BoxType::get(firBuilder->getNoneType());
44+
mlir::Value number = firBuilder->create<fir::UndefOp>(loc, intTy);
45+
mlir::Value value = firBuilder->create<fir::UndefOp>(loc, charTy);
46+
mlir::Value trimName = firBuilder->create<fir::UndefOp>(loc, i1Ty);
47+
mlir::Value errmsg = firBuilder->create<fir::UndefOp>(loc, charTy);
48+
// genGetCommandArgument expects `length` and `status` to be memory references
49+
mlir::Value length = firBuilder->create<fir::AllocaOp>(loc, intTy);
50+
mlir::Value status = firBuilder->create<fir::AllocaOp>(loc, intTy);
51+
52+
fir::runtime::genGetEnvironmentVariable(
53+
*firBuilder, loc, number, value, length, status, trimName, errmsg);
54+
checkCallOpFromResultBox(
55+
value, "_FortranAEnvVariableValue", /*nbArgs=*/6, /*addLocArgs=*/false);
56+
mlir::Block *block = firBuilder->getBlock();
57+
EXPECT_TRUE(block) << "Failed to retrieve the block!";
58+
checkBlockForCallOp(block, "_FortranAEnvVariableLength", /*nbArgs=*/4);
59+
}

flang/unittests/Optimizer/Builder/Runtime/RuntimeCallTestBase.h

+2
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct RuntimeCallTest : public testing::Test {
3333
kindMap = std::make_unique<fir::KindMapping>(&context);
3434
firBuilder = std::make_unique<fir::FirOpBuilder>(mod, *kindMap);
3535

36+
i1Ty = firBuilder->getI1Type();
3637
i8Ty = firBuilder->getI8Type();
3738
i16Ty = firBuilder->getIntegerType(16);
3839
i32Ty = firBuilder->getI32Type();
@@ -58,6 +59,7 @@ struct RuntimeCallTest : public testing::Test {
5859
std::unique_ptr<fir::FirOpBuilder> firBuilder;
5960

6061
// Commonly used types
62+
mlir::Type i1Ty;
6163
mlir::Type i8Ty;
6264
mlir::Type i16Ty;
6365
mlir::Type i32Ty;

0 commit comments

Comments
 (0)