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