[Openmp-commits] [llvm] [openmp] [OpenMP][Runtime] Handling crash with `OMP_TARGET_OFFLOAD=DISABLED` and invoking `omp_get_default_device()` (PR #171789)
Amit Tiwari via Openmp-commits
openmp-commits at lists.llvm.org
Thu Dec 18 06:26:51 PST 2025
================
@@ -1197,6 +1189,17 @@ int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)(void) {
return KMP_EXPAND_NAME(FTN_GET_NUM_DEVICES)();
}
+int FTN_STDCALL KMP_EXPAND_NAME(FTN_GET_DEFAULT_DEVICE)(void) {
+#if KMP_MIC || KMP_OS_DARWIN || defined(KMP_STUB)
+ return 0;
+#else
+ // When offloading is disabled, return the initial device (host)
+ if (__kmp_target_offload == tgt_disabled)
+ return KMP_EXPAND_NAME(FTN_GET_INITIAL_DEVICE)();
----------------
amitamd7 wrote:
The fix is actually safe. We’re not faking anything. Here’s what is happening: the ICV still preserves the user’s intent because `td_icvs.default_device` keeps whatever value the user sets (like `5`), which is exactly what the OpenMP spec expects. At the same time, the API side is safe because `omp_get_default_device()` will return initial_device when offload is disabled, so we don’t risk crashing by sending work to a non-existent device. Further, to add for `kmp_alloc.cpp` that access `td_icvs.default_device` are guarded by `__kmp_target_mem_available` which is FALSE when Offload is disabled.
https://github.com/llvm/llvm-project/pull/171789
More information about the Openmp-commits
mailing list