Skip to content

Commit cbecb26

Browse files
committed
[flang] Implement IERRNO intrinsic
1 parent 5a34e6f commit cbecb26

File tree

4 files changed

+29
-0
lines changed

4 files changed

+29
-0
lines changed

flang/docs/Intrinsics.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1095,3 +1095,14 @@ program chdir_func
10951095
print *, "status: ", status
10961096
end program chdir_func
10971097
```
1098+
1099+
### Non-Standard Intrinsics: IERRNO
1100+
1101+
#### Description
1102+
`IERRNO()` returns the last system error number, as given by the C `errno` variable.
1103+
1104+
#### Usage and Info
1105+
1106+
- **Standard:** GNU extension
1107+
- **Class:** function
1108+
- **Syntax:** `RESULT = IERRNO()`

flang/include/flang/Runtime/extensions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,5 +72,8 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
7272
// GNU extension subroutine CHDIR(NAME, [STATUS])
7373
int RTNAME(Chdir)(const char *name);
7474

75+
// GNU extension function IERRNO()
76+
int FORTRAN_PROCEDURE_NAME(ierrno)();
77+
7578
} // extern "C"
7679
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_

flang/runtime/extensions.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -260,5 +260,7 @@ int RTNAME(Chdir)(const char *name) {
260260
#endif
261261
}
262262

263+
int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }
264+
263265
} // namespace Fortran::runtime
264266
} // extern "C"
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
! RUN: bbc -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
2+
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
3+
4+
! CHECK-LABEL: func @_QPtest_ierrno(
5+
subroutine test_ierrno()
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.declare %[[VAL_0]] {uniq_name = "_QFtest_ierrnoEi"} : (!fir.ref<i32>) -> !fir.ref<i32>
10+
! CHECK: %[[VAL_2:.*]] = fir.call @_QPierrno() fastmath<contract> : () -> i32
11+
! CHECK: fir.store %[[VAL_2]] to %[[VAL_1]] : !fir.ref<i32>
12+
! CHECK: return
13+
end subroutine test_ierrno

0 commit comments

Comments
 (0)