[ORC] Add ExecutorSymbolDef toPtr / fromPtr convenience functions.
This will simplify conversion of a number of APIs from ExecutorAddr to ExecutorSymbolDef.
This commit is contained in:
parent
01045b75a1
commit
81c0f3023f
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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.
|
||||
|
@ -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) {}
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user