[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:34:48 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:

Agreed. That's why the tests compare `omp_get_default_device() == omp_get_initial_device()` (updating in few minutes) without assuming what value `omp_get_initial_device()` returns. The tests will work correctly whether it returns `omp_get_num_devices()` (current) or `omp_initial_device` (potential future implementation).

https://github.com/llvm/llvm-project/pull/171789


More information about the Openmp-commits mailing list