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