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