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