OmniSciDB  f632821e96
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::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::shared_ptr< GpuCompilationContextgpu_compilation_context_
 
std::unique_ptr< llvm::TargetMachine > nvptx_target_machine_
 

Additional Inherited Members

- Static Public Member Functions inherited from CodeGenerator
static llvm::ConstantInt * codegenIntConst (const Analyzer::Constant *constant, CgenState *cgen_state)
 
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, llvm::LLVMContext &context)
 
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend (const CudaMgr_Namespace::NvidiaDeviceArch arch)
 
static bool alwaysCloneRuntimeFunction (const llvm::Function *func)
 
static std::shared_ptr< GpuCompilationContextgenerateNativeGPUCode (llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co, const GPUTarget &gpu_target)
 
static void link_udf_module (const std::unique_ptr< llvm::Module > &udf_module, llvm::Module &module, CgenState *cgen_state, llvm::Linker::Flags flags=llvm::Linker::Flags::None)
 
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 557 of file CodeGenerator.h.

Member Typedef Documentation

◆ ColumnMap

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

Definition at line 589 of file CodeGenerator.h.

Constructor & Destructor Documentation

◆ ScalarCodeGenerator()

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

Definition at line 560 of file CodeGenerator.h.

561  : 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 162 of file ScalarCodeGenerator.cpp.

References CHECK_LT.

165  {
166  int arg_idx = plan_state_->getLocalColumnId(column, fetch_column);
167  CHECK_LT(static_cast<size_t>(arg_idx), cgen_state_->row_func_->arg_size());
168  llvm::Value* arg = cgen_state_->row_func_->arg_begin() + arg_idx + 1;
169  return {arg};
170 }
CgenState * cgen_state_
llvm::Function * row_func_
Definition: CgenState.h:331
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:207

◆ 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 AUTOMATIC_IR_METADATA, 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().

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

◆ generateNativeCode()

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

Definition at line 140 of file ScalarCodeGenerator.cpp.

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

142  {
143  CHECK(module_ && !execution_engine_.get()) << "Invalid code generator state";
144  module_.release();
145  switch (co.device_type) {
148  generateNativeCPUCode(compiled_expression.func, {compiled_expression.func}, co);
149  return {execution_engine_->getPointerToFunction(compiled_expression.func)};
150  }
152  return generateNativeGPUCode(
153  compiled_expression.func, compiled_expression.wrapper_func, co);
154  }
155  default: {
156  LOG(FATAL) << "Invalid device type";
157  return {}; // satisfy -Wreturn-type
158  }
159  }
160 }
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:188
llvm::ExecutionEngine * get()
ExecutorDeviceType device_type
std::vector< void * > generateNativeGPUCode(llvm::Function *func, llvm::Function *wrapper_func, const CompilationOptions &co)
#define CHECK(condition)
Definition: Logger.h:197
std::unique_ptr< llvm::Module > module_

◆ generateNativeGPUCode()

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

Definition at line 172 of file ScalarCodeGenerator.cpp.

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

175  {
176  if (!nvptx_target_machine_) {
179  }
180  if (!cuda_mgr_) {
181  cuda_mgr_ = std::make_unique<CudaMgr_Namespace::CudaMgr>(0);
182  }
183  const auto& dev_props = cuda_mgr_->getAllDeviceProperties();
184  int block_size = dev_props.front().maxThreadsPerBlock;
185  GPUTarget gpu_target;
186  gpu_target.nvptx_target_machine = nvptx_target_machine_.get();
187  gpu_target.cuda_mgr = cuda_mgr_.get();
188  gpu_target.block_size = block_size;
189  gpu_target.cgen_state = cgen_state_;
190  gpu_target.row_func_not_inlined = false;
192  func, wrapper_func, {func, wrapper_func}, co, gpu_target);
193  return gpu_compilation_context_->getNativeFunctionPointers();
194 }
CgenState * cgen_state_
std::unique_ptr< llvm::TargetMachine > nvptx_target_machine_
std::shared_ptr< GpuCompilationContext > gpu_compilation_context_
static std::shared_ptr< GpuCompilationContext > generateNativeGPUCode(llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co, const GPUTarget &gpu_target)
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend(const CudaMgr_Namespace::NvidiaDeviceArch arch)
+ Here is the call graph for this function:

◆ getCudaMgr()

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

Definition at line 586 of file CodeGenerator.h.

586 { return cuda_mgr_.get(); }
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_

◆ 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 608 of file CodeGenerator.h.

◆ execution_engine_

ExecutionEngineWrapper ScalarCodeGenerator::execution_engine_
private

Definition at line 605 of file CodeGenerator.h.

◆ gpu_compilation_context_

std::shared_ptr<GpuCompilationContext> ScalarCodeGenerator::gpu_compilation_context_
private

Definition at line 609 of file CodeGenerator.h.

◆ module_

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

Definition at line 604 of file CodeGenerator.h.

◆ nvptx_target_machine_

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

Definition at line 610 of file CodeGenerator.h.

◆ own_cgen_state_

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

Definition at line 606 of file CodeGenerator.h.

◆ own_plan_state_

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

Definition at line 607 of file CodeGenerator.h.


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