[Openmp-commits] [openmp] [OpenMP][RISCV] Inherit rounding mode across worker reuse (PR #237077)

via Openmp-commits openmp-commits at lists.llvm.org
Thu Oct 29 21:17:15 PDT 2026


https://github.com/SmaCow666 created https://github.com/llvm/llvm-project/pull/237077

## Summary

Propagate the primary thread's dynamic floating-point rounding mode to reused
OpenMP workers on RV64 Linux targets with declared hardware floating-point support.

The runtime already has a floating-point-control inheritance mechanism for x86.
RV64 currently falls through the empty propagation/update macros. A worker reused
after the primary thread changes its rounding mode can therefore execute the next
parallel region with a stale `frm` value.

This change adds RV64 support to the existing mechanism and a two-thread
fresh/reused-team regression.

## Reproducer

The added `omp_fp_rounding_inheritance.c` test:

1. Disables dynamic team sizing.
2. Sets each of `FE_TONEAREST`, `FE_DOWNWARD`, `FE_UPWARD` and `FE_TOWARDZERO` on
   the primary thread.
3. Enters a two-thread parallel region after each change.
4. Checks both the team size and each thread's `fegetround()` result.
5. Restores the primary thread's original rounding mode.

The test runs with `KMP_INHERIT_FP_CONTROL=TRUE`. It reads the control state
directly, so the check does not depend on arithmetic expression optimization.

Patched final output:

```text
FP_ROUND_RESULT errors=0
```

## Root cause

Changing the primary thread's thread-local rounding state does not update the
state of workers already in the pool. RV64 has no implementation in the existing
`propagateFPControl`/`updateHWFPControl` hooks, so worker reuse exposes the stale
rounding state.

## Fix

- Enable an internal RV64 FP-control path for Linux when `__riscv_flen >= 32`.
- Read `frm` with `frrm` and save the rounding mode in the team's FP-control state.
- Update the worker's `frm` with `fsrm` when inheritance is enabled and the mode
  differs.
- Reuse `KMP_INHERIT_FP_CONTROL` for opt-out behavior.
- Initialize the new team fields and document the RV64 scope in `Runtimes.md`.

The implementation accesses `frm` alone. Each thread retains its accrued
`fflags`; vector fixed-point rounding through `vxrm` is outside this mechanism.
Targets without declared hardware FP support retain the fallback path.

## Tests

Complete baseline and patched runtimes, including all objects affected by the
internal team layout, were independently rebuilt from
`fda4724ce92b99e4d7b90af9ee264d7650a63b32`. The target configuration used
Clang 21.1.8, RV64GC/LP64D and the runtimes CMake entry point.

- The proposed regression executes through the pinned source's unchanged
  upstream lit configuration in QEMU: baseline FAIL with three mismatches,
  patched PASS with zero.
- A GCC-compiled caller confirms the same three-to-zero result. With inheritance
  disabled, both GCC and Clang callers retain the expected three mismatches,
  checking that opt-out behavior is preserved.
- Additional target boundary checks pass: exception flags (2 checks), nested
  teams/restoration (7), resizing 2→4→2 (8), and repeated steady teams (18).
- Baseline and patched libraries each pass 22 tasks/synchronization/scheduling
  contracts with 1/2 threads. The actual loaded target libraries were verified.
- The actual feature guard with `kmp_platform.h` enables the RV64 Linux FP
  profile and disables RV64 Linux without declared FP and x86 Linux profiles.
  The corresponding no-FP/non-RV lit gate controls are UNSUPPORTED as intended.
- Complete x86-64 baseline and patched runtimes also build and both pass the
  four-mode rounding test, exercising the existing non-RV implementation.

The no-FP check is a feature-gate/preprocessing check, not a complete RV64IMAC
libc/runtime build. The full upstream suite was not run; this environment did not
configure FileCheck. Earlier fixed-version U54/Jupiter validation remains
separate from these new main/QEMU results.


>From b63d2c7d38f8e3719361b72f2e2ad53faa97b976 Mon Sep 17 00:00:00 2001
From: XuhuiNiu <uiniu666 at gmain.com>
Date: Fri, 30 Oct 2026 11:25:16 +0800
Subject: [PATCH] [OpenMP][RISCV] Inherit rounding mode across worker reuse

Implement frm propagation through the existing team FP-control hooks
for RV64 Linux hardware-FP targets. Preserve per-thread fflags and the
existing inheritance opt-out. Add rounding-mode reuse regression coverage.
---
 openmp/docs/design/Runtimes.md                |  4 +++
 openmp/runtime/src/kmp.h                      | 16 +++++++--
 openmp/runtime/src/kmp_global.cpp             |  4 ++-
 openmp/runtime/src/kmp_runtime.cpp            | 31 ++++++++++++++++-
 openmp/runtime/src/kmp_settings.cpp           |  4 +--
 openmp/runtime/test/lit.cfg                   | 16 +++++++++
 .../parallel/omp_fp_rounding_inheritance.c    | 33 +++++++++++++++++++
 7 files changed, 102 insertions(+), 6 deletions(-)
 create mode 100644 openmp/runtime/test/parallel/omp_fp_rounding_inheritance.c

diff --git a/openmp/docs/design/Runtimes.md b/openmp/docs/design/Runtimes.md
index c9640f143857ce..cc4ca224e221ec 100644
--- a/openmp/docs/design/Runtimes.md
+++ b/openmp/docs/design/Runtimes.md
@@ -602,6 +602,10 @@ Enables (`true`) or disables (`false`) the copying of the floating-point
 control settings of the primary thread to the floating-point control settings
 of the OpenMP worker threads at the start of each parallel region.
 
+On RISC-V Linux hardware-FP targets, this controls dynamic scalar rounding
+through `frm`. Each thread retains its own accrued exception flags; vector
+fixed-point rounding (`vxrm`) is outside this control.
+
 **Default:** `true`
 
 #### KMP_LIBRARY
diff --git a/openmp/runtime/src/kmp.h b/openmp/runtime/src/kmp.h
index 9978268b52aa02..d5d5dfb359b35f 100644
--- a/openmp/runtime/src/kmp.h
+++ b/openmp/runtime/src/kmp.h
@@ -16,6 +16,13 @@
 
 #include "kmp_config.h"
 
+// Floating-point CSR access is available only for a declared hardware FP target.
+#if KMP_ARCH_RISCV64 && KMP_OS_LINUX && defined(__riscv_flen) && __riscv_flen >= 32
+#define KMP_RISCV_FP_CONTROL 1
+#else
+#define KMP_RISCV_FP_CONTROL 0
+#endif
+
 /* #define BUILD_PARALLEL_ORDERED 1 */
 
 /* This fix replaces gettimeofday with clock_gettime for better scalability on
@@ -3167,7 +3174,10 @@ typedef struct KMP_ALIGN_CACHE kmp_base_team {
   kmp_int8 t_pad2b;
   kmp_int16 t_x87_fpu_control_word; // FP control regs
   kmp_uint32 t_mxcsr;
-#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
+#elif KMP_RISCV_FP_CONTROL
+  kmp_int8 t_fp_control_saved;
+  kmp_uint32 t_riscv_rounding;
+#endif /* FP control */
 
   void *t_inline_argv[KMP_INLINE_ARGV_ENTRIES];
 
@@ -3517,8 +3527,10 @@ extern KMP_THREAD_LOCAL int __kmp_gtid;
 #endif
 extern int __kmp_tls_gtid_min; /* #threads below which use sp search for gtid */
 extern int __kmp_foreign_tp; // If true, separate TP var for each foreign thread
-#if KMP_ARCH_X86 || KMP_ARCH_X86_64
+#if KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_RISCV_FP_CONTROL
 extern int __kmp_inherit_fp_control; // copy fp creg(s) parent->workers at fork
+#endif
+#if KMP_ARCH_X86 || KMP_ARCH_X86_64
 extern kmp_int16 __kmp_init_x87_fpu_control_word; // init thread's FP ctrl reg
 extern kmp_uint32 __kmp_init_mxcsr; /* init thread's mxscr */
 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
diff --git a/openmp/runtime/src/kmp_global.cpp b/openmp/runtime/src/kmp_global.cpp
index 78ae429e3d4655..e14c787d632ff0 100644
--- a/openmp/runtime/src/kmp_global.cpp
+++ b/openmp/runtime/src/kmp_global.cpp
@@ -185,8 +185,10 @@ KMP_THREAD_LOCAL int __kmp_gtid = KMP_GTID_DNE;
 #endif /* KMP_TDATA_GTID */
 int __kmp_tls_gtid_min = INT_MAX;
 int __kmp_foreign_tp = TRUE;
-#if KMP_ARCH_X86 || KMP_ARCH_X86_64
+#if KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_RISCV_FP_CONTROL
 int __kmp_inherit_fp_control = TRUE;
+#endif
+#if KMP_ARCH_X86 || KMP_ARCH_X86_64
 kmp_int16 __kmp_init_x87_fpu_control_word = 0;
 kmp_uint32 __kmp_init_mxcsr = 0;
 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
diff --git a/openmp/runtime/src/kmp_runtime.cpp b/openmp/runtime/src/kmp_runtime.cpp
index 68ea0410310b3f..2c61f0c98958c6 100644
--- a/openmp/runtime/src/kmp_runtime.cpp
+++ b/openmp/runtime/src/kmp_runtime.cpp
@@ -1167,6 +1167,32 @@ inline static void updateHWFPControl(kmp_team_t *team) {
     }
   }
 }
+#elif KMP_RISCV_FP_CONTROL
+// Access frm alone: fflags and reserved fcsr bits belong to the current thread.
+static inline kmp_uint32 __kmp_riscv_read_rounding() {
+  kmp_uint32 mode;
+  __asm__ __volatile__("frrm %0" : "=r"(mode) : : "memory");
+  return mode;
+}
+
+static inline void __kmp_riscv_write_rounding(kmp_uint32 mode) {
+  __asm__ __volatile__("fsrm %0" : : "r"(mode) : "memory");
+}
+
+inline static void propagateFPControl(kmp_team_t *team) {
+  if (__kmp_inherit_fp_control) {
+    KMP_CHECK_UPDATE(team->t.t_riscv_rounding, __kmp_riscv_read_rounding());
+    KMP_CHECK_UPDATE(team->t.t_fp_control_saved, TRUE);
+  } else {
+    KMP_CHECK_UPDATE(team->t.t_fp_control_saved, FALSE);
+  }
+}
+
+inline static void updateHWFPControl(kmp_team_t *team) {
+  if (__kmp_inherit_fp_control && team->t.t_fp_control_saved &&
+      team->t.t_riscv_rounding != __kmp_riscv_read_rounding())
+    __kmp_riscv_write_rounding(team->t.t_riscv_rounding);
+}
 #else
 #define propagateFPControl(x) ((void)0)
 #define updateHWFPControl(x) ((void)0)
@@ -4754,7 +4780,10 @@ static void __kmp_initialize_team(kmp_team_t *team, int new_nproc,
   team->t.t_fp_control_saved = FALSE; /* not needed */
   team->t.t_x87_fpu_control_word = 0; /* not needed */
   team->t.t_mxcsr = 0; /* not needed */
-#endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
+#elif KMP_RISCV_FP_CONTROL
+  team->t.t_fp_control_saved = FALSE;
+  team->t.t_riscv_rounding = 0;
+#endif /* FP control */
 
   team->t.t_construct = 0;
 
diff --git a/openmp/runtime/src/kmp_settings.cpp b/openmp/runtime/src/kmp_settings.cpp
index f01231b7a168b7..6750d8f7df90f2 100644
--- a/openmp/runtime/src/kmp_settings.cpp
+++ b/openmp/runtime/src/kmp_settings.cpp
@@ -791,7 +791,7 @@ static void __kmp_stg_print_duplicate_lib_ok(kmp_str_buf_t *buffer,
 // -----------------------------------------------------------------------------
 // KMP_INHERIT_FP_CONTROL
 
-#if KMP_ARCH_X86 || KMP_ARCH_X86_64
+#if KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_RISCV_FP_CONTROL
 
 static void __kmp_stg_parse_inherit_fp_control(char const *name,
                                                char const *value, void *data) {
@@ -5575,7 +5575,7 @@ static kmp_setting_t __kmp_stg_table[] = {
      __kmp_stg_print_handle_signals, NULL, 0, 0},
 #endif
 
-#if KMP_ARCH_X86 || KMP_ARCH_X86_64
+#if KMP_ARCH_X86 || KMP_ARCH_X86_64 || KMP_RISCV_FP_CONTROL
     {"KMP_INHERIT_FP_CONTROL", __kmp_stg_parse_inherit_fp_control,
      __kmp_stg_print_inherit_fp_control, NULL, 0, 0},
 #endif /* KMP_ARCH_X86 || KMP_ARCH_X86_64 */
diff --git a/openmp/runtime/test/lit.cfg b/openmp/runtime/test/lit.cfg
index 6a63fce1b16923..17461705898f0b 100644
--- a/openmp/runtime/test/lit.cfg
+++ b/openmp/runtime/test/lit.cfg
@@ -167,6 +167,22 @@ if target_arch:
   if target_arch in ['x86_64', 'i386']:
     config.available_features.add('x86-target-arch')
 
+# FP-control inheritance requires the same declared hardware FP as the runtime.
+if target_arch == 'riscv64':
+    fp_probe = [config.test_c_compiler] + \
+        shlex.split(config.test_openmp_flags) + \
+        shlex.split(config.test_extra_flags) + ['-dM', '-E', '-x', 'c', '-']
+    fp_result = subprocess.run(fp_probe, input='', capture_output=True, text=True)
+    if fp_result.returncode:
+        lit_config.warning('Cannot inspect RISC-V test compiler FP features: ' +
+                           fp_result.stderr.strip())
+    else:
+        fp_macros = dict(re.findall(
+            r'^#define (__riscv_(?:xlen|flen)) ([0-9]+)$', fp_result.stdout, re.M))
+        if fp_macros.get('__riscv_xlen') == '64' and \
+                int(fp_macros.get('__riscv_flen', '0')) >= 32:
+            config.available_features.add('riscv-hardware-fp')
+
 import multiprocessing
 try:
     if multiprocessing.cpu_count() > 1:
diff --git a/openmp/runtime/test/parallel/omp_fp_rounding_inheritance.c b/openmp/runtime/test/parallel/omp_fp_rounding_inheritance.c
new file mode 100644
index 00000000000000..c38dd666019a67
--- /dev/null
+++ b/openmp/runtime/test/parallel/omp_fp_rounding_inheritance.c
@@ -0,0 +1,33 @@
+// RUN: %libomp-compile
+// RUN: env OMP_DYNAMIC=FALSE KMP_INHERIT_FP_CONTROL=TRUE %libomp-run
+// REQUIRES: linux, riscv64-target-arch, riscv-hardware-fp
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+
+#include <fenv.h>
+#include <omp.h>
+#include <stdio.h>
+
+// Reused workers must inherit the new rounding mode at each parallel region.
+// Inspect control state directly, without relying on arithmetic optimization.
+int main(void) {
+  const int modes[] = {FE_TONEAREST, FE_DOWNWARD, FE_UPWARD, FE_TOWARDZERO};
+  int original = fegetround();
+  int errors = 0;
+  omp_set_dynamic(0);
+  for (int stage = 0; stage < 4; ++stage) {
+    if (fesetround(modes[stage]))
+      return 2;
+    int bad = 0;
+#pragma omp parallel num_threads(2) reduction(+:bad)
+    {
+      if (omp_get_num_threads() != 2 || fegetround() != modes[stage])
+        ++bad;
+    }
+    printf("FP_ROUND_STAGE stage=%d errors=%d\n", stage, bad);
+    errors += bad;
+  }
+  if (fesetround(original))
+    return 2;
+  printf("FP_ROUND_RESULT errors=%d\n", errors);
+  return errors != 0;
+}



More information about the Openmp-commits mailing list