28 auto void_type = llvm::Type::getVoidTy(cgen_state->
context_);
30 std::vector<llvm::Type*> parameter_types(2, int8_ptr_type);
31 const auto func_type = llvm::FunctionType::get(void_type, parameter_types,
false);
32 auto function = llvm::Function::Create(
33 func_type, llvm::Function::ExternalLinkage, name, cgen_state->
module_);
35 llvm::BasicBlock::Create(cgen_state->
context_,
".entry",
function, 0);
43 std::string read_arg_name{
"read_stub_arg_"};
47 read_arg_name +=
"int";
51 read_arg_name +=
"float";
55 read_arg_name +=
"double";
59 read_arg_name +=
"pi8";
63 read_arg_name +=
"pi32";
67 read_arg_name +=
"pi64";
71 read_arg_name +=
"pvoid";
75 read_arg_name +=
"ppi64";
79 LOG(
FATAL) <<
"Invalid type: " <<
static_cast<int>(arg_type);
124 CHECK_LT(static_cast<size_t>(i), inputs.size());
125 return inputs[i].int_val;
131 CHECK_LT(static_cast<size_t>(i), inputs.size());
132 return inputs[i].float_val;
138 CHECK_LT(static_cast<size_t>(i), inputs.size());
139 return inputs[i].double_val;
145 CHECK_LT(static_cast<size_t>(i), inputs.size());
146 return inputs[i].ptr;
165 const void* inputs_handle,
173 const int64_t int_val) {
185 const std::string&
name,
186 const std::vector<Type>& arg_types,
188 const bool is_external) {
195 const auto stub_name = name +
"_stub";
200 const auto compilation_context =
202 if (compilation_context) {
208 auto cgen_state = executor->getCgenStatePtr();
209 cgen_state->set_module_shallow_copy(executor->get_rt_module());
212 auto& ctx = cgen_state->context_;
213 std::vector<llvm::Value*> callee_args;
214 auto inputs_it =
function->arg_begin() + 1;
215 for (
size_t i = 0; i < arg_types.size(); ++i) {
216 const auto arg_type = arg_types[i];
218 const auto llvm_arg_type =
llvm_type(arg_type, ctx);
219 auto callee_arg = cgen_state->emitExternalCall(
220 read_arg_name, llvm_arg_type, {&*inputs_it, cgen_state->llInt<int32_t>(i)});
222 CHECK(llvm_arg_type->isIntegerTy());
223 callee_arg = cgen_state->ir_builder_.CreateTrunc(callee_arg, llvm_arg_type);
225 CHECK(llvm_arg_type->isPointerTy());
226 callee_arg = cgen_state->ir_builder_.CreateBitCast(callee_arg, llvm_arg_type);
228 callee_args.push_back(callee_arg);
230 const auto llvm_ret_type =
llvm_type(ret_type, ctx);
231 auto value = is_external
232 ? cgen_state->emitExternalCall(name, llvm_ret_type, callee_args)
233 : cgen_state->emitCall(name, callee_args);
234 auto output = &*(
function->arg_begin());
235 auto void_type = llvm::Type::getVoidTy(ctx);
236 std::string write_arg_name{
"write_stub_result_"};
239 write_arg_name +=
"int";
240 const auto i64_type =
get_int_type(64, cgen_state->context_);
241 value = cgen_state->ir_builder_.CreateSExt(value, i64_type);
245 write_arg_name +=
"int";
246 const auto i64_type =
get_int_type(64, cgen_state->context_);
247 value = cgen_state->ir_builder_.CreateSExt(value, i64_type);
255 LOG(
FATAL) <<
"Invalid type: " <<
static_cast<int>(ret_type);
259 cgen_state->emitExternalCall(write_arg_name, void_type, {output, value});
261 cgen_state->ir_builder_.CreateRetVoid();
266 auto cpu_compilation_context = std::make_shared<CpuCompilationContext>(std::move(ee));
267 cpu_compilation_context->setFunctionPointer(
function);
270 std::move(cpu_compilation_context));
RUNTIME_EXPORT int64_t read_stub_arg_int(const void *inputs_handle, const int32_t i)
RUNTIME_EXPORT double read_stub_arg_double(const void *inputs_handle, const int32_t i)
std::vector< ReductionInterpreter::EvalValue > InputsType
llvm::IRBuilder ir_builder_
RUNTIME_EXPORT const int32_t * read_stub_arg_pi32(const void *inputs_handle, const int32_t i)
std::vector< std::string > CodeCacheKey
static ExecutionEngineWrapper generateNativeCPUCode(llvm::Function *func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co)
RUNTIME_EXPORT void write_stub_result_int(void *output_handle, const int64_t int_val)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
RUNTIME_EXPORT const int64_t *const * read_stub_arg_ppi64(const void *inputs_handle, const int32_t i)
static std::shared_ptr< Executor > getExecutor(const ExecutorId id, const std::string &debug_dir="", const std::string &debug_file="", const SystemParameters &system_parameters=SystemParameters())
static Stub generateStub(const size_t executor_id, const std::string &name, const std::vector< Type > &arg_types, const Type ret_type, const bool is_external)
void verify_function_ir(const llvm::Function *func)
llvm::LLVMContext & context_
RUNTIME_EXPORT float read_stub_arg_float(const void *inputs_handle, const int32_t i)
RUNTIME_EXPORT const int8_t * read_stub_arg_pi8(const void *inputs_handle, const int32_t i)
RUNTIME_EXPORT const int32_t * read_stub_arg_pi64(const void *inputs_handle, const int32_t i)
RUNTIME_EXPORT const void * read_stub_arg_pvoid(const void *inputs_handle, const int32_t i)
llvm::Type * llvm_type(const Type type, llvm::LLVMContext &ctx)
std::string get_stub_read_argument_name(const Type arg_type)
ReductionInterpreter::EvalValue(*)(void *output_handle, const void *inputs_handle) Stub
static std::shared_ptr< QueryEngine > getInstance()
bool is_pointer_type(const Type type)
bool is_integer_type(const Type type)
llvm::Function * create_stub_function(const std::string &name, CgenState *cgen_state)