OmniSciDB  2b310ab3b2
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ConstantIR.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::Constant* constant,
21  const EncodingType enc_type,
22  const int dict_id,
23  const CompilationOptions& co) {
25  if (co.hoist_literals) {
26  std::vector<const Analyzer::Constant*> constants(
27  executor()->deviceCount(co.device_type), constant);
28  return codegenHoistedConstants(constants, enc_type, dict_id);
29  }
30  const auto& type_info = constant->get_type_info();
31  const auto type =
32  type_info.is_decimal() ? decimal_to_int_type(type_info) : type_info.get_type();
33  switch (type) {
34  case kBOOLEAN:
35  return {llvm::ConstantInt::get(get_int_type(8, cgen_state_->context_),
36  constant->get_constval().boolval)};
37  case kTINYINT:
38  case kSMALLINT:
39  case kINT:
40  case kBIGINT:
41  case kTIME:
42  case kTIMESTAMP:
43  case kDATE:
44  case kINTERVAL_DAY_TIME:
46  return {codegenIntConst(constant)};
47  case kFLOAT:
48  return {llvm::ConstantFP::get(llvm::Type::getFloatTy(cgen_state_->context_),
49  constant->get_constval().floatval)};
50  case kDOUBLE:
51  return {llvm::ConstantFP::get(llvm::Type::getDoubleTy(cgen_state_->context_),
52  constant->get_constval().doubleval)};
53  case kVARCHAR:
54  case kCHAR:
55  case kTEXT: {
56  CHECK(constant->get_constval().stringval || constant->get_is_null());
57  if (constant->get_is_null()) {
58  if (enc_type == kENCODING_DICT) {
59  return {
60  cgen_state_->llInt(static_cast<int32_t>(inline_int_null_val(type_info)))};
61  }
62  return {cgen_state_->llInt(int64_t(0)),
63  llvm::Constant::getNullValue(
64  llvm::PointerType::get(get_int_type(8, cgen_state_->context_), 0)),
65  cgen_state_->llInt(int32_t(0))};
66  }
67  const auto& str_const = *constant->get_constval().stringval;
68  if (enc_type == kENCODING_DICT) {
69  return {
71  ->getStringDictionaryProxy(
72  dict_id, executor()->getRowSetMemoryOwner(), true)
73  ->getIdOfString(str_const))};
74  }
75  return {cgen_state_->llInt(int64_t(0)),
76  cgen_state_->addStringConstant(str_const),
77  cgen_state_->llInt(static_cast<int32_t>(str_const.size()))};
78  }
79  default:
80  CHECK(false);
81  }
82  abort();
83 }
84 
85 llvm::ConstantInt* CodeGenerator::codegenIntConst(const Analyzer::Constant* constant) {
87  const auto& type_info = constant->get_type_info();
88  if (constant->get_is_null()) {
89  return cgen_state_->inlineIntNull(type_info);
90  }
91  const auto type =
92  type_info.is_decimal() ? decimal_to_int_type(type_info) : type_info.get_type();
93  switch (type) {
94  case kTINYINT:
95  return cgen_state_->llInt(constant->get_constval().tinyintval);
96  case kSMALLINT:
97  return cgen_state_->llInt(constant->get_constval().smallintval);
98  case kINT:
99  return cgen_state_->llInt(constant->get_constval().intval);
100  case kBIGINT:
101  return cgen_state_->llInt(constant->get_constval().bigintval);
102  case kTIME:
103  case kTIMESTAMP:
104  case kDATE:
105  case kINTERVAL_DAY_TIME:
107  return cgen_state_->llInt(constant->get_constval().bigintval);
108  default:
109  abort();
110  }
111 }
112 
114  const SQLTypeInfo& type_info,
115  const EncodingType enc_type,
116  const int dict_id,
117  const int16_t lit_off) {
119  std::string literal_name = "literal_" + std::to_string(lit_off);
120  auto lit_buff_query_func_lv = get_arg_by_name(cgen_state_->query_func_, "literals");
121  const auto lit_buf_start = cgen_state_->query_func_entry_ir_builder_.CreateGEP(
122  lit_buff_query_func_lv, cgen_state_->llInt(lit_off));
123  if (type_info.is_string() && enc_type != kENCODING_DICT) {
125  CHECK_EQ(size_t(4),
127  auto off_and_len_ptr = cgen_state_->query_func_entry_ir_builder_.CreateBitCast(
128  lit_buf_start,
129  llvm::PointerType::get(get_int_type(32, cgen_state_->context_), 0));
130  // packed offset + length, 16 bits each
131  auto off_and_len =
132  cgen_state_->query_func_entry_ir_builder_.CreateLoad(off_and_len_ptr);
133  auto off_lv = cgen_state_->query_func_entry_ir_builder_.CreateLShr(
135  off_and_len, cgen_state_->llInt(int32_t(0xffff0000))),
136  cgen_state_->llInt(int32_t(16)));
137  auto len_lv = cgen_state_->query_func_entry_ir_builder_.CreateAnd(
138  off_and_len, cgen_state_->llInt(int32_t(0x0000ffff)));
139 
140  auto var_start = cgen_state_->llInt(int64_t(0));
141  auto var_start_address = cgen_state_->query_func_entry_ir_builder_.CreateGEP(
142  lit_buff_query_func_lv, off_lv);
143  auto var_length = len_lv;
144 
145  var_start->setName(literal_name + "_start");
146  var_start_address->setName(literal_name + "_start_address");
147  var_length->setName(literal_name + "_length");
148 
149  return {var_start, var_start_address, var_length};
150  } else if (type_info.is_array() &&
151  (enc_type == kENCODING_NONE || enc_type == kENCODING_GEOINT)) {
152  if (enc_type == kENCODING_NONE) {
154  } else if (enc_type == kENCODING_GEOINT) {
156  CHECK_EQ(kTINYINT, type_info.get_subtype());
157  }
158 
159  auto off_and_len_ptr = cgen_state_->query_func_entry_ir_builder_.CreateBitCast(
160  lit_buf_start,
161  llvm::PointerType::get(get_int_type(32, cgen_state_->context_), 0));
162  // packed offset + length, 16 bits each
163  auto off_and_len =
164  cgen_state_->query_func_entry_ir_builder_.CreateLoad(off_and_len_ptr);
165  auto off_lv = cgen_state_->query_func_entry_ir_builder_.CreateLShr(
167  off_and_len, cgen_state_->llInt(int32_t(0xffff0000))),
168  cgen_state_->llInt(int32_t(16)));
169  auto len_lv = cgen_state_->query_func_entry_ir_builder_.CreateAnd(
170  off_and_len, cgen_state_->llInt(int32_t(0x0000ffff)));
171 
172  auto var_start_address = cgen_state_->query_func_entry_ir_builder_.CreateGEP(
173  lit_buff_query_func_lv, off_lv);
174  auto var_length = len_lv;
175 
176  var_start_address->setName(literal_name + "_start_address");
177  var_length->setName(literal_name + "_length");
178 
179  return {var_start_address, var_length};
180  }
181 
182  llvm::Type* val_ptr_type{nullptr};
183  const auto val_bits = get_bit_width(type_info);
184  CHECK_EQ(size_t(0), val_bits % 8);
185  if (type_info.is_integer() || type_info.is_decimal() || type_info.is_time() ||
186  type_info.is_timeinterval() || type_info.is_string() || type_info.is_boolean()) {
187  val_ptr_type = llvm::PointerType::get(
188  llvm::IntegerType::get(cgen_state_->context_, val_bits), 0);
189  } else {
190  CHECK(type_info.get_type() == kFLOAT || type_info.get_type() == kDOUBLE);
191  val_ptr_type = (type_info.get_type() == kFLOAT)
192  ? llvm::Type::getFloatPtrTy(cgen_state_->context_)
193  : llvm::Type::getDoublePtrTy(cgen_state_->context_);
194  }
195  auto lit_lv = cgen_state_->query_func_entry_ir_builder_.CreateLoad(
196  cgen_state_->query_func_entry_ir_builder_.CreateBitCast(lit_buf_start,
197  val_ptr_type));
198  lit_lv->setName(literal_name);
199  return {lit_lv};
200 }
201 
203  const SQLTypeInfo& type_info,
204  const EncodingType enc_type,
205  const int16_t lit_off,
206  const std::vector<llvm::Value*>& literal_loads) {
208  std::string literal_name = "literal_" + std::to_string(lit_off);
209 
210  if (type_info.is_string() && enc_type != kENCODING_DICT) {
211  CHECK_EQ(literal_loads.size(), 3u);
212 
213  llvm::Value* var_start = literal_loads[0];
214  llvm::Value* var_start_address = literal_loads[1];
215  llvm::Value* var_length = literal_loads[2];
216 
217  llvm::PointerType* placeholder0_type =
218  llvm::PointerType::get(var_start->getType(), 0);
219  auto placeholder0 = cgen_state_->ir_builder_.CreateLoad(
220  cgen_state_->ir_builder_.CreateIntToPtr(cgen_state_->llInt(0), placeholder0_type),
221  "__placeholder__" + literal_name + "_start");
222  llvm::PointerType* placeholder1_type =
223  llvm::PointerType::get(var_start_address->getType(), 0);
224  auto placeholder1 = cgen_state_->ir_builder_.CreateLoad(
225  cgen_state_->ir_builder_.CreateIntToPtr(cgen_state_->llInt(0), placeholder1_type),
226  "__placeholder__" + literal_name + "_start_address");
227  llvm::PointerType* placeholder2_type =
228  llvm::PointerType::get(var_length->getType(), 0);
229  auto placeholder2 = cgen_state_->ir_builder_.CreateLoad(
230  cgen_state_->ir_builder_.CreateIntToPtr(cgen_state_->llInt(0), placeholder2_type),
231  "__placeholder__" + literal_name + "_length");
232 
233  cgen_state_->row_func_hoisted_literals_[placeholder0] = {lit_off, 0};
234  cgen_state_->row_func_hoisted_literals_[placeholder1] = {lit_off, 1};
235  cgen_state_->row_func_hoisted_literals_[placeholder2] = {lit_off, 2};
236 
237  return {placeholder0, placeholder1, placeholder2};
238  }
239 
240  if (type_info.is_array() &&
241  (enc_type == kENCODING_NONE || enc_type == kENCODING_GEOINT)) {
242  CHECK_EQ(literal_loads.size(), 2u);
243 
244  llvm::Value* var_start_address = literal_loads[0];
245  llvm::Value* var_length = literal_loads[1];
246 
247  llvm::PointerType* placeholder0_type =
248  llvm::PointerType::get(var_start_address->getType(), 0);
249  auto placeholder0 = cgen_state_->ir_builder_.CreateLoad(
250  cgen_state_->ir_builder_.CreateIntToPtr(cgen_state_->llInt(0), placeholder0_type),
251  "__placeholder__" + literal_name + "_start_address");
252  llvm::PointerType* placeholder1_type =
253  llvm::PointerType::get(var_length->getType(), 0);
254  auto placeholder1 = cgen_state_->ir_builder_.CreateLoad(
255  cgen_state_->ir_builder_.CreateIntToPtr(cgen_state_->llInt(0), placeholder1_type),
256  "__placeholder__" + literal_name + "_length");
257 
258  cgen_state_->row_func_hoisted_literals_[placeholder0] = {lit_off, 0};
259  cgen_state_->row_func_hoisted_literals_[placeholder1] = {lit_off, 1};
260 
261  return {placeholder0, placeholder1};
262  }
263 
264  CHECK_EQ(literal_loads.size(), 1u);
265  llvm::Value* to_return_lv = literal_loads[0];
266 
267  auto placeholder0 = cgen_state_->ir_builder_.CreateLoad(
268  cgen_state_->ir_builder_.CreateIntToPtr(
269  cgen_state_->llInt(0), llvm::PointerType::get(to_return_lv->getType(), 0)),
270  "__placeholder__" + literal_name);
271 
272  cgen_state_->row_func_hoisted_literals_[placeholder0] = {lit_off, 0};
273 
274  return {placeholder0};
275 }
276 
277 std::vector<llvm::Value*> CodeGenerator::codegenHoistedConstants(
278  const std::vector<const Analyzer::Constant*>& constants,
279  const EncodingType enc_type,
280  const int dict_id) {
282  CHECK(!constants.empty());
283 
284  const auto& type_info = constants.front()->get_type_info();
285  int16_t lit_off{-1};
286  for (size_t device_id = 0; device_id < constants.size(); ++device_id) {
287  const auto constant = constants[device_id];
288  const auto& crt_type_info = constant->get_type_info();
289  CHECK(type_info == crt_type_info);
290  const int16_t dev_lit_off =
291  cgen_state_->getOrAddLiteral(constant, enc_type, dict_id, device_id);
292  if (device_id) {
293  CHECK_EQ(lit_off, dev_lit_off);
294  } else {
295  lit_off = dev_lit_off;
296  }
297  }
298  CHECK_GE(lit_off, int16_t(0));
299 
300  std::vector<llvm::Value*> hoisted_literal_loads;
301  auto entry = cgen_state_->query_func_literal_loads_.find(lit_off);
302 
303  if (entry == cgen_state_->query_func_literal_loads_.end()) {
304  hoisted_literal_loads =
305  codegenHoistedConstantsLoads(type_info, enc_type, dict_id, lit_off);
306  cgen_state_->query_func_literal_loads_[lit_off] = hoisted_literal_loads;
307  } else {
308  hoisted_literal_loads = entry->second;
309  }
310 
311  std::vector<llvm::Value*> literal_placeholders = codegenHoistedConstantsPlaceholders(
312  type_info, enc_type, lit_off, hoisted_literal_loads);
313  return literal_placeholders;
314 }
int8_t tinyintval
Definition: sqltypes.h:135
HOST DEVICE SQLTypes get_subtype() const
Definition: sqltypes.h:260
#define CHECK_EQ(x, y)
Definition: Logger.h:205
Definition: sqltypes.h:51
CgenState * cgen_state_
llvm::Value * addStringConstant(const std::string &str)
Definition: CgenState.h:194
bool boolval
Definition: sqltypes.h:134
llvm::Function * query_func_
Definition: CgenState.h:351
std::unordered_map< llvm::Value *, HoistedLiteralLoadLocator > row_func_hoisted_literals_
Definition: CgenState.h:359
llvm::IRBuilder ir_builder_
Definition: CgenState.h:332
bool get_is_null() const
Definition: Analyzer.h:334
#define CHECK_GE(x, y)
Definition: Logger.h:210
std::unordered_map< int, std::vector< llvm::Value * > > query_func_literal_loads_
Definition: CgenState.h:353
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:259
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
int32_t intval
Definition: sqltypes.h:137
bool is_time() const
Definition: sqltypes.h:422
std::string to_string(char const *&&v)
static size_t literalBytes(const CgenState::LiteralValue &lit)
Definition: CgenState.h:361
float floatval
Definition: sqltypes.h:139
boost::variant< int8_t, int16_t, int32_t, int64_t, float, double, std::pair< std::string, int >, std::string, std::vector< double >, std::vector< int32_t >, std::vector< int8_t >, std::pair< std::vector< int8_t >, int >> LiteralValue
Definition: CgenState.h:189
EncodingType
Definition: sqltypes.h:156
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:330
size_t getOrAddLiteral(const Analyzer::Constant *constant, const EncodingType enc_type, const int dict_id, const int device_id)
Definition: CgenState.h:53
CHECK(cgen_state)
llvm::Value * get_arg_by_name(llvm::Function *func, const std::string &name)
Definition: Execute.h:95
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const int dict_id)
Definition: ConstantIR.cpp:277
bool is_integer() const
Definition: sqltypes.h:418
std::vector< llvm::Value * > codegenHoistedConstantsLoads(const SQLTypeInfo &type_info, const EncodingType enc_type, const int dict_id, const int16_t lit_off)
Definition: ConstantIR.cpp:113
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:27
int64_t bigintval
Definition: sqltypes.h:138
std::vector< llvm::Value * > codegenHoistedConstantsPlaceholders(const SQLTypeInfo &type_info, const EncodingType enc_type, const int16_t lit_off, const std::vector< llvm::Value * > &literal_loads)
Definition: ConstantIR.cpp:202
bool is_timeinterval() const
Definition: sqltypes.h:427
int16_t smallintval
Definition: sqltypes.h:136
bool is_boolean() const
Definition: sqltypes.h:423
llvm::IRBuilder query_func_entry_ir_builder_
Definition: CgenState.h:352
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:78
std::string * stringval
Definition: sqltypes.h:143
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:298
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:26
Definition: sqltypes.h:54
Definition: sqltypes.h:55
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:267
Datum get_constval() const
Definition: Analyzer.h:335
Definition: sqltypes.h:43
llvm::ConstantInt * codegenIntConst(const Analyzer::Constant *constant)
Definition: ConstantIR.cpp:85
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:303
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
Definition: sqltypes.h:47
bool is_string() const
Definition: sqltypes.h:416
bool is_decimal() const
Definition: sqltypes.h:419
bool is_array() const
Definition: sqltypes.h:424
double doubleval
Definition: sqltypes.h:140
Executor * executor() const