[llvm] [OFFLOAD] Fix an issue with nested close, alloc mapping when using USM (PR #208122)

via llvm-commits llvm-commits at lists.llvm.org
Tue Jul 7 20:24:57 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-offload

Author: fineg74

<details>
<summary>Changes</summary>

The purpose of this PR is to fix an issue when map(close, alloc ... ) is applied to a subsection of another existing mapping when used in usm. Currently when using usm the runtime is not aware of the existing mapping which leads to allocation of an additional buffer for close and it may lead to crashes or missed updates in some cases.
The fix adds a mapping that lets the runtime to utilize existing mapping rather than allocating a new buffer

---
Full diff: https://github.com/llvm/llvm-project/pull/208122.diff


3 Files Affected:

- (modified) offload/libomptarget/OpenMP/Mapping.cpp (+8) 
- (added) offload/test/unified_shared_memory/close_sub_section.c (+32) 
- (added) offload/test/unified_shared_memory/close_sub_section_oob.c (+32) 


``````````diff
diff --git a/offload/libomptarget/OpenMP/Mapping.cpp b/offload/libomptarget/OpenMP/Mapping.cpp
index 1bb2e424bd083..a9266bf7ca1f9 100644
--- a/offload/libomptarget/OpenMP/Mapping.cpp
+++ b/offload/libomptarget/OpenMP/Mapping.cpp
@@ -282,6 +282,14 @@ TargetPointerResultTy MappingInfoTy::getTargetPointer(
       LR.TPR.Flags.IsPresent = false;
       LR.TPR.Flags.IsHostPointer = true;
       LR.TPR.TargetPointer = HstPtrBegin;
+      // Create a mapping for a case when map(close, alloc:...) is applied to a
+      // subsection of previously mapped allocation. The mapping would prevent
+      // map(close, alloc:...) from creating a new allocation as it would reuse
+      // the mapped allocation instead.
+      HDTTMap->emplace(new HostDataToTargetTy(
+          (uintptr_t)HstPtrBase, (uintptr_t)HstPtrBegin,
+          (uintptr_t)HstPtrBegin + Size, (uintptr_t)HstPtrBegin,
+          (uintptr_t)HstPtrBegin, true, HstPtrName));
     }
   } else if (HasPresentModifier) {
     ODBG(ODT_Mapping) << "Mapping required by 'present' map type modifier does "
diff --git a/offload/test/unified_shared_memory/close_sub_section.c b/offload/test/unified_shared_memory/close_sub_section.c
new file mode 100644
index 0000000000000..06cf444c49283
--- /dev/null
+++ b/offload/test/unified_shared_memory/close_sub_section.c
@@ -0,0 +1,32 @@
+// RUN: %libomptarget-compile-run-and-check-generic
+
+// REQUIRES: unified_shared_memory
+// UNSUPPORTED: clang-6, clang-7, clang-8, clang-9
+
+// amdgpu runtime crash
+// Fails on nvptx with error: an illegal memory access was encountered
+// UNSUPPORTED: amdgcn-amd-amdhsa
+// UNSUPPORTED: nvptx64-nvidia-cuda
+// UNSUPPORTED: nvptx64-nvidia-cuda-LTO
+// UNSUPPORTED: intelgpu
+
+#pragma omp requires unified_shared_memory
+
+#include <stdio.h>
+
+int main() {
+  double base[10] = {0};
+
+#pragma omp target data map(from : base[0 : 10])
+  {
+// close range covers base[1] and should properly
+// update the budder from the outer mapping.
+#pragma omp target map(close, alloc : base[1 : 9])
+    {
+      base[1] = 99.0;
+    }
+  }
+  // CHECK: base[1] = 99.000000 (expected 99.0)
+  printf("base[1] = %f (expected 99.0)\n", base[1]);
+  return 0;
+}
\ No newline at end of file
diff --git a/offload/test/unified_shared_memory/close_sub_section_oob.c b/offload/test/unified_shared_memory/close_sub_section_oob.c
new file mode 100644
index 0000000000000..1638f4a6f3c4c
--- /dev/null
+++ b/offload/test/unified_shared_memory/close_sub_section_oob.c
@@ -0,0 +1,32 @@
+// RUN: %libomptarget-compile-run-and-check-generic
+
+// REQUIRES: unified_shared_memory
+// UNSUPPORTED: clang-6, clang-7, clang-8, clang-9
+
+// amdgpu runtime crash
+// Fails on nvptx with error: an illegal memory access was encountered
+// UNSUPPORTED: amdgcn-amd-amdhsa
+// UNSUPPORTED: nvptx64-nvidia-cuda
+// UNSUPPORTED: nvptx64-nvidia-cuda-LTO
+// UNSUPPORTED: intelgpu
+
+#pragma omp requires unified_shared_memory
+
+#include <stdio.h>
+
+int main() {
+  double base[10] = {0};
+
+#pragma omp target data map(from : base[0 : 10])
+  {
+// close on sub-section base[2:8]; access to base[1] is outside the close
+// buffer
+#pragma omp target map(close, alloc : base[2 : 8])
+    {
+      base[1] = 99.0;
+    }
+  }
+  // CHECK: base[1] = 99.000000 (expected 99.0)
+  printf("base[1] = %f (expected 99.0)\n", base[1]);
+  return 0;
+}
\ No newline at end of file

``````````

</details>


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


More information about the llvm-commits mailing list