OmniSciDB  21ac014ffc
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups 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<DiamondCodegen> null_check;
131  };
132 
133  static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value* byte_stream,
134  llvm::Value* pos,
135  const SQLTypeInfo& ti,
136  CgenState* cgen_state);
137 
138  private:
139  std::vector<llvm::Value*> codegen(const Analyzer::Constant*,
140  const EncodingType enc_type,
141  const int dict_id,
142  const CompilationOptions&);
143 
144  virtual std::vector<llvm::Value*> codegenColumn(const Analyzer::ColumnVar*,
145  const bool fetch_column,
146  const CompilationOptions&);
147 
148  llvm::Value* codegenArith(const Analyzer::BinOper*, const CompilationOptions&);
149 
150  llvm::Value* codegenUMinus(const Analyzer::UOper*, const CompilationOptions&);
151 
152  llvm::Value* codegenCmp(const Analyzer::BinOper*, const CompilationOptions&);
153 
154  llvm::Value* codegenCmp(const SQLOps,
155  const SQLQualifier,
156  std::vector<llvm::Value*>,
157  const SQLTypeInfo&,
158  const Analyzer::Expr*,
159  const CompilationOptions&);
160 
161  llvm::Value* codegenIsNull(const Analyzer::UOper*, const CompilationOptions&);
162 
163  llvm::Value* codegenIsNullNumber(llvm::Value*, const SQLTypeInfo&);
164 
165  llvm::Value* codegenLogical(const Analyzer::BinOper*, const CompilationOptions&);
166 
167  llvm::Value* codegenLogical(const Analyzer::UOper*, const CompilationOptions&);
168 
169  llvm::Value* codegenCast(const Analyzer::UOper*, const CompilationOptions&);
170 
171  llvm::Value* codegenCast(llvm::Value* operand_lv,
172  const SQLTypeInfo& operand_ti,
173  const SQLTypeInfo& ti,
174  const bool operand_is_const,
175  const CompilationOptions& co);
176 
177  llvm::Value* codegen(const Analyzer::InValues*, const CompilationOptions&);
178 
179  llvm::Value* codegen(const Analyzer::InIntegerSet* expr, const CompilationOptions& co);
180 
181  std::vector<llvm::Value*> codegen(const Analyzer::CaseExpr*, const CompilationOptions&);
182 
183  llvm::Value* codegen(const Analyzer::ExtractExpr*, const CompilationOptions&);
184 
185  llvm::Value* codegen(const Analyzer::DateaddExpr*, const CompilationOptions&);
186 
187  llvm::Value* codegen(const Analyzer::DatediffExpr*, const CompilationOptions&);
188 
189  llvm::Value* codegen(const Analyzer::DatetruncExpr*, const CompilationOptions&);
190 
191  llvm::Value* codegen(const Analyzer::CharLengthExpr*, const CompilationOptions&);
192 
193  llvm::Value* codegen(const Analyzer::KeyForStringExpr*, const CompilationOptions&);
194 
195  llvm::Value* codegen(const Analyzer::SampleRatioExpr*, const CompilationOptions&);
196 
197  llvm::Value* codegen(const Analyzer::LowerExpr*, const CompilationOptions&);
198 
199  llvm::Value* codegen(const Analyzer::LikeExpr*, const CompilationOptions&);
200 
201  llvm::Value* codegen(const Analyzer::RegexpExpr*, const CompilationOptions&);
202 
203  llvm::Value* codegenUnnest(const Analyzer::UOper*, const CompilationOptions&);
204 
205  llvm::Value* codegenArrayAt(const Analyzer::BinOper*, const CompilationOptions&);
206 
207  llvm::Value* codegen(const Analyzer::CardinalityExpr*, const CompilationOptions&);
208 
209  std::vector<llvm::Value*> codegenArrayExpr(const Analyzer::ArrayExpr*,
210  const CompilationOptions&);
211 
212  std::vector<llvm::Value*> codegenGeoColumnVar(const Analyzer::GeoColumnVar*,
213  const bool fetch_columns,
214  const CompilationOptions& co);
215 
216  std::vector<llvm::Value*> codegenGeoExpr(const Analyzer::GeoExpr*,
217  const CompilationOptions&);
218 
219  std::vector<llvm::Value*> codegenGeoConstant(const Analyzer::GeoConstant*,
220  const CompilationOptions&);
221 
222  std::vector<llvm::Value*> codegenGeoOperator(const Analyzer::GeoOperator*,
223  const CompilationOptions&);
224 
225  std::vector<llvm::Value*> codegenGeoFunctionOperator(
227  const CompilationOptions&);
228 
229  std::vector<llvm::Value*> codegenGeoUOper(const Analyzer::GeoUOper*,
230  const CompilationOptions&);
231 
232  std::vector<llvm::Value*> codegenGeoBinOper(const Analyzer::GeoBinOper*,
233  const CompilationOptions&);
234 
235  std::vector<llvm::Value*> codegenGeosPredicateCall(const std::string&,
236  std::vector<llvm::Value*>,
237  const CompilationOptions&);
238 
239  std::vector<llvm::Value*> codegenGeosConstructorCall(const std::string&,
240  std::vector<llvm::Value*>,
241  const CompilationOptions&);
242 
243  std::vector<llvm::Value*> codegenGeoArgs(
244  const std::vector<std::shared_ptr<Analyzer::Expr>>&,
245  const CompilationOptions&);
246 
247  llvm::Value* codegenFunctionOper(const Analyzer::FunctionOper*,
248  const CompilationOptions&);
249 
252  const CompilationOptions&);
253 
254  llvm::Value* codegen(const Analyzer::BinOper*, const CompilationOptions&);
255 
256  llvm::Value* codegen(const Analyzer::UOper*, const CompilationOptions&);
257 
258  std::vector<llvm::Value*> codegenHoistedConstantsLoads(const SQLTypeInfo& type_info,
259  const EncodingType enc_type,
260  const int dict_id,
261  const int16_t lit_off);
262 
263  std::vector<llvm::Value*> codegenHoistedConstantsPlaceholders(
264  const SQLTypeInfo& type_info,
265  const EncodingType enc_type,
266  const int16_t lit_off,
267  const std::vector<llvm::Value*>& literal_loads);
268 
269  std::vector<llvm::Value*> codegenColVar(const Analyzer::ColumnVar*,
270  const bool fetch_column,
271  const bool update_query_plan,
272  const CompilationOptions&);
273 
274  llvm::Value* codegenFixedLengthColVar(const Analyzer::ColumnVar* col_var,
275  llvm::Value* col_byte_stream,
276  llvm::Value* pos_arg);
277 
278  // Generates code for a fixed length column when a window function is active.
279  llvm::Value* codegenFixedLengthColVarInWindow(const Analyzer::ColumnVar* col_var,
280  llvm::Value* col_byte_stream,
281  llvm::Value* pos_arg);
282 
283  // Generate the position for the given window function and the query iteration position.
284  llvm::Value* codegenWindowPosition(WindowFunctionContext* window_func_context,
285  llvm::Value* pos_arg);
286 
287  std::vector<llvm::Value*> codegenVariableLengthStringColVar(
288  llvm::Value* col_byte_stream,
289  llvm::Value* pos_arg);
290 
291  llvm::Value* codegenRowId(const Analyzer::ColumnVar* col_var,
292  const CompilationOptions& co);
293 
294  llvm::Value* codgenAdjustFixedEncNull(llvm::Value*, const SQLTypeInfo&);
295 
296  std::vector<llvm::Value*> codegenOuterJoinNullPlaceholder(
297  const Analyzer::ColumnVar* col_var,
298  const bool fetch_column,
299  const CompilationOptions& co);
300 
301  llvm::Value* codegenIntArith(const Analyzer::BinOper*,
302  llvm::Value*,
303  llvm::Value*,
304  const CompilationOptions&);
305 
306  llvm::Value* codegenFpArith(const Analyzer::BinOper*, llvm::Value*, llvm::Value*);
307 
308  llvm::Value* codegenCastTimestampToDate(llvm::Value* ts_lv,
309  const int dimen,
310  const bool nullable);
311 
312  llvm::Value* codegenCastBetweenTimestamps(llvm::Value* ts_lv,
313  const SQLTypeInfo& operand_dimen,
314  const SQLTypeInfo& target_dimen,
315  const bool nullable);
316 
317  llvm::Value* codegenCastFromString(llvm::Value* operand_lv,
318  const SQLTypeInfo& operand_ti,
319  const SQLTypeInfo& ti,
320  const bool operand_is_const,
321  const CompilationOptions& co);
322 
323  llvm::Value* codegenCastToFp(llvm::Value* operand_lv,
324  const SQLTypeInfo& operand_ti,
325  const SQLTypeInfo& ti);
326 
327  llvm::Value* codegenCastFromFp(llvm::Value* operand_lv,
328  const SQLTypeInfo& operand_ti,
329  const SQLTypeInfo& ti);
330 
331  llvm::Value* codegenAdd(const Analyzer::BinOper*,
332  llvm::Value*,
333  llvm::Value*,
334  const std::string& null_typename,
335  const std::string& null_check_suffix,
336  const SQLTypeInfo&,
337  const CompilationOptions&);
338 
339  llvm::Value* codegenSub(const Analyzer::BinOper*,
340  llvm::Value*,
341  llvm::Value*,
342  const std::string& null_typename,
343  const std::string& null_check_suffix,
344  const SQLTypeInfo&,
345  const CompilationOptions&);
346 
347  void codegenSkipOverflowCheckForNull(llvm::Value* lhs_lv,
348  llvm::Value* rhs_lv,
349  llvm::BasicBlock* no_overflow_bb,
350  const SQLTypeInfo& ti);
351 
352  llvm::Value* codegenMul(const Analyzer::BinOper*,
353  llvm::Value*,
354  llvm::Value*,
355  const std::string& null_typename,
356  const std::string& null_check_suffix,
357  const SQLTypeInfo&,
358  const CompilationOptions&,
359  bool downscale = true);
360 
361  llvm::Value* codegenDiv(llvm::Value*,
362  llvm::Value*,
363  const std::string& null_typename,
364  const std::string& null_check_suffix,
365  const SQLTypeInfo&,
366  bool upscale = true);
367 
368  llvm::Value* codegenDeciDiv(const Analyzer::BinOper*, const CompilationOptions&);
369 
370  llvm::Value* codegenMod(llvm::Value*,
371  llvm::Value*,
372  const std::string& null_typename,
373  const std::string& null_check_suffix,
374  const SQLTypeInfo&);
375 
376  llvm::Value* codegenCase(const Analyzer::CaseExpr*,
377  llvm::Type* case_llvm_type,
378  const bool is_real_str,
379  const CompilationOptions&);
380 
381  llvm::Value* codegenExtractHighPrecisionTimestamps(llvm::Value*,
382  const SQLTypeInfo&,
383  const ExtractField&);
384 
385  llvm::Value* codegenDateTruncHighPrecisionTimestamps(llvm::Value*,
386  const SQLTypeInfo&,
387  const DatetruncField&);
388 
389  llvm::Value* codegenCmpDecimalConst(const SQLOps,
390  const SQLQualifier,
391  const Analyzer::Expr*,
392  const SQLTypeInfo&,
393  const Analyzer::Expr*,
394  const CompilationOptions&);
395 
396  llvm::Value* codegenOverlaps(const SQLOps,
397  const SQLQualifier,
398  const std::shared_ptr<Analyzer::Expr>,
399  const std::shared_ptr<Analyzer::Expr>,
400  const CompilationOptions&);
401 
402  llvm::Value* codegenStrCmp(const SQLOps,
403  const SQLQualifier,
404  const std::shared_ptr<Analyzer::Expr>,
405  const std::shared_ptr<Analyzer::Expr>,
406  const CompilationOptions&);
407 
408  llvm::Value* codegenQualifierCmp(const SQLOps,
409  const SQLQualifier,
410  std::vector<llvm::Value*>,
411  const Analyzer::Expr*,
412  const CompilationOptions&);
413 
414  llvm::Value* codegenLogicalShortCircuit(const Analyzer::BinOper*,
415  const CompilationOptions&);
416 
417  llvm::Value* codegenDictLike(const std::shared_ptr<Analyzer::Expr> arg,
418  const Analyzer::Constant* pattern,
419  const bool ilike,
420  const bool is_simple,
421  const char escape_char,
422  const CompilationOptions&);
423 
424  llvm::Value* codegenDictStrCmp(const std::shared_ptr<Analyzer::Expr>,
425  const std::shared_ptr<Analyzer::Expr>,
426  const SQLOps,
427  const CompilationOptions& co);
428 
429  llvm::Value* codegenDictRegexp(const std::shared_ptr<Analyzer::Expr> arg,
430  const Analyzer::Constant* pattern,
431  const char escape_char,
432  const CompilationOptions&);
433 
434  // Returns the IR value which holds true iff at least one match has been found for outer
435  // join, null if there's no outer join condition on the given nesting level.
436  llvm::Value* foundOuterJoinMatch(const size_t nesting_level) const;
437 
439 
440  llvm::Value* colByteStream(const Analyzer::ColumnVar* col_var,
441  const bool fetch_column,
442  const bool hoist_literals);
443 
444  std::shared_ptr<const Analyzer::Expr> hashJoinLhs(const Analyzer::ColumnVar* rhs) const;
445 
446  std::shared_ptr<const Analyzer::ColumnVar> hashJoinLhsTuple(
447  const Analyzer::ColumnVar* rhs,
448  const Analyzer::BinOper* tautological_eq) const;
449 
450  std::unique_ptr<InValuesBitmap> createInValuesBitmap(const Analyzer::InValues*,
451  const CompilationOptions&);
452 
453  bool checkExpressionRanges(const Analyzer::UOper*, int64_t, int64_t);
454 
455  bool checkExpressionRanges(const Analyzer::BinOper*, int64_t, int64_t);
456 
458  llvm::BasicBlock* args_null_bb;
459  llvm::BasicBlock* args_notnull_bb;
460  llvm::BasicBlock* orig_bb;
461  };
462 
463  std::tuple<ArgNullcheckBBs, llvm::Value*> beginArgsNullcheck(
464  const Analyzer::FunctionOper* function_oper,
465  const std::vector<llvm::Value*>& orig_arg_lvs);
466 
467  llvm::Value* endArgsNullcheck(const ArgNullcheckBBs&,
468  llvm::Value*,
469  llvm::Value*,
470  const Analyzer::FunctionOper*);
471 
473  const std::vector<llvm::Value*>&);
474 
475  llvm::Value* codegenCompression(const SQLTypeInfo& type_info);
476 
477  std::pair<llvm::Value*, llvm::Value*> codegenArrayBuff(llvm::Value* chunk,
478  llvm::Value* row_pos,
479  SQLTypes array_type,
480  bool cast_and_extend);
481 
482  void codegenBufferArgs(const std::string& udf_func_name,
483  size_t param_num,
484  llvm::Value* buffer_buf,
485  llvm::Value* buffer_size,
486  llvm::Value* buffer_is_null,
487  std::vector<llvm::Value*>& output_args);
488 
489  llvm::StructType* createPointStructType(const std::string& udf_func_name,
490  size_t param_num);
491 
492  void codegenGeoPointArgs(const std::string& udf_func_name,
493  size_t param_num,
494  llvm::Value* point_buf,
495  llvm::Value* point_size,
496  llvm::Value* compression,
497  llvm::Value* input_srid,
498  llvm::Value* output_srid,
499  std::vector<llvm::Value*>& output_args);
500 
501  llvm::StructType* createLineStringStructType(const std::string& udf_func_name,
502  size_t param_num);
503 
504  void codegenGeoLineStringArgs(const std::string& udf_func_name,
505  size_t param_num,
506  llvm::Value* line_string_buf,
507  llvm::Value* line_string_size,
508  llvm::Value* compression,
509  llvm::Value* input_srid,
510  llvm::Value* output_srid,
511  std::vector<llvm::Value*>& output_args);
512 
513  llvm::StructType* createPolygonStructType(const std::string& udf_func_name,
514  size_t param_num);
515 
516  void codegenGeoPolygonArgs(const std::string& udf_func_name,
517  size_t param_num,
518  llvm::Value* polygon_buf,
519  llvm::Value* polygon_size,
520  llvm::Value* ring_sizes_buf,
521  llvm::Value* num_rings,
522  llvm::Value* compression,
523  llvm::Value* input_srid,
524  llvm::Value* output_srid,
525  std::vector<llvm::Value*>& output_args);
526 
527  llvm::StructType* createMultiPolygonStructType(const std::string& udf_func_name,
528  size_t param_num);
529 
530  void codegenGeoMultiPolygonArgs(const std::string& udf_func_name,
531  size_t param_num,
532  llvm::Value* polygon_coords,
533  llvm::Value* polygon_coords_size,
534  llvm::Value* ring_sizes_buf,
535  llvm::Value* ring_sizes,
536  llvm::Value* polygon_bounds,
537  llvm::Value* polygon_bounds_sizes,
538  llvm::Value* compression,
539  llvm::Value* input_srid,
540  llvm::Value* output_srid,
541  std::vector<llvm::Value*>& output_args);
542 
543  std::vector<llvm::Value*> codegenFunctionOperCastArgs(
544  const Analyzer::FunctionOper*,
545  const ExtensionFunction*,
546  const std::vector<llvm::Value*>&,
547  const std::vector<size_t>&,
548  const std::unordered_map<llvm::Value*, llvm::Value*>&,
549  const CompilationOptions&);
550 
551  // Return LLVM intrinsic providing fast arithmetic with overflow check
552  // for the given binary operation.
553  llvm::Function* getArithWithOverflowIntrinsic(const Analyzer::BinOper* bin_oper,
554  llvm::Type* type);
555 
556  // Generate code for the given binary operation with overflow check.
557  // Signed integer add, sub and mul operations are supported. Overflow
558  // check is performed using LLVM arithmetic intrinsics which are not
559  // supported for GPU. Return the IR value which holds operation result.
560  llvm::Value* codegenBinOpWithOverflowForCPU(const Analyzer::BinOper* bin_oper,
561  llvm::Value* lhs_lv,
562  llvm::Value* rhs_lv,
563  const std::string& null_check_suffix,
564  const SQLTypeInfo& ti);
565 
566  Executor* executor_;
567 
568  protected:
569  Executor* executor() const {
570  if (!executor_) {
571  throw ExecutorRequired();
572  }
573  return executor_;
574  }
575 
578 
579  friend class GroupByAndAggregate;
580 };
581 
582 // Code generator specialized for scalar expressions which doesn't require an executor.
584  public:
585  // Constructor which takes the runtime module.
586  ScalarCodeGenerator(std::unique_ptr<llvm::Module> module)
587  : CodeGenerator(nullptr, nullptr), module_(std::move(module)) {}
588 
589  // Function generated for a given analyzer expression. For GPU, a wrapper which meets
590  // the kernel signature constraints (returns void, takes all arguments as pointers) is
591  // generated. Also returns the list of column expressions for which compatible input
592  // parameters must be passed to the input of the generated function.
594  llvm::Function* func;
595  llvm::Function* wrapper_func;
596  std::vector<std::shared_ptr<Analyzer::ColumnVar>> inputs;
597  };
598 
599  // Compiles the given scalar expression to IR and the list of columns in the expression,
600  // needed to provide inputs to the generated function.
602  const bool fetch_columns,
603  const CompilationOptions& co);
604 
605  // Generates the native function pointers for each device.
606  // NB: this is separated from the compile method to allow building higher level code
607  // generators which can inline the IR for evaluating a single expression (for example
608  // loops).
609  std::vector<void*> generateNativeCode(const CompiledExpression& compiled_expression,
610  const CompilationOptions& co);
611 
613 
614  using ColumnMap =
615  std::unordered_map<InputColDescriptor, std::shared_ptr<Analyzer::ColumnVar>>;
616 
617  private:
618  std::vector<llvm::Value*> codegenColumn(const Analyzer::ColumnVar*,
619  const bool fetch_column,
620  const CompilationOptions&) override;
621 
622  // Collect the columns used by the given analyzer expressions and fills in the column
623  // map to be used during code generation.
625 
626  std::vector<void*> generateNativeGPUCode(llvm::Function* func,
627  llvm::Function* wrapper_func,
628  const CompilationOptions& co);
629 
630  std::unique_ptr<llvm::Module> module_;
632  std::unique_ptr<CgenState> own_cgen_state_;
633  std::unique_ptr<PlanState> own_plan_state_;
634  std::unique_ptr<CudaMgr_Namespace::CudaMgr> cuda_mgr_;
635  std::shared_ptr<GpuCompilationContext> gpu_compilation_context_;
636  std::unique_ptr<llvm::TargetMachine> nvptx_target_machine_;
637 };
638 
643 std::unique_ptr<llvm::Module> runtime_module_shallow_copy(CgenState* cgen_state);
644 
648 std::vector<llvm::Value*> generate_column_heads_load(const int num_columns,
649  llvm::Value* byte_stream_arg,
650  llvm::IRBuilder<>& ir_builder,
651  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:1001
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:366
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:254
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:475
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
std::vector< llvm::Value * > codegenGeoExpr(const Analyzer::GeoExpr *, const CompilationOptions &)
Definition: GeoIR.cpp:79
std::vector< llvm::Value * > codegenGeoFunctionOperator(const Analyzer::GeoFunctionOperator *, const CompilationOptions &)
Definition: GeoIR.cpp:165
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:398
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:285
std::vector< llvm::Value * > codegenGeoOperator(const Analyzer::GeoOperator *, const CompilationOptions &)
Definition: GeoIR.cpp:110
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:410
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:119
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:209
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:671
llvm::Value * codegenOverlaps(const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
Definition: CompareIR.cpp:285
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:89
std::vector< llvm::Value * > codegenGeosPredicateCall(const std::string &, std::vector< llvm::Value * >, const CompilationOptions &)
Definition: GeoIR.cpp:639
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:546
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 *)
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck(llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
Definition: GeoIR.cpp:23
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
std::vector< llvm::Value * > codegenGeoColumnVar(const Analyzer::GeoColumnVar *, const bool fetch_columns, const CompilationOptions &co)
Definition: GeoIR.cpp:52
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:230
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:558
llvm::Value * finalize(llvm::Value *null_lv, llvm::Value *notnull_lv)
Definition: IRCodegen.cpp:1036
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< llvm::Value * > codegenGeoConstant(const Analyzer::GeoConstant *, const CompilationOptions &)
Definition: GeoIR.cpp:97
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:298
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
std::unique_ptr< DiamondCodegen > null_check
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)