[llvm] 797916b - [OpenMP][flang] Fix crash in host offload (#187847)

via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 26 08:02:12 PDT 2026


Author: Robert Imschweiler
Date: 2026-03-26T16:02:01+01:00
New Revision: 797916b221d13f79e3012fa31147262dae97e1db

URL: https://github.com/llvm/llvm-project/commit/797916b221d13f79e3012fa31147262dae97e1db
DIFF: https://github.com/llvm/llvm-project/commit/797916b221d13f79e3012fa31147262dae97e1db.diff

LOG: [OpenMP][flang] Fix crash in host offload (#187847)

Guard `getGridValue` in `OMPIRBuilder` to avoid reaching the
`unreachable` in `getGridValue` when offloading to host device without
an explicit num_threads clause.

Added: 
    mlir/test/Target/LLVMIR/omptarget-region-host-device-llvm.mlir

Modified: 
    llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
index 9193bf5a0c85a..2bd054501506a 100644
--- a/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
+++ b/llvm/lib/Frontend/OpenMP/OMPIRBuilder.cpp
@@ -169,6 +169,10 @@ static void restoreIPandDebugLoc(llvm::IRBuilderBase &Builder,
     Builder.SetCurrentDebugLocation(BB->back().getStableDebugLoc());
 }
 
+static bool hasGridValue(const Triple &T) {
+  return T.isAMDGPU() || T.isNVPTX() || T.isSPIRV();
+}
+
 static const omp::GV &getGridValue(const Triple &T, Function *Kernel) {
   if (T.isAMDGPU()) {
     StringRef Features =
@@ -7773,9 +7777,15 @@ OpenMPIRBuilder::InsertPointTy OpenMPIRBuilder::createTargetInit(
   // If MaxThreads not set, select the maximum between the default workgroup
   // size and the MinThreads value.
   int32_t MaxThreadsVal = Attrs.MaxThreads.front();
-  if (MaxThreadsVal < 0)
-    MaxThreadsVal = std::max(
-        int32_t(getGridValue(T, Kernel).GV_Default_WG_Size), Attrs.MinThreads);
+  if (MaxThreadsVal < 0) {
+    if (hasGridValue(T)) {
+      MaxThreadsVal =
+          std::max(int32_t(getGridValue(T, Kernel).GV_Default_WG_Size),
+                   Attrs.MinThreads);
+    } else {
+      MaxThreadsVal = Attrs.MinThreads;
+    }
+  }
 
   if (MaxThreadsVal > 0)
     writeThreadBoundsForKernel(T, *Kernel, Attrs.MinThreads, MaxThreadsVal);

diff  --git a/mlir/test/Target/LLVMIR/omptarget-region-host-device-llvm.mlir b/mlir/test/Target/LLVMIR/omptarget-region-host-device-llvm.mlir
new file mode 100644
index 0000000000000..c99d0c7f1029b
--- /dev/null
+++ b/mlir/test/Target/LLVMIR/omptarget-region-host-device-llvm.mlir
@@ -0,0 +1,14 @@
+// RUN: mlir-translate -mlir-to-llvmir %s | FileCheck %s
+
+// Verify that host offloading doesn't crash the OMPIRBuilder.
+module attributes {llvm.target_triple = "x86_64-unknown-linux-gnu", omp.is_target_device = true} {
+  llvm.func @omp_target_region_host_device() {
+    omp.target {
+      omp.terminator
+    }
+    llvm.return
+  }
+}
+
+// CHECK:      define void @omp_target_region_host_device()
+// CHECK:      define weak_odr protected void @__omp_offloading_{{[^_]+}}_{{[^_]+}}_omp_target_region_host_device_l{{[0-9]+}}(ptr %[[ADDR_A:.*]])


        


More information about the llvm-commits mailing list