OmniSciDB  1dac507f6e
 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 244 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().

244  {
245  std::lock_guard<std::mutex> s_stubs_cache_lock(s_stubs_cache_mutex);
247 }
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 169 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(), 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().

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