OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
CodeGenerator.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 <llvm/IR/Value.h>
20 
21 #include "../Analyzer/Analyzer.h"
22 #include "Execute.h"
23 
24 // Code generation utility to be used for queries and scalar expressions.
26  public:
28  : executor_(executor)
29  , cgen_state_(executor->cgen_state_.get())
30  , plan_state_(executor->plan_state_.get()) {}
31 
32  // Overload which can be used without an executor, for SQL scalar expression code
33  // generation.
34  CodeGenerator(CgenState* cgen_state, PlanState* plan_state)
35  : executor_(nullptr), cgen_state_(cgen_state), plan_state_(plan_state) {}
36 
37  // Generates IR value(s) for the given analyzer expression.
38  std::vector<llvm::Value*> codegen(const Analyzer::Expr*,
39  const bool fetch_columns,
40  const CompilationOptions&);
41 
42  // Generates constant values in the literal buffer of a query.
43  std::vector<llvm::Value*> codegenHoistedConstants(
44  const std::vector<const Analyzer::Constant*>& constants,
45  const EncodingType enc_type,
46  const int dict_id);
47 
48  static llvm::ConstantInt* codegenIntConst(const Analyzer::Constant* constant,
49  CgenState* cgen_state);
50 
51  llvm::Value* codegenCastBetweenIntTypes(llvm::Value* operand_lv,
52  const SQLTypeInfo& operand_ti,
53  const SQLTypeInfo& ti,
54  bool upscale = true);
55 
56  void codegenCastBetweenIntTypesOverflowChecks(llvm::Value* operand_lv,
57  const SQLTypeInfo& operand_ti,
58  const SQLTypeInfo& ti,
59  const int64_t scale);
60 
61  // Generates the index of the current row in the context of query execution.
62  llvm::Value* posArg(const Analyzer::Expr*) const;
63 
64  llvm::Value* toBool(llvm::Value*);
65 
66  llvm::Value* castArrayPointer(llvm::Value* ptr, const SQLTypeInfo& elem_ti);
67 
68  static std::unordered_set<llvm::Function*> markDeadRuntimeFuncs(
69  llvm::Module& module,
70  const std::vector<llvm::Function*>& roots,
71  const std::vector<llvm::Function*>& leaves);
72 
74  llvm::Function* func,
75  const std::unordered_set<llvm::Function*>& live_funcs,
76  const CompilationOptions& co);
77 
78  static std::string generatePTX(const std::string& cuda_llir,
79  llvm::TargetMachine* nvptx_target_machine,
80  llvm::LLVMContext& context);
81 
82  static std::unique_ptr<llvm::TargetMachine> initializeNVPTXBackend(
84 
85  static bool alwaysCloneRuntimeFunction(const llvm::Function* func);
86 
87  struct GPUTarget {
88  llvm::TargetMachine* nvptx_target_machine;
90  unsigned block_size;
93  };
94 
95  static std::shared_ptr<GpuCompilationContext> generateNativeGPUCode(
96  llvm::Function* func,
97  llvm::Function* wrapper_func,
98  const std::unordered_set<llvm::Function*>& live_funcs,
99  const CompilationOptions& co,
100  const GPUTarget& gpu_target);
101 
102  static void link_udf_module(const std::unique_ptr<llvm::Module>& udf_module,
103  llvm::Module& module,
104  CgenState* cgen_state,
105  llvm::Linker::Flags flags = llvm::Linker::Flags::None);
106 
107  static bool prioritizeQuals(const RelAlgExecutionUnit& ra_exe_unit,
108  std::vector<Analyzer::Expr*>& primary_quals,
109  std::vector<Analyzer::Expr*>& deferred_quals,
110  const PlanState::HoistedFiltersSet& hoisted_quals);
111 
112  struct ExecutorRequired : public std::runtime_error {
114  : std::runtime_error("Executor required to generate this expression") {}
115  };
116 
119  Executor* executor,
120  llvm::Value* nullable_lv,
121  const SQLTypeInfo& nullable_ti,
122  const std::string& name = "");
123 
124  llvm::Value* finalize(llvm::Value* null_lv, llvm::Value* notnull_lv);
125 
126  CgenState* cgen_state{nullptr};
127  std::string name;
128  llvm::BasicBlock* nullcheck_bb{nullptr};
129  llvm::PHINode* nullcheck_value{nullptr};
130  std::unique_ptr<GroupByAndAggregate::DiamondCodegen> null_check;
131  };
132 
133  private:
134  std::vector<llvm::Value*> codegen(const Analyzer::Constant*,
135  const EncodingType enc_type,
136  const int dict_id,
137  const CompilationOptions&);
138 
139  virtual std::vector<llvm::Value*> codegenColumn(const Analyzer::ColumnVar*,
140  const bool fetch_column,
141  const CompilationOptions&);
142 
143  llvm::Value* codegenArith(const Analyzer::BinOper*, const CompilationOptions&);
144 
145  llvm::Value* codegenUMinus(const Analyzer::UOper*, const CompilationOptions&);
146 
147  llvm::Value* codegenCmp(const Analyzer::BinOper*, const CompilationOptions&);
148 
149  llvm::Value* codegenCmp(const SQLOps,
150  const SQLQualifier,
151  std::vector<llvm::Value*>,
152  const SQLTypeInfo&,
153  const Analyzer::Expr*,
154  const CompilationOptions&);
155 
156  llvm::Value* codegenIsNull(const Analyzer::UOper*, const CompilationOptions&);
157 
158  llvm::Value* codegenIsNullNumber(llvm::Value*, const SQLTypeInfo&);
159 
160  llvm::Value* codegenLogical(const Analyzer::BinOper*, const CompilationOptions&);
161 
162  llvm::Value* codegenLogical(const Analyzer::UOper*, const CompilationOptions&);
163 
164  llvm::Value* codegenCast(const Analyzer::UOper*, const CompilationOptions&);
165 
166  llvm::Value* codegenCast(llvm::Value* operand_lv,
167  const SQLTypeInfo& operand_ti,
168  const SQLTypeInfo& ti,
169  const bool operand_is_const,
170  const CompilationOptions& co);
171 
172  llvm::Value* codegen(const Analyzer::InValues*, const CompilationOptions&);
173 
174  llvm::Value* codegen(const Analyzer::InIntegerSet* expr, const CompilationOptions& co);
175 
176  std::vector<llvm::Value*> codegen(const Analyzer::CaseExpr*, const CompilationOptions&);
177 
178  llvm::Value* codegen(const Analyzer::ExtractExpr*, const CompilationOptions&);
179 
180  llvm::Value* codegen(const Analyzer::DateaddExpr*, const CompilationOptions&);
181 
182  llvm::Value* codegen(const Analyzer::DatediffExpr*, const CompilationOptions&);
183 
184  llvm::Value* codegen(const Analyzer::DatetruncExpr*, const CompilationOptions&);
185 
186  llvm::Value* codegen(const Analyzer::CharLengthExpr*, const CompilationOptions&);
187 
188  llvm::Value* codegen(const Analyzer::KeyForStringExpr*, const CompilationOptions&);
189 
190  llvm::Value* codegen(const Analyzer::SampleRatioExpr*, const CompilationOptions&);
191 
192  llvm::Value* codegen(const Analyzer::LowerExpr*, const CompilationOptions&);
193 
194  llvm::Value* codegen(const Analyzer::LikeExpr*, const CompilationOptions&);
195 
196  llvm::Value* codegen(const Analyzer::RegexpExpr*, const CompilationOptions&);
197 
198  llvm::Value* codegenUnnest(const Analyzer::UOper*, const CompilationOptions&);
199 
200  llvm::Value* codegenArrayAt(const Analyzer::BinOper*, const CompilationOptions&);
201 
202  llvm::Value* codegen(const Analyzer::CardinalityExpr*, const CompilationOptions&);
203 
204  std::vector<llvm::Value*> codegenArrayExpr(const Analyzer::ArrayExpr*,
205  const CompilationOptions&);
206 
207  std::vector<llvm::Value*> codegenGeoUOper(const Analyzer::GeoUOper*,
208  const CompilationOptions&);
209 
210  std::vector<llvm::Value*> codegenGeoBinOper(const Analyzer::GeoBinOper*,
211  const CompilationOptions&);
212 
213  std::vector<llvm::Value*> codegenGeosPredicateCall(const std::string&,
214  std::vector<llvm::Value*>,
215  const CompilationOptions&);
216 
217  std::vector<llvm::Value*> codegenGeosConstructorCall(const std::string&,
218  std::vector<llvm::Value*>,
219  const CompilationOptions&);
220 
221  std::vector<llvm::Value*> codegenGeoArgs(
222  const std::vector<std::shared_ptr<Analyzer::Expr>>&,
223  const CompilationOptions&);
224 
225  llvm::Value* codegenFunctionOper(const Analyzer::FunctionOper*,
226  const CompilationOptions&);
227 
230  const CompilationOptions&);
231 
232  llvm::Value* codegen(const Analyzer::BinOper*, const CompilationOptions&);
233 
234  llvm::Value* codegen(const Analyzer::UOper*, const CompilationOptions&);
235 
236  std::vector<llvm::Value*> codegenHoistedConstantsLoads(const SQLTypeInfo& type_info,
237  const EncodingType enc_type,
238  const int dict_id,
239  const int16_t lit_off);
240 
241  std::vector<llvm::Value*> codegenHoistedConstantsPlaceholders(
242  const SQLTypeInfo& type_info,
243  const EncodingType enc_type,
244  const int16_t lit_off,
245  const std::vector<llvm::Value*>& literal_loads);
246 
247  std::vector<llvm::Value*> codegenColVar(const Analyzer::ColumnVar*,
248  const bool fetch_column,
249  const bool update_query_plan,
250  const CompilationOptions&);
251 
252  llvm::Value* codegenFixedLengthColVar(const Analyzer::ColumnVar* col_var,
253  llvm::Value* col_byte_stream,
254  llvm::Value* pos_arg);
255 
256  // Generates code for a fixed length column when a window function is active.
257  llvm::Value* codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar* col_var,
258  llvm::Value* col_byte_stream,
259  llvm::Value* pos_arg);
260 
261  // Generate the position for the given window function and the query iteration position.
262  llvm::Value* codegenWindowPosition(WindowFunctionContext* window_func_context,
263  llvm::Value* pos_arg);
264 
265  std::vector<llvm::Value*> codegenVariableLengthStringColVar(
266  llvm::Value* col_byte_stream,
267  llvm::Value* pos_arg);
268 
269  llvm::Value* codegenRowId(const Analyzer::ColumnVar* col_var,
270  const CompilationOptions& co);
271 
272  llvm::Value* codgenAdjustFixedEncNull(llvm::Value*, const SQLTypeInfo&);
273 
274  std::vector<llvm::Value*> codegenOuterJoinNullPlaceholder(
275  const Analyzer::ColumnVar* col_var,
276  const bool fetch_column,
277  const CompilationOptions& co);
278 
279  llvm::Value* codegenIntArith(const Analyzer::BinOper*,
280  llvm::Value*,
281  llvm::Value*,
282  const CompilationOptions&);
283 
284  llvm::Value* codegenFpArith(const Analyzer::BinOper*, llvm::Value*, llvm::Value*);
285 
286  llvm::Value* codegenCastTimestampToDate(llvm::Value* ts_lv,
287  const int dimen,
288  const bool nullable);
289 
290  llvm::Value* codegenCastBetweenTimestamps(llvm::Value* ts_lv,
291  const SQLTypeInfo& operand_dimen,
292  const SQLTypeInfo& target_dimen,
293  const bool nullable);
294 
295  llvm::Value* codegenCastFromString(llvm::Value* operand_lv,
296  const SQLTypeInfo& operand_ti,
297  const SQLTypeInfo& ti,
298  const bool operand_is_const,
299  const CompilationOptions& co);
300 
301  llvm::Value* codegenCastToFp(llvm::Value* operand_lv,
302  const SQLTypeInfo& operand_ti,
303  const SQLTypeInfo& ti);
304 
305  llvm::Value* codegenCastFromFp(llvm::Value* operand_lv,
306  const SQLTypeInfo& operand_ti,
307  const SQLTypeInfo& ti);
308 
309  llvm::Value* codegenAdd(const Analyzer::BinOper*,
310  llvm::Value*,
311  llvm::Value*,
312  const std::string& null_typename,
313  const std::string& null_check_suffix,
314  const SQLTypeInfo&,
315  const CompilationOptions&);
316 
317  llvm::Value* codegenSub(const Analyzer::BinOper*,
318  llvm::Value*,
319  llvm::Value*,
320  const std::string& null_typename,
321  const std::string& null_check_suffix,
322  const SQLTypeInfo&,
323  const CompilationOptions&);
324 
325  void codegenSkipOverflowCheckForNull(llvm::Value* lhs_lv,
326  llvm::Value* rhs_lv,
327  llvm::BasicBlock* no_overflow_bb,
328  const SQLTypeInfo& ti);
329 
330  llvm::Value* codegenMul(const Analyzer::BinOper*,
331  llvm::Value*,
332  llvm::Value*,
333  const std::string& null_typename,
334  const std::string& null_check_suffix,
335  const SQLTypeInfo&,
336  const CompilationOptions&,
337  bool downscale = true);
338 
339  llvm::Value* codegenDiv(llvm::Value*,
340  llvm::Value*,
341  const std::string& null_typename,
342  const std::string& null_check_suffix,
343  const SQLTypeInfo&,
344  bool upscale = true);
345 
346  llvm::Value* codegenDeciDiv(const Analyzer::BinOper*, const CompilationOptions&);
347 
348  llvm::Value* codegenMod(llvm::Value*,
349  llvm::Value*,
350  const std::string& null_typename,
351  const std::string& null_check_suffix,
352  const SQLTypeInfo&);
353 
354  llvm::Value* codegenCase(const Analyzer::CaseExpr*,
355  llvm::Type* case_llvm_type,
356  const bool is_real_str,
357  const CompilationOptions&);
358 
359  llvm::Value* codegenExtractHighPrecisionTimestamps(llvm::Value*,
360  const SQLTypeInfo&,
361  const ExtractField&);
362 
363  llvm::Value* codegenDateTruncHighPrecisionTimestamps(llvm::Value*,
364  const SQLTypeInfo&,
365  const DatetruncField&);
366 
367  llvm::Value* codegenCmpDecimalConst(const SQLOps,
368  const SQLQualifier,
369  const Analyzer::Expr*,
370  const SQLTypeInfo&,
371  const Analyzer::Expr*,
372  const CompilationOptions&);
373 
374  llvm::Value* codegenOverlaps(const SQLOps,
375  const SQLQualifier,
376  const std::shared_ptr<Analyzer::Expr>,
377  const std::shared_ptr<Analyzer::Expr>,
378  const CompilationOptions&);
379 
380  llvm::Value* codegenStrCmp(const SQLOps,
381  const SQLQualifier,
382  const std::shared_ptr<Analyzer::Expr>,
383  const std::shared_ptr<Analyzer::Expr>,
384  const CompilationOptions&);
385 
386  llvm::Value* codegenQualifierCmp(const SQLOps,
387  const SQLQualifier,
388  std::vector<llvm::Value*>,
389  const Analyzer::Expr*,
390  const CompilationOptions&);
391 
392  llvm::Value* codegenLogicalShortCircuit(const Analyzer::BinOper*,
393  const CompilationOptions&);
394 
395  llvm::Value* codegenDictLike(const std::shared_ptr<Analyzer::Expr> arg,
396  const Analyzer::Constant* pattern,
397  const bool ilike,
398  const bool is_simple,
399  const char escape_char,
400  const CompilationOptions&);
401 
402  llvm::Value* codegenDictStrCmp(const std::shared_ptr<Analyzer::Expr>,
403  const std::shared_ptr<Analyzer::Expr>,
404  const SQLOps,
405  const CompilationOptions& co);
406 
407  llvm::Value* codegenDictRegexp(const std::shared_ptr<Analyzer::Expr> arg,
408  const Analyzer::Constant* pattern,
409  const char escape_char,
410  const CompilationOptions&);
411 
412  // Returns the IR value which holds true iff at least one match has been found for outer
413  // join, null if there's no outer join condition on the given nesting level.
414  llvm::Value* foundOuterJoinMatch(const size_t nesting_level) const;
415 
417 
418  llvm::Value* colByteStream(const Analyzer::ColumnVar* col_var,
419  const bool fetch_column,
420  const bool hoist_literals);
421 
422  std::shared_ptr<const Analyzer::Expr> hashJoinLhs(const Analyzer::ColumnVar* rhs) const;
423 
424  std::shared_ptr<const Analyzer::ColumnVar> hashJoinLhsTuple(
425  const Analyzer::ColumnVar* rhs,
426  const Analyzer::BinOper* tautological_eq) const;
427 
428  std::unique_ptr<InValuesBitmap> createInValuesBitmap(const Analyzer::InValues*,
429  const CompilationOptions&);
430 
431  bool checkExpressionRanges(const Analyzer::UOper*, int64_t, int64_t);
432 
433  bool checkExpressionRanges(const Analyzer::BinOper*, int64_t, int64_t);
434 
436  llvm::BasicBlock* args_null_bb;
437  llvm::BasicBlock* args_notnull_bb;
438  llvm::BasicBlock* orig_bb;
439  };
440 
441  std::tuple<ArgNullcheckBBs, llvm::Value*> beginArgsNullcheck(
442  const Analyzer::FunctionOper* function_oper,
443  const std::vector<llvm::Value*>& orig_arg_lvs);
444 
445  llvm::Value* endArgsNullcheck(const ArgNullcheckBBs&,
446  llvm::Value*,
447  llvm::Value*,
448  const Analyzer::FunctionOper*);
449 
451  const std::vector<llvm::Value*>&);
452 
453  llvm::Value* codegenCompression(const SQLTypeInfo& type_info);
454 
455  std::pair<llvm::Value*, llvm::Value*> codegenArrayBuff(llvm::Value* chunk,
456  llvm::Value* row_pos,
457  SQLTypes array_type,
458  bool cast_and_extend);
459 
460  void codegenBufferArgs(const std::string& udf_func_name,
461  size_t param_num,
462  llvm::Value* buffer_buf,
463  llvm::Value* buffer_size,
464  llvm::Value* buffer_is_null,
465  std::vector<llvm::Value*>& output_args);
466 
467  llvm::StructType* createPointStructType(const std::string& udf_func_name,
468  size_t param_num);
469 
470  void codegenGeoPointArgs(const std::string& udf_func_name,
471  size_t param_num,
472  llvm::Value* point_buf,
473  llvm::Value* point_size,
474  llvm::Value* compression,
475  llvm::Value* input_srid,
476  llvm::Value* output_srid,
477  std::vector<llvm::Value*>& output_args);
478 
479  llvm::StructType* createLineStringStructType(const std::string& udf_func_name,
480  size_t param_num);
481 
482  void codegenGeoLineStringArgs(const std::string& udf_func_name,
483  size_t param_num,
484  llvm::Value* line_string_buf,
485  llvm::Value* line_string_size,
486  llvm::Value* compression,
487  llvm::Value* input_srid,
488  llvm::Value* output_srid,
489  std::vector<llvm::Value*>& output_args);
490 
491  llvm::StructType* createPolygonStructType(const std::string& udf_func_name,
492  size_t param_num);
493 
494  void codegenGeoPolygonArgs(const std::string& udf_func_name,
495  size_t param_num,
496  llvm::Value* polygon_buf,
497  llvm::Value* polygon_size,
498  llvm::Value* ring_sizes_buf,
499  llvm::Value* num_rings,
500  llvm::Value* compression,
501  llvm::Value* input_srid,
502  llvm::Value* output_srid,
503  std::vector<llvm::Value*>& output_args);
504 
505  llvm::StructType* createMultiPolygonStructType(const std::string& udf_func_name,
506  size_t param_num);
507 
508  void codegenGeoMultiPolygonArgs(const std::string& udf_func_name,
509  size_t param_num,
510  llvm::Value* polygon_coords,
511  llvm::Value* polygon_coords_size,
512  llvm::Value* ring_sizes_buf,
513  llvm::Value* ring_sizes,
514  llvm::Value* polygon_bounds,
515  llvm::Value* polygon_bounds_sizes,
516  llvm::Value* compression,
517  llvm::Value* input_srid,
518  llvm::Value* output_srid,
519  std::vector<llvm::Value*>& output_args);
520 
521  std::vector<llvm::Value*> codegenFunctionOperCastArgs(
522  const Analyzer::FunctionOper*,
523  const ExtensionFunction*,
524  const std::vector<llvm::Value*>&,
525  const std::vector<size_t>&,
526  const std::unordered_map<llvm::Value*, llvm::Value*>&,
527  const CompilationOptions&);
528 
529  // Return LLVM intrinsic providing fast arithmetic with overflow check
530  // for the given binary operation.
531  llvm::Function* getArithWithOverflowIntrinsic(const Analyzer::BinOper* bin_oper,
532  llvm::Type* type);
533 
534  // Generate code for the given binary operation with overflow check.
535  // Signed integer add, sub and mul operations are supported. Overflow
536  // check is performed using LLVM arithmetic intrinsics which are not
537  // supported for GPU. Return the IR value which holds operation result.
538  llvm::Value* codegenBinOpWithOverflowForCPU(const Analyzer::BinOper* bin_oper,
539  llvm::Value* lhs_lv,
540  llvm::Value* rhs_lv,
541  const std::string& null_check_suffix,
542  const SQLTypeInfo& ti);
543 
544  Executor* executor_;
545 
546  protected:
547  Executor* executor() const {
548  if (!executor_) {
549  throw ExecutorRequired();
550  }
551  return executor_;
552  }
553 
556 
557  friend class GroupByAndAggregate;
558 };
559 
560 // Code generator specialized for scalar expressions which doesn't require an executor.
562  public:
563  // Constructor which takes the runtime module.
564  ScalarCodeGenerator(std::unique_ptr<llvm::Module> module)
565  : CodeGenerator(nullptr, nullptr), module_(std::move(module)) {}
566 
567  // Function generated for a given analyzer expression. For GPU, a wrapper which meets
568  // the kernel signature constraints (returns void, takes all arguments as pointers) is
569  // generated. Also returns the list of column expressions for which compatible input
570  // parameters must be passed to the input of the generated function.
572  llvm::Function* func;
573  llvm::Function* wrapper_func;
574  std::vector<std::shared_ptr<Analyzer::ColumnVar>> inputs;
575  };
576 
577  // Compiles the given scalar expression to IR and the list of columns in the expression,
578  // needed to provide inputs to the generated function.
580  const bool fetch_columns,
581  const CompilationOptions& co);
582 
583  // Generates the native function pointers for each device.
584  // NB: this is separated from the compile method to allow building higher level code
585  // generators which can inline the IR for evaluating a single expression (for example
586  // loops).
587  std::vector<void*> generateNativeCode(const CompiledExpression& compiled_expression,
588  const CompilationOptions& co);
589 
591 
592  using ColumnMap =
593  std::unordered_map<InputColDescriptor, std::shared_ptr<Analyzer::ColumnVar>>;
594 
595  private:
596  std::vector<llvm::Value*> codegenColumn(const Analyzer::ColumnVar*,
597  const bool fetch_column,
598  const CompilationOptions&) override;
599 
600  // Collect the columns used by the given analyzer expressions and fills in the column
601  // map to be used during code generation.
603 
604  std::vector<void*> generateNativeGPUCode(llvm::Function* func,
605  llvm::Function* wrapper_func,
606  const CompilationOptions& co);
607 
608  std::unique_ptr<llvm::Module> module_;
610  std::unique_ptr<CgenState> own_cgen_state_;
611  std::unique_ptr<PlanState> own_plan_state_;
612  std::unique_ptr<CudaMgr_Namespace::CudaMgr> cuda_mgr_;
613  std::shared_ptr<GpuCompilationContext> gpu_compilation_context_;
614  std::unique_ptr<llvm::TargetMachine> nvptx_target_machine_;
615 };
616 
621 std::unique_ptr<llvm::Module> runtime_module_shallow_copy(CgenState* cgen_state);
622 
626 std::vector<llvm::Value*> generate_column_heads_load(const int num_columns,
627  llvm::Value* byte_stream_arg,
628  llvm::IRBuilder<>& ir_builder,
629  llvm::LLVMContext& ctx);
llvm::StructType * createLineStringStructType(const std::string &udf_func_name, size_t param_num)
void codegenGeoMultiPolygonArgs(const std::string &udf_func_name, size_t param_num, llvm::Value *polygon_coords, llvm::Value *polygon_coords_size, llvm::Value *ring_sizes_buf, llvm::Value *ring_sizes, llvm::Value *polygon_bounds, llvm::Value *polygon_bounds_sizes, llvm::Value *compression, llvm::Value *input_srid, llvm::Value *output_srid, std::vector< llvm::Value * > &output_args)
llvm::Value * codegenIntArith(const Analyzer::BinOper *, llvm::Value *, llvm::Value *, const CompilationOptions &)
CompiledExpression compile(const Analyzer::Expr *expr, const bool fetch_columns, const CompilationOptions &co)
NullCheckCodegen(CgenState *cgen_state, Executor *executor, llvm::Value *nullable_lv, const SQLTypeInfo &nullable_ti, const std::string &name="")
Definition: IRCodegen.cpp:984
llvm::Value * codegenCastFromFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:419
std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &) override
llvm::Value * codegenStrCmp(const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
Definition: CompareIR.cpp:320
std::vector< void * > generateNativeCode(const CompiledExpression &compiled_expression, const CompilationOptions &co)
std::unique_ptr< llvm::Module > runtime_module_shallow_copy(CgenState *cgen_state)
llvm::BasicBlock * args_notnull_bb
llvm::BasicBlock * nullcheck_bb
SQLTypes
Definition: sqltypes.h:37
std::unique_ptr< PlanState > own_plan_state_
llvm::Value * codegenArith(const Analyzer::BinOper *, const CompilationOptions &)
CgenState * cgen_state_
ExecutionEngineWrapper execution_engine_
void codegenGeoPolygonArgs(const std::string &udf_func_name, size_t param_num, llvm::Value *polygon_buf, llvm::Value *polygon_size, llvm::Value *ring_sizes_buf, llvm::Value *num_rings, llvm::Value *compression, llvm::Value *input_srid, llvm::Value *output_srid, std::vector< llvm::Value * > &output_args)
SQLQualifier
Definition: sqldefs.h:69
llvm::Value * codegenRowId(const Analyzer::ColumnVar *col_var, const CompilationOptions &co)
Definition: ColumnIR.cpp:320
std::vector< llvm::Value * > codegenFunctionOperCastArgs(const Analyzer::FunctionOper *, const ExtensionFunction *, const std::vector< llvm::Value * > &, const std::vector< size_t > &, const std::unordered_map< llvm::Value *, llvm::Value * > &, const CompilationOptions &)
SQLOps
Definition: sqldefs.h:29
llvm::Value * codegenMod(llvm::Value *, llvm::Value *, const std::string &null_typename, const std::string &null_check_suffix, const SQLTypeInfo &)
llvm::Value * codegenFunctionOperNullArg(const Analyzer::FunctionOper *, const std::vector< llvm::Value * > &)
std::vector< llvm::Value * > codegenOuterJoinNullPlaceholder(const Analyzer::ColumnVar *col_var, const bool fetch_column, const CompilationOptions &co)
Definition: ColumnIR.cpp:422
std::unique_ptr< llvm::TargetMachine > nvptx_target_machine_
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:512
std::shared_ptr< GpuCompilationContext > gpu_compilation_context_
llvm::Value * castArrayPointer(llvm::Value *ptr, const SQLTypeInfo &elem_ti)
llvm::Value * codegenCastToFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:387
llvm::Value * codgenAdjustFixedEncNull(llvm::Value *, const SQLTypeInfo &)
Definition: ColumnIR.cpp:374
llvm::Value * foundOuterJoinMatch(const size_t nesting_level) const
Definition: ColumnIR.cpp:415
llvm::Value * codegenExtractHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const ExtractField &)
Definition: DateTimeIR.cpp:255
llvm::StructType * createPointStructType(const std::string &udf_func_name, size_t param_num)
virtual std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &)
Definition: ColumnIR.cpp:92
std::unordered_set< std::shared_ptr< Analyzer::Expr >> HoistedFiltersSet
Definition: PlanState.h:45
llvm::Value * codegenDeciDiv(const Analyzer::BinOper *, const CompilationOptions &)
static ExecutionEngineWrapper generateNativeCPUCode(llvm::Function *func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co)
llvm::Value * codegenArrayAt(const Analyzer::BinOper *, const CompilationOptions &)
Definition: ArrayIR.cpp:26
std::unordered_map< InputColDescriptor, std::shared_ptr< Analyzer::ColumnVar >> ColumnMap
static std::string generatePTX(const std::string &cuda_llir, llvm::TargetMachine *nvptx_target_machine, llvm::LLVMContext &context)
std::vector< llvm::Value * > codegenGeoBinOper(const Analyzer::GeoBinOper *, const CompilationOptions &)
Definition: GeoIR.cpp:86
const CudaMgr_Namespace::CudaMgr * cuda_mgr
Definition: CodeGenerator.h:89
void codegenBufferArgs(const std::string &udf_func_name, size_t param_num, llvm::Value *buffer_buf, llvm::Value *buffer_size, llvm::Value *buffer_is_null, std::vector< llvm::Value * > &output_args)
std::unique_ptr< CudaMgr_Namespace::CudaMgr > cuda_mgr_
static std::shared_ptr< GpuCompilationContext > generateNativeGPUCode(llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co, const GPUTarget &gpu_target)
llvm::Value * codegenFpArith(const Analyzer::BinOper *, llvm::Value *, llvm::Value *)
llvm::Value * codegenIsNull(const Analyzer::UOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:380
llvm::TargetMachine * nvptx_target_machine
Definition: CodeGenerator.h:88
std::pair< llvm::Value *, llvm::Value * > codegenArrayBuff(llvm::Value *chunk, llvm::Value *row_pos, SQLTypes array_type, bool cast_and_extend)
std::shared_ptr< const Analyzer::Expr > hashJoinLhs(const Analyzer::ColumnVar *rhs) const
Definition: ColumnIR.cpp:549
EncodingType
Definition: sqltypes.h:227
CudaMgr_Namespace::CudaMgr * getCudaMgr() const
llvm::Value * codegenDiv(llvm::Value *, llvm::Value *, const std::string &null_typename, const std::string &null_check_suffix, const SQLTypeInfo &, bool upscale=true)
llvm::Value * codegenCmpDecimalConst(const SQLOps, const SQLQualifier, const Analyzer::Expr *, const SQLTypeInfo &, const Analyzer::Expr *, const CompilationOptions &)
Definition: CompareIR.cpp:352
std::tuple< ArgNullcheckBBs, llvm::Value * > beginArgsNullcheck(const Analyzer::FunctionOper *function_oper, const std::vector< llvm::Value * > &orig_arg_lvs)
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const int dict_id)
Definition: ConstantIR.cpp:279
static std::unordered_set< llvm::Function * > markDeadRuntimeFuncs(llvm::Module &module, const std::vector< llvm::Function * > &roots, const std::vector< llvm::Function * > &leaves)
ScalarCodeGenerator(std::unique_ptr< llvm::Module > module)
DatetruncField
Definition: DateTruncate.h:27
std::vector< llvm::Value * > codegenGeoUOper(const Analyzer::GeoUOper *, const CompilationOptions &)
Definition: GeoIR.cpp:21
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:115
void codegenSkipOverflowCheckForNull(llvm::Value *lhs_lv, llvm::Value *rhs_lv, llvm::BasicBlock *no_overflow_bb, const SQLTypeInfo &ti)
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:204
CodeGenerator(Executor *executor)
Definition: CodeGenerator.h:27
llvm::Value * codegenBinOpWithOverflowForCPU(const Analyzer::BinOper *bin_oper, llvm::Value *lhs_lv, llvm::Value *rhs_lv, const std::string &null_check_suffix, const SQLTypeInfo &ti)
llvm::Value * codegenFunctionOper(const Analyzer::FunctionOper *, const CompilationOptions &)
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:256
Executor * executor_
std::vector< llvm::Value * > codegenGeosConstructorCall(const std::string &, std::vector< llvm::Value * >, const CompilationOptions &)
Definition: GeoIR.cpp:282
llvm::Value * codegenOverlaps(const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
Definition: CompareIR.cpp:239
llvm::Value * codegenDictStrCmp(const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const SQLOps, const CompilationOptions &co)
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
Definition: CastIR.cpp:118
static void link_udf_module(const std::unique_ptr< llvm::Module > &udf_module, llvm::Module &module, CgenState *cgen_state, llvm::Linker::Flags flags=llvm::Linker::Flags::None)
std::vector< llvm::Value * > codegenArrayExpr(const Analyzer::ArrayExpr *, const CompilationOptions &)
Definition: ArrayIR.cpp:91
llvm::Value * codegenDictRegexp(const std::shared_ptr< Analyzer::Expr > arg, const Analyzer::Constant *pattern, const char escape_char, const CompilationOptions &)
llvm::BasicBlock * args_null_bb
llvm::Function * getArithWithOverflowIntrinsic(const Analyzer::BinOper *bin_oper, llvm::Type *type)
std::vector< llvm::Value * > codegenColVar(const Analyzer::ColumnVar *, const bool fetch_column, const bool update_query_plan, const CompilationOptions &)
Definition: ColumnIR.cpp:104
llvm::Value * codegenUMinus(const Analyzer::UOper *, const CompilationOptions &)
void codegenGeoPointArgs(const std::string &udf_func_name, size_t param_num, llvm::Value *point_buf, llvm::Value *point_size, llvm::Value *compression, llvm::Value *input_srid, llvm::Value *output_srid, std::vector< llvm::Value * > &output_args)
PlanState * plan_state_
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:28
static llvm::ConstantInt * codegenIntConst(const Analyzer::Constant *constant, CgenState *cgen_state)
Definition: ConstantIR.cpp:85
std::unique_ptr< GroupByAndAggregate::DiamondCodegen > null_check
std::vector< llvm::Value * > codegenGeosPredicateCall(const std::string &, std::vector< llvm::Value * >, const CompilationOptions &)
Definition: GeoIR.cpp:250
llvm::StructType * createPolygonStructType(const std::string &udf_func_name, size_t param_num)
llvm::Value * codegenQualifierCmp(const SQLOps, const SQLQualifier, std::vector< llvm::Value * >, const Analyzer::Expr *, const CompilationOptions &)
Definition: CompareIR.cpp:500
llvm::Value * endArgsNullcheck(const ArgNullcheckBBs &, llvm::Value *, llvm::Value *, const Analyzer::FunctionOper *)
llvm::Value * codegenSub(const Analyzer::BinOper *, llvm::Value *, llvm::Value *, const std::string &null_typename, const std::string &null_check_suffix, const SQLTypeInfo &, const CompilationOptions &)
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:329
static bool alwaysCloneRuntimeFunction(const llvm::Function *func)
ColumnMap prepare(const Analyzer::Expr *)
std::unique_ptr< CgenState > own_cgen_state_
llvm::Value * codegenCase(const Analyzer::CaseExpr *, llvm::Type *case_llvm_type, const bool is_real_str, const CompilationOptions &)
Definition: CaseIR.cpp:55
std::vector< llvm::Value * > generate_column_heads_load(const int num_columns, llvm::Value *byte_stream_arg, llvm::IRBuilder<> &ir_builder, llvm::LLVMContext &ctx)
Expression class for the LOWER (lowercase) string function. The &quot;arg&quot; constructor parameter must be a...
Definition: Analyzer.h:791
std::vector< void * > generateNativeGPUCode(llvm::Function *func, llvm::Function *wrapper_func, const CompilationOptions &co)
ExtractField
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
llvm::Value * codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:276
static bool prioritizeQuals(const RelAlgExecutionUnit &ra_exe_unit, std::vector< Analyzer::Expr * > &primary_quals, std::vector< Analyzer::Expr * > &deferred_quals, const PlanState::HoistedFiltersSet &hoisted_quals)
Definition: LogicalIR.cpp:157
llvm::Value * codegenFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *, const CompilationOptions &)
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:184
llvm::Value * codegenCastBetweenTimestamps(llvm::Value *ts_lv, const SQLTypeInfo &operand_dimen, const SQLTypeInfo &target_dimen, const bool nullable)
Definition: CastIR.cpp:155
llvm::Value * codegenUnnest(const Analyzer::UOper *, const CompilationOptions &)
Definition: ArrayIR.cpp:20
std::vector< llvm::Value * > codegenGeoArgs(const std::vector< std::shared_ptr< Analyzer::Expr >> &, const CompilationOptions &)
Definition: GeoIR.cpp:169
llvm::Value * finalize(llvm::Value *null_lv, llvm::Value *notnull_lv)
Definition: IRCodegen.cpp:1018
llvm::Value * colByteStream(const Analyzer::ColumnVar *col_var, const bool fetch_column, const bool hoist_literals)
Definition: ColumnIR.cpp:496
llvm::Value * codegenIsNullNumber(llvm::Value *, const SQLTypeInfo &)
Definition: LogicalIR.cpp:409
llvm::Value * codegenLogical(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:298
llvm::Value * codegenWindowPosition(WindowFunctionContext *window_func_context, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:226
std::unique_ptr< llvm::Module > module_
llvm::Value * codegenCompression(const SQLTypeInfo &type_info)
std::vector< std::shared_ptr< Analyzer::ColumnVar > > inputs
llvm::Value * codegenCastFromString(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const bool operand_is_const, const CompilationOptions &co)
Definition: CastIR.cpp:187
CodeGenerator(CgenState *cgen_state, PlanState *plan_state)
Definition: CodeGenerator.h:34
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:20
llvm::Value * codegenDateTruncHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const DatetruncField &)
Definition: DateTimeIR.cpp:299
std::unique_ptr< InValuesBitmap > createInValuesBitmap(const Analyzer::InValues *, const CompilationOptions &)
Definition: InValuesIR.cpp:111
llvm::Value * codegenMul(const Analyzer::BinOper *, llvm::Value *, llvm::Value *, const std::string &null_typename, const std::string &null_check_suffix, const SQLTypeInfo &, const CompilationOptions &, bool downscale=true)
std::shared_ptr< const Analyzer::ColumnVar > hashJoinLhsTuple(const Analyzer::ColumnVar *rhs, const Analyzer::BinOper *tautological_eq) const
Definition: ColumnIR.cpp:607
std::vector< llvm::Value * > codegenVariableLengthStringColVar(llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:305
void codegenGeoLineStringArgs(const std::string &udf_func_name, size_t param_num, llvm::Value *line_string_buf, llvm::Value *line_string_size, llvm::Value *compression, llvm::Value *input_srid, llvm::Value *output_srid, std::vector< llvm::Value * > &output_args)
llvm::Value * codegenAdd(const Analyzer::BinOper *, llvm::Value *, llvm::Value *, const std::string &null_typename, const std::string &null_check_suffix, const SQLTypeInfo &, const CompilationOptions &)
bool checkExpressionRanges(const Analyzer::UOper *, int64_t, int64_t)
llvm::Value * codegenFixedLengthColVar(const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
Definition: ColumnIR.cpp:239
llvm::Value * codegenLogicalShortCircuit(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:196
static std::unique_ptr< llvm::TargetMachine > initializeNVPTXBackend(const CudaMgr_Namespace::NvidiaDeviceArch arch)
llvm::Value * resolveGroupedColumnReference(const Analyzer::ColumnVar *)
Definition: ColumnIR.cpp:477
llvm::Value * codegenDictLike(const std::shared_ptr< Analyzer::Expr > arg, const Analyzer::Constant *pattern, const bool ilike, const bool is_simple, const char escape_char, const CompilationOptions &)
Executor * executor() const
llvm::StructType * createMultiPolygonStructType(const std::string &udf_func_name, size_t param_num)