OmniSciDB  8a228a1076
CaseIR.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2017 MapD Technologies, 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 "Execute.h"
19 
20 std::vector<llvm::Value*> CodeGenerator::codegen(const Analyzer::CaseExpr* case_expr,
21  const CompilationOptions& co) {
23  const auto case_ti = case_expr->get_type_info();
24  llvm::Type* case_llvm_type = nullptr;
25  bool is_real_str = false;
26  if (case_ti.is_integer() || case_ti.is_time() || case_ti.is_decimal()) {
27  case_llvm_type = get_int_type(get_bit_width(case_ti), cgen_state_->context_);
28  } else if (case_ti.is_fp()) {
29  case_llvm_type = case_ti.get_type() == kFLOAT
30  ? llvm::Type::getFloatTy(cgen_state_->context_)
31  : llvm::Type::getDoubleTy(cgen_state_->context_);
32  } else if (case_ti.is_string()) {
33  if (case_ti.get_compression() == kENCODING_DICT) {
34  case_llvm_type =
35  get_int_type(8 * case_ti.get_logical_size(), cgen_state_->context_);
36  } else {
37  is_real_str = true;
38  case_llvm_type = get_int_type(64, cgen_state_->context_);
39  }
40  } else if (case_ti.is_boolean()) {
41  case_llvm_type = get_int_type(8 * case_ti.get_logical_size(), cgen_state_->context_);
42  }
43  CHECK(case_llvm_type);
44  const auto& else_ti = case_expr->get_else_expr()->get_type_info();
45  CHECK_EQ(else_ti.get_type(), case_ti.get_type());
46  llvm::Value* case_val = codegenCase(case_expr, case_llvm_type, is_real_str, co);
47  std::vector<llvm::Value*> ret_vals{case_val};
48  if (is_real_str) {
49  ret_vals.push_back(cgen_state_->emitCall("extract_str_ptr", {case_val}));
50  ret_vals.push_back(cgen_state_->emitCall("extract_str_len", {case_val}));
51  }
52  return ret_vals;
53 }
54 
55 llvm::Value* CodeGenerator::codegenCase(const Analyzer::CaseExpr* case_expr,
56  llvm::Type* case_llvm_type,
57  const bool is_real_str,
58  const CompilationOptions& co) {
60  // Here the linear control flow will diverge and expressions cached during the
61  // code branch code generation (currently just column decoding) are not going
62  // to be available once we're done generating the case. Take a snapshot of
63  // the cache with FetchCacheAnchor and restore it once we're done with CASE.
65  const auto& expr_pair_list = case_expr->get_expr_pair_list();
66  std::vector<llvm::Value*> then_lvs;
67  std::vector<llvm::BasicBlock*> then_bbs;
68  const auto end_bb =
69  llvm::BasicBlock::Create(cgen_state_->context_, "end_case", cgen_state_->row_func_);
70  for (const auto& expr_pair : expr_pair_list) {
72  const auto when_lv = toBool(codegen(expr_pair.first.get(), true, co).front());
73  const auto cmp_bb = cgen_state_->ir_builder_.GetInsertBlock();
74  const auto then_bb = llvm::BasicBlock::Create(
75  cgen_state_->context_, "then_case", cgen_state_->row_func_);
76  cgen_state_->ir_builder_.SetInsertPoint(then_bb);
77  auto then_bb_lvs = codegen(expr_pair.second.get(), true, co);
78  if (is_real_str) {
79  if (then_bb_lvs.size() == 3) {
80  then_lvs.push_back(
81  cgen_state_->emitCall("string_pack", {then_bb_lvs[1], then_bb_lvs[2]}));
82  } else {
83  then_lvs.push_back(then_bb_lvs.front());
84  }
85  } else {
86  CHECK_EQ(size_t(1), then_bb_lvs.size());
87  then_lvs.push_back(then_bb_lvs.front());
88  }
89  then_bbs.push_back(cgen_state_->ir_builder_.GetInsertBlock());
90  cgen_state_->ir_builder_.CreateBr(end_bb);
91  const auto when_bb = llvm::BasicBlock::Create(
92  cgen_state_->context_, "when_case", cgen_state_->row_func_);
93  cgen_state_->ir_builder_.SetInsertPoint(cmp_bb);
94  cgen_state_->ir_builder_.CreateCondBr(when_lv, then_bb, when_bb);
95  cgen_state_->ir_builder_.SetInsertPoint(when_bb);
96  }
97  const auto else_expr = case_expr->get_else_expr();
98  CHECK(else_expr);
99  auto else_lvs = codegen(else_expr, true, co);
100  llvm::Value* else_lv{nullptr};
101  if (else_lvs.size() == 3) {
102  else_lv = cgen_state_->emitCall("string_pack", {else_lvs[1], else_lvs[2]});
103  } else {
104  else_lv = else_lvs.front();
105  }
106  CHECK(else_lv);
107  auto else_bb = cgen_state_->ir_builder_.GetInsertBlock();
108  cgen_state_->ir_builder_.CreateBr(end_bb);
109  cgen_state_->ir_builder_.SetInsertPoint(end_bb);
110  auto then_phi =
111  cgen_state_->ir_builder_.CreatePHI(case_llvm_type, expr_pair_list.size() + 1);
112  CHECK_EQ(then_bbs.size(), then_lvs.size());
113  for (size_t i = 0; i < then_bbs.size(); ++i) {
114  then_phi->addIncoming(then_lvs[i], then_bbs[i]);
115  }
116  then_phi->addIncoming(else_lv, else_bb);
117  return then_phi;
118 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:319
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::Function * row_func_
Definition: CgenState.h:315
const Expr * get_else_expr() const
Definition: Analyzer.h:1153
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:317
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1150
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:26
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value *> &args)
Definition: CgenState.cpp:137
llvm::Value * codegenCase(const Analyzer::CaseExpr *, llvm::Type *case_llvm_type, const bool is_real_str, const CompilationOptions &)
Definition: CaseIR.cpp:55
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:335
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
#define CHECK(condition)
Definition: Logger.h:197