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

#include <ResultSetReductionInterpreterStubs.h>

+ Collaboration diagram for StubGenerator:

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 std::string &name, const std::vector< Type > &arg_types, const Type ret_type, const bool is_external)
 
static void clearCache ()
 

Static Private Attributes

static CodeCache s_stubs_cache
 
static std::mutex s_stubs_cache_mutex
 

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

void StubGenerator::clearCache ( )
static

Definition at line 256 of file ResultSetReductionInterpreterStubs.cpp.

References LruCache< key_t, value_t, hash_t >::clear(), s_stubs_cache, and s_stubs_cache_mutex.

Referenced by ResultSetReductionJIT::clearCache().

256  {
257  std::lock_guard<std::mutex> s_stubs_cache_lock(s_stubs_cache_mutex);
259 }
void clear()
Definition: LruCache.hpp:57
static std::mutex s_stubs_cache_mutex

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 172 of file ResultSetReductionInterpreterStubs.cpp.

References Executor::addCodeToCache(), cgen_state, CHECK(), CPU, anonymous_namespace{ResultSetReductionInterpreterStubs.cpp}::create_stub_function(), logger::FATAL, CodeGenerator::generateNativeCPUCode(), LruCache< key_t, value_t, hash_t >::get(), get_int_type(), anonymous_namespace{ResultSetReductionInterpreterStubs.cpp}::get_stub_read_argument_name(), Int32, Int8, is_integer_type(), is_pointer_type(), llvm_type(), LOG, module(), ReductionJIT, runtime_module_shallow_copy(), s_stubs_cache, s_stubs_cache_mutex, verify_function_ir(), and Void.

Referenced by ReductionInterpreterImpl::bindStub().

175  {
176  const auto stub_name = name + "_stub";
177  CodeCacheKey key{stub_name};
178  std::lock_guard<std::mutex> s_stubs_cache_lock(s_stubs_cache_mutex);
179  const auto compilation_context = s_stubs_cache.get(key);
180  if (compilation_context) {
181  auto cpu_context =
182  std::dynamic_pointer_cast<CpuCompilationContext>(compilation_context->first);
183  CHECK(cpu_context);
184  return reinterpret_cast<StubGenerator::Stub>(cpu_context->func());
185  }
186  auto cgen_state = std::make_unique<CgenState>(std::vector<InputTableInfo>{}, false);
187  std::unique_ptr<llvm::Module> module(runtime_module_shallow_copy(cgen_state.get()));
188  cgen_state->module_ = module.get();
189  const auto function = create_stub_function(stub_name, cgen_state.get());
190  CHECK(function);
191  auto& ctx = cgen_state->context_;
192  std::vector<llvm::Value*> callee_args;
193  auto inputs_it = function->arg_begin() + 1;
194  for (size_t i = 0; i < arg_types.size(); ++i) {
195  const auto arg_type = arg_types[i];
196  const auto read_arg_name = get_stub_read_argument_name(arg_type);
197  const auto llvm_arg_type = llvm_type(arg_type, ctx);
198  auto callee_arg = cgen_state->emitExternalCall(
199  read_arg_name, llvm_arg_type, {&*inputs_it, cgen_state->llInt<int32_t>(i)});
200  if (is_integer_type(arg_type)) {
201  CHECK(llvm_arg_type->isIntegerTy());
202  callee_arg = cgen_state->ir_builder_.CreateTrunc(callee_arg, llvm_arg_type);
203  } else if (is_pointer_type(arg_type)) {
204  CHECK(llvm_arg_type->isPointerTy());
205  callee_arg = cgen_state->ir_builder_.CreateBitCast(callee_arg, llvm_arg_type);
206  }
207  callee_args.push_back(callee_arg);
208  }
209  const auto llvm_ret_type = llvm_type(ret_type, ctx);
210  auto value = is_external
211  ? cgen_state->emitExternalCall(name, llvm_ret_type, callee_args)
212  : cgen_state->emitCall(name, callee_args);
213  auto output = &*(function->arg_begin());
214  auto void_type = llvm::Type::getVoidTy(ctx);
215  std::string write_arg_name{"write_stub_result_"};
216  switch (ret_type) {
217  case Type::Int8: {
218  write_arg_name += "int";
219  const auto i64_type = get_int_type(64, cgen_state->context_);
220  value = cgen_state->ir_builder_.CreateSExt(value, i64_type);
221  break;
222  }
223  case Type::Int32: {
224  write_arg_name += "int";
225  const auto i64_type = get_int_type(64, cgen_state->context_);
226  value = cgen_state->ir_builder_.CreateSExt(value, i64_type);
227  break;
228  }
229  case Type::Void: {
230  value = nullptr;
231  break;
232  }
233  default: {
234  LOG(FATAL) << "Invalid type: " << static_cast<int>(ret_type);
235  }
236  }
237  if (value) {
238  cgen_state->emitExternalCall(write_arg_name, void_type, {output, value});
239  }
240  cgen_state->ir_builder_.CreateRetVoid();
241  verify_function_ir(function);
244  module.release();
245  auto ee = CodeGenerator::generateNativeCPUCode(function, {function}, co);
246  auto func_ptr =
247  reinterpret_cast<StubGenerator::Stub>(ee->getPointerToFunction(function));
248 
249  auto cpu_compilation_context = std::make_shared<CpuCompilationContext>(std::move(ee));
250  cpu_compilation_context->setFunctionPointer(function);
252  key, cpu_compilation_context, function->getParent(), s_stubs_cache);
253  return func_ptr;
254 }
std::unique_ptr< llvm::Module > module(runtime_module_shallow_copy(cgen_state))
std::unique_ptr< llvm::Module > runtime_module_shallow_copy(CgenState *cgen_state)
#define LOG(tag)
Definition: Logger.h:188
std::vector< std::string > CodeCacheKey
Definition: CodeCache.h:25
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)
false auto cgen_state
static std::mutex s_stubs_cache_mutex
void verify_function_ir(const llvm::Function *func)
CHECK(cgen_state)
static void addCodeToCache(const CodeCacheKey &, std::shared_ptr< CompilationContext >, llvm::Module *, CodeCache &)
llvm::Type * llvm_type(const Type type, llvm::LLVMContext &ctx)
ReductionInterpreter::EvalValue(*)(void *output_handle, const void *inputs_handle) Stub
bool is_pointer_type(const Type type)
value_t * get(const key_t &key)
Definition: LruCache.hpp:39
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:

Member Data Documentation

CodeCache StubGenerator::s_stubs_cache
staticprivate

Definition at line 42 of file ResultSetReductionInterpreterStubs.h.

Referenced by clearCache(), and generateStub().

std::mutex StubGenerator::s_stubs_cache_mutex
staticprivate

Definition at line 43 of file ResultSetReductionInterpreterStubs.h.

Referenced by clearCache(), and generateStub().


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