[Openmp-commits] [openmp] 158aa99 - [OpenMP][NFC] Introduce helper functions to hide casts and such

Johannes Doerfert via Openmp-commits openmp-commits at lists.llvm.org
Sun Jan 15 11:44:34 PST 2023


Author: Johannes Doerfert
Date: 2023-01-15T11:43:50-08:00
New Revision: 158aa99d39a27e5008293ab7148ec6c403f7fc8f

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

LOG: [OpenMP][NFC] Introduce helper functions to hide casts and such

Differential Revision: https://reviews.llvm.org/D140719

Added: 
    

Modified: 
    openmp/libomptarget/include/Utilities.h
    openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp
    openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
    openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
    openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/include/Utilities.h b/openmp/libomptarget/include/Utilities.h
index 9dec6e661447e..b769d06378932 100644
--- a/openmp/libomptarget/include/Utilities.h
+++ b/openmp/libomptarget/include/Utilities.h
@@ -23,9 +23,12 @@
 #include <algorithm>
 #include <atomic>
 #include <cassert>
+#include <cstddef>
 #include <cstdint>
 #include <cstdlib>
 #include <functional>
+#include <limits>
+#include <memory>
 #include <sstream>
 #include <string>
 
@@ -226,6 +229,25 @@ inline Error Envar<Ty>::init(StringRef Name, GetterFunctor Getter,
   return Error::success();
 }
 
+/// Return the 
diff erence (in bytes) between \p Begin and \p End.
+template <typename Ty = char>
+ptr
diff _t getPtrDiff(const void *End, const void *Begin) {
+  return reinterpret_cast<const Ty *>(End) -
+         reinterpret_cast<const Ty *>(Begin);
+}
+
+/// Return \p Ptr advanced by \p Offset bytes.
+template <typename Ty> Ty *advanceVoidPtr(Ty *Ptr, int64_t Offset) {
+  static_assert(std::is_void<Ty>::value);
+  return const_cast<char *>(reinterpret_cast<const char *>(Ptr) + Offset);
+}
+
+/// Return \p Ptr aligned to \p Alignment bytes.
+template <typename Ty> Ty *alignPtr(Ty *Ptr, int64_t Alignment) {
+  size_t Space = std::numeric_limits<size_t>::max();
+  return std::align(Alignment, sizeof(char), Ptr, Space);
+}
+
 } // namespace target
 } // namespace omp
 } // namespace llvm

diff  --git a/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp b/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp
index 28e70341fe1fc..4eefd19d2b0d1 100644
--- a/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp
+++ b/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp
@@ -2232,7 +2232,7 @@ struct AMDGPUGlobalHandlerTy final : public GenericGlobalHandlerTy {
                                  GlobalTy &ImageGlobal) override {
     // The global's address in AMDGPU is computed as the image begin + the ELF
     // symbol value. Notice we do not add the ELF section offset.
-    ImageGlobal.setPtr((char *)Image.getStart() + Symbol.st_value);
+    ImageGlobal.setPtr(advanceVoidPtr(Image.getStart(), Symbol.st_value));
 
     // Set the global's size.
     ImageGlobal.setSize(Symbol.st_size);
@@ -2462,7 +2462,7 @@ Error AMDGPUKernelTy::launchImpl(GenericDeviceTy &GenericDevice,
   // Initialize implicit arguments.
   utils::AMDGPUImplicitArgsTy *ImplArgs =
       reinterpret_cast<utils::AMDGPUImplicitArgsTy *>(
-          static_cast<char *>(AllArgs) + KernelArgsSize);
+          advanceVoidPtr(AllArgs, KernelArgsSize));
 
   // Initialize the implicit arguments to zero.
   std::memset(ImplArgs, 0, ImplicitArgsSize);

diff  --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
index 79e2ba5e7dc78..da20164cb2119 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp
@@ -13,6 +13,7 @@
 #include "GlobalHandler.h"
 #include "ELFSymbols.h"
 #include "PluginInterface.h"
+#include "Utilities.h"
 
 #include <cstring>
 
@@ -52,8 +53,8 @@ Error GenericGlobalHandlerTy::getGlobalMetadataFromELF(
 
   // The global's address is computed as the image begin + the ELF section
   // offset + the ELF symbol value.
-  ImageGlobal.setPtr((char *)Image.getStart() + Section.sh_offset +
-                     Symbol.st_value);
+  ImageGlobal.setPtr(
+      advanceVoidPtr(Image.getStart(), Section.sh_offset + Symbol.st_value));
 
   // Set the global's size.
   ImageGlobal.setSize(Symbol.st_size);

diff  --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
index 615a873c0cabe..aa0e599135261 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/JIT.cpp
@@ -127,7 +127,7 @@ createModuleFromMemoryBuffer(std::unique_ptr<MemoryBuffer> &MB,
 Expected<std::unique_ptr<Module>>
 createModuleFromImage(__tgt_device_image *Image, LLVMContext &Context) {
   StringRef Data((const char *)Image->ImageStart,
-                 (char *)Image->ImageEnd - (char *)Image->ImageStart);
+                 target::getPtrDiff(Image->ImageEnd, Image->ImageStart));
   std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(
       Data, /* BufferName */ "", /* RequiresNullTerminator */ false);
   return createModuleFromMemoryBuffer(MB, Context);
@@ -378,8 +378,7 @@ bool checkBitcodeImage(__tgt_device_image *Image, Triple::ArchType TA) {
   }
 
   StringRef Data(reinterpret_cast<const char *>(Image->ImageStart),
-                 reinterpret_cast<char *>(Image->ImageEnd) -
-                     reinterpret_cast<char *>(Image->ImageStart));
+                 target::getPtrDiff(Image->ImageEnd, Image->ImageStart));
   std::unique_ptr<MemoryBuffer> MB = MemoryBuffer::getMemBuffer(
       Data, /* BufferName */ "", /* RequiresNullTerminator */ false);
   if (!MB)

diff  --git a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
index 19808eb2d535d..752ee2e38de16 100644
--- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
+++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h
@@ -132,7 +132,7 @@ class DeviceImageTy {
 
   /// Get the image size.
   size_t getSize() const {
-    return ((char *)TgtImage->ImageEnd) - ((char *)TgtImage->ImageStart);
+    return getPtrDiff(TgtImage->ImageEnd, TgtImage->ImageStart);
   }
 
   /// Get a memory buffer reference to the whole image.
@@ -469,7 +469,7 @@ struct GenericDeviceTy : public DeviceAllocatorTy {
     --It;
 
     // Evaluate whether the buffer is contained in the pinned allocation.
-    return ((const char *)It->first + It->second > (const char *)Buffer);
+    return (advanceVoidPtr(It->first, It->second) > (const char *)Buffer);
   }
 
   /// Return the execution mode used for kernel \p Name.


        


More information about the Openmp-commits mailing list