[flang-commits] [flang] d6b1354 - [flang-rt][cuda] Add CUFRegisterHostMemoryRange entry point (#229572)

via flang-commits flang-commits at lists.llvm.org
Tue Oct 6 14:52:37 PDT 2026


Author: Valentin Clement (バレンタイン クレメン)
Date: 2026-10-06T14:52:28-07:00
New Revision: d6b1354a663321ab44302dae95289c38ff68286b

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

LOG: [flang-rt][cuda] Add CUFRegisterHostMemoryRange entry point (#229572)

Extracted runtime part from #229213

Added: 
    

Modified: 
    flang-rt/lib/cuda/registration.cpp
    flang/include/flang/Runtime/CUDA/registration.h

Removed: 
    


################################################################################
diff  --git a/flang-rt/lib/cuda/registration.cpp b/flang-rt/lib/cuda/registration.cpp
index 1910b149653a9d..ec93a061cf716e 100644
--- a/flang-rt/lib/cuda/registration.cpp
+++ b/flang-rt/lib/cuda/registration.cpp
@@ -11,6 +11,8 @@
 #include "flang/Runtime/CUDA/common.h"
 
 #include "cuda_runtime.h"
+#include <cstdint>
+#include <unistd.h>
 
 namespace Fortran::runtime::cuda {
 
@@ -55,6 +57,29 @@ void RTDEF(CUFRegisterManagedVariable)(
 
 void RTDEF(CUFInitModule)(void **module) { __cudaInitModule(module); }
 
+void RTDEF(CUFRegisterHostMemoryRange)(void *begin, void *end) {
+  if (!begin || end <= begin)
+    return;
+  const auto pageSize{static_cast<std::uintptr_t>(sysconf(_SC_PAGESIZE))};
+  const auto first{reinterpret_cast<std::uintptr_t>(begin) & ~(pageSize - 1)};
+  const auto last{
+      (reinterpret_cast<std::uintptr_t>(end) + pageSize - 1) & ~(pageSize - 1)};
+  cudaError_t err{cudaHostRegister(reinterpret_cast<void *>(first),
+      last - first, cudaHostRegisterPortable | cudaHostRegisterMapped)};
+  if (err == cudaErrorHostMemoryAlreadyRegistered) {
+    // Clear the error state left by the failed call.
+    (void)cudaGetLastError();
+    return;
+  }
+  if (err != cudaSuccess) {
+    const char *name{cudaGetErrorName(err)};
+    Terminator terminator{__FILE__, __LINE__};
+    terminator.Crash("cudaHostRegister(%p, %zu) failed with '%s'",
+        reinterpret_cast<void *>(first), static_cast<std::size_t>(last - first),
+        name ? name : "<unknown>");
+  }
+}
+
 } // extern "C"
 
 } // namespace Fortran::runtime::cuda

diff  --git a/flang/include/flang/Runtime/CUDA/registration.h b/flang/include/flang/Runtime/CUDA/registration.h
index 74dbf9e1890768..9286d7173adc8e 100644
--- a/flang/include/flang/Runtime/CUDA/registration.h
+++ b/flang/include/flang/Runtime/CUDA/registration.h
@@ -37,6 +37,11 @@ void RTDECL(CUFRegisterManagedVariable)(
 /// unified memory addresses.
 void RTDECL(CUFInitModule)(void **module);
 
+/// Register the pages spanning [\p begin, \p end) with the CUDA runtime so
+/// device code can access them through their host address. Registering the
+/// same range more than once is allowed.
+void RTDECL(CUFRegisterHostMemoryRange)(void *begin, void *end);
+
 } // extern "C"
 
 } // namespace Fortran::runtime::cuda


        


More information about the flang-commits mailing list