Skip to content

Commit 6ad466c

Browse files
committed
[flang] Implement IERRNO intrinsic
1 parent 95ff3b5 commit 6ad466c

File tree

8 files changed

+41
-0
lines changed

8 files changed

+41
-0
lines changed

flang/include/flang/Optimizer/Builder/IntrinsicCall.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -307,6 +307,8 @@ struct IntrinsicLibrary {
307307
mlir::Value genIeor(mlir::Type, llvm::ArrayRef<mlir::Value>);
308308
fir::ExtendedValue genIndex(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
309309
mlir::Value genIor(mlir::Type, llvm::ArrayRef<mlir::Value>);
310+
mlir::Value genIerrno(mlir::Type resultType,
311+
llvm::ArrayRef<mlir::Value> args);
310312
fir::ExtendedValue genIparity(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
311313
fir::ExtendedValue genIsContiguous(mlir::Type,
312314
llvm::ArrayRef<fir::ExtendedValue>);

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,5 +58,9 @@ mlir::Value genGetEnvVariable(fir::FirOpBuilder &, mlir::Location,
5858
mlir::Value genGetCwd(fir::FirOpBuilder &builder, mlir::Location loc,
5959
mlir::Value c);
6060

61+
/// Generate a call to the Ierrno runtime function which implements
62+
/// the IERRNO intrinsic.
63+
mlir::Value genIerrno(fir::FirOpBuilder &, mlir::Location);
64+
6165
} // namespace fir::runtime
6266
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H

flang/include/flang/Runtime/command.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,10 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
5959
// Calls getcwd()
6060
std::int32_t RTNAME(GetCwd)(
6161
const Descriptor &cwd, const char *sourceFile, int line);
62+
63+
// IERRNO
64+
// Returns the last system error number, as given by the C errno variable.
65+
int RTNAME(Ierrno)();
6266
}
6367
} // namespace Fortran::runtime
6468

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -591,6 +591,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
591591
{{"i", OperandUnsigned}, {"j", OperandUnsigned, Rank::elementalOrBOZ}},
592592
OperandUnsigned},
593593
{"ieor", {{"i", BOZ}, {"j", SameIntOrUnsigned}}, SameIntOrUnsigned},
594+
{"ierrno", {}, DefaultInt},
594595
{"image_index",
595596
{{"coarray", AnyData, Rank::coarray}, {"sub", AnyInt, Rank::vector}},
596597
DefaultInt, Rank::scalar, IntrinsicClass::transformationalFunction},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -403,6 +403,7 @@ static constexpr IntrinsicHandler handlers[]{
403403
{"ieee_unordered", &I::genIeeeUnordered},
404404
{"ieee_value", &I::genIeeeValue},
405405
{"ieor", &I::genIeor},
406+
{"ierrno", &I::genIerrno},
406407
{"index",
407408
&I::genIndex,
408409
{{{"string", asAddr},
@@ -5546,6 +5547,13 @@ mlir::Value IntrinsicLibrary::genIeor(mlir::Type resultType,
55465547
args[1]);
55475548
}
55485549

5550+
// IERRNO
5551+
mlir::Value IntrinsicLibrary::genIerrno(mlir::Type resultType,
5552+
llvm::ArrayRef<mlir::Value> args) {
5553+
assert(args.size() == 0);
5554+
return fir::runtime::genIerrno(builder, loc);
5555+
}
5556+
55495557
// INDEX
55505558
fir::ExtendedValue
55515559
IntrinsicLibrary::genIndex(mlir::Type resultType,

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,11 @@ mlir::Value fir::runtime::genGetCwd(fir::FirOpBuilder &builder,
101101
builder, loc, runtimeFuncTy, cwd, sourceFile, sourceLine);
102102
return builder.create<fir::CallOp>(loc, func, args).getResult(0);
103103
}
104+
105+
mlir::Value fir::runtime::genIerrno(fir::FirOpBuilder &builder,
106+
mlir::Location loc) {
107+
auto runtimeFunc =
108+
fir::runtime::getRuntimeFunc<mkRTKey(Ierrno)>(loc, builder);
109+
mlir::FunctionType runtimeFuncTy = runtimeFunc.getFunctionType();
110+
return builder.create<fir::CallOp>(loc, runtimeFunc).getResult(0);
111+
}

flang/runtime/command.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -261,4 +261,6 @@ std::int32_t RTNAME(GetCwd)(
261261
return status;
262262
}
263263

264+
int RTNAME(Ierrno)() { return errno; }
265+
264266
} // namespace Fortran::runtime
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
! RUN: bbc -emit-fir -hlfir=false %s -o - | FileCheck --check-prefixes=CHECK %s
2+
! RUN: %flang_fc1 -emit-fir -flang-deprecated-no-hlfir %s -o - | FileCheck --check-prefixes=CHECK %s
3+
4+
! CHECK-LABEL: func @_QPtest_ierrno(
5+
subroutine test_ierrno(name)
6+
integer :: i
7+
i = ierrno()
8+
! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_ierrnoEi"}
9+
! CHECK: %[[VAL_1:.*]] = fir.call @_FortranAIerrno
10+
! CHECK: fir.store %[[VAL_1]] to %[[VAL_0]] : !fir.ref<i32>
11+
! CHECK: return
12+
end subroutine test_ierrno

0 commit comments

Comments
 (0)