OmniSciDB  fe05a0c208
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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 = llvm::BasicBlock::Create(
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(cgen_state_->context_,
75  "then_case",
77  /*insert_before=*/end_bb);
78  cgen_state_->ir_builder_.SetInsertPoint(then_bb);
79  auto then_bb_lvs = codegen(expr_pair.second.get(), true, co);
80  if (is_real_str) {
81  if (then_bb_lvs.size() == 3) {
82  then_lvs.push_back(
83  cgen_state_->emitCall("string_pack", {then_bb_lvs[1], then_bb_lvs[2]}));
84  } else {
85  then_lvs.push_back(then_bb_lvs.front());
86  }
87  } else {
88  CHECK_EQ(size_t(1), then_bb_lvs.size());
89  then_lvs.push_back(then_bb_lvs.front());
90  }
91  then_bbs.push_back(cgen_state_->ir_builder_.GetInsertBlock());
92  cgen_state_->ir_builder_.CreateBr(end_bb);
93  const auto when_bb = llvm::BasicBlock::Create(
95  cgen_state_->ir_builder_.SetInsertPoint(cmp_bb);
96  cgen_state_->ir_builder_.CreateCondBr(when_lv, then_bb, when_bb);
97  cgen_state_->ir_builder_.SetInsertPoint(when_bb);
98  }
99  const auto else_expr = case_expr->get_else_expr();
100  CHECK(else_expr);
101  auto else_lvs = codegen(else_expr, true, co);
102  llvm::Value* else_lv{nullptr};
103  if (else_lvs.size() == 3) {
104  else_lv = cgen_state_->emitCall("string_pack", {else_lvs[1], else_lvs[2]});
105  } else {
106  else_lv = else_lvs.front();
107  }
108  CHECK(else_lv);
109  auto else_bb = cgen_state_->ir_builder_.GetInsertBlock();
110  cgen_state_->ir_builder_.CreateBr(end_bb);
111  cgen_state_->ir_builder_.SetInsertPoint(end_bb);
112  auto then_phi =
113  cgen_state_->ir_builder_.CreatePHI(case_llvm_type, expr_pair_list.size() + 1);
114  CHECK_EQ(then_bbs.size(), then_lvs.size());
115  for (size_t i = 0; i < then_bbs.size(); ++i) {
116  then_phi->addIncoming(then_lvs[i], then_bbs[i]);
117  }
118  then_phi->addIncoming(else_lv, else_bb);
119  return then_phi;
120 }
#define CHECK_EQ(x, y)
Definition: Logger.h:211
const Expr * get_else_expr() const
Definition: Analyzer.h:1152
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:335
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:333
llvm::Function * current_func_
Definition: CgenState.h:327
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:174
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:28
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:343
#define CHECK(condition)
Definition: Logger.h:203
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1149