OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
ScalarCodeGenerator.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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  const auto& column_key = column->getColumnKey();
28  InputColDescriptor input_desc(column_key.column_id,
29  column_key.table_id,
30  column_key.db_id,
31  column->get_rte_idx());
32  m.emplace(input_desc,
33  std::static_pointer_cast<Analyzer::ColumnVar>(column->deep_copy()));
34  return m;
35  }
36 
38  const ScalarCodeGenerator::ColumnMap& aggregate,
39  const ScalarCodeGenerator::ColumnMap& next_result) const override {
40  auto result = aggregate;
41  result.insert(next_result.begin(), next_result.end());
42  return result;
43  }
44 };
45 
46 std::vector<InputTableInfo> g_table_infos;
47 
48 llvm::Type* llvm_type_from_sql(const SQLTypeInfo& ti, llvm::LLVMContext& ctx) {
49  switch (ti.get_type()) {
50  case kINT: {
51  return get_int_type(32, ctx);
52  }
53  default: {
54  LOG(FATAL) << "Unsupported type";
55  return nullptr; // satisfy -Wreturn-type
56  }
57  }
58 }
59 
60 } // namespace
61 
63  UsedColumnExpressions visitor;
64  const auto used_columns = visitor.visit(expr);
65  std::list<std::shared_ptr<const InputColDescriptor>> global_col_ids;
66  for (const auto& used_column : used_columns) {
67  const auto& table_key = used_column.first.getScanDesc().getTableKey();
68  global_col_ids.push_back(std::make_shared<InputColDescriptor>(
69  used_column.first.getColId(),
70  table_key.table_id,
71  table_key.db_id,
72  used_column.first.getScanDesc().getNestLevel()));
73  }
74  plan_state_->allocateLocalColumnIds(global_col_ids);
75  return used_columns;
76 }
77 
79  const Analyzer::Expr* expr,
80  const bool fetch_columns,
81  const CompilationOptions& co) {
82  own_plan_state_ = std::make_unique<PlanState>(
83  false, std::vector<InputTableInfo>{}, PlanState::DeletedColumnsMap{}, nullptr);
85  const auto used_columns = prepare(expr);
86  std::vector<llvm::Type*> arg_types(plan_state_->global_to_local_col_ids_.size() + 1);
87  std::vector<std::shared_ptr<Analyzer::ColumnVar>> inputs(arg_types.size() - 1);
88  auto& ctx = module_->getContext();
89  for (const auto& kv : plan_state_->global_to_local_col_ids_) {
90  size_t arg_idx = kv.second;
91  CHECK_LT(arg_idx, arg_types.size());
92  const auto it = used_columns.find(kv.first);
93  const auto col_expr = it->second;
94  inputs[arg_idx] = col_expr;
95  const auto& ti = col_expr->get_type_info();
96  arg_types[arg_idx + 1] = llvm_type_from_sql(ti, ctx);
97  }
98  arg_types[0] =
99  llvm::PointerType::get(llvm_type_from_sql(expr->get_type_info(), ctx), 0);
100  auto ft = llvm::FunctionType::get(get_int_type(32, ctx), arg_types, false);
101  auto scalar_expr_func = llvm::Function::Create(
102  ft, llvm::Function::ExternalLinkage, "scalar_expr", module_.get());
103  auto bb_entry = llvm::BasicBlock::Create(ctx, ".entry", scalar_expr_func, 0);
104  own_cgen_state_ = std::make_unique<CgenState>(g_table_infos.size(), false);
105  own_cgen_state_->module_ = module_.get();
106  own_cgen_state_->row_func_ = own_cgen_state_->current_func_ = scalar_expr_func;
107  own_cgen_state_->ir_builder_.SetInsertPoint(bb_entry);
110  const auto expr_lvs = codegen(expr, fetch_columns, co);
111  CHECK_EQ(expr_lvs.size(), size_t(1));
112  cgen_state_->ir_builder_.CreateStore(expr_lvs.front(),
113  cgen_state_->row_func_->arg_begin());
114  cgen_state_->ir_builder_.CreateRet(ll_int<int32_t>(0, ctx));
116  std::vector<llvm::Type*> wrapper_arg_types(arg_types.size() + 1);
117  wrapper_arg_types[0] = llvm::PointerType::get(get_int_type(32, ctx), 0);
118  wrapper_arg_types[1] = arg_types[0];
119  for (size_t i = 1; i < arg_types.size(); ++i) {
120  wrapper_arg_types[i + 1] = llvm::PointerType::get(arg_types[i], 0);
121  }
122  auto wrapper_ft =
123  llvm::FunctionType::get(llvm::Type::getVoidTy(ctx), wrapper_arg_types, false);
124  auto wrapper_scalar_expr_func =
125  llvm::Function::Create(wrapper_ft,
126  llvm::Function::ExternalLinkage,
127  "wrapper_scalar_expr",
128  module_.get());
129  auto wrapper_bb_entry =
130  llvm::BasicBlock::Create(ctx, ".entry", wrapper_scalar_expr_func, 0);
131  llvm::IRBuilder<> b(ctx);
132  b.SetInsertPoint(wrapper_bb_entry);
133  std::vector<llvm::Value*> loaded_args = {wrapper_scalar_expr_func->arg_begin() + 1};
134  for (size_t i = 2; i < wrapper_arg_types.size(); ++i) {
135  auto* value = wrapper_scalar_expr_func->arg_begin() + i;
136  loaded_args.push_back(
137  b.CreateLoad(value->getType()->getPointerElementType(), value));
138  }
139  auto error_lv = b.CreateCall(scalar_expr_func, loaded_args);
140  b.CreateStore(error_lv, wrapper_scalar_expr_func->arg_begin());
141  b.CreateRetVoid();
142  return {scalar_expr_func, wrapper_scalar_expr_func, inputs};
143  }
144  return {scalar_expr_func, nullptr, inputs};
145 }
146 
148  Executor* executor,
149  const CompiledExpression& compiled_expression,
150  const CompilationOptions& co) {
151  CHECK(module_ && !execution_engine_.get()) << "Invalid code generator state";
152  module_.release();
153  switch (co.device_type) {
156  generateNativeCPUCode(compiled_expression.func, {compiled_expression.func}, co);
157  return {execution_engine_->getPointerToFunction(compiled_expression.func)};
158  }
160  return generateNativeGPUCode(
161  executor, compiled_expression.func, compiled_expression.wrapper_func, co);
162  }
163  default: {
164  LOG(FATAL) << "Invalid device type";
165  return {}; // satisfy -Wreturn-type
166  }
167  }
168 }
169 
170 std::vector<llvm::Value*> ScalarCodeGenerator::codegenColumn(
171  const Analyzer::ColumnVar* column,
172  const bool fetch_column,
173  const CompilationOptions& co) {
174  int arg_idx = plan_state_->getLocalColumnId(column, fetch_column);
175  CHECK_LT(static_cast<size_t>(arg_idx), cgen_state_->row_func_->arg_size());
176  llvm::Value* arg = cgen_state_->row_func_->arg_begin() + arg_idx + 1;
177  return {arg};
178 }
179 
181  Executor* executor,
182  llvm::Function* func,
183  llvm::Function* wrapper_func,
184  const CompilationOptions& co) {
185  if (!nvptx_target_machine_) {
188  }
189  if (!cuda_mgr_) {
190  cuda_mgr_ = std::make_unique<CudaMgr_Namespace::CudaMgr>(0);
191  }
192  GPUTarget gpu_target;
193  gpu_target.nvptx_target_machine = nvptx_target_machine_.get();
194  gpu_target.cuda_mgr = cuda_mgr_.get();
195  gpu_target.cgen_state = cgen_state_;
196  gpu_target.row_func_not_inlined = false;
199  func,
200  wrapper_func,
201  {func, wrapper_func},
202  /*is_gpu_smem_used=*/false,
203  co,
204  gpu_target);
205  return gpu_compilation_context_->getNativeFunctionPointers();
206 }
ScalarCodeGenerator::ColumnMap visitColumnVar(const Analyzer::ColumnVar *column) const override
#define CHECK_EQ(x, y)
Definition: Logger.h:301
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
llvm::Type * llvm_type_from_sql(const SQLTypeInfo &ti, llvm::LLVMContext &ctx)
std::unordered_map< shared::TableKey, const ColumnDescriptor * > DeletedColumnsMap
Definition: PlanState.h:44
std::unique_ptr< PlanState > own_plan_state_
CgenState * cgen_state_
ExecutionEngineWrapper execution_engine_
#define LOG(tag)
Definition: Logger.h:285
llvm::ExecutionEngine * get()
llvm::IRBuilder ir_builder_
Definition: CgenState.h:384
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:66
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:391
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
llvm::Function * row_func_
Definition: CgenState.h:374
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_
llvm::TargetMachine * nvptx_target_machine
int getLocalColumnId(const Analyzer::ColumnVar *col_var, const bool fetch_column)
Definition: PlanState.cpp:52
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:40
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
ExecutorDeviceType device_type
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define CHECK_LT(x, y)
Definition: Logger.h:303
const shared::ColumnKey & getColumnKey() const
Definition: Analyzer.h:198
static std::shared_ptr< GpuCompilationContext > generateNativeGPUCode(Executor *executor, llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function * > &live_funcs, const bool is_gpu_smem_used, const CompilationOptions &co, const GPUTarget &gpu_target)
ColumnMap prepare(const Analyzer::Expr *)
std::unique_ptr< CgenState > own_cgen_state_
std::vector< void * > generateNativeGPUCode(Executor *executor, llvm::Function *func, llvm::Function *wrapper_func, const CompilationOptions &co)
#define CHECK(condition)
Definition: Logger.h:291
std::unique_ptr< llvm::Module > module_
Definition: sqltypes.h:72
std::vector< void * > generateNativeCode(Executor *executor, const CompiledExpression &compiled_expression, const CompilationOptions &co)
int32_t get_rte_idx() const
Definition: Analyzer.h:202
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend(const CudaMgr_Namespace::NvidiaDeviceArch arch)