Skip to content

Commit 55b68c7

Browse files
committed
[ORC] Port CallableTraitsHelper from the new ORC runtime.
The code for this commit was taken with minimal modification to fit LLVM style from llvm-project/orc-rt/include/orc-rt/CallableTraitsHelper.h and llvm-project/orc-rt/unittests/CallableTraitsHelperTest.cpp (originally committed in 40fce32) CallableTraitsHelper identifies the return type and argument types of a callable type and passes those to an implementation class template to operate on. E.g. the CallableArgInfoImpl class exposes these types as typedefs. Porting CallableTraitsHelper from the new ORC runtime will allow us to simplify existing and upcoming "callable-traits" classes in ORC.
1 parent 6638d59 commit 55b68c7

File tree

3 files changed

+145
-0
lines changed

3 files changed

+145
-0
lines changed
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
//===- CallableTraitsHelper.h - Callable arg/ret type extractor -*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// CallableTraitsHelper API.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef LLVM_EXECUTIONENGINE_ORC_CALLABLETRAITSHELPER_H
14+
#define LLVM_EXECUTIONENGINE_ORC_CALLABLETRAITSHELPER_H
15+
16+
#include <tuple>
17+
#include <type_traits>
18+
19+
namespace llvm::orc {
20+
21+
/// CallableTraitsHelper takes an implementation class template Impl and some
22+
/// callable type C and passes the return and argument types of C to the Impl
23+
/// class template.
24+
///
25+
/// This can be used to simplify the implementation of classes that need to
26+
/// operate on callable types.
27+
template <template <typename...> typename ImplT, typename C>
28+
struct CallableTraitsHelper
29+
: public CallableTraitsHelper<
30+
ImplT,
31+
decltype(&std::remove_cv_t<std::remove_reference_t<C>>::operator())> {
32+
};
33+
34+
template <template <typename...> typename ImplT, typename RetT,
35+
typename... ArgTs>
36+
struct CallableTraitsHelper<ImplT, RetT(ArgTs...)>
37+
: public ImplT<RetT, ArgTs...> {};
38+
39+
template <template <typename...> typename ImplT, typename RetT,
40+
typename... ArgTs>
41+
struct CallableTraitsHelper<ImplT, RetT (*)(ArgTs...)>
42+
: public CallableTraitsHelper<ImplT, RetT(ArgTs...)> {};
43+
44+
template <template <typename...> typename ImplT, typename RetT,
45+
typename... ArgTs>
46+
struct CallableTraitsHelper<ImplT, RetT (&)(ArgTs...)>
47+
: public CallableTraitsHelper<ImplT, RetT(ArgTs...)> {};
48+
49+
template <template <typename...> typename ImplT, typename ClassT, typename RetT,
50+
typename... ArgTs>
51+
struct CallableTraitsHelper<ImplT, RetT (ClassT::*)(ArgTs...)>
52+
: public CallableTraitsHelper<ImplT, RetT(ArgTs...)> {};
53+
54+
template <template <typename...> typename ImplT, typename ClassT, typename RetT,
55+
typename... ArgTs>
56+
struct CallableTraitsHelper<ImplT, RetT (ClassT::*)(ArgTs...) const>
57+
: public CallableTraitsHelper<ImplT, RetT(ArgTs...)> {};
58+
59+
namespace detail {
60+
template <typename RetT, typename... ArgTs> struct CallableArgInfoImpl {
61+
using ReturnType = RetT;
62+
using ArgsTupleType = std::tuple<ArgTs...>;
63+
};
64+
} // namespace detail
65+
66+
/// CallableArgInfo provides typedefs for the return type and argument types
67+
/// (as a tuple) of the given callable type.
68+
template <typename Callable>
69+
struct CallableArgInfo
70+
: public CallableTraitsHelper<detail::CallableArgInfoImpl, Callable> {};
71+
72+
} // namespace llvm::orc
73+
74+
#endif // LLVM_EXECUTIONENGINE_ORC_CALLABLETRAITSHELPER_H

llvm/unittests/ExecutionEngine/Orc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ set(LLVM_LINK_COMPONENTS
1818
)
1919

2020
add_llvm_unittest(OrcJITTests
21+
CallableTraitsHelperTest.cpp
2122
CoreAPIsTest.cpp
2223
ExecutorAddressTest.cpp
2324
ExecutionSessionWrapperFunctionCallsTest.cpp
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
//===- CallableTraitsHelperTest.cpp ---------------------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// Tests for llvm::orc::CallableTraitsHelper APIs.
10+
//
11+
// NOTE: All tests in this file are testing compile-time functionality, so the
12+
// tests at runtime all end up being noops. That's fine -- those are
13+
// cheap.
14+
//===----------------------------------------------------------------------===//
15+
16+
#include "llvm/ExecutionEngine/Orc/CallableTraitsHelper.h"
17+
#include "gtest/gtest.h"
18+
19+
using namespace llvm;
20+
using namespace llvm::orc;
21+
22+
static void freeVoidVoid() {}
23+
24+
TEST(CallableTraitsHelperTest, FreeVoidVoid) {
25+
(void)freeVoidVoid;
26+
typedef CallableArgInfo<decltype(freeVoidVoid)> CAI;
27+
static_assert(std::is_void_v<CAI::ReturnType>);
28+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<>>);
29+
}
30+
31+
static int freeBinaryOp(int, float) { return 0; }
32+
33+
TEST(CallableTraitsHelperTest, FreeBinaryOp) {
34+
(void)freeBinaryOp;
35+
typedef CallableArgInfo<decltype(freeBinaryOp)> CAI;
36+
static_assert(std::is_same_v<CAI::ReturnType, int>);
37+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<int, float>>);
38+
}
39+
40+
TEST(CallableTraitsHelperTest, VoidVoidObj) {
41+
auto VoidVoid = []() {};
42+
typedef CallableArgInfo<decltype(VoidVoid)> CAI;
43+
static_assert(std::is_void_v<CAI::ReturnType>);
44+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<>>);
45+
}
46+
47+
TEST(CallableTraitsHelperTest, BinaryOpObj) {
48+
auto BinaryOp = [](int X, float Y) -> int { return X + Y; };
49+
typedef CallableArgInfo<decltype(BinaryOp)> CAI;
50+
static_assert(std::is_same_v<CAI::ReturnType, int>);
51+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<int, float>>);
52+
}
53+
54+
TEST(CallableTraitsHelperTest, PreservesLValueRef) {
55+
auto RefOp = [](int &) {};
56+
typedef CallableArgInfo<decltype(RefOp)> CAI;
57+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<int &>>);
58+
}
59+
60+
TEST(CallableTraitsHelperTest, PreservesLValueRefConstness) {
61+
auto RefOp = [](const int &) {};
62+
typedef CallableArgInfo<decltype(RefOp)> CAI;
63+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<const int &>>);
64+
}
65+
66+
TEST(CallableTraitsHelperTest, PreservesRValueRef) {
67+
auto RefOp = [](int &&) {};
68+
typedef CallableArgInfo<decltype(RefOp)> CAI;
69+
static_assert(std::is_same_v<CAI::ArgsTupleType, std::tuple<int &&>>);
70+
}

0 commit comments

Comments
 (0)