OmniSciDB  340b00dbf6
 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 257 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().

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