[llvm] [openmp] [OpenMP][Runtime] Handling crash with `OMP_TARGET_OFFLOAD=DISABLED` and invoking `omp_get_default_device()` (PR #171789)

via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 07:13:16 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();
----------------
jprotze wrote:

`omp_get_initial_device()` can return `omp_initial_device` or `omp_get_num_devices()`. The two functions don't need to return the same value.

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


More information about the llvm-commits mailing list