From: Johannes Doerfert Date: Wed, 28 Dec 2022 06:02:47 +0000 (-0800) Subject: [OpenMP][NFC] Introduce helper functions to hide casts and such X-Git-Tag: upstream/17.0.6~20872 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=158aa99d39a27e5008293ab7148ec6c403f7fc8f;p=platform%2Fupstream%2Fllvm.git [OpenMP][NFC] Introduce helper functions to hide casts and such Differential Revision: https://reviews.llvm.org/D140719 --- diff --git a/openmp/libomptarget/include/Utilities.h b/openmp/libomptarget/include/Utilities.h index 9dec6e6..b769d06 100644 --- a/openmp/libomptarget/include/Utilities.h +++ b/openmp/libomptarget/include/Utilities.h @@ -23,9 +23,12 @@ #include #include #include +#include #include #include #include +#include +#include #include #include @@ -226,6 +229,25 @@ inline Error Envar::init(StringRef Name, GetterFunctor Getter, return Error::success(); } +/// Return the difference (in bytes) between \p Begin and \p End. +template +ptrdiff_t getPtrDiff(const void *End, const void *Begin) { + return reinterpret_cast(End) - + reinterpret_cast(Begin); +} + +/// 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(reinterpret_cast(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 diff --git a/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp b/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp index 28e7034..4eefd19 100644 --- a/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp +++ b/openmp/libomptarget/plugins-nextgen/amdgpu/src/rtl.cpp @@ -2232,7 +2232,7 @@ private: 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( - static_cast(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 79e2ba5..da20164 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 @@ -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 615a873..aa0e599 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 &MB, Expected> 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 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(Image->ImageStart), - reinterpret_cast(Image->ImageEnd) - - reinterpret_cast(Image->ImageStart)); + target::getPtrDiff(Image->ImageEnd, Image->ImageStart)); std::unique_ptr 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 19808eb..752ee2e 100644 --- a/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h +++ b/openmp/libomptarget/plugins-nextgen/common/PluginInterface/PluginInterface.h @@ -132,7 +132,7 @@ public: /// 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 @@ protected: --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.