[llvm] 0e7ecc6 - [ExecutionEngine] Add a jitTargetAddressToFunction utility function.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Mon Dec 2 01:57:04 PST 2019


Author: Lang Hames
Date: 2019-12-02T01:52:52-08:00
New Revision: 0e7ecc651a47f818abafdfe5f928923f789fe0bc

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

LOG: [ExecutionEngine] Add a jitTargetAddressToFunction utility function.

jitTargetAddressToFunction takes a JITTargetAddress and returns a pointer of
the given function pointer type suitable for calling to invoke the function
at the target address.

jitTargetAddressToFunction currently behaves the same as
jitTargetAddressToPointer, but in the near future will be updated to perform
pointer signing on architectures that require it (e.g. arm64e). For this
reason it should always be preferred when generating callable pointers for
JIT'd functions.

Added: 
    

Modified: 
    llvm/include/llvm/ExecutionEngine/JITSymbol.h

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/ExecutionEngine/JITSymbol.h b/llvm/include/llvm/ExecutionEngine/JITSymbol.h
index c0f1ca4b9876..7a2a6cfa5203 100644
--- a/llvm/include/llvm/ExecutionEngine/JITSymbol.h
+++ b/llvm/include/llvm/ExecutionEngine/JITSymbol.h
@@ -41,6 +41,11 @@ class SymbolRef;
 using JITTargetAddress = uint64_t;
 
 /// Convert a JITTargetAddress to a pointer.
+///
+/// Note: This is a raw cast of the address bit pattern to the given pointer
+/// type. When casting to a function pointer in order to execute JIT'd code
+/// jitTargetAddressToFunction should be preferred, as it will also perform
+/// pointer signing on targets that require it.
 template <typename T> T jitTargetAddressToPointer(JITTargetAddress Addr) {
   static_assert(std::is_pointer<T>::value, "T must be a pointer type");
   uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
@@ -48,6 +53,19 @@ template <typename T> T jitTargetAddressToPointer(JITTargetAddress Addr) {
   return reinterpret_cast<T>(IntPtr);
 }
 
+/// Convert a JITTargetAddress to a callable function pointer.
+///
+/// Casts the given address to a callable function pointer. This operation
+/// will perform pointer signing for platforms that require it (e.g. arm64e).
+template <typename T> T jitTargetAddressToFunction(JITTargetAddress Addr) {
+  static_assert(
+      std::is_pointer<T>::value &&
+          std::is_function<typename std::remove_pointer<T>::type>::value,
+      "T must be a function pointer type");
+  return jitTargetAddressToPointer<T>(Addr);
+}
+
+/// Convert a pointer to a JITTargetAddress.
 template <typename T> JITTargetAddress pointerToJITTargetAddress(T *Ptr) {
   return static_cast<JITTargetAddress>(reinterpret_cast<uintptr_t>(Ptr));
 }


        


More information about the llvm-commits mailing list