OmniSciDB  340b00dbf6
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CgenState.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, 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 #pragma once
18 
19 #include "IRCodegenUtils.h"
20 #include "InValuesBitmap.h"
21 #include "InputMetadata.h"
22 #include "LLVMGlobalContext.h"
23 
24 #include "../Analyzer/Analyzer.h"
25 #include "../Shared/InsertionOrderedMap.h"
26 
27 #include <llvm/IR/Constants.h>
28 #include <llvm/IR/IRBuilder.h>
29 #include <llvm/Transforms/Utils/ValueMapper.h>
30 
31 struct CgenState {
32  public:
33  CgenState(const size_t num_query_infos, const bool contains_left_deep_outer_join)
34  : module_(nullptr)
35  , row_func_(nullptr)
36  , filter_func_(nullptr)
37  , current_func_(nullptr)
38  , row_func_bb_(nullptr)
39  , filter_func_bb_(nullptr)
40  , row_func_call_(nullptr)
41  , filter_func_call_(nullptr)
44  , contains_left_deep_outer_join_(contains_left_deep_outer_join)
45  , outer_join_match_found_per_level_(std::max(num_query_infos, size_t(1)) - 1)
48  , query_func_(nullptr)
50 
51  CgenState(llvm::LLVMContext& context)
52  : module_(nullptr)
53  , row_func_(nullptr)
54  , context_(context)
59  , query_func_(nullptr)
61 
62  size_t getOrAddLiteral(const Analyzer::Constant* constant,
63  const EncodingType enc_type,
64  const int dict_id,
65  const int device_id) {
66  const auto& ti = constant->get_type_info();
67  const auto type = ti.is_decimal() ? decimal_to_int_type(ti) : ti.get_type();
68  switch (type) {
69  case kBOOLEAN:
70  return getOrAddLiteral(constant->get_is_null()
71  ? int8_t(inline_int_null_val(ti))
72  : int8_t(constant->get_constval().boolval ? 1 : 0),
73  device_id);
74  case kTINYINT:
75  return getOrAddLiteral(constant->get_is_null()
76  ? int8_t(inline_int_null_val(ti))
77  : constant->get_constval().tinyintval,
78  device_id);
79  case kSMALLINT:
80  return getOrAddLiteral(constant->get_is_null()
81  ? int16_t(inline_int_null_val(ti))
82  : constant->get_constval().smallintval,
83  device_id);
84  case kINT:
85  return getOrAddLiteral(constant->get_is_null() ? int32_t(inline_int_null_val(ti))
86  : constant->get_constval().intval,
87  device_id);
88  case kBIGINT:
89  return getOrAddLiteral(constant->get_is_null()
90  ? int64_t(inline_int_null_val(ti))
91  : constant->get_constval().bigintval,
92  device_id);
93  case kFLOAT:
94  return getOrAddLiteral(constant->get_is_null()
95  ? float(inline_fp_null_val(ti))
96  : constant->get_constval().floatval,
97  device_id);
98  case kDOUBLE:
99  return getOrAddLiteral(constant->get_is_null()
100  ? inline_fp_null_val(ti)
101  : constant->get_constval().doubleval,
102  device_id);
103  case kCHAR:
104  case kTEXT:
105  case kVARCHAR:
106  if (enc_type == kENCODING_DICT) {
107  if (constant->get_is_null()) {
108  return getOrAddLiteral(int32_t(inline_int_null_val(ti)), device_id);
109  }
110  return getOrAddLiteral(
111  std::make_pair(*constant->get_constval().stringval, dict_id), device_id);
112  }
113  CHECK_EQ(kENCODING_NONE, enc_type);
114  if (constant->get_is_null()) {
115  throw std::runtime_error(
116  "CHAR / VARCHAR NULL literal not supported in this context"); // TODO(alex):
117  // support
118  // null
119  }
120  return getOrAddLiteral(*constant->get_constval().stringval, device_id);
121  case kTIME:
122  case kTIMESTAMP:
123  case kDATE:
124  case kINTERVAL_DAY_TIME:
126  // TODO(alex): support null
127  return getOrAddLiteral(constant->get_constval().bigintval, device_id);
128  case kARRAY: {
129  if (enc_type == kENCODING_NONE) {
130  if (ti.get_subtype() == kDOUBLE) {
131  std::vector<double> double_array_literal;
132  for (const auto& value : constant->get_value_list()) {
133  const auto c = dynamic_cast<const Analyzer::Constant*>(value.get());
134  CHECK(c);
135  double d = c->get_constval().doubleval;
136  double_array_literal.push_back(d);
137  }
138  return getOrAddLiteral(double_array_literal, device_id);
139  }
140  if (ti.get_subtype() == kINT) {
141  std::vector<int32_t> int32_array_literal;
142  for (const auto& value : constant->get_value_list()) {
143  const auto c = dynamic_cast<const Analyzer::Constant*>(value.get());
144  CHECK(c);
145  int32_t i = c->get_constval().intval;
146  int32_array_literal.push_back(i);
147  }
148  return getOrAddLiteral(int32_array_literal, device_id);
149  }
150  if (ti.get_subtype() == kTINYINT) {
151  std::vector<int8_t> int8_array_literal;
152  for (const auto& value : constant->get_value_list()) {
153  const auto c = dynamic_cast<const Analyzer::Constant*>(value.get());
154  CHECK(c);
155  int8_t i = c->get_constval().tinyintval;
156  int8_array_literal.push_back(i);
157  }
158  if (ti.get_comp_param() == 64) {
159  return getOrAddLiteral(std::make_pair(int8_array_literal, 64), device_id);
160  }
161  return getOrAddLiteral(int8_array_literal, device_id);
162  }
163  throw std::runtime_error("Unsupported literal array");
164  }
165  if (enc_type == kENCODING_GEOINT) {
166  if (ti.get_subtype() == kTINYINT) {
167  std::vector<int8_t> int8_array_literal;
168  for (const auto& value : constant->get_value_list()) {
169  const auto c = dynamic_cast<const Analyzer::Constant*>(value.get());
170  CHECK(c);
171  int8_t i = c->get_constval().tinyintval;
172  int8_array_literal.push_back(i);
173  }
174  if (ti.get_comp_param() == 32) {
175  return getOrAddLiteral(std::make_pair(int8_array_literal, 32), device_id);
176  }
177  return getOrAddLiteral(int8_array_literal, device_id);
178  }
179  }
180  throw std::runtime_error("Encoded literal arrays are not supported");
181  }
182  default:
183  abort();
184  }
185  }
186 
187  using LiteralValue = boost::variant<int8_t,
188  int16_t,
189  int32_t,
190  int64_t,
191  float,
192  double,
193  std::pair<std::string, int>,
194  std::string,
195  std::vector<double>,
196  std::vector<int32_t>,
197  std::vector<int8_t>,
198  std::pair<std::vector<int8_t>, int>>;
199  using LiteralValues = std::vector<LiteralValue>;
200 
201  const std::unordered_map<int, LiteralValues>& getLiterals() const { return literals_; }
202 
203  llvm::Value* addStringConstant(const std::string& str) {
204  llvm::Value* str_lv = ir_builder_.CreateGlobalString(
205  str, "str_const_" + std::to_string(std::hash<std::string>()(str)));
206  auto i8_ptr = llvm::PointerType::get(get_int_type(8, context_), 0);
207  str_constants_.push_back(str_lv);
208  str_lv = ir_builder_.CreateBitCast(str_lv, i8_ptr);
209  return str_lv;
210  }
211 
213  std::unique_ptr<InValuesBitmap>& in_values_bitmap) {
214  if (in_values_bitmap->isEmpty()) {
215  return in_values_bitmap.get();
216  }
217  in_values_bitmaps_.emplace_back(std::move(in_values_bitmap));
218  return in_values_bitmaps_.back().get();
219  }
220  // look up a runtime function based on the name, return type and type of
221  // the arguments and call it; x64 only, don't call from GPU codegen
222  llvm::Value* emitExternalCall(
223  const std::string& fname,
224  llvm::Type* ret_type,
225  const std::vector<llvm::Value*> args,
226  const std::vector<llvm::Attribute::AttrKind>& fnattrs = {},
227  const bool has_struct_return = false) {
228  std::vector<llvm::Type*> arg_types;
229  for (const auto arg : args) {
230  arg_types.push_back(arg->getType());
231  }
232  auto func_ty = llvm::FunctionType::get(ret_type, arg_types, false);
233  llvm::AttributeList attrs;
234  if (!fnattrs.empty()) {
235  std::vector<std::pair<unsigned, llvm::Attribute>> indexedAttrs;
236  indexedAttrs.reserve(fnattrs.size());
237  for (auto attr : fnattrs) {
238  indexedAttrs.emplace_back(llvm::AttributeList::FunctionIndex,
239  llvm::Attribute::get(context_, attr));
240  }
241  attrs = llvm::AttributeList::get(context_,
242  {&indexedAttrs.front(), indexedAttrs.size()});
243  }
244 
245  auto func_p = module_->getOrInsertFunction(fname, func_ty, attrs);
246  CHECK(func_p);
247  auto callee = func_p.getCallee();
248  llvm::Function* func{nullptr};
249  if (auto callee_cast = llvm::dyn_cast<llvm::ConstantExpr>(callee)) {
250  // Get or insert function automatically adds a ConstantExpr cast if the return type
251  // of the existing function does not match the supplied return type.
252  CHECK(callee_cast->isCast());
253  CHECK_EQ(callee_cast->getNumOperands(), size_t(1));
254  func = llvm::dyn_cast<llvm::Function>(callee_cast->getOperand(0));
255  } else {
256  func = llvm::dyn_cast<llvm::Function>(callee);
257  }
258  CHECK(func);
259  llvm::FunctionType* func_type = func_p.getFunctionType();
260  CHECK(func_type);
261  if (has_struct_return) {
262  const auto arg_ti = func_type->getParamType(0);
263  CHECK(arg_ti->isPointerTy() && arg_ti->getPointerElementType()->isStructTy());
264  auto attr_list = func->getAttributes();
265  llvm::AttrBuilder arr_arg_builder(attr_list.getParamAttributes(0));
266  arr_arg_builder.addAttribute(llvm::Attribute::StructRet);
267  func->addParamAttrs(0, arr_arg_builder);
268  }
269  const size_t arg_start = has_struct_return ? 1 : 0;
270  for (size_t i = arg_start; i < func->arg_size(); i++) {
271  const auto arg_ti = func_type->getParamType(i);
272  if (arg_ti->isPointerTy() && arg_ti->getPointerElementType()->isStructTy()) {
273  auto attr_list = func->getAttributes();
274  llvm::AttrBuilder arr_arg_builder(attr_list.getParamAttributes(i));
275  arr_arg_builder.addByValAttr(arg_ti->getPointerElementType());
276  func->addParamAttrs(i, arr_arg_builder);
277  }
278  }
279  llvm::Value* result = ir_builder_.CreateCall(func_p, args);
280  // check the assumed type
281  CHECK_EQ(result->getType(), ret_type);
282  return result;
283  }
284 
285  llvm::Value* emitCall(const std::string& fname, const std::vector<llvm::Value*>& args);
286 
287  size_t getLiteralBufferUsage(const int device_id) { return literal_bytes_[device_id]; }
288 
289  llvm::Value* castToTypeIn(llvm::Value* val, const size_t bit_width);
290 
291  std::pair<llvm::ConstantInt*, llvm::ConstantInt*> inlineIntMaxMin(
292  const size_t byte_width,
293  const bool is_signed);
294 
295  llvm::ConstantInt* inlineIntNull(const SQLTypeInfo&);
296 
297  llvm::ConstantFP* inlineFpNull(const SQLTypeInfo&);
298 
299  template <class T>
300  llvm::ConstantInt* llInt(const T v) const {
302  }
303 
304  llvm::ConstantFP* llFp(const float v) const {
305  return static_cast<llvm::ConstantFP*>(
306  llvm::ConstantFP::get(llvm::Type::getFloatTy(context_), v));
307  }
308 
309  llvm::ConstantFP* llFp(const double v) const {
310  return static_cast<llvm::ConstantFP*>(
311  llvm::ConstantFP::get(llvm::Type::getDoubleTy(context_), v));
312  }
313 
314  llvm::ConstantInt* llBool(const bool v) const { return ::ll_bool(v, context_); }
315 
316  void emitErrorCheck(llvm::Value* condition, llvm::Value* errorCode, std::string label);
317 
318  llvm::Module* module_;
319  llvm::Function* row_func_;
320  llvm::Function* filter_func_;
321  llvm::Function* current_func_;
322  llvm::BasicBlock* row_func_bb_;
323  llvm::BasicBlock* filter_func_bb_;
324  llvm::CallInst* row_func_call_;
325  llvm::CallInst* filter_func_call_;
326  std::vector<llvm::Function*> helper_functions_;
327  llvm::LLVMContext& context_;
328  llvm::ValueToValueMapTy vmap_; // used for cloning the runtime module
329  llvm::IRBuilder<> ir_builder_;
330  std::unordered_map<int, std::vector<llvm::Value*>> fetch_cache_;
333  llvm::Value* lv;
334  };
335  std::vector<FunctionOperValue> ext_call_cache_;
336  std::vector<llvm::Value*> group_by_expr_cache_;
337  std::vector<llvm::Value*> str_constants_;
338  std::vector<llvm::Value*> frag_offsets_;
340  std::vector<llvm::Value*> outer_join_match_found_per_level_;
341  std::unordered_map<int, llvm::Value*> scan_idx_to_hash_pos_;
343  std::vector<std::unique_ptr<const InValuesBitmap>> in_values_bitmaps_;
346 
347  llvm::Function* query_func_;
348  llvm::IRBuilder<> query_func_entry_ir_builder_;
349  std::unordered_map<int, std::vector<llvm::Value*>> query_func_literal_loads_;
350 
354  };
355  std::unordered_map<llvm::Value*, HoistedLiteralLoadLocator> row_func_hoisted_literals_;
356 
357  static size_t literalBytes(const CgenState::LiteralValue& lit) {
358  switch (lit.which()) {
359  case 0:
360  return 1; // int8_t
361  case 1:
362  return 2; // int16_t
363  case 2:
364  return 4; // int32_t
365  case 3:
366  return 8; // int64_t
367  case 4:
368  return 4; // float
369  case 5:
370  return 8; // double
371  case 6:
372  return 4; // std::pair<std::string, int>
373  case 7:
374  return 4; // std::string
375  case 8:
376  return 4; // std::vector<double>
377  case 9:
378  return 4; // std::vector<int32_t>
379  case 10:
380  return 4; // std::vector<int8_t>
381  case 11:
382  return 4; // std::pair<std::vector<int8_t>, int>
383  default:
384  abort();
385  }
386  }
387 
388  static size_t addAligned(const size_t off_in, const size_t alignment) {
389  size_t off = off_in;
390  if (off % alignment != 0) {
391  off += (alignment - off % alignment);
392  }
393  return off + alignment;
394  }
395 
396  private:
397  template <class T>
398  size_t getOrAddLiteral(const T& val, const int device_id) {
399  const LiteralValue var_val(val);
400  size_t literal_found_off{0};
401  auto& literals = literals_[device_id];
402  for (const auto& literal : literals) {
403  const auto lit_bytes = literalBytes(literal);
404  literal_found_off = addAligned(literal_found_off, lit_bytes);
405  if (literal == var_val) {
406  return literal_found_off - lit_bytes;
407  }
408  }
409  literals.emplace_back(val);
410  const auto lit_bytes = literalBytes(var_val);
411  literal_bytes_[device_id] = addAligned(literal_bytes_[device_id], lit_bytes);
412  return literal_bytes_[device_id] - lit_bytes;
413  }
414 
415  std::unordered_map<int, LiteralValues> literals_;
416  std::unordered_map<int, size_t> literal_bytes_;
417 };
418 
std::vector< llvm::Function * > helper_functions_
Definition: CgenState.h:326
int8_t tinyintval
Definition: sqltypes.h:206
CgenState(const size_t num_query_infos, const bool contains_left_deep_outer_join)
Definition: CgenState.h:33
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:337
#define CHECK_EQ(x, y)
Definition: Logger.h:205
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:106
std::vector< llvm::Value * > outer_join_match_found_per_level_
Definition: CgenState.h:340
Definition: sqltypes.h:51
const Analyzer::FunctionOper * foper
Definition: CgenState.h:332
llvm::Value * addStringConstant(const std::string &str)
Definition: CgenState.h:203
bool boolval
Definition: sqltypes.h:205
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:222
llvm::Function * query_func_
Definition: CgenState.h:347
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
std::unordered_map< llvm::Value *, HoistedLiteralLoadLocator > row_func_hoisted_literals_
Definition: CgenState.h:355
llvm::IRBuilder ir_builder_
Definition: CgenState.h:329
bool get_is_null() const
Definition: Analyzer.h:334
std::vector< llvm::Value * > str_constants_
Definition: CgenState.h:337
llvm::ConstantInt * llBool(const bool v) const
Definition: CgenState.h:314
std::unordered_map< int, std::vector< llvm::Value * > > query_func_literal_loads_
Definition: CgenState.h:349
bool needs_geos_
Definition: CgenState.h:345
InsertionOrderedMap filter_func_args_
Definition: CgenState.h:342
const bool contains_left_deep_outer_join_
Definition: CgenState.h:339
CgenState(llvm::LLVMContext &context)
Definition: CgenState.h:51
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
const std::unordered_map< int, LiteralValues > & getLiterals() const
Definition: CgenState.h:201
double inline_fp_null_val(const SQL_TYPE_INFO &ti)
int32_t intval
Definition: sqltypes.h:208
size_t getOrAddLiteral(const T &val, const int device_id)
Definition: CgenState.h:398
llvm::ConstantFP * llFp(const double v) const
Definition: CgenState.h:309
std::string to_string(char const *&&v)
static size_t literalBytes(const CgenState::LiteralValue &lit)
Definition: CgenState.h:357
std::vector< FunctionOperValue > ext_call_cache_
Definition: CgenState.h:335
llvm::Function * row_func_
Definition: CgenState.h:319
float floatval
Definition: sqltypes.h:210
std::vector< llvm::Value * > group_by_expr_cache_
Definition: CgenState.h:336
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:198
EncodingType
Definition: sqltypes.h:227
llvm::Module * module_
Definition: CgenState.h:318
llvm::LLVMContext & context_
Definition: CgenState.h:327
llvm::Function * current_func_
Definition: CgenState.h:321
size_t getOrAddLiteral(const Analyzer::Constant *constant, const EncodingType enc_type, const int dict_id, const int device_id)
Definition: CgenState.h:62
std::unordered_map< int, std::vector< llvm::Value * > > fetch_cache_
Definition: CgenState.h:330
static size_t addAligned(const size_t off_in, const size_t alignment)
Definition: CgenState.h:388
llvm::CallInst * filter_func_call_
Definition: CgenState.h:325
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:27
int64_t bigintval
Definition: sqltypes.h:209
std::vector< LiteralValue > LiteralValues
Definition: CgenState.h:199
const InValuesBitmap * addInValuesBitmap(std::unique_ptr< InValuesBitmap > &in_values_bitmap)
Definition: CgenState.h:212
std::unordered_map< int, size_t > literal_bytes_
Definition: CgenState.h:416
int16_t smallintval
Definition: sqltypes.h:207
std::unordered_map< int, llvm::Value * > scan_idx_to_hash_pos_
Definition: CgenState.h:341
bool needs_error_check_
Definition: CgenState.h:344
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:304
llvm::IRBuilder query_func_entry_ir_builder_
Definition: CgenState.h:348
llvm::BasicBlock * filter_func_bb_
Definition: CgenState.h:323
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:137
std::string * stringval
Definition: sqltypes.h:214
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:302
std::vector< std::unique_ptr< const InValuesBitmap > > in_values_bitmaps_
Definition: CgenState.h:343
Definition: sqltypes.h:54
Definition: sqltypes.h:55
llvm::Function * filter_func_
Definition: CgenState.h:320
std::unordered_map< int, LiteralValues > literals_
Definition: CgenState.h:415
std::vector< llvm::Value * > frag_offsets_
Definition: CgenState.h:338
size_t getLiteralBufferUsage(const int device_id)
Definition: CgenState.h:287
Datum get_constval() const
Definition: Analyzer.h:335
void emitErrorCheck(llvm::Value *condition, llvm::Value *errorCode, std::string label)
Definition: CgenState.cpp:161
Definition: sqltypes.h:43
llvm::LLVMContext & getGlobalLLVMContext()
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:300
bool g_enable_watchdog false
Definition: Execute.cpp:73
#define CHECK(condition)
Definition: Logger.h:197
llvm::ValueToValueMapTy vmap_
Definition: CgenState.h:328
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
llvm::ConstantInt * ll_bool(const bool v, llvm::LLVMContext &context)
Definition: sqltypes.h:47
llvm::CallInst * row_func_call_
Definition: CgenState.h:324
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:77
double doubleval
Definition: sqltypes.h:211
llvm::BasicBlock * row_func_bb_
Definition: CgenState.h:322
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:65