OmniSciDB  c07336695a
ScalarCodeGenerator Class Reference

#include <CodeGenerator.h>

+ Inheritance diagram for ScalarCodeGenerator:
+ Collaboration diagram for ScalarCodeGenerator:

Classes

struct  CompiledExpression
 

Public Types

using ColumnMap = std::unordered_map< InputColDescriptor, std::shared_ptr< Analyzer::ColumnVar > >
 

Public Member Functions

 ScalarCodeGenerator (std::unique_ptr< llvm::Module > module)
 
CompiledExpression compile (const Analyzer::Expr *expr, const bool fetch_columns, const CompilationOptions &co)
 
std::vector< void * > generateNativeCode (const CompiledExpression &compiled_expression, const CompilationOptions &co)
 
CudaMgr_Namespace::CudaMgrgetCudaMgr () const
 
- Public Member Functions inherited from CodeGenerator
 CodeGenerator (Executor *executor)
 
 CodeGenerator (CgenState *cgen_state, PlanState *plan_state)
 
std::vector< llvm::Value * > codegen (const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenHoistedConstants (const std::vector< const Analyzer::Constant *> &constants, const EncodingType enc_type, const int dict_id)
 
llvm::ConstantInt * codegenIntConst (const Analyzer::Constant *constant)
 
llvm::Value * codegenCastBetweenIntTypes (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
 
void codegenCastBetweenIntTypesOverflowChecks (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
 
llvm::Value * posArg (const Analyzer::Expr *) const
 
llvm::Value * toBool (llvm::Value *)
 
llvm::Value * castArrayPointer (llvm::Value *ptr, const SQLTypeInfo &elem_ti)
 

Private Member Functions

std::vector< llvm::Value * > codegenColumn (const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &) override
 
ColumnMap prepare (const Analyzer::Expr *)
 
std::vector< void * > generateNativeGPUCode (llvm::Function *func, llvm::Function *wrapper_func, const CompilationOptions &co)
 

Private Attributes

std::unique_ptr< llvm::Module > module_
 
ExecutionEngineWrapper execution_engine_
 
std::unique_ptr< CgenStateown_cgen_state_
 
std::unique_ptr< PlanStateown_plan_state_
 
std::unique_ptr< CudaMgr_Namespace::CudaMgrcuda_mgr_
 
std::vector< std::unique_ptr< GpuCompilationContext > > gpu_compilation_contexts_
 
std::unique_ptr< llvm::TargetMachine > nvptx_target_machine_
 

Additional Inherited Members

- Static Public Member Functions inherited from CodeGenerator
static std::unordered_set< llvm::Function * > markDeadRuntimeFuncs (llvm::Module &module, const std::vector< llvm::Function *> &roots, const std::vector< llvm::Function *> &leaves)
 
static ExecutionEngineWrapper generateNativeCPUCode (llvm::Function *func, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co)
 
static std::string generatePTX (const std::string &cuda_llir, llvm::TargetMachine *nvptx_target_machine, CgenState *cgen_state)
 
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend ()
 
static GPUCode generateNativeGPUCode (llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co, const GPUTarget &gpu_target)
 
static bool prioritizeQuals (const RelAlgExecutionUnit &ra_exe_unit, std::vector< Analyzer::Expr *> &primary_quals, std::vector< Analyzer::Expr *> &deferred_quals)
 
- Protected Member Functions inherited from CodeGenerator
Executorexecutor () const
 
- Protected Attributes inherited from CodeGenerator
CgenStatecgen_state_
 
PlanStateplan_state_
 

Detailed Description

Definition at line 445 of file CodeGenerator.h.

Member Typedef Documentation

◆ ColumnMap

using ScalarCodeGenerator::ColumnMap = std::unordered_map<InputColDescriptor, std::shared_ptr<Analyzer::ColumnVar> >

Definition at line 477 of file CodeGenerator.h.

Constructor & Destructor Documentation

◆ ScalarCodeGenerator()

ScalarCodeGenerator::ScalarCodeGenerator ( std::unique_ptr< llvm::Module >  module)
inline

Definition at line 448 of file CodeGenerator.h.

449  : CodeGenerator(nullptr, nullptr), module_(std::move(module)) {}
CodeGenerator(Executor *executor)
Definition: CodeGenerator.h:27
std::unique_ptr< llvm::Module > module_

Member Function Documentation

◆ codegenColumn()

std::vector< llvm::Value * > ScalarCodeGenerator::codegenColumn ( const Analyzer::ColumnVar column,
const bool  fetch_column,
const CompilationOptions co 
)
overrideprivatevirtual

Reimplemented from CodeGenerator.

Definition at line 160 of file ScalarCodeGenerator.cpp.

References CHECK_LT.

163  {
164  int arg_idx = plan_state_->getLocalColumnId(column, fetch_column);
165  CHECK_LT(static_cast<size_t>(arg_idx), cgen_state_->row_func_->arg_size());
166  llvm::Value* arg = cgen_state_->row_func_->arg_begin() + arg_idx + 1;
167  return {arg};
168 }
CgenState * cgen_state_
llvm::Function * row_func_
Definition: CgenState.h:264
int getLocalColumnId(const Analyzer::ColumnVar *col_var, const bool fetch_column)
Definition: PlanState.cpp:62
PlanState * plan_state_
#define CHECK_LT(x, y)
Definition: Logger.h:197

◆ compile()

ScalarCodeGenerator::CompiledExpression ScalarCodeGenerator::compile ( const Analyzer::Expr expr,
const bool  fetch_columns,
const CompilationOptions co 
)

Definition at line 73 of file ScalarCodeGenerator.cpp.

References CHECK_EQ, CHECK_LT, CompilationOptions::device_type_, anonymous_namespace{ScalarCodeGenerator.cpp}::g_table_infos, get_int_type(), Analyzer::Expr::get_type_info(), GPU, and anonymous_namespace{ScalarCodeGenerator.cpp}::llvm_type_from_sql().

Referenced by TEST().

76  {
77  own_plan_state_ = std::make_unique<PlanState>(false, nullptr);
79  const auto used_columns = prepare(expr);
80  std::vector<llvm::Type*> arg_types(plan_state_->global_to_local_col_ids_.size() + 1);
81  std::vector<std::shared_ptr<Analyzer::ColumnVar>> inputs(arg_types.size() - 1);
82  auto& ctx = module_->getContext();
83  for (const auto& kv : plan_state_->global_to_local_col_ids_) {
84  size_t arg_idx = kv.second;
85  CHECK_LT(arg_idx, arg_types.size());
86  const auto it = used_columns.find(kv.first);
87  const auto col_expr = it->second;
88  inputs[arg_idx] = col_expr;
89  const auto& ti = col_expr->get_type_info();
90  arg_types[arg_idx + 1] = llvm_type_from_sql(ti, ctx);
91  }
92  arg_types[0] =
93  llvm::PointerType::get(llvm_type_from_sql(expr->get_type_info(), ctx), 0);
94  auto ft = llvm::FunctionType::get(get_int_type(32, ctx), arg_types, false);
95  auto scalar_expr_func = llvm::Function::Create(
96  ft, llvm::Function::ExternalLinkage, "scalar_expr", module_.get());
97  auto bb_entry = llvm::BasicBlock::Create(ctx, ".entry", scalar_expr_func, 0);
98  own_cgen_state_ = std::make_unique<CgenState>(g_table_infos, false);
99  own_cgen_state_->module_ = module_.get();
100  own_cgen_state_->row_func_ = scalar_expr_func;
101  own_cgen_state_->ir_builder_.SetInsertPoint(bb_entry);
103  const auto expr_lvs = codegen(expr, fetch_columns, co);
104  CHECK_EQ(expr_lvs.size(), size_t(1));
105  cgen_state_->ir_builder_.CreateStore(expr_lvs.front(),
106  cgen_state_->row_func_->arg_begin());
107  cgen_state_->ir_builder_.CreateRet(ll_int<int32_t>(0, ctx));
109  std::vector<llvm::Type*> wrapper_arg_types(arg_types.size() + 1);
110  wrapper_arg_types[0] = llvm::PointerType::get(get_int_type(32, ctx), 0);
111  wrapper_arg_types[1] = arg_types[0];
112  for (size_t i = 1; i < arg_types.size(); ++i) {
113  wrapper_arg_types[i + 1] = llvm::PointerType::get(arg_types[i], 0);
114  }
115  auto wrapper_ft =
116  llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), wrapper_arg_types, false);
117  auto wrapper_scalar_expr_func =
118  llvm::Function::Create(wrapper_ft,
119  llvm::Function::ExternalLinkage,
120  "wrapper_scalar_expr",
121  module_.get());
122  auto wrapper_bb_entry =
123  llvm::BasicBlock::Create(ctx, ".entry", wrapper_scalar_expr_func, 0);
124  llvm::IRBuilder<> b(ctx);
125  b.SetInsertPoint(wrapper_bb_entry);
126  std::vector<llvm::Value*> loaded_args = {wrapper_scalar_expr_func->arg_begin() + 1};
127  for (size_t i = 2; i < wrapper_arg_types.size(); ++i) {
128  loaded_args.push_back(b.CreateLoad(wrapper_scalar_expr_func->arg_begin() + i));
129  }
130  auto error_lv = b.CreateCall(scalar_expr_func, loaded_args);
131  b.CreateStore(error_lv, wrapper_scalar_expr_func->arg_begin());
132  b.CreateRetVoid();
133  return {scalar_expr_func, wrapper_scalar_expr_func, inputs};
134  }
135  return {scalar_expr_func, nullptr, inputs};
136 }
#define CHECK_EQ(x, y)
Definition: Logger.h:195
llvm::Type * llvm_type_from_sql(const SQLTypeInfo &ti, llvm::LLVMContext &ctx)
std::unique_ptr< PlanState > own_plan_state_
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:268
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::Function * row_func_
Definition: CgenState.h:264
ExecutorDeviceType device_type_
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:25
#define CHECK_LT(x, y)
Definition: Logger.h:197
ColumnMap prepare(const Analyzer::Expr *)
std::unique_ptr< CgenState > own_cgen_state_
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
std::unique_ptr< llvm::Module > module_
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ generateNativeCode()

std::vector< void * > ScalarCodeGenerator::generateNativeCode ( const CompiledExpression compiled_expression,
const CompilationOptions co 
)

Definition at line 138 of file ScalarCodeGenerator.cpp.

References CHECK, CPU, CompilationOptions::device_type_, logger::FATAL, ScalarCodeGenerator::CompiledExpression::func, GPU, LOG, and ScalarCodeGenerator::CompiledExpression::wrapper_func.

Referenced by TEST().

140  {
141  CHECK(module_ && !execution_engine_.get()) << "Invalid code generator state";
142  module_.release();
143  switch (co.device_type_) {
146  generateNativeCPUCode(compiled_expression.func, {compiled_expression.func}, co);
147  return {execution_engine_->getPointerToFunction(compiled_expression.func)};
148  }
150  return generateNativeGPUCode(
151  compiled_expression.func, compiled_expression.wrapper_func, co);
152  }
153  default: {
154  LOG(FATAL) << "Invalid device type";
155  return {}; // satisfy -Wreturn-type
156  }
157  }
158 }
static ExecutionEngineWrapper generateNativeCPUCode(llvm::Function *func, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co)
ExecutionEngineWrapper execution_engine_
#define LOG(tag)
Definition: Logger.h:182
llvm::ExecutionEngine * get()
Definition: CodeCache.h:45
ExecutorDeviceType device_type_
std::vector< void * > generateNativeGPUCode(llvm::Function *func, llvm::Function *wrapper_func, const CompilationOptions &co)
#define CHECK(condition)
Definition: Logger.h:187
std::unique_ptr< llvm::Module > module_
+ Here is the caller graph for this function:

◆ generateNativeGPUCode()

std::vector< void * > ScalarCodeGenerator::generateNativeGPUCode ( llvm::Function *  func,
llvm::Function *  wrapper_func,
const CompilationOptions co 
)
private

Definition at line 170 of file ScalarCodeGenerator.cpp.

References CodeGenerator::GPUTarget::block_size, CodeGenerator::GPUTarget::cgen_state, CodeGenerator::GPUTarget::cuda_mgr, CodeGenerator::generateNativeGPUCode(), CodeGenerator::GPUTarget::nvptx_target_machine, and CodeGenerator::GPUTarget::row_func_not_inlined.

173  {
174  if (!nvptx_target_machine_) {
176  }
177  if (!cuda_mgr_) {
178  cuda_mgr_ = std::make_unique<CudaMgr_Namespace::CudaMgr>(0);
179  }
180  const auto& dev_props = cuda_mgr_->getAllDeviceProperties();
181  int block_size = dev_props.front().maxThreadsPerBlock;
182  GPUTarget gpu_target;
183  gpu_target.nvptx_target_machine = nvptx_target_machine_.get();
184  gpu_target.cuda_mgr = cuda_mgr_.get();
185  gpu_target.block_size = block_size;
186  gpu_target.cgen_state = cgen_state_;
187  gpu_target.row_func_not_inlined = false;
188  const auto gpu_code = CodeGenerator::generateNativeGPUCode(
189  func, wrapper_func, {func, wrapper_func}, co, gpu_target);
190  for (const auto& cached_function : gpu_code.cached_functions) {
191  gpu_compilation_contexts_.emplace_back(std::get<1>(cached_function));
192  }
193  std::vector<void*> native_function_pointers;
194  for (const auto& cached_function : gpu_code.cached_functions) {
195  native_function_pointers.push_back(std::get<0>(cached_function));
196  }
197  return native_function_pointers;
198 }
CgenState * cgen_state_
std::unique_ptr< llvm::TargetMachine > nvptx_target_machine_
std::vector< std::unique_ptr< GpuCompilationContext > > gpu_compilation_contexts_
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_
static GPUCode generateNativeGPUCode(llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co, const GPUTarget &gpu_target)
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend()
+ Here is the call graph for this function:

◆ getCudaMgr()

CudaMgr_Namespace::CudaMgr* ScalarCodeGenerator::getCudaMgr ( ) const
inline

Definition at line 474 of file CodeGenerator.h.

Referenced by TEST().

474 { return cuda_mgr_.get(); }
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_
+ Here is the caller graph for this function:

◆ prepare()

ScalarCodeGenerator::ColumnMap ScalarCodeGenerator::prepare ( const Analyzer::Expr expr)
private

Definition at line 59 of file ScalarCodeGenerator.cpp.

59  {
60  UsedColumnExpressions visitor;
61  const auto used_columns = visitor.visit(expr);
62  std::list<std::shared_ptr<const InputColDescriptor>> global_col_ids;
63  for (const auto& used_column : used_columns) {
64  global_col_ids.push_back(std::make_shared<InputColDescriptor>(
65  used_column.first.getColId(),
66  used_column.first.getScanDesc().getTableId(),
67  used_column.first.getScanDesc().getNestLevel()));
68  }
69  plan_state_->allocateLocalColumnIds(global_col_ids);
70  return used_columns;
71 }
void allocateLocalColumnIds(const std::list< std::shared_ptr< const InputColDescriptor >> &global_col_ids)
Definition: PlanState.cpp:50
PlanState * plan_state_

Member Data Documentation

◆ cuda_mgr_

std::unique_ptr<CudaMgr_Namespace::CudaMgr> ScalarCodeGenerator::cuda_mgr_
private

Definition at line 496 of file CodeGenerator.h.

◆ execution_engine_

ExecutionEngineWrapper ScalarCodeGenerator::execution_engine_
private

Definition at line 493 of file CodeGenerator.h.

◆ gpu_compilation_contexts_

std::vector<std::unique_ptr<GpuCompilationContext> > ScalarCodeGenerator::gpu_compilation_contexts_
private

Definition at line 497 of file CodeGenerator.h.

◆ module_

std::unique_ptr<llvm::Module> ScalarCodeGenerator::module_
private

Definition at line 492 of file CodeGenerator.h.

◆ nvptx_target_machine_

std::unique_ptr<llvm::TargetMachine> ScalarCodeGenerator::nvptx_target_machine_
private

Definition at line 498 of file CodeGenerator.h.

◆ own_cgen_state_

std::unique_ptr<CgenState> ScalarCodeGenerator::own_cgen_state_
private

Definition at line 494 of file CodeGenerator.h.

◆ own_plan_state_

std::unique_ptr<PlanState> ScalarCodeGenerator::own_plan_state_
private

Definition at line 495 of file CodeGenerator.h.


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