OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
StubGenerator Class Reference

#include <ResultSetReductionInterpreterStubs.h>

Public Types

using Stub = ReductionInterpreter::EvalValue(*)(void *output_handle, const void *inputs_handle)
 
using InputsType = std::vector< ReductionInterpreter::EvalValue >
 

Static Public Member Functions

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)
 

Detailed Description

Definition at line 25 of file ResultSetReductionInterpreterStubs.h.

Member Typedef Documentation

using StubGenerator::Stub = ReductionInterpreter::EvalValue (*)(void* output_handle, const void* inputs_handle)

Definition at line 30 of file ResultSetReductionInterpreterStubs.h.

Member Function Documentation

StubGenerator::Stub StubGenerator::generateStub ( const size_t  executor_id,
const std::string &  name,
const std::vector< Type > &  arg_types,
const Type  ret_type,
const bool  is_external 
)
static

Definition at line 184 of file ResultSetReductionInterpreterStubs.cpp.

References CHECK, CPU, anonymous_namespace{ResultSetReductionInterpreterStubs.cpp}::create_stub_function(), logger::FATAL, CodeGenerator::generateNativeCPUCode(), get_int_type(), anonymous_namespace{ResultSetReductionInterpreterStubs.cpp}::get_stub_read_argument_name(), Executor::getExecutor(), QueryEngine::getInstance(), Int32, Int8, is_integer_type(), is_pointer_type(), llvm_type(), LOG, ReductionJIT, verify_function_ir(), and Void.

Referenced by ReductionInterpreterImpl::bindStub().

188  {
189  // Multiple executors may trigger the generation of the same
190  // stub. We'll use get_or_wait/put methods of code cache accessor to
191  // let the first executor to generate the stub while other executors
192  // will wait until the stub has been put to the code cache.
193 
194  auto executor = Executor::getExecutor(executor_id);
195  const auto stub_name = name + "_stub";
196  CodeCacheKey key{stub_name};
197 
198  // get_or_wait locks globally unless (i) this is the first attempt
199  // to compile for given key, or (ii) when key exists in code cache.
200  const auto compilation_context =
201  QueryEngine::getInstance()->s_stubs_accessor->get_or_wait(key);
202  if (compilation_context) {
203  return reinterpret_cast<StubGenerator::Stub>(compilation_context->get()->func());
204  }
205 
206  // compilation is locked per executor
207  Executor::CgenStateManager cgenstate_manager(*executor.get());
208  auto cgen_state = executor->getCgenStatePtr();
209  cgen_state->set_module_shallow_copy(executor->get_rt_module());
210  const auto function = create_stub_function(stub_name, cgen_state);
211  CHECK(function);
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];
217  const auto read_arg_name = get_stub_read_argument_name(arg_type);
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)});
221  if (is_integer_type(arg_type)) {
222  CHECK(llvm_arg_type->isIntegerTy());
223  callee_arg = cgen_state->ir_builder_.CreateTrunc(callee_arg, llvm_arg_type);
224  } else if (is_pointer_type(arg_type)) {
225  CHECK(llvm_arg_type->isPointerTy());
226  callee_arg = cgen_state->ir_builder_.CreateBitCast(callee_arg, llvm_arg_type);
227  }
228  callee_args.push_back(callee_arg);
229  }
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_"};
237  switch (ret_type) {
238  case Type::Int8: {
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);
242  break;
243  }
244  case Type::Int32: {
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);
248  break;
249  }
250  case Type::Void: {
251  value = nullptr;
252  break;
253  }
254  default: {
255  LOG(FATAL) << "Invalid type: " << static_cast<int>(ret_type);
256  }
257  }
258  if (value) {
259  cgen_state->emitExternalCall(write_arg_name, void_type, {output, value});
260  }
261  cgen_state->ir_builder_.CreateRetVoid();
262  verify_function_ir(function);
265  auto ee = CodeGenerator::generateNativeCPUCode(function, {function}, co);
266  auto cpu_compilation_context = std::make_shared<CpuCompilationContext>(std::move(ee));
267  cpu_compilation_context->setFunctionPointer(function);
268  auto func_ptr = reinterpret_cast<StubGenerator::Stub>(cpu_compilation_context->func());
269  QueryEngine::getInstance()->s_stubs_accessor->reset(key,
270  std::move(cpu_compilation_context));
271  return func_ptr;
272 }
#define LOG(tag)
Definition: Logger.h:285
std::vector< std::string > CodeCacheKey
Definition: CodeCache.h:24
static ExecutionEngineWrapper generateNativeCPUCode(llvm::Function *func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
static std::shared_ptr< Executor > getExecutor(const ExecutorId id, const std::string &debug_dir="", const std::string &debug_file="", const SystemParameters &system_parameters=SystemParameters())
Definition: Execute.cpp:513
void verify_function_ir(const llvm::Function *func)
llvm::Type * llvm_type(const Type type, llvm::LLVMContext &ctx)
ReductionInterpreter::EvalValue(*)(void *output_handle, const void *inputs_handle) Stub
#define CHECK(condition)
Definition: Logger.h:291
static std::shared_ptr< QueryEngine > getInstance()
Definition: QueryEngine.h:89
bool is_pointer_type(const Type type)
string name
Definition: setup.in.py:72
bool is_integer_type(const Type type)
llvm::Function * create_stub_function(const std::string &name, CgenState *cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:


The documentation for this class was generated from the following files: