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