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