[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 05:33:15 PST 2025
================
@@ -0,0 +1,37 @@
+// RUN: %libomptarget-compile-generic
+// RUN: env OMP_TARGET_OFFLOAD=disabled %libomptarget-run-generic 2>&1 |
+// %fcheck-generic
+//
+// Test omp_get_default_device() API behavior when offload is disabled
+
+#include <omp.h>
+#include <stdio.h>
+
+int main() {
+ // Test 1: Default behavior
+ int dev1 = omp_get_default_device();
+ // CHECK: Test 1: {{0}}
+ printf("Test 1: %d\n", dev1);
+
+ // Test 2: After setting device
+ omp_set_default_device(3);
+ int dev2 = omp_get_default_device();
+ // CHECK: Test 2: {{0}}
+ printf("Test 2: %d\n", dev2);
+
+ // Test 3: Multiple sets
+ for (int i = 0; i < 5; i++) {
+ omp_set_default_device(i + 10);
+ int dev = omp_get_default_device();
+ // CHECK: Test 3.{{[0-4]}}: {{0}}
+ printf("Test 3.%d: %d\n", i, dev);
+ }
+
+ // Test 4: Consistency with initial device
+ int initial = omp_get_initial_device();
----------------
amitamd7 wrote:
You’re right about the portability concern for the value in `omp_get_initial_device()`. However, in LLVM’s implementation, `omp_get_initial_device()` is defined as `omp_get_num_devices()`, which is always `0` when offload is disabled. Theoretically, other implementations may use different values, but for LLVM-specific tests this is safe. Anyway, I’ve updated the tests to check equality relationships rather than hard-coded values (`0` in our case).
https://github.com/llvm/llvm-project/pull/171789
More information about the Openmp-commits
mailing list