[llvm] 81c0f30 - [ORC] Add ExecutorSymbolDef toPtr / fromPtr convenience functions.

Lang Hames via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 21 14:54:02 PST 2025


Author: Lang Hames
Date: 2025-01-22T09:53:42+11:00
New Revision: 81c0f3023fc38e3ea720045407a17f47653ea2ac

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

LOG: [ORC] Add ExecutorSymbolDef toPtr / fromPtr convenience functions.

This will simplify conversion of a number of APIs from ExecutorAddr to
ExecutorSymbolDef.

Added: 
    

Modified: 
    llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
    llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
    llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
    llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
    llvm/examples/Kaleidoscope/Chapter4/toy.cpp
    llvm/examples/Kaleidoscope/Chapter5/toy.cpp
    llvm/examples/Kaleidoscope/Chapter6/toy.cpp
    llvm/examples/Kaleidoscope/Chapter7/toy.cpp
    llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h
    llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
index 1b35ba404d29b2..426886c72e54d6 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp
@@ -1159,7 +1159,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      auto *FP = Sym.getAddress().toPtr<double (*)()>();
+      auto *FP = Sym.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
index 1b35ba404d29b2..426886c72e54d6 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp
@@ -1159,7 +1159,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      auto *FP = Sym.getAddress().toPtr<double (*)()>();
+      auto *FP = Sym.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
index 1b35ba404d29b2..426886c72e54d6 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp
@@ -1159,7 +1159,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      auto *FP = Sym.getAddress().toPtr<double (*)()>();
+      auto *FP = Sym.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
index 2c8d4941291e08..1891635dbfd354 100644
--- a/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
+++ b/llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp
@@ -1157,7 +1157,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      auto *FP = Sym.getAddress().toPtr<double (*)()>();
+      auto *FP = Sym.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
index 1bbc294bf35263..0f58391c506679 100644
--- a/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter4/toy.cpp
@@ -643,7 +643,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
+      double (*FP)() = ExprSymbol.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
index 48936bddb1d4f2..7117eaf4982b03 100644
--- a/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter5/toy.cpp
@@ -917,7 +917,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
+      double (*FP)() = ExprSymbol.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
index ebe4322287b21f..cb7b6cc8651c13 100644
--- a/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter6/toy.cpp
@@ -1036,7 +1036,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
+      double (*FP)() = ExprSymbol.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
index 374f2c03b48e02..91b7191a07c6f8 100644
--- a/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
+++ b/llvm/examples/Kaleidoscope/Chapter7/toy.cpp
@@ -1207,7 +1207,7 @@ static void HandleTopLevelExpression() {
 
       // Get the symbol's address and cast it to the right type (takes no
       // arguments, returns a double) so we can call it as a native function.
-      double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
+      double (*FP)() = ExprSymbol.toPtr<double (*)()>();
       fprintf(stderr, "Evaluated to %f\n", FP());
 
       // Delete the anonymous expression module from the JIT.

diff  --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h
index 68ccdf83bd1201..0756ab5ea9881f 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h
@@ -23,6 +23,37 @@ namespace orc {
 /// Represents a defining location for a JIT symbol.
 class ExecutorSymbolDef {
 public:
+  /// Create an ExecutorSymbolDef from the given pointer.
+  /// Warning: This should only be used when JITing in-process.
+  template <typename T, typename UnwrapFn = ExecutorAddr::defaultUnwrap<T>>
+  static ExecutorSymbolDef fromPtr(T *Ptr,
+                                   JITSymbolFlags BaseFlags = JITSymbolFlags(),
+                                   UnwrapFn &&Unwrap = UnwrapFn()) {
+    auto *UP = Unwrap(Ptr);
+    JITSymbolFlags Flags = BaseFlags;
+    if (std::is_function_v<T>)
+      Flags |= JITSymbolFlags::Callable;
+    return ExecutorSymbolDef(
+        ExecutorAddr::fromPtr(UP, ExecutorAddr::rawPtr<T>()), Flags);
+  }
+
+  /// Cast this ExecutorSymbolDef to a pointer of the given type.
+  /// Warning: This should only be used when JITing in-process.
+  template <typename T, typename WrapFn =
+                            ExecutorAddr::defaultWrap<std::remove_pointer_t<T>>>
+  std::enable_if_t<std::is_pointer<T>::value, T>
+  toPtr(WrapFn &&Wrap = WrapFn()) const {
+    return Addr.toPtr<T>(std::forward<WrapFn>(Wrap));
+  }
+
+  /// Cast this ExecutorSymbolDef to a pointer of the given function type.
+  /// Warning: This should only be used when JITing in-process.
+  template <typename T, typename WrapFn = ExecutorAddr::defaultWrap<T>>
+  std::enable_if_t<std::is_function<T>::value, T *>
+  toPtr(WrapFn &&Wrap = WrapFn()) const {
+    return Addr.toPtr<T>(std::forward<WrapFn>(Wrap));
+  }
+
   ExecutorSymbolDef() = default;
   ExecutorSymbolDef(ExecutorAddr Addr, JITSymbolFlags Flags)
     : Addr(Addr), Flags(Flags) {}

diff  --git a/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp b/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
index e8b22b3d4bbb75..3de77031291c53 100644
--- a/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
@@ -8,6 +8,7 @@
 
 #include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
 #include "OrcTestCommon.h"
+#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
 
 using namespace llvm;
 using namespace llvm::orc;
@@ -107,4 +108,35 @@ TEST(ExecutorAddrTest, AddrRanges) {
   EXPECT_GT(R1, R0);
 }
 
+TEST(ExecutorSymbolDef, PointerConversion) {
+  int X = 0;
+
+  auto XHiddenSym = ExecutorSymbolDef::fromPtr(&X);
+  int *XHiddenPtr = XHiddenSym.toPtr<int *>();
+
+  auto XExportedSym = ExecutorSymbolDef::fromPtr(&X, JITSymbolFlags::Exported);
+  int *XExportedPtr = XExportedSym.toPtr<int *>();
+
+  EXPECT_EQ(XHiddenPtr, &X);
+  EXPECT_EQ(XExportedPtr, &X);
+
+  EXPECT_EQ(XHiddenSym.getFlags(), JITSymbolFlags());
+  EXPECT_EQ(XExportedSym.getFlags(), JITSymbolFlags::Exported);
+}
+
+TEST(ExecutorSymbolDef, FunctionPointerConversion) {
+  auto FHiddenSym = ExecutorSymbolDef::fromPtr(&F);
+  void (*FHiddenPtr)() = FHiddenSym.toPtr<void()>();
+
+  auto FExportedSym = ExecutorSymbolDef::fromPtr(&F, JITSymbolFlags::Exported);
+  void (*FExportedPtr)() = FExportedSym.toPtr<void()>();
+
+  EXPECT_EQ(FHiddenPtr, &F);
+  EXPECT_EQ(FExportedPtr, &F);
+
+  EXPECT_EQ(FHiddenSym.getFlags(), JITSymbolFlags::Callable);
+  EXPECT_EQ(FExportedSym.getFlags(),
+            JITSymbolFlags::Exported | JITSymbolFlags::Callable);
+}
+
 } // namespace


        


More information about the llvm-commits mailing list