Skip to content

Commit 81c0f30

Browse files
committed
[ORC] Add ExecutorSymbolDef toPtr / fromPtr convenience functions.
This will simplify conversion of a number of APIs from ExecutorAddr to ExecutorSymbolDef.
1 parent 01045b7 commit 81c0f30

File tree

10 files changed

+71
-8
lines changed

10 files changed

+71
-8
lines changed

llvm/examples/Kaleidoscope/BuildingAJIT/Chapter1/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ static void HandleTopLevelExpression() {
11591159

11601160
// Get the symbol's address and cast it to the right type (takes no
11611161
// arguments, returns a double) so we can call it as a native function.
1162-
auto *FP = Sym.getAddress().toPtr<double (*)()>();
1162+
auto *FP = Sym.toPtr<double (*)()>();
11631163
fprintf(stderr, "Evaluated to %f\n", FP());
11641164

11651165
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/BuildingAJIT/Chapter2/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ static void HandleTopLevelExpression() {
11591159

11601160
// Get the symbol's address and cast it to the right type (takes no
11611161
// arguments, returns a double) so we can call it as a native function.
1162-
auto *FP = Sym.getAddress().toPtr<double (*)()>();
1162+
auto *FP = Sym.toPtr<double (*)()>();
11631163
fprintf(stderr, "Evaluated to %f\n", FP());
11641164

11651165
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/BuildingAJIT/Chapter3/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,7 @@ static void HandleTopLevelExpression() {
11591159

11601160
// Get the symbol's address and cast it to the right type (takes no
11611161
// arguments, returns a double) so we can call it as a native function.
1162-
auto *FP = Sym.getAddress().toPtr<double (*)()>();
1162+
auto *FP = Sym.toPtr<double (*)()>();
11631163
fprintf(stderr, "Evaluated to %f\n", FP());
11641164

11651165
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/BuildingAJIT/Chapter4/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1157,7 +1157,7 @@ static void HandleTopLevelExpression() {
11571157

11581158
// Get the symbol's address and cast it to the right type (takes no
11591159
// arguments, returns a double) so we can call it as a native function.
1160-
auto *FP = Sym.getAddress().toPtr<double (*)()>();
1160+
auto *FP = Sym.toPtr<double (*)()>();
11611161
fprintf(stderr, "Evaluated to %f\n", FP());
11621162

11631163
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/Chapter4/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -643,7 +643,7 @@ static void HandleTopLevelExpression() {
643643

644644
// Get the symbol's address and cast it to the right type (takes no
645645
// arguments, returns a double) so we can call it as a native function.
646-
double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
646+
double (*FP)() = ExprSymbol.toPtr<double (*)()>();
647647
fprintf(stderr, "Evaluated to %f\n", FP());
648648

649649
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/Chapter5/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -917,7 +917,7 @@ static void HandleTopLevelExpression() {
917917

918918
// Get the symbol's address and cast it to the right type (takes no
919919
// arguments, returns a double) so we can call it as a native function.
920-
double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
920+
double (*FP)() = ExprSymbol.toPtr<double (*)()>();
921921
fprintf(stderr, "Evaluated to %f\n", FP());
922922

923923
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/Chapter6/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1036,7 +1036,7 @@ static void HandleTopLevelExpression() {
10361036

10371037
// Get the symbol's address and cast it to the right type (takes no
10381038
// arguments, returns a double) so we can call it as a native function.
1039-
double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
1039+
double (*FP)() = ExprSymbol.toPtr<double (*)()>();
10401040
fprintf(stderr, "Evaluated to %f\n", FP());
10411041

10421042
// Delete the anonymous expression module from the JIT.

llvm/examples/Kaleidoscope/Chapter7/toy.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1207,7 +1207,7 @@ static void HandleTopLevelExpression() {
12071207

12081208
// Get the symbol's address and cast it to the right type (takes no
12091209
// arguments, returns a double) so we can call it as a native function.
1210-
double (*FP)() = ExprSymbol.getAddress().toPtr<double (*)()>();
1210+
double (*FP)() = ExprSymbol.toPtr<double (*)()>();
12111211
fprintf(stderr, "Evaluated to %f\n", FP());
12121212

12131213
// Delete the anonymous expression module from the JIT.

llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,37 @@ namespace orc {
2323
/// Represents a defining location for a JIT symbol.
2424
class ExecutorSymbolDef {
2525
public:
26+
/// Create an ExecutorSymbolDef from the given pointer.
27+
/// Warning: This should only be used when JITing in-process.
28+
template <typename T, typename UnwrapFn = ExecutorAddr::defaultUnwrap<T>>
29+
static ExecutorSymbolDef fromPtr(T *Ptr,
30+
JITSymbolFlags BaseFlags = JITSymbolFlags(),
31+
UnwrapFn &&Unwrap = UnwrapFn()) {
32+
auto *UP = Unwrap(Ptr);
33+
JITSymbolFlags Flags = BaseFlags;
34+
if (std::is_function_v<T>)
35+
Flags |= JITSymbolFlags::Callable;
36+
return ExecutorSymbolDef(
37+
ExecutorAddr::fromPtr(UP, ExecutorAddr::rawPtr<T>()), Flags);
38+
}
39+
40+
/// Cast this ExecutorSymbolDef to a pointer of the given type.
41+
/// Warning: This should only be used when JITing in-process.
42+
template <typename T, typename WrapFn =
43+
ExecutorAddr::defaultWrap<std::remove_pointer_t<T>>>
44+
std::enable_if_t<std::is_pointer<T>::value, T>
45+
toPtr(WrapFn &&Wrap = WrapFn()) const {
46+
return Addr.toPtr<T>(std::forward<WrapFn>(Wrap));
47+
}
48+
49+
/// Cast this ExecutorSymbolDef to a pointer of the given function type.
50+
/// Warning: This should only be used when JITing in-process.
51+
template <typename T, typename WrapFn = ExecutorAddr::defaultWrap<T>>
52+
std::enable_if_t<std::is_function<T>::value, T *>
53+
toPtr(WrapFn &&Wrap = WrapFn()) const {
54+
return Addr.toPtr<T>(std::forward<WrapFn>(Wrap));
55+
}
56+
2657
ExecutorSymbolDef() = default;
2758
ExecutorSymbolDef(ExecutorAddr Addr, JITSymbolFlags Flags)
2859
: Addr(Addr), Flags(Flags) {}

llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88

99
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h"
1010
#include "OrcTestCommon.h"
11+
#include "llvm/ExecutionEngine/Orc/Shared/ExecutorSymbolDef.h"
1112

1213
using namespace llvm;
1314
using namespace llvm::orc;
@@ -107,4 +108,35 @@ TEST(ExecutorAddrTest, AddrRanges) {
107108
EXPECT_GT(R1, R0);
108109
}
109110

111+
TEST(ExecutorSymbolDef, PointerConversion) {
112+
int X = 0;
113+
114+
auto XHiddenSym = ExecutorSymbolDef::fromPtr(&X);
115+
int *XHiddenPtr = XHiddenSym.toPtr<int *>();
116+
117+
auto XExportedSym = ExecutorSymbolDef::fromPtr(&X, JITSymbolFlags::Exported);
118+
int *XExportedPtr = XExportedSym.toPtr<int *>();
119+
120+
EXPECT_EQ(XHiddenPtr, &X);
121+
EXPECT_EQ(XExportedPtr, &X);
122+
123+
EXPECT_EQ(XHiddenSym.getFlags(), JITSymbolFlags());
124+
EXPECT_EQ(XExportedSym.getFlags(), JITSymbolFlags::Exported);
125+
}
126+
127+
TEST(ExecutorSymbolDef, FunctionPointerConversion) {
128+
auto FHiddenSym = ExecutorSymbolDef::fromPtr(&F);
129+
void (*FHiddenPtr)() = FHiddenSym.toPtr<void()>();
130+
131+
auto FExportedSym = ExecutorSymbolDef::fromPtr(&F, JITSymbolFlags::Exported);
132+
void (*FExportedPtr)() = FExportedSym.toPtr<void()>();
133+
134+
EXPECT_EQ(FHiddenPtr, &F);
135+
EXPECT_EQ(FExportedPtr, &F);
136+
137+
EXPECT_EQ(FHiddenSym.getFlags(), JITSymbolFlags::Callable);
138+
EXPECT_EQ(FExportedSym.getFlags(),
139+
JITSymbolFlags::Exported | JITSymbolFlags::Callable);
140+
}
141+
110142
} // namespace

0 commit comments

Comments
 (0)