OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ScalarCodeGenerator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "CodeGenerator.h"
18 #include "ScalarExprVisitor.h"
19 
20 namespace {
21 
22 class UsedColumnExpressions : public ScalarExprVisitor<ScalarCodeGenerator::ColumnMap> {
23  protected:
25  const Analyzer::ColumnVar* column) const override {
27  InputColDescriptor input_desc(
28  column->get_column_id(), column->get_table_id(), column->get_rte_idx());
29  m.emplace(input_desc,
30  std::static_pointer_cast<Analyzer::ColumnVar>(column->deep_copy()));
31  return m;
32  }
33 
35  const ScalarCodeGenerator::ColumnMap& aggregate,
36  const ScalarCodeGenerator::ColumnMap& next_result) const override {
37  auto result = aggregate;
38  result.insert(next_result.begin(), next_result.end());
39  return result;
40  }
41 };
42 
43 std::vector<InputTableInfo> g_table_infos;
44 
45 llvm::Type* llvm_type_from_sql(const SQLTypeInfo& ti, llvm::LLVMContext& ctx) {
46  switch (ti.get_type()) {
47  case kINT: {
48  return get_int_type(32, ctx);
49  }
50  default: {
51  LOG(FATAL) << "Unsupported type";
52  return nullptr; // satisfy -Wreturn-type
53  }
54  }
55 }
56 
57 } // namespace
58 
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 }
72 
74  const Analyzer::Expr* expr,
75  const bool fetch_columns,
76  const CompilationOptions& co) {
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 }
139 
141  const CompiledExpression& compiled_expression,
142  const CompilationOptions& co) {
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 }
161 
162 std::vector<llvm::Value*> ScalarCodeGenerator::codegenColumn(
163  const Analyzer::ColumnVar* column,
164  const bool fetch_column,
165  const CompilationOptions& co) {
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 }
171 
173  llvm::Function* func,
174  llvm::Function* wrapper_func,
175  const CompilationOptions& co) {
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 }
ScalarCodeGenerator::ColumnMap visitColumnVar(const Analyzer::ColumnVar *column) const override
int get_table_id() const
Definition: Analyzer.h:194
#define CHECK_EQ(x, y)
Definition: Logger.h:205
CompiledExpression compile(const Analyzer::Expr *expr, const bool fetch_columns, const CompilationOptions &co)
std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &) override
std::vector< void * > generateNativeCode(const CompiledExpression &compiled_expression, const CompilationOptions &co)
llvm::Type * llvm_type_from_sql(const SQLTypeInfo &ti, llvm::LLVMContext &ctx)
std::unique_ptr< PlanState > own_plan_state_
CgenState * cgen_state_
ExecutionEngineWrapper execution_engine_
#define LOG(tag)
Definition: Logger.h:188
llvm::ExecutionEngine * get()
llvm::IRBuilder ir_builder_
Definition: CgenState.h:329
std::unique_ptr< llvm::TargetMachine > nvptx_target_machine_
std::shared_ptr< GpuCompilationContext > gpu_compilation_context_
std::shared_ptr< Analyzer::Expr > deep_copy() const override
Definition: Analyzer.cpp:57
static ExecutionEngineWrapper generateNativeCPUCode(llvm::Function *func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:330
std::unordered_map< InputColDescriptor, std::shared_ptr< Analyzer::ColumnVar >> ColumnMap
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
const CudaMgr_Namespace::CudaMgr * cuda_mgr
Definition: CodeGenerator.h:89
llvm::Function * row_func_
Definition: CgenState.h:319
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_
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)
llvm::TargetMachine * nvptx_target_machine
Definition: CodeGenerator.h:88
int getLocalColumnId(const Analyzer::ColumnVar *col_var, const bool fetch_column)
Definition: PlanState.cpp:62
std::unordered_map< TableId, const ColumnDescriptor * > DeletedColumnsMap
Definition: PlanState.h:44
ScalarCodeGenerator::ColumnMap aggregateResult(const ScalarCodeGenerator::ColumnMap &aggregate, const ScalarCodeGenerator::ColumnMap &next_result) const override
void allocateLocalColumnIds(const std::list< std::shared_ptr< const InputColDescriptor >> &global_col_ids)
Definition: PlanState.cpp:50
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
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
int get_rte_idx() const
Definition: Analyzer.h:196
ColumnMap prepare(const Analyzer::Expr *)
std::unique_ptr< CgenState > own_cgen_state_
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_
Definition: sqltypes.h:47
int get_column_id() const
Definition: Analyzer.h:195
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend(const CudaMgr_Namespace::NvidiaDeviceArch arch)