OmniSciDB  c1a53651b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CaseIR.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 "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 = createStringViewStructType();
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_->ir_builder_.CreateExtractValue(case_val, 0));
50  ret_vals.push_back(cgen_state_->ir_builder_.CreateExtractValue(case_val, 1));
51  ret_vals.back() = cgen_state_->ir_builder_.CreateTrunc(
52  ret_vals.back(), llvm::Type::getInt32Ty(cgen_state_->context_));
53  }
54  return ret_vals;
55 }
56 
57 llvm::Value* CodeGenerator::codegenCase(const Analyzer::CaseExpr* case_expr,
58  llvm::Type* case_llvm_type,
59  const bool is_real_str,
60  const CompilationOptions& co) {
62  // Here the linear control flow will diverge and expressions cached during the
63  // code branch code generation (currently just column decoding) are not going
64  // to be available once we're done generating the case. Take a snapshot of
65  // the cache with FetchCacheAnchor and restore it once we're done with CASE.
67  const auto& expr_pair_list = case_expr->get_expr_pair_list();
68  std::vector<llvm::Value*> then_lvs;
69  std::vector<llvm::BasicBlock*> then_bbs;
70  const auto end_bb = llvm::BasicBlock::Create(
72  for (const auto& expr_pair : expr_pair_list) {
74  const auto when_lv = toBool(codegen(expr_pair.first.get(), true, co).front());
75  const auto cmp_bb = cgen_state_->ir_builder_.GetInsertBlock();
76  const auto then_bb = llvm::BasicBlock::Create(cgen_state_->context_,
77  "then_case",
79  /*insert_before=*/end_bb);
80  cgen_state_->ir_builder_.SetInsertPoint(then_bb);
81  auto then_bb_lvs = codegen(expr_pair.second.get(), true, co);
82  if (is_real_str) {
83  if (then_bb_lvs.size() == 3) {
84  then_lvs.push_back(
85  cgen_state_->emitCall("string_pack", {then_bb_lvs[1], then_bb_lvs[2]}));
86  } else {
87  then_lvs.push_back(then_bb_lvs.front());
88  }
89  } else {
90  CHECK_EQ(size_t(1), then_bb_lvs.size());
91  then_lvs.push_back(then_bb_lvs.front());
92  }
93  then_bbs.push_back(cgen_state_->ir_builder_.GetInsertBlock());
94  cgen_state_->ir_builder_.CreateBr(end_bb);
95  const auto when_bb = llvm::BasicBlock::Create(
97  cgen_state_->ir_builder_.SetInsertPoint(cmp_bb);
98  cgen_state_->ir_builder_.CreateCondBr(when_lv, then_bb, when_bb);
99  cgen_state_->ir_builder_.SetInsertPoint(when_bb);
100  }
101  const auto else_expr = case_expr->get_else_expr();
102  CHECK(else_expr);
103  auto else_lvs = codegen(else_expr, true, co);
104  llvm::Value* else_lv{nullptr};
105  if (else_lvs.size() == 3) {
106  else_lv = cgen_state_->emitCall("string_pack", {else_lvs[1], else_lvs[2]});
107  } else {
108  else_lv = else_lvs.front();
109  }
110  CHECK(else_lv);
111  auto else_bb = cgen_state_->ir_builder_.GetInsertBlock();
112  cgen_state_->ir_builder_.CreateBr(end_bb);
113  cgen_state_->ir_builder_.SetInsertPoint(end_bb);
114  auto then_phi =
115  cgen_state_->ir_builder_.CreatePHI(case_llvm_type, expr_pair_list.size() + 1);
116  CHECK_EQ(then_bbs.size(), then_lvs.size());
117  for (size_t i = 0; i < then_bbs.size(); ++i) {
118  then_phi->addIncoming(then_lvs[i], then_bbs[i]);
119  }
120  then_phi->addIncoming(else_lv, else_bb);
121  return then_phi;
122 }
#define CHECK_EQ(x, y)
Definition: Logger.h:301
const Expr * get_else_expr() const
Definition: Analyzer.h:1265
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:377
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:375
llvm::Function * current_func_
Definition: CgenState.h:369
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:79
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:216
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
llvm::StructType * createStringViewStructType()
llvm::Value * codegenCase(const Analyzer::CaseExpr *, llvm::Type *case_llvm_type, const bool is_real_str, const CompilationOptions &)
Definition: CaseIR.cpp:57
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
#define CHECK(condition)
Definition: Logger.h:291
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1262