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