Index: openmp/libomptarget/include/Utilities.h =================================================================== --- openmp/libomptarget/include/Utilities.h +++ openmp/libomptarget/include/Utilities.h @@ -23,9 +23,11 @@ #include #include #include +#include #include #include #include +#include #include #include @@ -226,6 +228,24 @@ return Error::success(); } +/// Return the difference (in bytes) between \p Begin and \p END. +template +ptrdiff_t getPtrDiff(const void *Begin, const void *END) { + return ((const Ty *)Begin) - ((const Ty *)(END)); +} + +/// Return \p Ptr advanced by \p Offset bytes. +template Ty *advanceVoidPtr(Ty *Ptr, int64_t Offset) { + static_assert(std::is_void::value); + return const_cast(&((const char *)Ptr)[Offset]); +} + +/// Return \p Ptr aligned to \p Alignment bytes. +template Ty *alignPtr(Ty *Ptr, int64_t Alignment) { + size_t Space = std::numeric_limits::max(); + return std::align(Alignment, sizeof(char), Ptr, Space); +} + } // namespace target } // namespace omp } // namespace llvm Index: openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp =================================================================== --- openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp +++ openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp @@ -2161,7 +2161,7 @@ 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); @@ -2382,7 +2382,7 @@ // Initialize implicit arguments. utils::AMDGPUImplicitArgsTy *ImplArgs = reinterpret_cast( - static_cast(AllArgs) + KernelArgsSize); + advanceVoidPtr(AllArgs, KernelArgsSize)); // Initialize the implicit arguments to zero. std::memset(ImplArgs, 0, ImplicitArgsSize); Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp =================================================================== --- openmp/libomptarget/plugins-nextgen/common/PluginInterface/GlobalHandler.cpp +++ 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 @@ -52,8 +53,8 @@ // 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); Index: openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h =================================================================== --- openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h +++ openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h @@ -141,7 +141,7 @@ /// 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. @@ -478,7 +478,7 @@ --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.