[llvm] 98616cf - [ORC] Add an ExecutorAddr::toPtr overload for function types.
Lang Hames via llvm-commits
llvm-commits at lists.llvm.org
Thu May 5 12:37:29 PDT 2022
Author: Lang Hames
Date: 2022-05-05T12:37:23-07:00
New Revision: 98616cfc02613d98964588fac6494ec7583c495f
URL: https://github.com/llvm/llvm-project/commit/98616cfc02613d98964588fac6494ec7583c495f
DIFF: https://github.com/llvm/llvm-project/commit/98616cfc02613d98964588fac6494ec7583c495f.diff
LOG: [ORC] Add an ExecutorAddr::toPtr overload for function types.
In the common case of converting an ExecutorAddr to a function pointer type,
this eliminates the need for the '(*)' boilerplate to explicitly specify a
function pointer. E.g.:
auto *F = A.toPtr<int(*)()>();
can now be written as
auto *F = A.toPtr<int()>();
Added:
Modified:
llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
Removed:
################################################################################
diff --git a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
index dc080cfc79d12..5d545f8abdb99 100644
--- a/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
+++ b/llvm/include/llvm/ExecutionEngine/Orc/Shared/ExecutorAddress.h
@@ -43,13 +43,22 @@ class ExecutorAddr {
/// Cast this ExecutorAddr to a pointer of the given type.
/// Warning: This should only be used when JITing in-process.
- template <typename T> T toPtr() const {
- static_assert(std::is_pointer<T>::value, "T must be a pointer type");
+ template <typename T>
+ std::enable_if_t<std::is_pointer<T>::value, T> toPtr() const {
uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t");
return reinterpret_cast<T>(IntPtr);
}
+ /// Cast this ExecutorAddr to a pointer of the given function type.
+ /// Warning: This should only be used when JITing in-process.
+ template <typename T>
+ std::enable_if_t<std::is_function<T>::value, T *> toPtr() const {
+ uintptr_t IntPtr = static_cast<uintptr_t>(Addr);
+ assert(IntPtr == Addr && "ExecutorAddr value out of range for uintptr_t");
+ return reinterpret_cast<T *>(IntPtr);
+ }
+
uint64_t getValue() const { return Addr; }
void setValue(uint64_t Addr) { this->Addr = Addr; }
bool isNull() const { return Addr == 0; }
diff --git a/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp b/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
index 99099a0c5591d..7cbfdffd49bcf 100644
--- a/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
+++ b/llvm/unittests/ExecutionEngine/Orc/ExecutorAddressTest.cpp
@@ -47,6 +47,17 @@ TEST(ExecutorAddrTest, PtrConversion) {
EXPECT_EQ(XPtr, &X);
}
+static void F() {}
+
+TEST(ExecutorAddrTest, PtrConversionWithFunctionType) {
+ // Test that function types (as opposed to function pointer types) can be
+ // used with toPtr.
+ auto FAddr = ExecutorAddr::fromPtr(F);
+ void (*FPtr)() = FAddr.toPtr<void()>();
+
+ EXPECT_EQ(FPtr, &F);
+}
+
TEST(ExecutorAddrTest, AddrRanges) {
ExecutorAddr A0(0), A1(1), A2(2), A3(3);
ExecutorAddrRange R0(A0, A1), R1(A1, A2), R2(A2, A3), R3(A0, A2), R4(A1, A3);
More information about the llvm-commits
mailing list