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