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