Skip to content

[flang] Implement IERRNO intrinsic #124281

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions flang/docs/Intrinsics.md
Original file line number Diff line number Diff line change
Expand Up @@ -1095,3 +1095,14 @@ program chdir_func
print *, "status: ", status
end program chdir_func
```

### Non-Standard Intrinsics: IERRNO

#### Description
`IERRNO()` returns the last system error number, as given by the C `errno` variable.

#### Usage and Info

- **Standard:** GNU extension
- **Class:** function
- **Syntax:** `RESULT = IERRNO()`
3 changes: 3 additions & 0 deletions flang/include/flang/Runtime/extensions.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,8 @@ std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name,
// GNU extension subroutine CHDIR(NAME, [STATUS])
int RTNAME(Chdir)(const char *name);

// GNU extension function IERRNO()
int FORTRAN_PROCEDURE_NAME(ierrno)();

} // extern "C"
#endif // FORTRAN_RUNTIME_EXTENSIONS_H_
2 changes: 2 additions & 0 deletions flang/runtime/extensions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,5 +260,7 @@ int RTNAME(Chdir)(const char *name) {
#endif
}

int FORTRAN_PROCEDURE_NAME(ierrno)() { return errno; }

} // namespace Fortran::runtime
} // extern "C"
13 changes: 13 additions & 0 deletions flang/test/Lower/Intrinsics/ierrno.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
! RUN: bbc -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s
! RUN: %flang_fc1 -emit-fir %s -o - | FileCheck --check-prefixes=CHECK %s

! CHECK-LABEL: func @_QPtest_ierrno(
subroutine test_ierrno()
integer :: i
i = ierrno()
! CHECK: %[[VAL_0:.*]] = fir.alloca i32 {bindc_name = "i", uniq_name = "_QFtest_ierrnoEi"}
! CHECK: %[[VAL_1:.*]] = fir.declare %[[VAL_0]] {uniq_name = "_QFtest_ierrnoEi"} : (!fir.ref<i32>) -> !fir.ref<i32>
! CHECK: %[[VAL_2:.*]] = fir.call @_QPierrno() fastmath<contract> : () -> i32
! CHECK: fir.store %[[VAL_2]] to %[[VAL_1]] : !fir.ref<i32>
! CHECK: return
end subroutine test_ierrno