Files
concrete/compiler/lib/ServerLib/genDynamicRankCall.py
Andi Drebes 7cc0c01326 fix(compiler): Do not use C-style casts in multi_arity_call_dynamic_rank
C-style casts between function pointers result in an error using g++
8.3.0. This patch introduces a workaround based on `reinterpret_cast`,
in which the original function pointer is first cast into an unsigned
integer of sufficient size and then cast into the target function
pointer type.

TODO: Check that this is actually valid C++ with defined behavior on
all implementations / platforms.
2022-04-11 17:24:23 +02:00

63 lines
2.2 KiB
Python

# Part of the Concrete Compiler Project, under the BSD3 License with Zama Exceptions.
# See https://github.com/zama-ai/concrete-compiler-internal/blob/master/LICENSE.txt for license information.
print(
"""// Part of the Concrete Compiler Project, under the BSD3 License with Zama
// Exceptions. See
// https://github.com/zama-ai/concrete-compiler-internal/blob/master/LICENSE.txt
// for license information.
// generated: see genDynamicRandCall.py
#include <cassert>
#include <vector>
#include "concretelang/ClientLib/Types.h"
#include "concretelang/ServerLib/DynamicArityCall.h"
#include "concretelang/ServerLib/ServerLambda.h"
namespace concretelang {
namespace serverlib {
// Helper class template that yields an unsigned integer type given a
// size in bytes
template <std::size_t size> struct int_type_of_size {};
template <> struct int_type_of_size<4> { typedef uint32_t type; };
template <> struct int_type_of_size<8> { typedef uint64_t type; };
// Converts one function pointer into another
// TODO: Not sure this is valid in all implementations / on all
// architectures
template <typename FnDstT, typename FnSrcT> FnDstT convert_fnptr(FnSrcT src) {
static_assert(sizeof(FnDstT) == sizeof(FnSrcT),
"Size of function types must match");
using inttype = typename int_type_of_size<sizeof(FnDstT)>::type;
inttype raw = reinterpret_cast<inttype>(src);
return reinterpret_cast<FnDstT>(raw);
}
TensorData multi_arity_call_dynamic_rank(void *(*func)(void *...),
std::vector<void *> args,
size_t rank) {
using concretelang::clientlib::MemRefDescriptor;
constexpr auto convert = concretelang::clientlib::tensorDataFromMemRef;
switch (rank) {""")
for tensor_rank in range(0, 33):
memref_rank = tensor_rank + 1
print(f""" case {tensor_rank}: {{
auto m = multi_arity_call(
convert_fnptr<MemRefDescriptor<{memref_rank}> (*)(void *...)>(func), args);
return convert({memref_rank}, m.allocated, m.aligned, m.offset, m.sizes, m.strides);
}}""")
print("""
default:
assert(false);
}
}""")
print("""
} // namespace serverlib
} // namespace concretelang""")