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