OmniSciDB  5ade3759e0
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  , has_external_calls_(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  in_values_bitmaps_.emplace_back(std::move(in_values_bitmap));
199  return in_values_bitmaps_.back().get();
200  }
201  // look up a runtime function based on the name, return type and type of
202  // the arguments and call it; x64 only, don't call from GPU codegen
203  llvm::Value* emitExternalCall(
204  const std::string& fname,
205  llvm::Type* ret_type,
206  const std::vector<llvm::Value*> args,
207  const std::vector<llvm::Attribute::AttrKind>& fnattrs = {}) {
208  has_external_calls_ = true;
209  std::vector<llvm::Type*> arg_types;
210  for (const auto arg : args) {
211  arg_types.push_back(arg->getType());
212  }
213  auto func_ty = llvm::FunctionType::get(ret_type, arg_types, false);
214  llvm::AttributeList attrs;
215  if (!fnattrs.empty()) {
216  std::vector<std::pair<unsigned, llvm::Attribute>> indexedAttrs;
217  indexedAttrs.reserve(fnattrs.size());
218  for (auto attr : fnattrs)
219  indexedAttrs.emplace_back(llvm::AttributeList::FunctionIndex,
220  llvm::Attribute::get(context_, attr));
221  attrs = llvm::AttributeList::get(context_,
222  {&indexedAttrs.front(), indexedAttrs.size()});
223  }
224  auto func_p = module_->getOrInsertFunction(fname, func_ty, attrs);
225  CHECK(func_p);
226  llvm::Value* result = ir_builder_.CreateCall(func_p, args);
227  // check the assumed type
228  CHECK_EQ(result->getType(), ret_type);
229  return result;
230  }
231 
232  llvm::Value* emitCall(const std::string& fname, const std::vector<llvm::Value*>& args);
233 
234  size_t getLiteralBufferUsage(const int device_id) { return literal_bytes_[device_id]; }
235 
236  llvm::Value* castToTypeIn(llvm::Value* val, const size_t bit_width);
237 
238  std::pair<llvm::ConstantInt*, llvm::ConstantInt*> inlineIntMaxMin(
239  const size_t byte_width,
240  const bool is_signed);
241 
242  llvm::ConstantInt* inlineIntNull(const SQLTypeInfo&);
243 
244  llvm::ConstantFP* inlineFpNull(const SQLTypeInfo&);
245 
246  template <class T>
247  llvm::ConstantInt* llInt(const T v) const {
249  }
250 
251  llvm::ConstantFP* llFp(const float v) const {
252  return static_cast<llvm::ConstantFP*>(
253  llvm::ConstantFP::get(llvm::Type::getFloatTy(context_), v));
254  }
255 
256  llvm::ConstantFP* llFp(const double v) const {
257  return static_cast<llvm::ConstantFP*>(
258  llvm::ConstantFP::get(llvm::Type::getDoubleTy(context_), v));
259  }
260 
261  llvm::ConstantInt* llBool(const bool v) const { return ::ll_bool(v, context_); }
262 
263  llvm::Module* module_;
264  llvm::Function* row_func_;
265  std::vector<llvm::Function*> helper_functions_;
266  llvm::LLVMContext& context_;
267  llvm::ValueToValueMapTy vmap_; // used for cloning the runtime module
268  llvm::IRBuilder<> ir_builder_;
269  std::unordered_map<int, std::vector<llvm::Value*>> fetch_cache_;
272  llvm::Value* lv;
273  };
274  std::vector<FunctionOperValue> ext_call_cache_;
275  std::vector<llvm::Value*> group_by_expr_cache_;
276  std::vector<llvm::Value*> str_constants_;
277  std::vector<llvm::Value*> frag_offsets_;
279  std::vector<llvm::Value*> outer_join_match_found_per_level_;
280  std::unordered_map<int, llvm::Value*> scan_idx_to_hash_pos_;
281  std::vector<std::unique_ptr<const InValuesBitmap>> in_values_bitmaps_;
282  const std::vector<InputTableInfo>& query_infos_;
284  // Track whether external calls have been emitted.
286 
287  llvm::Function* query_func_;
288  llvm::IRBuilder<> query_func_entry_ir_builder_;
289  std::unordered_map<int, std::vector<llvm::Value*>> query_func_literal_loads_;
290 
294  };
295  std::unordered_map<llvm::Value*, HoistedLiteralLoadLocator> row_func_hoisted_literals_;
296 
297  static size_t literalBytes(const CgenState::LiteralValue& lit) {
298  switch (lit.which()) {
299  case 0:
300  return 1; // int8_t
301  case 1:
302  return 2; // int16_t
303  case 2:
304  return 4; // int32_t
305  case 3:
306  return 8; // int64_t
307  case 4:
308  return 4; // float
309  case 5:
310  return 8; // double
311  case 6:
312  return 4; // std::pair<std::string, int>
313  case 7:
314  return 4; // std::string
315  case 8:
316  return 4; // std::vector<double>
317  case 9:
318  return 4; // std::vector<int32_t>
319  case 10:
320  return 4; // std::vector<int8_t>
321  case 11:
322  return 4; // std::pair<std::vector<int8_t>, int>
323  default:
324  abort();
325  }
326  }
327 
328  static size_t addAligned(const size_t off_in, const size_t alignment) {
329  size_t off = off_in;
330  if (off % alignment != 0) {
331  off += (alignment - off % alignment);
332  }
333  return off + alignment;
334  }
335 
336  private:
337  template <class T>
338  size_t getOrAddLiteral(const T& val, const int device_id) {
339  const LiteralValue var_val(val);
340  size_t literal_found_off{0};
341  auto& literals = literals_[device_id];
342  for (const auto& literal : literals) {
343  const auto lit_bytes = literalBytes(literal);
344  literal_found_off = addAligned(literal_found_off, lit_bytes);
345  if (literal == var_val) {
346  return literal_found_off - lit_bytes;
347  }
348  }
349  literals.emplace_back(val);
350  const auto lit_bytes = literalBytes(var_val);
351  literal_bytes_[device_id] = addAligned(literal_bytes_[device_id], lit_bytes);
352  return literal_bytes_[device_id] - lit_bytes;
353  }
354 
355  std::unordered_map<int, LiteralValues> literals_;
356  std::unordered_map<int, size_t> literal_bytes_;
357 };
std::vector< llvm::Function * > helper_functions_
Definition: CgenState.h:265
int8_t tinyintval
Definition: sqltypes.h:123
#define CHECK_EQ(x, y)
Definition: Logger.h:195
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:279
void d(const SQLTypes expected_type, const std::string &str)
Definition: ImportTest.cpp:268
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:251
Definition: sqltypes.h:51
EncodingType
Definition: encodetypes.h:22
const std::vector< InputTableInfo > & query_infos_
Definition: CgenState.h:282
const Analyzer::FunctionOper * foper
Definition: CgenState.h:271
llvm::Value * addStringConstant(const std::string &str)
Definition: CgenState.h:187
bool boolval
Definition: sqltypes.h:122
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:330
llvm::Function * query_func_
Definition: CgenState.h:287
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:295
bool has_external_calls_
Definition: CgenState.h:285
llvm::IRBuilder ir_builder_
Definition: CgenState.h:268
void c(const std::string &query_string, const ExecutorDeviceType device_type)
std::vector< llvm::Value * > str_constants_
Definition: CgenState.h:276
std::unordered_map< int, std::vector< llvm::Value * > > query_func_literal_loads_
Definition: CgenState.h:289
const bool contains_left_deep_outer_join_
Definition: CgenState.h:278
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:125
size_t getOrAddLiteral(const T &val, const int device_id)
Definition: CgenState.h:338
std::string to_string(char const *&&v)
static size_t literalBytes(const CgenState::LiteralValue &lit)
Definition: CgenState.h:297
std::vector< FunctionOperValue > ext_call_cache_
Definition: CgenState.h:274
llvm::Function * row_func_
Definition: CgenState.h:264
llvm::ConstantInt * llBool(const bool v) const
Definition: CgenState.h:261
float floatval
Definition: sqltypes.h:127
Datum get_constval() const
Definition: Analyzer.h:328
std::vector< llvm::Value * > group_by_expr_cache_
Definition: CgenState.h:275
llvm::Module * module_
Definition: CgenState.h:263
llvm::ConstantFP * llFp(const double v) const
Definition: CgenState.h:256
llvm::LLVMContext & context_
Definition: CgenState.h:266
size_t getOrAddLiteral(const Analyzer::Constant *constant, const EncodingType enc_type, const int dict_id, const int device_id)
Definition: CgenState.h:46
T v(const TargetValue &r)
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:269
static size_t addAligned(const size_t off_in, const size_t alignment)
Definition: CgenState.h:328
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:24
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value *> args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={})
Definition: CgenState.h:203
int64_t bigintval
Definition: sqltypes.h:126
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:356
int16_t smallintval
Definition: sqltypes.h:124
std::unordered_map< int, llvm::Value * > scan_idx_to_hash_pos_
Definition: CgenState.h:280
bool needs_error_check_
Definition: CgenState.h:283
llvm::IRBuilder query_func_entry_ir_builder_
Definition: CgenState.h:288
std::string * stringval
Definition: sqltypes.h:131
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:268
bool get_is_null() const
Definition: Analyzer.h:327
std::vector< std::unique_ptr< const InValuesBitmap > > in_values_bitmaps_
Definition: CgenState.h:281
Definition: sqltypes.h:54
Definition: sqltypes.h:55
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value *> &args)
Definition: CgenState.cpp:134
std::unordered_map< int, LiteralValues > literals_
Definition: CgenState.h:355
std::vector< llvm::Value * > frag_offsets_
Definition: CgenState.h:277
size_t getLiteralBufferUsage(const int device_id)
Definition: CgenState.h:234
Definition: sqltypes.h:43
llvm::LLVMContext & getGlobalLLVMContext()
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
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:187
llvm::ValueToValueMapTy vmap_
Definition: CgenState.h:267
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:47
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:74
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:247
double doubleval
Definition: sqltypes.h:128
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:62