Skip to content

Commit 0ff6e49

Browse files
committed
GETUID runtime and lowering intrinsics
Returns the numerical user ID of the current process. The return value of GETUID is an INTEGER of the default kind.
1 parent 1ad920f commit 0ff6e49

File tree

12 files changed

+74
-1
lines changed

12 files changed

+74
-1
lines changed

flang/docs/Intrinsics.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -750,7 +750,7 @@ This phase currently supports all the intrinsic procedures listed above but the
750750
| Coarray intrinsic functions | IMAGE_INDEX, COSHAPE |
751751
| Object characteristic inquiry functions | ALLOCATED, ASSOCIATED, EXTENDS_TYPE_OF, IS_CONTIGUOUS, PRESENT, RANK, SAME_TYPE, STORAGE_SIZE |
752752
| Type inquiry intrinsic functions | BIT_SIZE, DIGITS, EPSILON, HUGE, KIND, MAXEXPONENT, MINEXPONENT, NEW_LINE, PRECISION, RADIX, RANGE, TINY|
753-
| Non-standard intrinsic functions | AND, OR, XOR, SHIFT, ZEXT, IZEXT, COSD, SIND, TAND, ACOSD, ASIND, ATAND, ATAN2D, COMPL, EQV, NEQV, INT8, JINT, JNINT, KNINT, QCMPLX, DREAL, DFLOAT, QEXT, QFLOAT, QREAL, DNUM, NUM, JNUM, KNUM, QNUM, RNUM, RAN, RANF, ILEN, SIZEOF, MCLOCK, SECNDS, COTAN, IBCHNG, ISHA, ISHC, ISHL, IXOR, IARG, IARGC, NARGS, NUMARG, BADDRESS, IADDR, CACHESIZE, EOF, FP_CLASS, INT_PTR_KIND, ISNAN, MALLOC |
753+
| Non-standard intrinsic functions | AND, OR, XOR, SHIFT, ZEXT, IZEXT, COSD, SIND, TAND, ACOSD, ASIND, ATAND, ATAN2D, COMPL, EQV, NEQV, INT8, JINT, JNINT, KNINT, QCMPLX, DREAL, DFLOAT, QEXT, QFLOAT, QREAL, DNUM, NUM, JNUM, KNUM, QNUM, RNUM, RAN, RANF, ILEN, SIZEOF, MCLOCK, SECNDS, COTAN, IBCHNG, ISHA, ISHC, ISHL, IXOR, IARG, IARGC, NARGS, NUMARG, BADDRESS, IADDR, CACHESIZE, EOF, FP_CLASS, INT_PTR_KIND, ISNAN, MALLOC, GETUID |
754754
| Intrinsic subroutines |MVBITS (elemental), CPU_TIME, DATE_AND_TIME, EVENT_QUERY, EXECUTE_COMMAND_LINE, GET_COMMAND, GET_COMMAND_ARGUMENT, GET_ENVIRONMENT_VARIABLE, MOVE_ALLOC, RANDOM_INIT, RANDOM_NUMBER, RANDOM_SEED, SYSTEM_CLOCK |
755755
| Atomic intrinsic subroutines | ATOMIC_ADD |
756756
| Collective intrinsic subroutines | CO_REDUCE |

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -225,6 +225,8 @@ struct IntrinsicLibrary {
225225
void genGetCommand(mlir::ArrayRef<fir::ExtendedValue> args);
226226
void genGetCommandArgument(mlir::ArrayRef<fir::ExtendedValue> args);
227227
void genGetEnvironmentVariable(llvm::ArrayRef<fir::ExtendedValue>);
228+
mlir::Value genGetUID(mlir::Type resultType,
229+
llvm::ArrayRef<mlir::Value> args);
228230
fir::ExtendedValue genIall(mlir::Type, llvm::ArrayRef<fir::ExtendedValue>);
229231
/// Lowering for the IAND intrinsic. The IAND intrinsic expects two arguments
230232
/// in the llvm::ArrayRef.

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,5 +49,9 @@ mlir::Value genGetEnvVariable(fir::FirOpBuilder &, mlir::Location,
4949
mlir::Value length, mlir::Value trimName,
5050
mlir::Value errmsg);
5151

52+
/// Generate a call to the GetUID runtime function which implements the
53+
/// GETUID intrinsic.
54+
mlir::Value genGetUID(fir::FirOpBuilder &, mlir::Location);
55+
5256
} // namespace fir::runtime
5357
#endif // FORTRAN_OPTIMIZER_BUILDER_RUNTIME_COMMAND_H

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,14 @@ using FuncTypeBuilderFunc = mlir::FunctionType (*)(mlir::MLIRContext *);
6262
/// standard type `i32` when `sizeof(int)` is 4.
6363
template <typename T>
6464
static constexpr TypeBuilderFunc getModel();
65+
66+
template <>
67+
constexpr TypeBuilderFunc getModel<unsigned int>() {
68+
return [](mlir::MLIRContext *context) -> mlir::Type {
69+
return mlir::IntegerType::get(context, 8 * sizeof(unsigned int));
70+
};
71+
}
72+
6573
template <>
6674
constexpr TypeBuilderFunc getModel<short int>() {
6775
return [](mlir::MLIRContext *context) -> mlir::Type {

flang/include/flang/Runtime/command.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@
1313

1414
#include <cstdint>
1515

16+
#ifdef _WIN32
17+
#else
18+
#include "sys/types.h"
19+
#endif
1620
namespace Fortran::runtime {
1721
class Descriptor;
1822

@@ -47,6 +51,10 @@ std::int32_t RTNAME(GetEnvVariable)(const Descriptor &name,
4751
bool trim_name = true, const Descriptor *errmsg = nullptr,
4852
const char *sourceFile = nullptr, int line = 0);
4953
}
54+
55+
// Calls getuid()
56+
uid_t RTNAME(GetUID)();
57+
5058
} // namespace Fortran::runtime
5159

5260
#endif // FORTRAN_RUNTIME_COMMAND_H_

flang/lib/Evaluate/intrinsics.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -500,6 +500,7 @@ static const IntrinsicInterface genericIntrinsicFunction[]{
500500
{"gamma", {{"x", SameReal}}, SameReal},
501501
{"get_team", {{"level", DefaultInt, Rank::scalar, Optionality::optional}},
502502
TeamType, Rank::scalar, IntrinsicClass::transformationalFunction},
503+
{"getuid", {}, DefaultInt},
503504
{"huge",
504505
{{"x", SameIntOrReal, Rank::anyOrAssumedRank, Optionality::required,
505506
common::Intent::In, {ArgFlag::canBeNull}}},

flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,6 +253,7 @@ static constexpr IntrinsicHandler handlers[]{
253253
{"trim_name", asAddr, handleDynamicOptional},
254254
{"errmsg", asBox, handleDynamicOptional}}},
255255
/*isElemental=*/false},
256+
{"getuid", &I::genGetUID},
256257
{"iachar", &I::genIchar},
257258
{"iall",
258259
&I::genIall,
@@ -2944,6 +2945,14 @@ void IntrinsicLibrary::genGetCommand(llvm::ArrayRef<fir::ExtendedValue> args) {
29442945
}
29452946
}
29462947

2948+
// GETUID
2949+
mlir::Value IntrinsicLibrary::genGetUID(mlir::Type resultType,
2950+
llvm::ArrayRef<mlir::Value> args) {
2951+
assert(args.size() == 0 && "getuid takes no input");
2952+
return builder.createConvert(loc, resultType,
2953+
fir::runtime::genGetUID(builder, loc));
2954+
}
2955+
29472956
// GET_COMMAND_ARGUMENT
29482957
void IntrinsicLibrary::genGetCommandArgument(
29492958
llvm::ArrayRef<fir::ExtendedValue> args) {

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,3 +80,11 @@ mlir::Value fir::runtime::genGetEnvVariable(fir::FirOpBuilder &builder,
8080
sourceFile, sourceLine);
8181
return builder.create<fir::CallOp>(loc, runtimeFunc, args).getResult(0);
8282
}
83+
84+
mlir::Value fir::runtime::genGetUID(fir::FirOpBuilder &builder,
85+
mlir::Location loc) {
86+
auto runtimeFunc =
87+
fir::runtime::getRuntimeFunc<mkRTKey(GetUID)>(loc, builder);
88+
89+
return builder.create<fir::CallOp>(loc, runtimeFunc).getResult(0);
90+
}

flang/runtime/command.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@
1515
#include <cstdlib>
1616
#include <limits>
1717

18+
#ifdef _WIN32
19+
#else
20+
#include <unistd.h>
21+
#endif
22+
1823
namespace Fortran::runtime {
1924
std::int32_t RTNAME(ArgumentCount)() {
2025
int argc{executionEnvironment.argc};
@@ -25,6 +30,8 @@ std::int32_t RTNAME(ArgumentCount)() {
2530
return 0;
2631
}
2732

33+
uid_t RTNAME(GetUID)() { return getuid(); }
34+
2835
// Returns the length of the \p string. Assumes \p string is valid.
2936
static std::int64_t StringLength(const char *string) {
3037
std::size_t length{std::strlen(string)};
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
! RUN: bbc -emit-hlfir %s -o - | FileCheck %s
2+
3+
! CHECK-LABEL: func.func @_QPall_args() {
4+
! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "uid", uniq_name = "_QFall_argsEuid"}
5+
! CHECK: %[[VAL_1:.*]]:2 = hlfir.declare %0 {uniq_name = "_QFall_argsEuid"} : (!fir.ref<i32>) -> (!fir.ref<i32>, !fir.ref<i32>)
6+
! CHECK: %[[VAL_2:.*]] = fir.call @_FortranAGetUID() fastmath<contract> : () -> i32
7+
! CHECK: hlfir.assign %[[VAL_2:.*]] to %[[VAL_1:.*]]#0 : i32, !fir.ref<i32>
8+
! CHECK: return
9+
! CHECK: }
10+
11+
subroutine all_args()
12+
integer :: uid
13+
uid = getuid()
14+
end

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,10 @@ TEST_F(RuntimeCallTest, genGetEnvVariable) {
4444
checkCallOp(result.getDefiningOp(), "_FortranAGetEnvVariable", /*nbArgs=*/5,
4545
/*addLocArgs=*/true);
4646
}
47+
48+
TEST_F(RuntimeCallTest, genGetUID) {
49+
mlir::Location loc = firBuilder->getUnknownLoc();
50+
mlir::Value result = fir::runtime::genGetUID(*firBuilder, loc);
51+
checkCallOp(result.getDefiningOp(), "_FortranAGetUID", /*nbArgs=*/0,
52+
/*addLocArgs=*/false);
53+
}

flang/unittests/Runtime/CommandTest.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,11 @@ TEST_F(ZeroArguments, GetCommandArgument) {
227227

228228
TEST_F(ZeroArguments, GetCommand) { CheckCommandValue(commandOnlyArgv, 1); }
229229

230+
TEST_F(ZeroArguments, GetUID) {
231+
// uid should always greater than 0, in both linux and windows
232+
EXPECT_GT(RTNAME(GetUID)(), 0U);
233+
}
234+
230235
static const char *oneArgArgv[]{"aProgram", "anArgumentOfLength20"};
231236
class OneArgument : public CommandFixture {
232237
protected:

0 commit comments

Comments
 (0)