OmniSciDB  ca0c39ec8f
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CodeGenerator Class Reference

#include <CodeGenerator.h>

+ Inheritance diagram for CodeGenerator:
+ Collaboration diagram for CodeGenerator:

Classes

struct  ArgNullcheckBBs
 
struct  ExecutorRequired
 
struct  GPUTarget
 
struct  NullCheckCodegen
 

Public Member Functions

 CodeGenerator (Executor *executor)
 
 CodeGenerator (CgenState *cgen_state, PlanState *plan_state)
 
std::vector< llvm::Value * > codegen (const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
 
llvm::Value * codegenPerRowStringOper (const Analyzer::StringOper *string_oper, const CompilationOptions &co)
 
llvm::Value * codegenPseudoStringOper (const Analyzer::ColumnVar *, const std::vector< StringOps_Namespace::StringOpInfo > &string_op_infos, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenHoistedConstants (const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const int dict_id)
 
llvm::Value * codegenCastBetweenIntTypes (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
 
void codegenCastBetweenIntTypesOverflowChecks (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
 
llvm::Value * posArg (const Analyzer::Expr *) const
 
llvm::Value * toBool (llvm::Value *)
 
llvm::Value * castArrayPointer (llvm::Value *ptr, const SQLTypeInfo &elem_ti)
 
llvm::Value * codegenCastBetweenTimestamps (llvm::Value *ts_lv, const SQLTypeInfo &operand_dimen, const SQLTypeInfo &target_dimen, const bool nullable)
 
llvm::Value * codegenWindowPosition (const WindowFunctionContext *window_func_context, llvm::Value *pos_arg)
 

Static Public Member Functions

static llvm::ConstantInt * codegenIntConst (const Analyzer::Constant *constant, CgenState *cgen_state)
 
static std::unordered_set
< llvm::Function * > 
markDeadRuntimeFuncs (llvm::Module &module, const std::vector< llvm::Function * > &roots, const std::vector< llvm::Function * > &leaves)
 
static ExecutionEngineWrapper generateNativeCPUCode (llvm::Function *func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co)
 
static std::string generatePTX (const std::string &cuda_llir, llvm::TargetMachine *nvptx_target_machine, llvm::LLVMContext &context)
 
static std::unique_ptr
< llvm::TargetMachine > 
initializeNVPTXBackend (const CudaMgr_Namespace::NvidiaDeviceArch arch)
 
static bool alwaysCloneRuntimeFunction (const llvm::Function *func)
 
static void linkModuleWithLibdevice (Executor *executor, llvm::Module &module, llvm::PassManagerBuilder &pass_manager_builder, const GPUTarget &gpu_target)
 
static std::shared_ptr
< GpuCompilationContext
generateNativeGPUCode (Executor *executor, llvm::Function *func, llvm::Function *wrapper_func, const std::unordered_set< llvm::Function * > &live_funcs, const bool is_gpu_smem_used, const CompilationOptions &co, const GPUTarget &gpu_target)
 
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)
 
static bool prioritizeQuals (const RelAlgExecutionUnit &ra_exe_unit, std::vector< Analyzer::Expr * > &primary_quals, std::vector< Analyzer::Expr * > &deferred_quals, const PlanState::HoistedFiltersSet &hoisted_quals)
 
static ArrayLoadCodegen codegenGeoArrayLoadAndNullcheck (llvm::Value *byte_stream, llvm::Value *pos, const SQLTypeInfo &ti, CgenState *cgen_state)
 

Protected Member Functions

Executorexecutor () const
 

Protected Attributes

CgenStatecgen_state_
 
PlanStateplan_state_
 

Private Member Functions

std::vector< llvm::Value * > codegen (const Analyzer::Constant *, const EncodingType enc_type, const int dict_id, const CompilationOptions &)
 
virtual std::vector
< llvm::Value * > 
codegenColumn (const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &)
 
llvm::Value * codegenArith (const Analyzer::BinOper *, const CompilationOptions &)
 
llvm::Value * codegenUMinus (const Analyzer::UOper *, const CompilationOptions &)
 
llvm::Value * codegenCmp (const Analyzer::BinOper *, const CompilationOptions &)
 
llvm::Value * codegenCmp (const SQLOps, const SQLQualifier, std::vector< llvm::Value * >, const SQLTypeInfo &, const Analyzer::Expr *, const CompilationOptions &)
 
llvm::Value * codegenIsNull (const Analyzer::UOper *, const CompilationOptions &)
 
llvm::Value * codegenIsNullNumber (llvm::Value *, const SQLTypeInfo &)
 
llvm::Value * codegenLogical (const Analyzer::BinOper *, const CompilationOptions &)
 
llvm::Value * codegenLogical (const Analyzer::UOper *, const CompilationOptions &)
 
llvm::Value * codegenCast (const Analyzer::UOper *, const CompilationOptions &)
 
llvm::Value * codegenCast (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const bool operand_is_const, const CompilationOptions &co)
 
llvm::Value * codegen (const Analyzer::InValues *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::InIntegerSet *expr, const CompilationOptions &co)
 
std::vector< llvm::Value * > codegen (const Analyzer::CaseExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::ExtractExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::DateaddExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::DatediffExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::DatetruncExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::CharLengthExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::KeyForStringExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::SampleRatioExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::WidthBucketExpr *, const CompilationOptions &)
 
llvm::Value * codegenConstantWidthBucketExpr (const Analyzer::WidthBucketExpr *, const CompilationOptions &)
 
llvm::Value * codegenWidthBucketExpr (const Analyzer::WidthBucketExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::StringOper *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::LikeExpr *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::RegexpExpr *, const CompilationOptions &)
 
llvm::Value * codegenUnnest (const Analyzer::UOper *, const CompilationOptions &)
 
llvm::Value * codegenArrayAt (const Analyzer::BinOper *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::CardinalityExpr *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenArrayExpr (const Analyzer::ArrayExpr *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeoColumnVar (const Analyzer::GeoColumnVar *, const bool fetch_columns, const CompilationOptions &co)
 
std::vector< llvm::Value * > codegenGeoExpr (const Analyzer::GeoExpr *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeoConstant (const Analyzer::GeoConstant *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeoOperator (const Analyzer::GeoOperator *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeoUOper (const Analyzer::GeoUOper *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeoBinOper (const Analyzer::GeoBinOper *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeosPredicateCall (const std::string &, std::vector< llvm::Value * >, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeosConstructorCall (const std::string &, std::vector< llvm::Value * >, llvm::Value *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenGeoArgs (const std::vector< std::shared_ptr< Analyzer::Expr >> &, const CompilationOptions &)
 
llvm::Value * codegenFunctionOper (const Analyzer::FunctionOper *, const CompilationOptions &)
 
llvm::Value * codegenFunctionOperWithCustomTypeHandling (const Analyzer::FunctionOperWithCustomTypeHandling *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::BinOper *, const CompilationOptions &)
 
llvm::Value * codegen (const Analyzer::UOper *, const CompilationOptions &)
 
std::vector< llvm::Value * > codegenHoistedConstantsLoads (const SQLTypeInfo &type_info, const EncodingType enc_type, const int dict_id, const int16_t lit_off, const size_t lit_bytes)
 
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)
 
std::vector< llvm::Value * > codegenColVar (const Analyzer::ColumnVar *, const bool fetch_column, const bool update_query_plan, const CompilationOptions &)
 
llvm::Value * codegenFixedLengthColVar (const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg, const WindowFunctionContext *window_function_context=nullptr)
 
llvm::Value * codegenFixedLengthColVarInWindow (const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg, const WindowFunctionContext *window_function_context=nullptr)
 
std::vector< llvm::Value * > codegenVariableLengthStringColVar (llvm::Value *col_byte_stream, llvm::Value *pos_arg)
 
llvm::Value * codegenRowId (const Analyzer::ColumnVar *col_var, const CompilationOptions &co)
 
llvm::Value * codgenAdjustFixedEncNull (llvm::Value *, const SQLTypeInfo &)
 
std::vector< llvm::Value * > codegenOuterJoinNullPlaceholder (const Analyzer::ColumnVar *col_var, const bool fetch_column, const CompilationOptions &co)
 
llvm::Value * codegenIntArith (const Analyzer::BinOper *, llvm::Value *, llvm::Value *, const CompilationOptions &)
 
llvm::Value * codegenFpArith (const Analyzer::BinOper *, llvm::Value *, llvm::Value *)
 
llvm::Value * codegenCastTimestampToTime (llvm::Value *ts_lv, const int dimen, const bool nullable)
 
llvm::Value * codegenCastTimestampToDate (llvm::Value *ts_lv, const int dimen, const bool nullable)
 
llvm::Value * codegenCastFromString (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const bool operand_is_const, const CompilationOptions &co)
 
llvm::Value * codegenCastToFp (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
 
llvm::Value * codegenCastFromFp (llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
 
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 &)
 
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 codegenSkipOverflowCheckForNull (llvm::Value *lhs_lv, llvm::Value *rhs_lv, llvm::BasicBlock *no_overflow_bb, const SQLTypeInfo &ti)
 
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)
 
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 * codegenDeciDiv (const Analyzer::BinOper *, const CompilationOptions &)
 
llvm::Value * codegenMod (llvm::Value *, llvm::Value *, const std::string &null_typename, const std::string &null_check_suffix, const SQLTypeInfo &)
 
llvm::Value * codegenCase (const Analyzer::CaseExpr *, llvm::Type *case_llvm_type, const bool is_real_str, const CompilationOptions &)
 
llvm::Value * codegenExtractHighPrecisionTimestamps (llvm::Value *, const SQLTypeInfo &, const ExtractField &)
 
llvm::Value * codegenDateTruncHighPrecisionTimestamps (llvm::Value *, const SQLTypeInfo &, const DatetruncField &)
 
llvm::Value * codegenCmpDecimalConst (const SQLOps, const SQLQualifier, const Analyzer::Expr *, const SQLTypeInfo &, const Analyzer::Expr *, const CompilationOptions &)
 
llvm::Value * codegenOverlaps (const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
 
llvm::Value * codegenStrCmp (const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
 
llvm::Value * codegenQualifierCmp (const SQLOps, const SQLQualifier, std::vector< llvm::Value * >, const Analyzer::Expr *, const CompilationOptions &)
 
llvm::Value * codegenLogicalShortCircuit (const Analyzer::BinOper *, const CompilationOptions &)
 
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::Value * codegenDictStrCmp (const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const SQLOps, const CompilationOptions &co)
 
llvm::Value * codegenDictRegexp (const std::shared_ptr< Analyzer::Expr > arg, const Analyzer::Constant *pattern, const char escape_char, const CompilationOptions &)
 
llvm::Value * foundOuterJoinMatch (const size_t nesting_level) const
 
llvm::Value * resolveGroupedColumnReference (const Analyzer::ColumnVar *)
 
llvm::Value * colByteStream (const Analyzer::ColumnVar *col_var, const bool fetch_column, const bool hoist_literals)
 
std::shared_ptr< const
Analyzer::Expr
hashJoinLhs (const Analyzer::ColumnVar *rhs) const
 
std::shared_ptr< const
Analyzer::ColumnVar
hashJoinLhsTuple (const Analyzer::ColumnVar *rhs, const Analyzer::BinOper *tautological_eq) const
 
bool needCastForHashJoinLhs (const Analyzer::ColumnVar *rhs) const
 
std::unique_ptr< InValuesBitmapcreateInValuesBitmap (const Analyzer::InValues *, const CompilationOptions &)
 
bool checkExpressionRanges (const Analyzer::UOper *, int64_t, int64_t)
 
bool checkExpressionRanges (const Analyzer::BinOper *, int64_t, int64_t)
 
std::tuple< ArgNullcheckBBs,
llvm::Value * > 
beginArgsNullcheck (const Analyzer::FunctionOper *function_oper, const std::vector< llvm::Value * > &orig_arg_lvs)
 
llvm::Value * endArgsNullcheck (const ArgNullcheckBBs &, llvm::Value *, llvm::Value *, const Analyzer::FunctionOper *)
 
llvm::Value * codegenFunctionOperNullArg (const Analyzer::FunctionOper *, const std::vector< llvm::Value * > &)
 
llvm::Value * codegenCompression (const SQLTypeInfo &type_info)
 
std::pair< llvm::Value
*, llvm::Value * > 
codegenArrayBuff (llvm::Value *chunk, llvm::Value *row_pos, SQLTypes array_type, bool cast_and_extend)
 
void codegenBufferArgs (const std::string &udf_func_name, size_t param_num, llvm::Value *buffer_buf, llvm::Value *buffer_size, llvm::Value *buffer_is_null, std::vector< llvm::Value * > &output_args)
 
llvm::StructType * createPointStructType (const std::string &udf_func_name, size_t param_num)
 
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)
 
llvm::StructType * createMultiPointStructType (const std::string &udf_func_name, size_t param_num)
 
void codegenGeoMultiPointArgs (const std::string &udf_func_name, size_t param_num, llvm::Value *multi_point_buf, llvm::Value *multi_point_size, llvm::Value *compression, llvm::Value *input_srid, llvm::Value *output_srid, std::vector< llvm::Value * > &output_args)
 
llvm::StructType * createLineStringStructType (const std::string &udf_func_name, size_t param_num)
 
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::StructType * createMultiLineStringStructType (const std::string &udf_func_name, size_t param_num)
 
void codegenGeoMultiLineStringArgs (const std::string &udf_func_name, size_t param_num, llvm::Value *multi_linestring_coords, llvm::Value *multi_linestring_size, llvm::Value *linestring_sizes, llvm::Value *linestring_sizes_size, llvm::Value *compression, llvm::Value *input_srid, llvm::Value *output_srid, std::vector< llvm::Value * > &output_args)
 
llvm::StructType * createPolygonStructType (const std::string &udf_func_name, size_t param_num)
 
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::StructType * createMultiPolygonStructType (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)
 
std::vector< llvm::Value * > codegenFunctionOperCastArgs (const Analyzer::FunctionOper *, const ExtensionFunction *, const std::vector< llvm::Value * > &, const std::vector< size_t > &, const std::unordered_map< llvm::Value *, llvm::Value * > &, const CompilationOptions &)
 
llvm::Function * getArithWithOverflowIntrinsic (const Analyzer::BinOper *bin_oper, llvm::Type *type)
 
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)
 

Private Attributes

Executorexecutor_
 

Friends

class GroupByAndAggregate
 

Detailed Description

Definition at line 25 of file CodeGenerator.h.

Constructor & Destructor Documentation

CodeGenerator::CodeGenerator ( Executor executor)
inline

Definition at line 27 of file CodeGenerator.h.

29  , cgen_state_(executor->cgen_state_.get())
30  , plan_state_(executor->plan_state_.get()) {}
CgenState * cgen_state_
Executor * executor_
PlanState * plan_state_
Executor * executor() const
CodeGenerator::CodeGenerator ( CgenState cgen_state,
PlanState plan_state 
)
inline

Definition at line 34 of file CodeGenerator.h.

35  : executor_(nullptr), cgen_state_(cgen_state), plan_state_(plan_state) {}
CgenState * cgen_state_
Executor * executor_
PlanState * plan_state_

Member Function Documentation

bool CodeGenerator::alwaysCloneRuntimeFunction ( const llvm::Function *  func)
static

Definition at line 1493 of file NativeCodegen.cpp.

Referenced by CgenState::set_module_shallow_copy().

1493  {
1494  return func->getName() == "query_stub_hoisted_literals" ||
1495  func->getName() == "multifrag_query_hoisted_literals" ||
1496  func->getName() == "query_stub" || func->getName() == "multifrag_query" ||
1497  func->getName() == "fixed_width_int_decode" ||
1498  func->getName() == "fixed_width_unsigned_decode" ||
1499  func->getName() == "diff_fixed_width_int_decode" ||
1500  func->getName() == "fixed_width_double_decode" ||
1501  func->getName() == "fixed_width_float_decode" ||
1502  func->getName() == "fixed_width_small_date_decode" ||
1503  func->getName() == "fixed_width_date_encode" ||
1504  func->getName() == "record_error_code" || func->getName() == "get_error_code" ||
1505  func->getName() == "pos_start_impl" || func->getName() == "pos_step_impl" ||
1506  func->getName() == "group_buff_idx_impl" ||
1507  func->getName() == "init_shared_mem" ||
1508  func->getName() == "init_shared_mem_nop" || func->getName() == "write_back_nop";
1509 }

+ Here is the caller graph for this function:

std::tuple< CodeGenerator::ArgNullcheckBBs, llvm::Value * > CodeGenerator::beginArgsNullcheck ( const Analyzer::FunctionOper function_oper,
const std::vector< llvm::Value * > &  orig_arg_lvs 
)
private

Definition at line 400 of file ExtensionsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, codegenFunctionOperNullArg(), CgenState::context_, CgenState::current_func_, anonymous_namespace{ExtensionsIR.cpp}::ext_func_call_requires_nullcheck(), anonymous_namespace{ExtensionsIR.cpp}::get_buffer_struct_type(), anonymous_namespace{ExtensionsIR.cpp}::get_llvm_type_from_sql_array_type(), Analyzer::Expr::get_type_info(), Analyzer::FunctionOper::getName(), and CgenState::ir_builder_.

Referenced by codegenFunctionOper(), and codegenFunctionOperWithCustomTypeHandling().

401  {
403  llvm::BasicBlock* args_null_bb{nullptr};
404  llvm::BasicBlock* args_notnull_bb{nullptr};
405  llvm::BasicBlock* orig_bb = cgen_state_->ir_builder_.GetInsertBlock();
406  llvm::Value* null_array_alloca{nullptr};
407  // Only generate the check if required (at least one argument must be nullable).
408  if (ext_func_call_requires_nullcheck(function_oper)) {
409  const auto func_ti = function_oper->get_type_info();
410  if (func_ti.is_buffer()) {
411  const auto arr_struct_ty = get_buffer_struct_type(
412  cgen_state_,
413  function_oper->getName(),
414  0,
416  func_ti.is_array() || func_ti.is_bytes());
417  null_array_alloca = cgen_state_->ir_builder_.CreateAlloca(arr_struct_ty);
418  }
419  const auto args_notnull_lv = cgen_state_->ir_builder_.CreateNot(
420  codegenFunctionOperNullArg(function_oper, orig_arg_lvs));
421  args_notnull_bb = llvm::BasicBlock::Create(
422  cgen_state_->context_, "args_notnull", cgen_state_->current_func_);
423  args_null_bb = llvm::BasicBlock::Create(
425  cgen_state_->ir_builder_.CreateCondBr(args_notnull_lv, args_notnull_bb, args_null_bb);
426  cgen_state_->ir_builder_.SetInsertPoint(args_notnull_bb);
427  }
428  return std::make_tuple(
429  CodeGenerator::ArgNullcheckBBs{args_null_bb, args_notnull_bb, orig_bb},
430  null_array_alloca);
431 }
llvm::StructType * get_buffer_struct_type(CgenState *cgen_state, const std::string &ext_func_name, size_t param_num, llvm::Type *elem_type, bool has_is_null)
CgenState * cgen_state_
llvm::Value * codegenFunctionOperNullArg(const Analyzer::FunctionOper *, const std::vector< llvm::Value * > &)
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
bool ext_func_call_requires_nullcheck(const Analyzer::FunctionOper *function_oper)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
llvm::Type * get_llvm_type_from_sql_array_type(const SQLTypeInfo ti, llvm::LLVMContext &ctx)
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::string getName() const
Definition: Analyzer.h:2306

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::castArrayPointer ( llvm::Value *  ptr,
const SQLTypeInfo elem_ti 
)

Definition at line 1703 of file ExtensionsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CgenState::context_, SQLTypeInfo::get_compression(), SQLTypeInfo::get_size(), SQLTypeInfo::get_type(), CgenState::ir_builder_, SQLTypeInfo::is_boolean(), SQLTypeInfo::is_integer(), SQLTypeInfo::is_string(), kDOUBLE, kENCODING_DICT, and kFLOAT.

Referenced by codegenFunctionOperCastArgs(), RangeJoinHashTable::codegenKey(), and OverlapsJoinHashTable::codegenKey().

1704  {
1706  if (elem_ti.get_type() == kFLOAT) {
1707  return cgen_state_->ir_builder_.CreatePointerCast(
1708  ptr, llvm::Type::getFloatPtrTy(cgen_state_->context_));
1709  }
1710  if (elem_ti.get_type() == kDOUBLE) {
1711  return cgen_state_->ir_builder_.CreatePointerCast(
1712  ptr, llvm::Type::getDoublePtrTy(cgen_state_->context_));
1713  }
1714  CHECK(elem_ti.is_integer() || elem_ti.is_boolean() ||
1715  (elem_ti.is_string() && elem_ti.get_compression() == kENCODING_DICT));
1716  switch (elem_ti.get_size()) {
1717  case 1:
1718  return cgen_state_->ir_builder_.CreatePointerCast(
1719  ptr, llvm::Type::getInt8PtrTy(cgen_state_->context_));
1720  case 2:
1721  return cgen_state_->ir_builder_.CreatePointerCast(
1722  ptr, llvm::Type::getInt16PtrTy(cgen_state_->context_));
1723  case 4:
1724  return cgen_state_->ir_builder_.CreatePointerCast(
1725  ptr, llvm::Type::getInt32PtrTy(cgen_state_->context_));
1726  case 8:
1727  return cgen_state_->ir_builder_.CreatePointerCast(
1728  ptr, llvm::Type::getInt64PtrTy(cgen_state_->context_));
1729  default:
1730  CHECK(false);
1731  }
1732  return nullptr;
1733 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:389
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:379
llvm::LLVMContext & context_
Definition: CgenState.h:439
bool is_integer() const
Definition: sqltypes.h:577
bool is_boolean() const
Definition: sqltypes.h:582
#define AUTOMATIC_IR_METADATA(CGENSTATE)
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:387
#define CHECK(condition)
Definition: Logger.h:222
bool is_string() const
Definition: sqltypes.h:575

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool CodeGenerator::checkExpressionRanges ( const Analyzer::UOper uoper,
int64_t  min,
int64_t  max 
)
private

Definition at line 628 of file ArithmeticIR.cpp.

References CHECK, executor(), executor_, Analyzer::Expr::get_type_info(), getExpressionRange(), Integer, SQLTypeInfo::is_decimal(), ExpressionRange::makeInvalidRange(), plan_state_, and PlanState::query_infos_.

Referenced by codegenAdd(), codegenMul(), codegenSub(), and codegenUMinus().

630  {
631  if (uoper->get_type_info().is_decimal()) {
632  return false;
633  }
634 
636  if (executor_) {
637  auto expr_range_info =
638  plan_state_->query_infos_.size() > 0
640  : ExpressionRange::makeInvalidRange();
641  if (expr_range_info.getType() != ExpressionRangeType::Integer) {
642  return false;
643  }
644  if (expr_range_info.getIntMin() >= min && expr_range_info.getIntMax() <= max) {
645  return true;
646  }
647  }
648 
649  return false;
650 }
Executor * executor_
const std::vector< InputTableInfo > & query_infos_
Definition: PlanState.h:68
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
PlanState * plan_state_
#define CHECK(condition)
Definition: Logger.h:222
bool is_decimal() const
Definition: sqltypes.h:578
Executor * executor() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

bool CodeGenerator::checkExpressionRanges ( const Analyzer::BinOper bin_oper,
int64_t  min,
int64_t  max 
)
private

Definition at line 187 of file ArithmeticIR.cpp.

References CHECK, executor(), executor_, Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_right_operand(), Analyzer::Expr::get_type_info(), getExpressionRange(), Integer, SQLTypeInfo::is_decimal(), anonymous_namespace{ArithmeticIR.cpp}::is_temporary_column(), ExpressionRange::makeInvalidRange(), plan_state_, and PlanState::query_infos_.

189  {
190  if (is_temporary_column(bin_oper->get_left_operand()) ||
191  is_temporary_column(bin_oper->get_right_operand())) {
192  // Computing the range for temporary columns is a lot more expensive than the overflow
193  // check.
194  return false;
195  }
196  if (bin_oper->get_type_info().is_decimal()) {
197  return false;
198  }
199 
201  if (executor_) {
202  auto expr_range_info =
203  plan_state_->query_infos_.size() > 0
205  : ExpressionRange::makeInvalidRange();
206  if (expr_range_info.getType() != ExpressionRangeType::Integer) {
207  return false;
208  }
209  if (expr_range_info.getIntMin() >= min && expr_range_info.getIntMax() <= max) {
210  return true;
211  }
212  }
213 
214  return false;
215 }
const Expr * get_right_operand() const
Definition: Analyzer.h:451
bool is_temporary_column(const Analyzer::Expr *expr)
Executor * executor_
const std::vector< InputTableInfo > & query_infos_
Definition: PlanState.h:68
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
PlanState * plan_state_
#define CHECK(condition)
Definition: Logger.h:222
const Expr * get_left_operand() const
Definition: Analyzer.h:450
bool is_decimal() const
Definition: sqltypes.h:578
Executor * executor() const

+ Here is the call graph for this function:

std::vector< llvm::Value * > CodeGenerator::codegen ( const Analyzer::Expr expr,
const bool  fetch_columns,
const CompilationOptions co 
)

Definition at line 30 of file IRCodegen.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK_NE, codegenArrayExpr(), codegenColumn(), codegenFunctionOper(), codegenFunctionOperWithCustomTypeHandling(), codegenGeoBinOper(), codegenGeoColumnVar(), codegenGeoExpr(), codegenGeoUOper(), executor_, Analyzer::Expr::get_type_info(), kENCODING_DICT, kNULLT, posArg(), and width_bucket_expr().

Referenced by Executor::buildHoistLeftHandSideFiltersCb(), Executor::buildIsDeletedCb(), Executor::buildJoinLoops(), codegen(), GroupByAndAggregate::codegenAggArg(), codegenArith(), codegenArrayAt(), codegenArrayExpr(), codegenCase(), codegenCast(), codegenCmp(), codegenCmpDecimalConst(), HashJoin::codegenColOrStringOper(), codegenColVar(), codegenConstantWidthBucketExpr(), codegenDeciDiv(), codegenDictLike(), codegenDictRegexp(), codegenDictStrCmp(), Executor::codegenFrameBoundExpr(), codegenFunctionOper(), codegenFunctionOperWithCustomTypeHandling(), codegenGeoArgs(), codegenGeoColumnVar(), codegenGeoConstant(), codegenGeoOperator(), codegenIsNull(), RangeJoinHashTable::codegenKey(), OverlapsJoinHashTable::codegenKey(), codegenLogical(), codegenLogicalShortCircuit(), OverlapsJoinHashTable::codegenManyKey(), codegenOuterJoinNullPlaceholder(), GroupByAndAggregate::codegenOutputSlot(), codegenPerRowStringOper(), codegenPseudoStringOper(), codegenQualifierCmp(), codegenRowId(), codegenUMinus(), codegenUnnest(), codegenWidthBucketExpr(), Executor::codegenWindowFunctionAggregateCalls(), ScalarCodeGenerator::compile(), and Executor::groupByColumnCodegen().

32  {
34  if (!expr) {
35  return {posArg(expr)};
36  }
37  auto bin_oper = dynamic_cast<const Analyzer::BinOper*>(expr);
38  if (bin_oper) {
39  return {codegen(bin_oper, co)};
40  }
41  auto u_oper = dynamic_cast<const Analyzer::UOper*>(expr);
42  if (u_oper) {
43  return {codegen(u_oper, co)};
44  }
45  auto geo_col_var = dynamic_cast<const Analyzer::GeoColumnVar*>(expr);
46  if (geo_col_var) {
47  // inherits from ColumnVar, so it is important we check this first
48  return codegenGeoColumnVar(geo_col_var, fetch_columns, co);
49  }
50  auto col_var = dynamic_cast<const Analyzer::ColumnVar*>(expr);
51  if (col_var) {
52  return codegenColumn(col_var, fetch_columns, co);
53  }
54  auto constant = dynamic_cast<const Analyzer::Constant*>(expr);
55  if (constant) {
56  const auto& ti = constant->get_type_info();
57  if (ti.get_type() == kNULLT) {
58  throw std::runtime_error(
59  "NULL type literals are not currently supported in this context.");
60  }
61  if (constant->get_is_null()) {
62  return {ti.is_fp()
63  ? static_cast<llvm::Value*>(executor_->cgen_state_->inlineFpNull(ti))
64  : static_cast<llvm::Value*>(executor_->cgen_state_->inlineIntNull(ti))};
65  }
66  if (ti.get_compression() == kENCODING_DICT) {
67  // The dictionary encoding case should be handled by the parent expression
68  // (cast, for now), here is too late to know the dictionary id if not already set
69  CHECK_NE(ti.get_comp_param(), 0);
70  return {codegen(constant, ti.get_compression(), ti.get_comp_param(), co)};
71  }
72  return {codegen(constant, ti.get_compression(), 0, co)};
73  }
74  auto case_expr = dynamic_cast<const Analyzer::CaseExpr*>(expr);
75  if (case_expr) {
76  return {codegen(case_expr, co)};
77  }
78  auto extract_expr = dynamic_cast<const Analyzer::ExtractExpr*>(expr);
79  if (extract_expr) {
80  return {codegen(extract_expr, co)};
81  }
82  auto dateadd_expr = dynamic_cast<const Analyzer::DateaddExpr*>(expr);
83  if (dateadd_expr) {
84  return {codegen(dateadd_expr, co)};
85  }
86  auto datediff_expr = dynamic_cast<const Analyzer::DatediffExpr*>(expr);
87  if (datediff_expr) {
88  return {codegen(datediff_expr, co)};
89  }
90  auto datetrunc_expr = dynamic_cast<const Analyzer::DatetruncExpr*>(expr);
91  if (datetrunc_expr) {
92  return {codegen(datetrunc_expr, co)};
93  }
94  auto charlength_expr = dynamic_cast<const Analyzer::CharLengthExpr*>(expr);
95  if (charlength_expr) {
96  return {codegen(charlength_expr, co)};
97  }
98  auto keyforstring_expr = dynamic_cast<const Analyzer::KeyForStringExpr*>(expr);
99  if (keyforstring_expr) {
100  return {codegen(keyforstring_expr, co)};
101  }
102  auto sample_ratio_expr = dynamic_cast<const Analyzer::SampleRatioExpr*>(expr);
103  if (sample_ratio_expr) {
104  return {codegen(sample_ratio_expr, co)};
105  }
106  auto string_oper_expr = dynamic_cast<const Analyzer::StringOper*>(expr);
107  if (string_oper_expr) {
108  return {codegen(string_oper_expr, co)};
109  }
110  auto cardinality_expr = dynamic_cast<const Analyzer::CardinalityExpr*>(expr);
111  if (cardinality_expr) {
112  return {codegen(cardinality_expr, co)};
113  }
114  auto like_expr = dynamic_cast<const Analyzer::LikeExpr*>(expr);
115  if (like_expr) {
116  return {codegen(like_expr, co)};
117  }
118  auto regexp_expr = dynamic_cast<const Analyzer::RegexpExpr*>(expr);
119  if (regexp_expr) {
120  return {codegen(regexp_expr, co)};
121  }
122  auto width_bucket_expr = dynamic_cast<const Analyzer::WidthBucketExpr*>(expr);
123  if (width_bucket_expr) {
124  return {codegen(width_bucket_expr, co)};
125  }
126  auto likelihood_expr = dynamic_cast<const Analyzer::LikelihoodExpr*>(expr);
127  if (likelihood_expr) {
128  return {codegen(likelihood_expr->get_arg(), fetch_columns, co)};
129  }
130  auto in_expr = dynamic_cast<const Analyzer::InValues*>(expr);
131  if (in_expr) {
132  return {codegen(in_expr, co)};
133  }
134  auto in_integer_set_expr = dynamic_cast<const Analyzer::InIntegerSet*>(expr);
135  if (in_integer_set_expr) {
136  return {codegen(in_integer_set_expr, co)};
137  }
138  auto function_oper_with_custom_type_handling_expr =
139  dynamic_cast<const Analyzer::FunctionOperWithCustomTypeHandling*>(expr);
140  if (function_oper_with_custom_type_handling_expr) {
142  function_oper_with_custom_type_handling_expr, co)};
143  }
144  auto array_oper_expr = dynamic_cast<const Analyzer::ArrayExpr*>(expr);
145  if (array_oper_expr) {
146  return {codegenArrayExpr(array_oper_expr, co)};
147  }
148  auto geo_uop = dynamic_cast<const Analyzer::GeoUOper*>(expr);
149  if (geo_uop) {
150  return {codegenGeoUOper(geo_uop, co)};
151  }
152  auto geo_binop = dynamic_cast<const Analyzer::GeoBinOper*>(expr);
153  if (geo_binop) {
154  return {codegenGeoBinOper(geo_binop, co)};
155  }
156  auto function_oper_expr = dynamic_cast<const Analyzer::FunctionOper*>(expr);
157  if (function_oper_expr) {
158  return {codegenFunctionOper(function_oper_expr, co)};
159  }
160  auto geo_expr = dynamic_cast<const Analyzer::GeoExpr*>(expr);
161  if (geo_expr) {
162  return codegenGeoExpr(geo_expr, co);
163  }
164  if (dynamic_cast<const Analyzer::OffsetInFragment*>(expr)) {
165  return {posArg(nullptr)};
166  }
167  if (dynamic_cast<const Analyzer::WindowFunction*>(expr)) {
168  throw NativeExecutionError("Window expression not supported in this context");
169  }
170  abort();
171 }
CgenState * cgen_state_
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:550
virtual std::vector< llvm::Value * > codegenColumn(const Analyzer::ColumnVar *, const bool fetch_column, const CompilationOptions &)
Definition: ColumnIR.cpp:92
std::vector< llvm::Value * > codegenGeoBinOper(const Analyzer::GeoBinOper *, const CompilationOptions &)
Definition: GeoIR.cpp:247
std::vector< llvm::Value * > codegenGeoExpr(const Analyzer::GeoExpr *, const CompilationOptions &)
Definition: GeoIR.cpp:97
#define CHECK_NE(x, y)
Definition: Logger.h:231
std::vector< llvm::Value * > codegenGeoUOper(const Analyzer::GeoUOper *, const CompilationOptions &)
Definition: GeoIR.cpp:167
llvm::Value * codegenFunctionOper(const Analyzer::FunctionOper *, const CompilationOptions &)
Executor * executor_
std::vector< llvm::Value * > codegenArrayExpr(const Analyzer::ArrayExpr *, const CompilationOptions &)
Definition: ArrayIR.cpp:91
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Expression class for string functions The &quot;arg&quot; constructor parameter must be an expression that reso...
Definition: Analyzer.h:1464
std::vector< llvm::Value * > codegenGeoColumnVar(const Analyzer::GeoColumnVar *, const bool fetch_columns, const CompilationOptions &co)
Definition: GeoIR.cpp:52
llvm::Value * codegenFunctionOperWithCustomTypeHandling(const Analyzer::FunctionOperWithCustomTypeHandling *, const CompilationOptions &)
RUNTIME_EXPORT ALWAYS_INLINE DEVICE int32_t width_bucket_expr(const double target_value, const bool reversed, const double lower_bound, const double upper_bound, const int32_t partition_count)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< llvm::Value * > CodeGenerator::codegen ( const Analyzer::Constant constant,
const EncodingType  enc_type,
const int  dict_id,
const CompilationOptions co 
)
private

Definition at line 20 of file ConstantIR.cpp.

References CgenState::addStringConstant(), AUTOMATIC_IR_METADATA, Datum::boolval, cgen_state_, CHECK, codegenGeoConstant(), codegenHoistedConstants(), codegenIntConst(), CgenState::context_, decimal_to_int_type(), CompilationOptions::device_type, Datum::doubleval, executor(), Datum::floatval, Analyzer::Constant::get_constval(), get_int_type(), Analyzer::Constant::get_is_null(), Analyzer::Expr::get_type_info(), CompilationOptions::hoist_literals, inline_int_null_val(), kBIGINT, kBOOLEAN, kCHAR, kDATE, kDOUBLE, kENCODING_DICT, kFLOAT, kINT, kINTERVAL_DAY_TIME, kINTERVAL_YEAR_MONTH, kSMALLINT, kTEXT, kTIME, kTIMESTAMP, kTINYINT, kVARCHAR, CgenState::llInt(), Datum::stringval, and run_benchmark_import::type.

23  {
25  if (co.hoist_literals) {
26  if (const auto geo_constant = dynamic_cast<const Analyzer::GeoConstant*>(constant)) {
27  return codegenGeoConstant(geo_constant, co);
28  } else {
29  std::vector<const Analyzer::Constant*> constants(
30  executor()->deviceCount(co.device_type), constant);
31  return codegenHoistedConstants(constants, enc_type, dict_id);
32  }
33  }
34  const auto& type_info = constant->get_type_info();
35  const auto type =
36  type_info.is_decimal() ? decimal_to_int_type(type_info) : type_info.get_type();
37  switch (type) {
38  case kBOOLEAN:
39  return {llvm::ConstantInt::get(get_int_type(8, cgen_state_->context_),
40  constant->get_constval().boolval)};
41  case kTINYINT:
42  case kSMALLINT:
43  case kINT:
44  case kBIGINT:
45  case kTIME:
46  case kTIMESTAMP:
47  case kDATE:
48  case kINTERVAL_DAY_TIME:
50  return {CodeGenerator::codegenIntConst(constant, cgen_state_)};
51  case kFLOAT:
52  return {llvm::ConstantFP::get(llvm::Type::getFloatTy(cgen_state_->context_),
53  constant->get_constval().floatval)};
54  case kDOUBLE:
55  return {llvm::ConstantFP::get(llvm::Type::getDoubleTy(cgen_state_->context_),
56  constant->get_constval().doubleval)};
57  case kVARCHAR:
58  case kCHAR:
59  case kTEXT: {
60  CHECK(constant->get_constval().stringval || constant->get_is_null());
61  if (constant->get_is_null()) {
62  if (enc_type == kENCODING_DICT) {
63  return {
64  cgen_state_->llInt(static_cast<int32_t>(inline_int_null_val(type_info)))};
65  }
66  return {cgen_state_->llInt(int64_t(0)),
67  llvm::Constant::getNullValue(
68  llvm::PointerType::get(get_int_type(8, cgen_state_->context_), 0)),
69  cgen_state_->llInt(int32_t(0))};
70  }
71  const auto& str_const = *constant->get_constval().stringval;
72  if (enc_type == kENCODING_DICT) {
73  return {
75  ->getStringDictionaryProxy(
76  dict_id, executor()->getRowSetMemoryOwner(), true)
77  ->getIdOfString(str_const))};
78  }
79  return {cgen_state_->llInt(int64_t(0)),
80  cgen_state_->addStringConstant(str_const),
81  cgen_state_->llInt(static_cast<int32_t>(str_const.size()))};
82  }
83  default:
84  CHECK(false);
85  }
86  abort();
87 }
Definition: sqltypes.h:64
CgenState * cgen_state_
llvm::Value * addStringConstant(const std::string &str)
Definition: CgenState.h:187
int8_t boolval
Definition: Datum.h:45
bool get_is_null() const
Definition: Analyzer.h:342
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
float floatval
Definition: Datum.h:50
llvm::LLVMContext & context_
Definition: CgenState.h:439
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const int dict_id)
Definition: ConstantIR.cpp:373
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::string * stringval
Definition: Datum.h:54
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:559
ExecutorDeviceType device_type
Definition: sqltypes.h:67
Definition: sqltypes.h:68
static llvm::ConstantInt * codegenIntConst(const Analyzer::Constant *constant, CgenState *cgen_state)
Definition: ConstantIR.cpp:89
Datum get_constval() const
Definition: Analyzer.h:343
Definition: sqltypes.h:56
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::vector< llvm::Value * > codegenGeoConstant(const Analyzer::GeoConstant *, const CompilationOptions &)
Definition: GeoIR.cpp:111
Definition: sqltypes.h:60
double doubleval
Definition: Datum.h:51
Executor * executor() const

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::InValues expr,
const CompilationOptions co 
)
private

Definition at line 23 of file InValuesIR.cpp.

References CgenState::addInValuesBitmap(), AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), InValuesBitmap::codegen(), codegenCmp(), CgenState::context_, createInValuesBitmap(), CgenState::emitCall(), executor(), Analyzer::InValues::get_arg(), Analyzer::Expr::get_type_info(), Analyzer::InValues::get_value_list(), CompilationOptions::hoist_literals, CgenState::inlineIntNull(), CgenState::ir_builder_, is_unnest(), kBOOLEAN, kEQ, kONE, CgenState::llInt(), run_benchmark_import::result, and toBool().

24  {
26  const auto in_arg = expr->get_arg();
27  if (is_unnest(in_arg)) {
28  throw std::runtime_error("IN not supported for unnested expressions");
29  }
30  const auto& expr_ti = expr->get_type_info();
31  CHECK(expr_ti.is_boolean());
32  const auto lhs_lvs = codegen(in_arg, true, co);
33  llvm::Value* result{nullptr};
34  if (expr_ti.get_notnull()) {
35  result = llvm::ConstantInt::get(llvm::IntegerType::getInt1Ty(cgen_state_->context_),
36  false);
37  } else {
38  result = cgen_state_->llInt(int8_t(0));
39  }
40  CHECK(result);
41  if (co.hoist_literals) { // TODO(alex): remove this constraint
42  auto in_vals_bitmap = createInValuesBitmap(expr, co);
43  if (in_vals_bitmap) {
44  if (in_vals_bitmap->isEmpty()) {
45  return in_vals_bitmap->hasNull()
47  : result;
48  }
49  CHECK_EQ(size_t(1), lhs_lvs.size());
50  return cgen_state_->addInValuesBitmap(in_vals_bitmap)
51  ->codegen(lhs_lvs.front(), executor());
52  }
53  }
54  if (expr_ti.get_notnull()) {
55  for (auto in_val : expr->get_value_list()) {
56  result = cgen_state_->ir_builder_.CreateOr(
57  result,
58  toBool(
59  codegenCmp(kEQ, kONE, lhs_lvs, in_arg->get_type_info(), in_val.get(), co)));
60  }
61  } else {
62  for (auto in_val : expr->get_value_list()) {
63  const auto crt =
64  codegenCmp(kEQ, kONE, lhs_lvs, in_arg->get_type_info(), in_val.get(), co);
65  result = cgen_state_->emitCall("logical_or",
66  {result, crt, cgen_state_->inlineIntNull(expr_ti)});
67  }
68  }
69  return result;
70 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
Definition: sqldefs.h:29
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
const InValuesBitmap * addInValuesBitmap(std::unique_ptr< InValuesBitmap > &in_values_bitmap)
Definition: CgenState.h:202
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:70
llvm::Value * codegen(llvm::Value *needle, Executor *executor) const
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:641
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:230
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
std::unique_ptr< InValuesBitmap > createInValuesBitmap(const Analyzer::InValues *, const CompilationOptions &)
Definition: InValuesIR.cpp:111
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1464
const Expr * get_arg() const
Definition: Analyzer.h:639
Executor * executor() const

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::InIntegerSet expr,
const CompilationOptions co 
)
private

Definition at line 72 of file InValuesIR.cpp.

References CgenState::addInValuesBitmap(), AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), InValuesBitmap::codegen(), CgenState::context_, Data_Namespace::CPU_LEVEL, CompilationOptions::device_type, executor(), Analyzer::InIntegerSet::get_arg(), Analyzer::Expr::get_type_info(), Analyzer::InIntegerSet::get_value_list(), GPU, Data_Namespace::GPU_LEVEL, CompilationOptions::hoist_literals, inline_int_null_val(), is_unnest(), CgenState::llInt(), and run_benchmark_import::result.

73  {
75  const auto in_arg = in_integer_set->get_arg();
76  if (is_unnest(in_arg)) {
77  throw std::runtime_error("IN not supported for unnested expressions");
78  }
79  const auto& ti = in_integer_set->get_arg()->get_type_info();
80  const auto needle_null_val = inline_int_null_val(ti);
81  if (!co.hoist_literals) {
82  // We never run without literal hoisting in real world scenarios, this avoids a crash
83  // when testing.
84  throw std::runtime_error(
85  "IN subquery with many right-hand side values not supported when literal "
86  "hoisting is disabled");
87  }
88  auto in_vals_bitmap = std::make_unique<InValuesBitmap>(
89  in_integer_set->get_value_list(),
90  needle_null_val,
93  executor()->deviceCount(co.device_type),
94  executor()->data_mgr_);
95  const auto& in_integer_set_ti = in_integer_set->get_type_info();
96  CHECK(in_integer_set_ti.is_boolean());
97  const auto lhs_lvs = codegen(in_arg, true, co);
98  llvm::Value* result{nullptr};
99  if (in_integer_set_ti.get_notnull()) {
100  result = llvm::ConstantInt::get(llvm::IntegerType::getInt1Ty(cgen_state_->context_),
101  false);
102  } else {
103  result = cgen_state_->llInt(int8_t(0));
104  }
105  CHECK(result);
106  CHECK_EQ(size_t(1), lhs_lvs.size());
107  return cgen_state_->addInValuesBitmap(in_vals_bitmap)
108  ->codegen(lhs_lvs.front(), executor());
109 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::LLVMContext & context_
Definition: CgenState.h:439
const InValuesBitmap * addInValuesBitmap(std::unique_ptr< InValuesBitmap > &in_values_bitmap)
Definition: CgenState.h:202
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
llvm::Value * codegen(llvm::Value *needle, Executor *executor) const
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1464
Executor * executor() const

+ Here is the call graph for this function:

std::vector< llvm::Value * > CodeGenerator::codegen ( const Analyzer::CaseExpr case_expr,
const CompilationOptions co 
)
private

Definition at line 20 of file CaseIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegenCase(), CgenState::context_, CgenState::emitCall(), get_bit_width(), Analyzer::CaseExpr::get_else_expr(), get_int_type(), Analyzer::Expr::get_type_info(), kENCODING_DICT, and kFLOAT.

21  {
23  const auto case_ti = case_expr->get_type_info();
24  llvm::Type* case_llvm_type = nullptr;
25  bool is_real_str = false;
26  if (case_ti.is_integer() || case_ti.is_time() || case_ti.is_decimal()) {
27  case_llvm_type = get_int_type(get_bit_width(case_ti), cgen_state_->context_);
28  } else if (case_ti.is_fp()) {
29  case_llvm_type = case_ti.get_type() == kFLOAT
30  ? llvm::Type::getFloatTy(cgen_state_->context_)
31  : llvm::Type::getDoubleTy(cgen_state_->context_);
32  } else if (case_ti.is_string()) {
33  if (case_ti.get_compression() == kENCODING_DICT) {
34  case_llvm_type =
35  get_int_type(8 * case_ti.get_logical_size(), cgen_state_->context_);
36  } else {
37  is_real_str = true;
38  case_llvm_type = get_int_type(64, cgen_state_->context_);
39  }
40  } else if (case_ti.is_boolean()) {
41  case_llvm_type = get_int_type(8 * case_ti.get_logical_size(), cgen_state_->context_);
42  }
43  CHECK(case_llvm_type);
44  const auto& else_ti = case_expr->get_else_expr()->get_type_info();
45  CHECK_EQ(else_ti.get_type(), case_ti.get_type());
46  llvm::Value* case_val = codegenCase(case_expr, case_llvm_type, is_real_str, co);
47  std::vector<llvm::Value*> ret_vals{case_val};
48  if (is_real_str) {
49  ret_vals.push_back(cgen_state_->emitCall("extract_str_ptr", {case_val}));
50  ret_vals.push_back(cgen_state_->emitCall("extract_str_len", {case_val}));
51  }
52  return ret_vals;
53 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
const Expr * get_else_expr() const
Definition: Analyzer.h:1260
CgenState * cgen_state_
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:439
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
llvm::Value * codegenCase(const Analyzer::CaseExpr *, llvm::Type *case_llvm_type, const bool is_real_str, const CompilationOptions &)
Definition: CaseIR.cpp:55
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::ExtractExpr extract_expr,
const CompilationOptions co 
)
private

Definition at line 76 of file DateTimeIR.cpp.

References AUTOMATIC_IR_METADATA, CHECK, DateTimeUtils::get_extract_timestamp_precision_scale(), Analyzer::ExtractExpr::get_field(), Analyzer::ExtractExpr::get_from_expr(), get_int_type(), Analyzer::Expr::get_type_info(), DateTimeUtils::is_subsecond_extract_field(), kDATE, kEPOCH, and kTIMESTAMP.

77  {
79  auto from_expr = codegen(extract_expr->get_from_expr(), true, co).front();
80  const int32_t extract_field{extract_expr->get_field()};
81  const auto& extract_expr_ti = extract_expr->get_from_expr()->get_type_info();
82  if (extract_field == kEPOCH) {
83  CHECK(extract_expr_ti.get_type() == kTIMESTAMP ||
84  extract_expr_ti.get_type() == kDATE);
85  if (from_expr->getType()->isIntegerTy(32)) {
86  from_expr =
87  cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
88  from_expr,
90  return from_expr;
91  }
92  }
93  CHECK(from_expr->getType()->isIntegerTy(64));
94  if (extract_expr_ti.is_high_precision_timestamp()) {
96  from_expr, extract_expr_ti, extract_expr->get_field());
97  }
98  if (!extract_expr_ti.is_high_precision_timestamp() &&
99  is_subsecond_extract_field(extract_expr->get_field())) {
100  from_expr =
101  extract_expr_ti.get_notnull()
102  ? cgen_state_->ir_builder_.CreateMul(
103  from_expr,
107  "mul_int64_t_nullable_lhs",
108  {from_expr,
111  cgen_state_->inlineIntNull(extract_expr_ti)});
112  }
113  const auto extract_fname = get_extract_function_name(extract_expr->get_field());
114  if (!extract_expr_ti.get_notnull()) {
115  llvm::BasicBlock* extract_nullcheck_bb{nullptr};
116  llvm::PHINode* extract_nullcheck_value{nullptr};
117  {
118  DiamondCodegen null_check(cgen_state_->ir_builder_.CreateICmp(
119  llvm::ICmpInst::ICMP_EQ,
120  from_expr,
121  cgen_state_->inlineIntNull(extract_expr_ti)),
122  executor(),
123  false,
124  "extract_nullcheck",
125  nullptr,
126  false);
127  // generate a phi node depending on whether we got a null or not
128  extract_nullcheck_bb = llvm::BasicBlock::Create(
129  cgen_state_->context_, "extract_nullcheck_bb", cgen_state_->current_func_);
130 
131  // update the blocks created by diamond codegen to point to the newly created phi
132  // block
133  cgen_state_->ir_builder_.SetInsertPoint(null_check.cond_true_);
134  cgen_state_->ir_builder_.CreateBr(extract_nullcheck_bb);
135  cgen_state_->ir_builder_.SetInsertPoint(null_check.cond_false_);
136  auto extract_call =
137  cgen_state_->emitExternalCall(extract_fname,
139  std::vector<llvm::Value*>{from_expr});
140  cgen_state_->ir_builder_.CreateBr(extract_nullcheck_bb);
141 
142  cgen_state_->ir_builder_.SetInsertPoint(extract_nullcheck_bb);
143  extract_nullcheck_value = cgen_state_->ir_builder_.CreatePHI(
144  get_int_type(64, cgen_state_->context_), 2, "extract_value");
145  extract_nullcheck_value->addIncoming(extract_call, null_check.cond_false_);
146  extract_nullcheck_value->addIncoming(cgen_state_->inlineIntNull(extract_expr_ti),
147  null_check.cond_true_);
148  }
149 
150  // diamond codegen will set the insert point in its destructor. override it to
151  // continue using the extract nullcheck bb
152  CHECK(extract_nullcheck_bb);
153  cgen_state_->ir_builder_.SetInsertPoint(extract_nullcheck_bb);
154  CHECK(extract_nullcheck_value);
155  return extract_nullcheck_value;
156  } else {
157  return cgen_state_->emitExternalCall(extract_fname,
159  std::vector<llvm::Value*>{from_expr});
160  }
161 }
const Expr * get_from_expr() const
Definition: Analyzer.h:1305
constexpr int64_t get_extract_timestamp_precision_scale(const ExtractField field)
Definition: DateTimeUtils.h:81
CgenState * cgen_state_
ExtractField get_field() const
Definition: Analyzer.h:1304
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
const char * get_extract_function_name(ExtractField field)
Definition: DateTimeIR.cpp:26
llvm::Value * codegenExtractHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const ExtractField &)
Definition: DateTimeIR.cpp:257
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqltypes.h:68
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
constexpr bool is_subsecond_extract_field(const ExtractField &field)
Definition: DateTimeUtils.h:95
Executor * executor() const

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::DateaddExpr dateadd_expr,
const CompilationOptions co 
)
private

Definition at line 163 of file DateTimeIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegen(), CgenState::context_, CgenState::emitExternalCall(), Analyzer::DateaddExpr::get_datetime_expr(), Analyzer::DateaddExpr::get_field(), get_int_type(), Analyzer::DateaddExpr::get_number_expr(), Analyzer::Expr::get_type_info(), CgenState::inlineIntNull(), DateTimeUtils::is_subsecond_dateadd_field(), kDATE, kTIMESTAMP, and CgenState::llInt().

164  {
166  const auto& dateadd_expr_ti = dateadd_expr->get_type_info();
167  CHECK(dateadd_expr_ti.get_type() == kTIMESTAMP || dateadd_expr_ti.get_type() == kDATE);
168  auto datetime = codegen(dateadd_expr->get_datetime_expr(), true, co).front();
169  CHECK(datetime->getType()->isIntegerTy(64));
170  auto number = codegen(dateadd_expr->get_number_expr(), true, co).front();
171 
172  const auto& datetime_ti = dateadd_expr->get_datetime_expr()->get_type_info();
173  std::vector<llvm::Value*> dateadd_args{
174  cgen_state_->llInt(static_cast<int32_t>(dateadd_expr->get_field())),
175  number,
176  datetime};
177  std::string dateadd_fname{"DateAdd"};
178  if (is_subsecond_dateadd_field(dateadd_expr->get_field()) ||
179  dateadd_expr_ti.is_high_precision_timestamp()) {
180  dateadd_fname += "HighPrecision";
181  dateadd_args.push_back(
182  cgen_state_->llInt(static_cast<int32_t>(datetime_ti.get_dimension())));
183  }
184  if (!datetime_ti.get_notnull()) {
185  dateadd_args.push_back(cgen_state_->inlineIntNull(datetime_ti));
186  dateadd_fname += "Nullable";
187  }
188  return cgen_state_->emitExternalCall(dateadd_fname,
190  dateadd_args,
191  {llvm::Attribute::NoUnwind,
192  llvm::Attribute::ReadNone,
193  llvm::Attribute::Speculatable});
194 }
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqltypes.h:68
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1347
DateaddField get_field() const
Definition: Analyzer.h:1345
constexpr bool is_subsecond_dateadd_field(const DateaddField field)
Definition: DateTimeUtils.h:99
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
const Expr * get_number_expr() const
Definition: Analyzer.h:1346

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::DatediffExpr datediff_expr,
const CompilationOptions co 
)
private

Definition at line 196 of file DateTimeIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegen(), CgenState::context_, CgenState::emitExternalCall(), Analyzer::DatediffExpr::get_end_expr(), Analyzer::DatediffExpr::get_field(), get_int_type(), Analyzer::DatediffExpr::get_start_expr(), Analyzer::Expr::get_type_info(), CgenState::inlineIntNull(), and CgenState::llInt().

197  {
199  auto start = codegen(datediff_expr->get_start_expr(), true, co).front();
200  CHECK(start->getType()->isIntegerTy(64));
201  auto end = codegen(datediff_expr->get_end_expr(), true, co).front();
202  CHECK(end->getType()->isIntegerTy(32) || end->getType()->isIntegerTy(64));
203  const auto& start_ti = datediff_expr->get_start_expr()->get_type_info();
204  const auto& end_ti = datediff_expr->get_end_expr()->get_type_info();
205  std::vector<llvm::Value*> datediff_args{
206  cgen_state_->llInt(static_cast<int32_t>(datediff_expr->get_field())), start, end};
207  std::string datediff_fname{"DateDiff"};
208  if (start_ti.is_high_precision_timestamp() || end_ti.is_high_precision_timestamp()) {
209  datediff_fname += "HighPrecision";
210  datediff_args.push_back(
211  cgen_state_->llInt(static_cast<int32_t>(start_ti.get_dimension())));
212  datediff_args.push_back(
213  cgen_state_->llInt(static_cast<int32_t>(end_ti.get_dimension())));
214  }
215  const auto& ret_ti = datediff_expr->get_type_info();
216  if (!start_ti.get_notnull() || !end_ti.get_notnull()) {
217  datediff_args.push_back(cgen_state_->inlineIntNull(ret_ti));
218  datediff_fname += "Nullable";
219  }
221  datediff_fname, get_int_type(64, cgen_state_->context_), datediff_args);
222 }
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const Expr * get_start_expr() const
Definition: Analyzer.h:1388
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
const Expr * get_end_expr() const
Definition: Analyzer.h:1389
#define CHECK(condition)
Definition: Logger.h:222
DatetruncField get_field() const
Definition: Analyzer.h:1387

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::DatetruncExpr datetrunc_expr,
const CompilationOptions co 
)
private

Definition at line 224 of file DateTimeIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegen(), codegenDateTruncHighPrecisionTimestamps(), CgenState::context_, datetrunc_fname_lookup, dtMICROSECOND, dtMILLISECOND, dtNANOSECOND, dtSECOND, CgenState::emitExternalCall(), executor(), field(), Analyzer::DatetruncExpr::get_field(), Analyzer::DatetruncExpr::get_from_expr(), get_int_type(), Analyzer::Expr::get_type_info(), and CgenState::ir_builder_.

225  {
227  auto from_expr = codegen(datetrunc_expr->get_from_expr(), true, co).front();
228  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
229  CHECK(from_expr->getType()->isIntegerTy(64));
230  DatetruncField const field = datetrunc_expr->get_field();
231  if (datetrunc_expr_ti.is_high_precision_timestamp()) {
232  return codegenDateTruncHighPrecisionTimestamps(from_expr, datetrunc_expr_ti, field);
233  }
234  static_assert(dtSECOND + 1 == dtMILLISECOND, "Please keep these consecutive.");
235  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
236  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
237  if (dtSECOND <= field && field <= dtNANOSECOND) {
238  return cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
239  from_expr,
241  }
242  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
243  const bool is_nullable = !datetrunc_expr_ti.get_notnull();
244  if (is_nullable) {
245  nullcheck_codegen = std::make_unique<NullCheckCodegen>(
246  cgen_state_, executor(), from_expr, datetrunc_expr_ti, "date_trunc_nullcheck");
247  }
248  char const* const fname = datetrunc_fname_lookup.at(field);
249  auto ret = cgen_state_->emitExternalCall(
250  fname, get_int_type(64, cgen_state_->context_), {from_expr});
251  if (is_nullable) {
252  ret = nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ret);
253  }
254  return ret;
255 }
CgenState * cgen_state_
#define NULL_BIGINT
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
DatetruncField get_field() const
Definition: Analyzer.h:1429
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
const rapidjson::Value & field(const rapidjson::Value &obj, const char field[]) noexcept
Definition: JsonAccessors.h:31
llvm::LLVMContext & context_
Definition: CgenState.h:439
DatetruncField
Definition: DateTruncate.h:27
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
constexpr std::array< char const *, dtINVALID > datetrunc_fname_lookup
Definition: DateTruncate.h:49
const Expr * get_from_expr() const
Definition: Analyzer.h:1430
#define CHECK(condition)
Definition: Logger.h:222
llvm::Value * codegenDateTruncHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const DatetruncField &)
Definition: DateTimeIR.cpp:301
Executor * executor() const

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::CharLengthExpr expr,
const CompilationOptions co 
)
private

Definition at line 132 of file StringOpsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK_EQ, codegen(), CgenState::emitCall(), and Analyzer::CharLengthExpr::get_arg().

133  {
135  auto str_lv = codegen(expr->get_arg(), true, co);
136  if (str_lv.size() != 3) {
137  CHECK_EQ(size_t(1), str_lv.size());
138  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
139  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
141  throw QueryMustRunOnCpu();
142  }
143  }
144  std::vector<llvm::Value*> charlength_args{str_lv[1], str_lv[2]};
145  std::string fn_name("char_length");
146  if (expr->get_calc_encoded_length()) {
147  fn_name += "_encoded";
148  }
149  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
150  if (is_nullable) {
151  fn_name += "_nullable";
152  charlength_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
153  }
154  return expr->get_calc_encoded_length()
156  fn_name, get_int_type(32, cgen_state_->context_), charlength_args)
157  : cgen_state_->emitCall(fn_name, charlength_args);
158 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
bool get_calc_encoded_length() const
Definition: Analyzer.h:713
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const Expr * get_arg() const
Definition: Analyzer.h:711
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::KeyForStringExpr expr,
const CompilationOptions co 
)
private

Definition at line 160 of file StringOpsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK_EQ, codegen(), CgenState::emitCall(), and Analyzer::KeyForStringExpr::get_arg().

161  {
163  auto str_lv = codegen(expr->get_arg(), true, co);
164  CHECK_EQ(size_t(1), str_lv.size());
165  return cgen_state_->emitCall("key_for_string_encoded", str_lv);
166 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const Expr * get_arg() const
Definition: Analyzer.h:760

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::SampleRatioExpr expr,
const CompilationOptions co 
)
private

Definition at line 217 of file IRCodegen.cpp.

References run_benchmark_import::args, AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), CgenState::context_, CgenState::emitCall(), executor(), Analyzer::SampleRatioExpr::get_arg(), kDOUBLE, ll_bool(), and posArg().

218  {
220  auto input_expr = expr->get_arg();
221  CHECK(input_expr);
222 
223  auto double_lv = codegen(input_expr, true, co);
224  CHECK_EQ(size_t(1), double_lv.size());
225 
226  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
227  const bool is_nullable = !input_expr->get_type_info().get_notnull();
228  if (is_nullable) {
229  nullcheck_codegen = std::make_unique<NullCheckCodegen>(cgen_state_,
230  executor(),
231  double_lv.front(),
232  input_expr->get_type_info(),
233  "sample_ratio_nullcheck");
234  }
235  CHECK_EQ(input_expr->get_type_info().get_type(), kDOUBLE);
236  std::vector<llvm::Value*> args{double_lv[0], posArg(nullptr)};
237  auto ret = cgen_state_->emitCall("sample_ratio", args);
238  if (nullcheck_codegen) {
239  ret = nullcheck_codegen->finalize(ll_bool(false, cgen_state_->context_), ret);
240  }
241  return ret;
242 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:550
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const Expr * get_arg() const
Definition: Analyzer.h:805
#define CHECK(condition)
Definition: Logger.h:222
llvm::ConstantInt * ll_bool(const bool v, llvm::LLVMContext &context)
Executor * executor() const

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::WidthBucketExpr expr,
const CompilationOptions co 
)
private

Definition at line 244 of file IRCodegen.cpp.

References AUTOMATIC_IR_METADATA, Analyzer::WidthBucketExpr::can_skip_out_of_bound_check(), cgen_state_, CHECK, codegenConstantWidthBucketExpr(), codegenWidthBucketExpr(), executor_, Analyzer::WidthBucketExpr::get_lower_bound(), Analyzer::WidthBucketExpr::get_partition_count(), Analyzer::WidthBucketExpr::get_partition_count_val(), Analyzer::WidthBucketExpr::get_target_value(), Analyzer::WidthBucketExpr::get_upper_bound(), getExpressionRange(), PlanState::getSimpleQuals(), Integer, Analyzer::WidthBucketExpr::is_constant_expr(), kCAST, plan_state_, PlanState::query_infos_, Analyzer::WidthBucketExpr::set_constant_expr(), and Analyzer::WidthBucketExpr::skip_out_of_bound_check().

245  {
247  auto target_value_expr = expr->get_target_value();
248  auto lower_bound_expr = expr->get_lower_bound();
249  auto upper_bound_expr = expr->get_upper_bound();
250  auto partition_count_expr = expr->get_partition_count();
251  CHECK(target_value_expr);
252  CHECK(lower_bound_expr);
253  CHECK(upper_bound_expr);
254  CHECK(partition_count_expr);
255 
256  auto is_constant_expr = [](const Analyzer::Expr* expr) {
257  auto target_expr = expr;
258  if (auto cast_expr = dynamic_cast<const Analyzer::UOper*>(expr)) {
259  if (cast_expr->get_optype() == SQLOps::kCAST) {
260  target_expr = cast_expr->get_operand();
261  }
262  }
263  // there are more complex constant expr like 1+2, 1/2*3, and so on
264  // but when considering a typical usage of width_bucket function
265  // it is sufficient to consider a singleton constant expr
266  auto constant_expr = dynamic_cast<const Analyzer::Constant*>(target_expr);
267  if (constant_expr) {
268  return true;
269  }
270  return false;
271  };
272  if (is_constant_expr(lower_bound_expr) && is_constant_expr(upper_bound_expr) &&
273  is_constant_expr(partition_count_expr)) {
274  expr->set_constant_expr();
275  }
276  // compute width_bucket's expresn range and check the possibility of avoiding oob check
277  auto col_range =
278  getExpressionRange(expr,
280  executor_,
281  boost::make_optional(plan_state_->getSimpleQuals()));
282  // check whether target_expr is valid
283  if (col_range.getType() == ExpressionRangeType::Integer &&
284  !expr->can_skip_out_of_bound_check() && col_range.getIntMin() > 0 &&
285  col_range.getIntMax() <= expr->get_partition_count_val()) {
286  // check whether target_col is not-nullable or has filter expr on it
287  if (!col_range.hasNulls()) {
288  // Even if the target_expr has its filter expression, target_col_range may exactly
289  // the same with the col_range of the target_expr's operand col,
290  // i.e., SELECT WIDTH_BUCKET(v1, 1, 10, 10) FROM T WHERE v1 != 1;
291  // In that query, col_range of v1 with/without considering the filter expression
292  // v1 != 1 have exactly the same col ranges, so we cannot recognize the existence
293  // of the filter expression based on them. Also, is (not) null is located in
294  // FilterNode, so we cannot trace it in here.
295  // todo (yoonmin): relax this to allow skipping oob check more cases
296  expr->skip_out_of_bound_check();
297  }
298  }
299  return expr->is_constant_expr() ? codegenConstantWidthBucketExpr(expr, co)
300  : codegenWidthBucketExpr(expr, co);
301 }
const Expr * get_partition_count() const
Definition: Analyzer.h:1044
bool is_constant_expr() const
Definition: Analyzer.h:1119
llvm::Value * codegenConstantWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:303
CgenState * cgen_state_
Definition: sqldefs.h:48
void set_constant_expr() const
Definition: Analyzer.h:1118
llvm::Value * codegenWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:376
Executor * executor_
const std::vector< InputTableInfo > & query_infos_
Definition: PlanState.h:68
ExpressionRange getExpressionRange(const Analyzer::BinOper *expr, const std::vector< InputTableInfo > &query_infos, const Executor *, boost::optional< std::list< std::shared_ptr< Analyzer::Expr >>> simple_quals)
#define AUTOMATIC_IR_METADATA(CGENSTATE)
PlanState * plan_state_
int32_t get_partition_count_val() const
Definition: Analyzer.cpp:3705
std::list< std::shared_ptr< Analyzer::Expr > > getSimpleQuals() const
Definition: PlanState.h:99
const Expr * get_target_value() const
Definition: Analyzer.h:1041
#define CHECK(condition)
Definition: Logger.h:222
bool can_skip_out_of_bound_check() const
Definition: Analyzer.h:1116
void skip_out_of_bound_check() const
Definition: Analyzer.h:1117
const Expr * get_lower_bound() const
Definition: Analyzer.h:1042
const Expr * get_upper_bound() const
Definition: Analyzer.h:1043

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::StringOper expr,
const CompilationOptions co 
)
private

Definition at line 318 of file StringOpsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK_EQ, CHECK_GE, codegen(), StringDictionaryTranslationMgr::codegen(), codegenPerRowStringOper(), CompilationOptions::device_type, executor(), Analyzer::Expr::get_type_info(), Analyzer::StringOper::getArg(), Analyzer::StringOper::getArity(), Analyzer::StringOper::hasNoneEncodedTextArg(), CgenState::moveStringDictionaryTranslationMgr(), and translate_dict_strings().

319  {
320  CHECK_GE(expr->getArity(), 1UL);
321  if (expr->hasNoneEncodedTextArg()) {
322  return codegenPerRowStringOper(expr, co);
323  }
325 
326  const auto& expr_ti = expr->get_type_info();
327  auto string_dictionary_translation_mgr =
329 
330  auto str_id_lv = codegen(expr->getArg(0), true, co);
331  CHECK_EQ(size_t(1), str_id_lv.size());
332 
333  return cgen_state_
334  ->moveStringDictionaryTranslationMgr(std::move(string_dictionary_translation_mgr))
335  ->codegen(str_id_lv[0], expr_ti, true /* add_nullcheck */, co);
336 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
llvm::Value * codegenPerRowStringOper(const Analyzer::StringOper *string_oper, const CompilationOptions &co)
bool hasNoneEncodedTextArg() const
Definition: Analyzer.h:1558
CgenState * cgen_state_
std::unique_ptr< StringDictionaryTranslationMgr > translate_dict_strings(const Analyzer::StringOper *expr, const ExecutorDeviceType device_type, Executor *executor)
#define CHECK_GE(x, y)
Definition: Logger.h:235
size_t getArity() const
Definition: Analyzer.h:1523
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const StringDictionaryTranslationMgr * moveStringDictionaryTranslationMgr(std::unique_ptr< const StringDictionaryTranslationMgr > &&str_dict_translation_mgr)
Definition: CgenState.h:196
llvm::Value * codegen(llvm::Value *str_id_input, const SQLTypeInfo &input_ti, const bool add_nullcheck, const CompilationOptions &co) const
const Expr * getArg(const size_t i) const
Definition: Analyzer.h:1535
Executor * executor() const

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::LikeExpr expr,
const CompilationOptions co 
)
private

Definition at line 371 of file StringOpsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), codegenDictLike(), CgenState::emitCall(), extract_cast_arg(), g_enable_watchdog, Analyzer::LikeExpr::get_arg(), Analyzer::LikeExpr::get_escape_expr(), Analyzer::LikeExpr::get_is_ilike(), Analyzer::LikeExpr::get_is_simple(), Analyzer::LikeExpr::get_like_expr(), Analyzer::LikeExpr::get_own_arg(), Analyzer::Expr::get_type_info(), is_unnest(), and kENCODING_NONE.

372  {
374  if (is_unnest(extract_cast_arg(expr->get_arg()))) {
375  throw std::runtime_error("LIKE not supported for unnested expressions");
376  }
377  char escape_char{'\\'};
378  if (expr->get_escape_expr()) {
379  auto escape_char_expr =
380  dynamic_cast<const Analyzer::Constant*>(expr->get_escape_expr());
381  CHECK(escape_char_expr);
382  CHECK(escape_char_expr->get_type_info().is_string());
383  CHECK_EQ(size_t(1), escape_char_expr->get_constval().stringval->size());
384  escape_char = (*escape_char_expr->get_constval().stringval)[0];
385  }
386  auto pattern = dynamic_cast<const Analyzer::Constant*>(expr->get_like_expr());
387  CHECK(pattern);
388  auto fast_dict_like_lv = codegenDictLike(expr->get_own_arg(),
389  pattern,
390  expr->get_is_ilike(),
391  expr->get_is_simple(),
392  escape_char,
393  co);
394  if (fast_dict_like_lv) {
395  return fast_dict_like_lv;
396  }
397  const auto& ti = expr->get_arg()->get_type_info();
398  CHECK(ti.is_string());
399  if (g_enable_watchdog && ti.get_compression() != kENCODING_NONE) {
400  throw WatchdogException(
401  "Cannot do LIKE / ILIKE on this dictionary encoded column, its cardinality is "
402  "too high");
403  }
404  auto str_lv = codegen(expr->get_arg(), true, co);
405  if (str_lv.size() != 3) {
406  CHECK_EQ(size_t(1), str_lv.size());
407  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
408  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
410  throw QueryMustRunOnCpu();
411  }
412  }
413  auto like_expr_arg_lvs = codegen(expr->get_like_expr(), true, co);
414  CHECK_EQ(size_t(3), like_expr_arg_lvs.size());
415  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
416  std::vector<llvm::Value*> str_like_args{
417  str_lv[1], str_lv[2], like_expr_arg_lvs[1], like_expr_arg_lvs[2]};
418  std::string fn_name{expr->get_is_ilike() ? "string_ilike" : "string_like"};
419  if (expr->get_is_simple()) {
420  fn_name += "_simple";
421  } else {
422  str_like_args.push_back(cgen_state_->llInt(int8_t(escape_char)));
423  }
424  if (is_nullable) {
425  fn_name += "_nullable";
426  str_like_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
427  }
428  return cgen_state_->emitCall(fn_name, str_like_args);
429 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
const Expr * get_escape_expr() const
Definition: Analyzer.h:907
CgenState * cgen_state_
const Analyzer::Expr * extract_cast_arg(const Analyzer::Expr *expr)
Definition: Execute.h:201
const Expr * get_arg() const
Definition: Analyzer.h:904
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
bool g_enable_watchdog
bool get_is_simple() const
Definition: Analyzer.h:909
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const Expr * get_like_expr() const
Definition: Analyzer.h:906
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:905
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1464
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386
bool get_is_ilike() const
Definition: Analyzer.h:908
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 &)

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::RegexpExpr expr,
const CompilationOptions co 
)
private

Definition at line 624 of file StringOpsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), codegenDictRegexp(), CompilationOptions::device_type, CgenState::emitCall(), extract_cast_arg(), g_enable_watchdog, Analyzer::RegexpExpr::get_arg(), Analyzer::RegexpExpr::get_escape_expr(), Analyzer::RegexpExpr::get_own_arg(), Analyzer::RegexpExpr::get_pattern_expr(), Analyzer::Expr::get_type_info(), GPU, is_unnest(), and kENCODING_NONE.

625  {
627  if (is_unnest(extract_cast_arg(expr->get_arg()))) {
628  throw std::runtime_error("REGEXP not supported for unnested expressions");
629  }
630  char escape_char{'\\'};
631  if (expr->get_escape_expr()) {
632  auto escape_char_expr =
633  dynamic_cast<const Analyzer::Constant*>(expr->get_escape_expr());
634  CHECK(escape_char_expr);
635  CHECK(escape_char_expr->get_type_info().is_string());
636  CHECK_EQ(size_t(1), escape_char_expr->get_constval().stringval->size());
637  escape_char = (*escape_char_expr->get_constval().stringval)[0];
638  }
639  auto pattern = dynamic_cast<const Analyzer::Constant*>(expr->get_pattern_expr());
640  CHECK(pattern);
641  auto fast_dict_pattern_lv =
642  codegenDictRegexp(expr->get_own_arg(), pattern, escape_char, co);
643  if (fast_dict_pattern_lv) {
644  return fast_dict_pattern_lv;
645  }
646  const auto& ti = expr->get_arg()->get_type_info();
647  CHECK(ti.is_string());
648  if (g_enable_watchdog && ti.get_compression() != kENCODING_NONE) {
649  throw WatchdogException(
650  "Cannot do REGEXP_LIKE on this dictionary encoded column, its cardinality is too "
651  "high");
652  }
653  // Now we know we are working on NONE ENCODED column. So switch back to CPU
655  throw QueryMustRunOnCpu();
656  }
657  auto str_lv = codegen(expr->get_arg(), true, co);
658  if (str_lv.size() != 3) {
659  CHECK_EQ(size_t(1), str_lv.size());
660  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
661  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
662  }
663  auto regexp_expr_arg_lvs = codegen(expr->get_pattern_expr(), true, co);
664  CHECK_EQ(size_t(3), regexp_expr_arg_lvs.size());
665  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
666  std::vector<llvm::Value*> regexp_args{
667  str_lv[1], str_lv[2], regexp_expr_arg_lvs[1], regexp_expr_arg_lvs[2]};
668  std::string fn_name("regexp_like");
669  regexp_args.push_back(cgen_state_->llInt(int8_t(escape_char)));
670  if (is_nullable) {
671  fn_name += "_nullable";
672  regexp_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
674  fn_name, get_int_type(8, cgen_state_->context_), regexp_args);
675  }
677  fn_name, get_int_type(1, cgen_state_->context_), regexp_args);
678 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:977
CgenState * cgen_state_
const Expr * get_escape_expr() const
Definition: Analyzer.h:979
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
const Expr * get_arg() const
Definition: Analyzer.h:976
const Analyzer::Expr * extract_cast_arg(const Analyzer::Expr *expr)
Definition: Execute.h:201
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
bool g_enable_watchdog
llvm::Value * codegenDictRegexp(const std::shared_ptr< Analyzer::Expr > arg, const Analyzer::Constant *pattern, const char escape_char, const CompilationOptions &)
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const Expr * get_pattern_expr() const
Definition: Analyzer.h:978
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1464
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::CardinalityExpr expr,
const CompilationOptions co 
)
private

Definition at line 68 of file ArrayIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegen(), CgenState::context_, CgenState::emitExternalCall(), Analyzer::CardinalityExpr::get_arg(), get_int_type(), Analyzer::Expr::get_type_info(), CgenState::inlineIntNull(), CgenState::llInt(), log2_bytes(), and posArg().

69  {
71  const auto arr_expr = expr->get_arg();
72  const auto& array_ti = arr_expr->get_type_info();
73  CHECK(array_ti.is_array());
74  const auto& elem_ti = array_ti.get_elem_type();
75  auto arr_lv = codegen(arr_expr, true, co);
76  std::string fn_name("array_size");
77 
78  std::vector<llvm::Value*> array_size_args{
79  arr_lv.front(),
80  posArg(arr_expr),
81  cgen_state_->llInt(log2_bytes(elem_ti.get_logical_size()))};
82  const bool is_nullable{!arr_expr->get_type_info().get_notnull()};
83  if (is_nullable) {
84  fn_name += "_nullable";
85  array_size_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
86  }
88  fn_name, get_int_type(32, cgen_state_->context_), array_size_args);
89 }
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:550
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
const Expr * get_arg() const
Definition: Analyzer.h:850

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::BinOper bin_oper,
const CompilationOptions co 
)
private

Definition at line 173 of file IRCodegen.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, codegenArith(), codegenArrayAt(), codegenCmp(), codegenLogical(), Analyzer::BinOper::get_optype(), IS_ARITHMETIC, IS_COMPARISON, IS_LOGIC, and kARRAY_AT.

174  {
176  const auto optype = bin_oper->get_optype();
177  if (IS_ARITHMETIC(optype)) {
178  return codegenArith(bin_oper, co);
179  }
180  if (IS_COMPARISON(optype)) {
181  return codegenCmp(bin_oper, co);
182  }
183  if (IS_LOGIC(optype)) {
184  return codegenLogical(bin_oper, co);
185  }
186  if (optype == kARRAY_AT) {
187  return codegenArrayAt(bin_oper, co);
188  }
189  abort();
190 }
#define IS_LOGIC(X)
Definition: sqldefs.h:60
llvm::Value * codegenArith(const Analyzer::BinOper *, const CompilationOptions &)
CgenState * cgen_state_
llvm::Value * codegenArrayAt(const Analyzer::BinOper *, const CompilationOptions &)
Definition: ArrayIR.cpp:26
SQLOps get_optype() const
Definition: Analyzer.h:447
#define AUTOMATIC_IR_METADATA(CGENSTATE)
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:230
llvm::Value * codegenLogical(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:298
#define IS_COMPARISON(X)
Definition: sqldefs.h:57

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegen ( const Analyzer::UOper u_oper,
const CompilationOptions co 
)
private

Definition at line 192 of file IRCodegen.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, codegenCast(), codegenIsNull(), codegenLogical(), codegenUMinus(), codegenUnnest(), Analyzer::UOper::get_optype(), kCAST, kISNULL, kNOT, kUMINUS, kUNNEST, and UNREACHABLE.

193  {
195  const auto optype = u_oper->get_optype();
196  switch (optype) {
197  case kNOT: {
198  return codegenLogical(u_oper, co);
199  }
200  case kCAST: {
201  return codegenCast(u_oper, co);
202  }
203  case kUMINUS: {
204  return codegenUMinus(u_oper, co);
205  }
206  case kISNULL: {
207  return codegenIsNull(u_oper, co);
208  }
209  case kUNNEST:
210  return codegenUnnest(u_oper, co);
211  default:
212  UNREACHABLE();
213  }
214  return nullptr;
215 }
CgenState * cgen_state_
#define UNREACHABLE()
Definition: Logger.h:266
Definition: sqldefs.h:48
llvm::Value * codegenIsNull(const Analyzer::UOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:380
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * codegenUMinus(const Analyzer::UOper *, const CompilationOptions &)
llvm::Value * codegenUnnest(const Analyzer::UOper *, const CompilationOptions &)
Definition: ArrayIR.cpp:20
llvm::Value * codegenLogical(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:298
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:21
Definition: sqldefs.h:38
SQLOps get_optype() const
Definition: Analyzer.h:378

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegenAdd ( const Analyzer::BinOper bin_oper,
llvm::Value *  lhs_lv,
llvm::Value *  rhs_lv,
const std::string &  null_typename,
const std::string &  null_check_suffix,
const SQLTypeInfo ti,
const CompilationOptions co 
)
private

Definition at line 217 of file ArithmeticIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, checkExpressionRanges(), codegenBinOpWithOverflowForCPU(), codegenSkipOverflowCheckForNull(), CgenState::context_, CPU, CgenState::current_func_, CompilationOptions::device_type, CgenState::emitCall(), Executor::ERR_OVERFLOW_OR_UNDERFLOW, SQLTypeInfo::get_size(), inline_int_null_val(), CgenState::inlineIntMaxMin(), CgenState::ir_builder_, SQLTypeInfo::is_decimal(), SQLTypeInfo::is_integer(), SQLTypeInfo::is_timeinterval(), CgenState::llInt(), and CgenState::needs_error_check_.

Referenced by codegenIntArith().

223  {
225  CHECK_EQ(lhs_lv->getType(), rhs_lv->getType());
226  CHECK(ti.is_integer() || ti.is_decimal() || ti.is_timeinterval());
227  llvm::Value* chosen_max{nullptr};
228  llvm::Value* chosen_min{nullptr};
229  std::tie(chosen_max, chosen_min) = cgen_state_->inlineIntMaxMin(ti.get_size(), true);
230  auto need_overflow_check =
231  !checkExpressionRanges(bin_oper,
232  static_cast<llvm::ConstantInt*>(chosen_min)->getSExtValue(),
233  static_cast<llvm::ConstantInt*>(chosen_max)->getSExtValue());
234 
235  if (need_overflow_check && co.device_type == ExecutorDeviceType::CPU) {
237  bin_oper, lhs_lv, rhs_lv, null_check_suffix, ti);
238  }
239 
240  llvm::BasicBlock* add_ok{nullptr};
241  llvm::BasicBlock* add_fail{nullptr};
242  if (need_overflow_check) {
244  add_ok = llvm::BasicBlock::Create(
246  if (!null_check_suffix.empty()) {
247  codegenSkipOverflowCheckForNull(lhs_lv, rhs_lv, add_ok, ti);
248  }
249  add_fail = llvm::BasicBlock::Create(
251  llvm::Value* detected{nullptr};
252  auto const_zero = llvm::ConstantInt::get(lhs_lv->getType(), 0, true);
253  auto overflow = cgen_state_->ir_builder_.CreateAnd(
254  cgen_state_->ir_builder_.CreateICmpSGT(lhs_lv, const_zero),
255  cgen_state_->ir_builder_.CreateICmpSGT(
256  rhs_lv, cgen_state_->ir_builder_.CreateSub(chosen_max, lhs_lv)));
257  auto underflow = cgen_state_->ir_builder_.CreateAnd(
258  cgen_state_->ir_builder_.CreateICmpSLT(lhs_lv, const_zero),
259  cgen_state_->ir_builder_.CreateICmpSLT(
260  rhs_lv, cgen_state_->ir_builder_.CreateSub(chosen_min, lhs_lv)));
261  detected = cgen_state_->ir_builder_.CreateOr(overflow, underflow);
262  cgen_state_->ir_builder_.CreateCondBr(detected, add_fail, add_ok);
263  cgen_state_->ir_builder_.SetInsertPoint(add_ok);
264  }
265  auto ret = null_check_suffix.empty()
266  ? cgen_state_->ir_builder_.CreateAdd(lhs_lv, rhs_lv)
268  "add_" + null_typename + null_check_suffix,
269  {lhs_lv, rhs_lv, cgen_state_->llInt(inline_int_null_val(ti))});
270  if (need_overflow_check) {
271  cgen_state_->ir_builder_.SetInsertPoint(add_fail);
272  cgen_state_->ir_builder_.CreateRet(
274  cgen_state_->ir_builder_.SetInsertPoint(add_ok);
275  }
276  return ret;
277 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
HOST DEVICE int get_size() const
Definition: sqltypes.h:389
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
bool is_integer() const
Definition: sqltypes.h:577
void codegenSkipOverflowCheckForNull(llvm::Value *lhs_lv, llvm::Value *rhs_lv, llvm::BasicBlock *no_overflow_bb, const SQLTypeInfo &ti)
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)
static const int32_t ERR_OVERFLOW_OR_UNDERFLOW
Definition: Execute.h:1386
bool is_timeinterval() const
Definition: sqltypes.h:586
bool needs_error_check_
Definition: CgenState.h:461
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
ExecutorDeviceType device_type
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
bool checkExpressionRanges(const Analyzer::UOper *, int64_t, int64_t)
bool is_decimal() const
Definition: sqltypes.h:578
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenArith ( const Analyzer::BinOper bin_oper,
const CompilationOptions co 
)
private

Definition at line 36 of file ArithmeticIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), codegenCastBetweenIntTypes(), codegenDeciDiv(), codegenFpArith(), codegenIntArith(), Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_optype(), Analyzer::BinOper::get_right_operand(), Analyzer::Expr::get_type_info(), IS_ARITHMETIC, and kDIVIDE.

Referenced by codegen().

37  {
39  const auto optype = bin_oper->get_optype();
40  CHECK(IS_ARITHMETIC(optype));
41  const auto lhs = bin_oper->get_left_operand();
42  const auto rhs = bin_oper->get_right_operand();
43  const auto& lhs_type = lhs->get_type_info();
44  const auto& rhs_type = rhs->get_type_info();
45 
46  if (lhs_type.is_decimal() && rhs_type.is_decimal() && optype == kDIVIDE) {
47  const auto ret = codegenDeciDiv(bin_oper, co);
48  if (ret) {
49  return ret;
50  }
51  }
52 
53  auto lhs_lv = codegen(lhs, true, co).front();
54  auto rhs_lv = codegen(rhs, true, co).front();
55  // Handle operations when a time interval operand is involved, an operation
56  // between an integer and a time interval isn't normalized by the analyzer.
57  if (lhs_type.is_timeinterval()) {
58  rhs_lv = codegenCastBetweenIntTypes(rhs_lv, rhs_type, lhs_type);
59  } else if (rhs_type.is_timeinterval()) {
60  lhs_lv = codegenCastBetweenIntTypes(lhs_lv, lhs_type, rhs_type);
61  } else {
62  CHECK_EQ(lhs_type.get_type(), rhs_type.get_type());
63  }
64  if (lhs_type.is_integer() || lhs_type.is_decimal() || lhs_type.is_timeinterval()) {
65  return codegenIntArith(bin_oper, lhs_lv, rhs_lv, co);
66  }
67  if (lhs_type.is_fp()) {
68  return codegenFpArith(bin_oper, lhs_lv, rhs_lv);
69  }
70  CHECK(false);
71  return nullptr;
72 }
llvm::Value * codegenIntArith(const Analyzer::BinOper *, llvm::Value *, llvm::Value *, const CompilationOptions &)
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
const Expr * get_right_operand() const
Definition: Analyzer.h:451
llvm::Value * codegenDeciDiv(const Analyzer::BinOper *, const CompilationOptions &)
llvm::Value * codegenFpArith(const Analyzer::BinOper *, llvm::Value *, llvm::Value *)
SQLOps get_optype() const
Definition: Analyzer.h:447
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:343
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
#define CHECK(condition)
Definition: Logger.h:222
const Expr * get_left_operand() const
Definition: Analyzer.h:450

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenArrayAt ( const Analyzer::BinOper array_at,
const CompilationOptions co 
)
private

Definition at line 26 of file ArrayIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), CgenState::context_, CgenState::emitExternalCall(), get_int_type(), Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_right_operand(), Analyzer::Expr::get_type_info(), CgenState::inlineFpNull(), CgenState::inlineIntNull(), CgenState::ir_builder_, kDOUBLE, posArg(), and to_string().

Referenced by codegen().

27  {
29  const auto arr_expr = array_at->get_left_operand();
30  const auto idx_expr = array_at->get_right_operand();
31  const auto& idx_ti = idx_expr->get_type_info();
32  CHECK(idx_ti.is_integer());
33  auto idx_lvs = codegen(idx_expr, true, co);
34  CHECK_EQ(size_t(1), idx_lvs.size());
35  auto idx_lv = idx_lvs.front();
36  if (idx_ti.get_logical_size() < 8) {
37  idx_lv = cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
38  idx_lv,
40  }
41  const auto& array_ti = arr_expr->get_type_info();
42  CHECK(array_ti.is_array());
43  const auto& elem_ti = array_ti.get_elem_type();
44  const std::string array_at_fname{
45  elem_ti.is_fp()
46  ? "array_at_" + std::string(elem_ti.get_type() == kDOUBLE ? "double_checked"
47  : "float_checked")
48  : "array_at_int" + std::to_string(elem_ti.get_logical_size() * 8) +
49  "_t_checked"};
50  const auto ret_ty =
51  elem_ti.is_fp()
52  ? (elem_ti.get_type() == kDOUBLE
53  ? llvm::Type::getDoubleTy(cgen_state_->context_)
54  : llvm::Type::getFloatTy(cgen_state_->context_))
55  : get_int_type(elem_ti.get_logical_size() * 8, cgen_state_->context_);
56  const auto arr_lvs = codegen(arr_expr, true, co);
57  CHECK_EQ(size_t(1), arr_lvs.size());
59  array_at_fname,
60  ret_ty,
61  {arr_lvs.front(),
62  posArg(arr_expr),
63  idx_lv,
64  elem_ti.is_fp() ? static_cast<llvm::Value*>(cgen_state_->inlineFpNull(elem_ti))
65  : static_cast<llvm::Value*>(cgen_state_->inlineIntNull(elem_ti))});
66 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
const Expr * get_right_operand() const
Definition: Analyzer.h:451
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:550
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::string to_string(char const *&&v)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define CHECK(condition)
Definition: Logger.h:222
const Expr * get_left_operand() const
Definition: Analyzer.h:450
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::pair< llvm::Value *, llvm::Value * > CodeGenerator::codegenArrayBuff ( llvm::Value *  chunk,
llvm::Value *  row_pos,
SQLTypes  array_type,
bool  cast_and_extend 
)
private

Definition at line 654 of file ExtensionsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CgenState::context_, CgenState::emitExternalCall(), SQLTypeInfo::get_elem_type(), kARRAY, and kENCODING_NONE.

Referenced by codegenFunctionOperCastArgs().

658  {
660  const auto elem_ti =
661  SQLTypeInfo(
662  SQLTypes::kARRAY, 0, 0, false, EncodingType::kENCODING_NONE, 0, array_type)
663  .get_elem_type();
664 
665  auto buff = cgen_state_->emitExternalCall(
666  "array_buff", llvm::Type::getInt32PtrTy(cgen_state_->context_), {chunk, row_pos});
667 
668  auto len = cgen_state_->emitExternalCall(
669  "array_size",
671  {chunk, row_pos, cgen_state_->llInt(log2_bytes(elem_ti.get_logical_size()))});
672 
673  if (cast_and_extend) {
674  buff = castArrayPointer(buff, elem_ti);
675  len =
676  cgen_state_->ir_builder_.CreateZExt(len, get_int_type(64, cgen_state_->context_));
677  }
678 
679  return std::make_pair(buff, len);
680 }
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::Value * castArrayPointer(llvm::Value *ptr, const SQLTypeInfo &elem_ti)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:956

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< llvm::Value * > CodeGenerator::codegenArrayExpr ( const Analyzer::ArrayExpr array_expr,
const CompilationOptions co 
)
private

Definition at line 91 of file ArrayIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegen(), CgenState::context_, CompilationOptions::device_type, CgenState::emitExternalCall(), executor(), get_int_array_type(), get_int_type(), Analyzer::Expr::get_type_info(), Analyzer::ArrayExpr::getElement(), Analyzer::ArrayExpr::getElementCount(), GPU, CgenState::ir_builder_, Analyzer::ArrayExpr::isLocalAlloc(), Analyzer::ArrayExpr::isNull(), kENCODING_GEOINT, CgenState::llFp(), and CgenState::llInt().

Referenced by codegen().

93  {
95  using ValueVector = std::vector<llvm::Value*>;
96  ValueVector argument_list;
97  auto& ir_builder(cgen_state_->ir_builder_);
98 
99  const auto& return_type = array_expr->get_type_info();
100  auto coord_compression = (return_type.get_compression() == kENCODING_GEOINT);
101  if (coord_compression) {
102  CHECK(array_expr->isLocalAlloc() && array_expr->getElementCount() == 2);
103  }
104  for (size_t i = 0; i < array_expr->getElementCount(); i++) {
105  const auto arg = array_expr->getElement(i);
106  const auto arg_lvs = codegen(arg, true, co);
107  if (arg_lvs.size() == 1) {
108  if (coord_compression) {
109  // Compress double coords on the fly
110  auto mult = cgen_state_->llFp(2147483647.0 / (i == 0 ? 180.0 : 90.0));
111  auto c = ir_builder.CreateCast(llvm::Instruction::CastOps::FPToSI,
112  ir_builder.CreateFMul(arg_lvs.front(), mult),
114  argument_list.push_back(c);
115  } else {
116  argument_list.push_back(arg_lvs.front());
117  }
118  } else {
119  throw std::runtime_error(
120  "Unexpected argument count during array[] code generation.");
121  }
122  }
123 
124  auto array_element_size_bytes =
125  return_type.get_elem_type().get_array_context_logical_size();
126  auto* array_index_type =
127  get_int_type(array_element_size_bytes * 8, cgen_state_->context_);
128  auto* array_type = get_int_array_type(
129  array_element_size_bytes * 8, array_expr->getElementCount(), cgen_state_->context_);
130 
131  if (array_expr->isNull()) {
132  return {llvm::ConstantPointerNull::get(
133  llvm::PointerType::get(get_int_type(64, cgen_state_->context_), 0)),
134  cgen_state_->llInt(0)};
135  }
136 
137  if (0 == array_expr->getElementCount()) {
138  llvm::Constant* dead_const = cgen_state_->llInt(0xdead);
139  llvm::Value* dead_pointer = llvm::ConstantExpr::getIntToPtr(
140  dead_const, llvm::PointerType::get(get_int_type(64, cgen_state_->context_), 0));
141  return {dead_pointer, cgen_state_->llInt(0)};
142  }
143 
144  llvm::Value* allocated_target_buffer;
145  if (array_expr->isLocalAlloc()) {
146  allocated_target_buffer = ir_builder.CreateAlloca(array_type);
147  } else {
149  throw QueryMustRunOnCpu();
150  }
151 
152  allocated_target_buffer =
153  cgen_state_->emitExternalCall("allocate_varlen_buffer",
154  llvm::Type::getInt8PtrTy(cgen_state_->context_),
155  {cgen_state_->llInt(array_expr->getElementCount()),
156  cgen_state_->llInt(array_element_size_bytes)});
158  "register_buffer_with_executor_rsm",
159  llvm::Type::getVoidTy(cgen_state_->context_),
160  {cgen_state_->llInt(reinterpret_cast<int64_t>(executor())),
161  allocated_target_buffer});
162  }
163  llvm::Value* casted_allocated_target_buffer =
164  ir_builder.CreatePointerCast(allocated_target_buffer, array_type->getPointerTo());
165 
166  for (size_t i = 0; i < array_expr->getElementCount(); i++) {
167  auto* element = argument_list[i];
168  auto* element_ptr = ir_builder.CreateGEP(
169  array_type,
170  casted_allocated_target_buffer,
171  std::vector<llvm::Value*>{cgen_state_->llInt(0), cgen_state_->llInt(i)});
172 
173  const auto& elem_ti = return_type.get_elem_type();
174  if (elem_ti.is_boolean()) {
175  const auto byte_casted_bit =
176  ir_builder.CreateIntCast(element, array_index_type, true);
177  ir_builder.CreateStore(byte_casted_bit, element_ptr);
178  } else if (elem_ti.is_fp()) {
179  switch (elem_ti.get_size()) {
180  case sizeof(double): {
181  const auto double_element_ptr = ir_builder.CreatePointerCast(
182  element_ptr, llvm::Type::getDoublePtrTy(cgen_state_->context_));
183  ir_builder.CreateStore(element, double_element_ptr);
184  break;
185  }
186  case sizeof(float): {
187  const auto float_element_ptr = ir_builder.CreatePointerCast(
188  element_ptr, llvm::Type::getFloatPtrTy(cgen_state_->context_));
189  ir_builder.CreateStore(element, float_element_ptr);
190  break;
191  }
192  default:
193  UNREACHABLE();
194  }
195  } else if (elem_ti.is_integer() || elem_ti.is_decimal() || elem_ti.is_date() ||
196  elem_ti.is_timestamp() || elem_ti.is_time() || elem_ti.is_timeinterval() ||
197  elem_ti.is_dict_encoded_string()) {
198  // TODO(adb): this validation and handling should be done elsewhere
199  const auto sign_extended_element = ir_builder.CreateSExt(element, array_index_type);
200  ir_builder.CreateStore(sign_extended_element, element_ptr);
201  } else {
202  throw std::runtime_error("Unsupported type used in ARRAY construction.");
203  }
204  }
205 
206  return {ir_builder.CreateGEP(
207  array_type, casted_allocated_target_buffer, cgen_state_->llInt(0)),
208  cgen_state_->llInt(array_expr->getElementCount())};
209 }
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
#define UNREACHABLE()
Definition: Logger.h:266
bool isNull() const
Definition: Analyzer.h:2541
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:310
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
size_t getElementCount() const
Definition: Analyzer.h:2539
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
bool isLocalAlloc() const
Definition: Analyzer.h:2540
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
llvm::ArrayType * get_int_array_type(int const width, int count, llvm::LLVMContext &context)
const Analyzer::Expr * getElement(const size_t i) const
Definition: Analyzer.h:2543
Executor * executor() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenBinOpWithOverflowForCPU ( const Analyzer::BinOper bin_oper,
llvm::Value *  lhs_lv,
llvm::Value *  rhs_lv,
const std::string &  null_check_suffix,
const SQLTypeInfo ti 
)
private

Definition at line 726 of file ArithmeticIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, codegenSkipOverflowCheckForNull(), CgenState::context_, CgenState::current_func_, Executor::ERR_OVERFLOW_OR_UNDERFLOW, getArithWithOverflowIntrinsic(), inline_int_null_val(), CgenState::ir_builder_, CgenState::llInt(), and CgenState::needs_error_check_.

Referenced by codegenAdd(), codegenMul(), and codegenSub().

731  {
734 
735  llvm::BasicBlock* check_ok = llvm::BasicBlock::Create(
737  llvm::BasicBlock* check_fail = llvm::BasicBlock::Create(
738  cgen_state_->context_, "ovf_detected", cgen_state_->current_func_);
739  llvm::BasicBlock* null_check{nullptr};
740 
741  if (!null_check_suffix.empty()) {
742  null_check = cgen_state_->ir_builder_.GetInsertBlock();
743  codegenSkipOverflowCheckForNull(lhs_lv, rhs_lv, check_ok, ti);
744  }
745 
746  // Compute result and overflow flag
747  auto func = getArithWithOverflowIntrinsic(bin_oper, lhs_lv->getType());
748  auto ret_and_overflow = cgen_state_->ir_builder_.CreateCall(
749  func, std::vector<llvm::Value*>{lhs_lv, rhs_lv});
750  auto ret = cgen_state_->ir_builder_.CreateExtractValue(ret_and_overflow,
751  std::vector<unsigned>{0});
752  auto overflow = cgen_state_->ir_builder_.CreateExtractValue(ret_and_overflow,
753  std::vector<unsigned>{1});
754  auto val_bb = cgen_state_->ir_builder_.GetInsertBlock();
755 
756  // Return error on overflow
757  cgen_state_->ir_builder_.CreateCondBr(overflow, check_fail, check_ok);
758  cgen_state_->ir_builder_.SetInsertPoint(check_fail);
759  cgen_state_->ir_builder_.CreateRet(
761 
762  cgen_state_->ir_builder_.SetInsertPoint(check_ok);
763 
764  // In case of null check we have to use NULL result on check fail
765  if (null_check) {
766  auto phi = cgen_state_->ir_builder_.CreatePHI(ret->getType(), 2);
767  phi->addIncoming(llvm::ConstantInt::get(ret->getType(), inline_int_null_val(ti)),
768  null_check);
769  phi->addIncoming(ret, val_bb);
770  ret = phi;
771  }
772 
773  return ret;
774 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
void codegenSkipOverflowCheckForNull(llvm::Value *lhs_lv, llvm::Value *rhs_lv, llvm::BasicBlock *no_overflow_bb, const SQLTypeInfo &ti)
static const int32_t ERR_OVERFLOW_OR_UNDERFLOW
Definition: Execute.h:1386
bool needs_error_check_
Definition: CgenState.h:461
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Function * getArithWithOverflowIntrinsic(const Analyzer::BinOper *bin_oper, llvm::Type *type)
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CodeGenerator::codegenBufferArgs ( const std::string &  udf_func_name,
size_t  param_num,
llvm::Value *  buffer_buf,
llvm::Value *  buffer_size,
llvm::Value *  buffer_is_null,
std::vector< llvm::Value * > &  output_args 
)
private

Definition at line 682 of file ExtensionsIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CgenState::context_, anonymous_namespace{ExtensionsIR.cpp}::get_buffer_struct_type(), and CgenState::ir_builder_.

Referenced by codegenFunctionOperCastArgs().

687  {
689  CHECK(buffer_buf);
690  CHECK(buffer_size);
691 
692  auto buffer_abstraction = get_buffer_struct_type(
693  cgen_state_, ext_func_name, param_num, buffer_buf->getType(), !!(buffer_null));
694  auto alloc_mem = cgen_state_->ir_builder_.CreateAlloca(buffer_abstraction);
695 
696  auto buffer_buf_ptr =
697  cgen_state_->ir_builder_.CreateStructGEP(buffer_abstraction, alloc_mem, 0);
698  cgen_state_->ir_builder_.CreateStore(buffer_buf, buffer_buf_ptr);
699 
700  auto buffer_size_ptr =
701  cgen_state_->ir_builder_.CreateStructGEP(buffer_abstraction, alloc_mem, 1);
702  cgen_state_->ir_builder_.CreateStore(buffer_size, buffer_size_ptr);
703 
704  if (buffer_null) {
705  auto bool_extended_type = llvm::Type::getInt8Ty(cgen_state_->context_);
706  auto buffer_null_extended =
707  cgen_state_->ir_builder_.CreateZExt(buffer_null, bool_extended_type);
708  auto buffer_is_null_ptr =
709  cgen_state_->ir_builder_.CreateStructGEP(buffer_abstraction, alloc_mem, 2);
710  cgen_state_->ir_builder_.CreateStore(buffer_null_extended, buffer_is_null_ptr);
711  }
712  output_args.push_back(alloc_mem);
713 }
llvm::StructType * get_buffer_struct_type(CgenState *cgen_state, const std::string &ext_func_name, size_t param_num, llvm::Type *elem_type, bool has_is_null)
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::LLVMContext & context_
Definition: CgenState.h:439
#define AUTOMATIC_IR_METADATA(CGENSTATE)
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCase ( const Analyzer::CaseExpr case_expr,
llvm::Type case_llvm_type,
const bool  is_real_str,
const CompilationOptions co 
)
private

Definition at line 55 of file CaseIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK_EQ, codegen(), CgenState::context_, CgenState::current_func_, CgenState::emitCall(), Analyzer::CaseExpr::get_expr_pair_list(), CgenState::ir_builder_, and toBool().

Referenced by codegen().

58  {
60  // Here the linear control flow will diverge and expressions cached during the
61  // code branch code generation (currently just column decoding) are not going
62  // to be available once we're done generating the case. Take a snapshot of
63  // the cache with FetchCacheAnchor and restore it once we're done with CASE.
65  const auto& expr_pair_list = case_expr->get_expr_pair_list();
66  std::vector<llvm::Value*> then_lvs;
67  std::vector<llvm::BasicBlock*> then_bbs;
68  const auto end_bb = llvm::BasicBlock::Create(
70  for (const auto& expr_pair : expr_pair_list) {
72  const auto when_lv = toBool(codegen(expr_pair.first.get(), true, co).front());
73  const auto cmp_bb = cgen_state_->ir_builder_.GetInsertBlock();
74  const auto then_bb = llvm::BasicBlock::Create(cgen_state_->context_,
75  "then_case",
77  /*insert_before=*/end_bb);
78  cgen_state_->ir_builder_.SetInsertPoint(then_bb);
79  auto then_bb_lvs = codegen(expr_pair.second.get(), true, co);
80  if (is_real_str) {
81  if (then_bb_lvs.size() == 3) {
82  then_lvs.push_back(
83  cgen_state_->emitCall("string_pack", {then_bb_lvs[1], then_bb_lvs[2]}));
84  } else {
85  then_lvs.push_back(then_bb_lvs.front());
86  }
87  } else {
88  CHECK_EQ(size_t(1), then_bb_lvs.size());
89  then_lvs.push_back(then_bb_lvs.front());
90  }
91  then_bbs.push_back(cgen_state_->ir_builder_.GetInsertBlock());
92  cgen_state_->ir_builder_.CreateBr(end_bb);
93  const auto when_bb = llvm::BasicBlock::Create(
95  cgen_state_->ir_builder_.SetInsertPoint(cmp_bb);
96  cgen_state_->ir_builder_.CreateCondBr(when_lv, then_bb, when_bb);
97  cgen_state_->ir_builder_.SetInsertPoint(when_bb);
98  }
99  const auto else_expr = case_expr->get_else_expr();
100  CHECK(else_expr);
101  auto else_lvs = codegen(else_expr, true, co);
102  llvm::Value* else_lv{nullptr};
103  if (else_lvs.size() == 3) {
104  else_lv = cgen_state_->emitCall("string_pack", {else_lvs[1], else_lvs[2]});
105  } else {
106  else_lv = else_lvs.front();
107  }
108  CHECK(else_lv);
109  auto else_bb = cgen_state_->ir_builder_.GetInsertBlock();
110  cgen_state_->ir_builder_.CreateBr(end_bb);
111  cgen_state_->ir_builder_.SetInsertPoint(end_bb);
112  auto then_phi =
113  cgen_state_->ir_builder_.CreatePHI(case_llvm_type, expr_pair_list.size() + 1);
114  CHECK_EQ(then_bbs.size(), then_lvs.size());
115  for (size_t i = 0; i < then_bbs.size(); ++i) {
116  then_phi->addIncoming(then_lvs[i], then_bbs[i]);
117  }
118  then_phi->addIncoming(else_lv, else_bb);
119  return then_phi;
120 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
const Expr * get_else_expr() const
Definition: Analyzer.h:1260
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
#define CHECK(condition)
Definition: Logger.h:222
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1257

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCast ( const Analyzer::UOper uoper,
const CompilationOptions co 
)
private

Definition at line 21 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK_EQ, codegen(), CgenState::context_, CgenState::emitCall(), get_int_type(), Analyzer::UOper::get_operand(), Analyzer::UOper::get_optype(), Analyzer::Expr::get_type_info(), CgenState::ir_builder_, and kCAST.

Referenced by codegen(), codegenCmpDecimalConst(), codegenFunctionOper(), and codegenFunctionOperWithCustomTypeHandling().

22  {
24  CHECK_EQ(uoper->get_optype(), kCAST);
25  const auto& ti = uoper->get_type_info();
26  const auto operand = uoper->get_operand();
27  const auto operand_as_const = dynamic_cast<const Analyzer::Constant*>(operand);
28  // For dictionary encoded constants, the cast holds the dictionary id
29  // information as the compression parameter; handle this case separately.
30  llvm::Value* operand_lv{nullptr};
31  if (operand_as_const) {
32  const auto operand_lvs =
33  codegen(operand_as_const, ti.get_compression(), ti.get_comp_param(), co);
34  if (operand_lvs.size() == 3) {
35  operand_lv = cgen_state_->emitCall("string_pack", {operand_lvs[1], operand_lvs[2]});
36  } else {
37  operand_lv = operand_lvs.front();
38  }
39  } else {
40  operand_lv = codegen(operand, true, co).front();
41  }
42 
43  // If the operand is a TextEncodingNone struct ({i8*, i64, i8})
44  // unpack it into an int64_t using "string_pack" so that codegenCast
45  // can properly cast it to a TextEncodingDict
46  if (operand_lv->getType()->isPointerTy() &&
47  operand_lv->getType()->getPointerElementType()->isStructTy()) {
48  auto _struct = cgen_state_->ir_builder_.CreateLoad(
49  operand_lv->getType()->getPointerElementType(), operand_lv);
50  auto ptr = cgen_state_->ir_builder_.CreateExtractValue(_struct, {0});
51  auto len = cgen_state_->ir_builder_.CreateTrunc(
52  cgen_state_->ir_builder_.CreateExtractValue(_struct, {1}),
54  operand_lv = cgen_state_->emitCall("string_pack", {ptr, len});
55  }
56  const auto& operand_ti = operand->get_type_info();
57  return codegenCast(operand_lv, operand_ti, ti, operand_as_const, co);
58 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
Definition: sqldefs.h:48
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
const Expr * get_operand() const
Definition: Analyzer.h:379
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:21
SQLOps get_optype() const
Definition: Analyzer.h:378

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCast ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti,
const bool  operand_is_const,
const CompilationOptions co 
)
private

Definition at line 69 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, anonymous_namespace{CastIR.cpp}::byte_array_cast(), CgenState::castToTypeIn(), cgen_state_, CHECK, codegenCastBetweenIntTypes(), codegenCastBetweenTimestamps(), codegenCastFromFp(), codegenCastFromString(), codegenCastTimestampToDate(), codegenCastTimestampToTime(), codegenCastToFp(), CgenState::context_, SQLTypeInfo::get_dimension(), get_int_array_type(), SQLTypeInfo::get_notnull(), SQLTypeInfo::get_size(), SQLTypeInfo::get_type(), CgenState::ir_builder_, SQLTypeInfo::is_boolean(), SQLTypeInfo::is_decimal(), SQLTypeInfo::is_integer(), SQLTypeInfo::is_string(), SQLTypeInfo::is_time(), SQLTypeInfo::is_timestamp(), kDATE, kTIME, and kTIMESTAMP.

73  {
75  if (byte_array_cast(operand_ti, ti)) {
76  auto* byte_array_type = get_int_array_type(8, ti.get_size(), cgen_state_->context_);
77  return cgen_state_->ir_builder_.CreatePointerCast(operand_lv,
78  byte_array_type->getPointerTo());
79  }
80  if (operand_lv->getType()->isIntegerTy()) {
81  if (operand_ti.is_string()) {
82  return codegenCastFromString(operand_lv, operand_ti, ti, operand_is_const, co);
83  }
84  CHECK(operand_ti.is_integer() || operand_ti.is_decimal() || operand_ti.is_time() ||
85  operand_ti.is_boolean());
86  if (operand_ti.is_boolean()) {
87  // cast boolean to int8
88  CHECK(operand_lv->getType()->isIntegerTy(1) ||
89  operand_lv->getType()->isIntegerTy(8));
90  if (operand_lv->getType()->isIntegerTy(1)) {
91  operand_lv = cgen_state_->castToTypeIn(operand_lv, 8);
92  }
93  if (ti.is_boolean()) {
94  return operand_lv;
95  }
96  }
97  if (operand_ti.is_integer() && operand_lv->getType()->isIntegerTy(8) &&
98  ti.is_boolean()) {
99  // cast int8 to boolean
100  return codegenCastBetweenIntTypes(operand_lv, operand_ti, ti);
101  }
102  if (operand_ti.get_type() == kTIMESTAMP && ti.get_type() == kDATE) {
103  // Maybe we should instead generate DatetruncExpr directly from RelAlgTranslator
104  // for this pattern. However, DatetruncExpr is supposed to return a timestamp,
105  // whereas this cast returns a date. The underlying type for both is still the same,
106  // but it still doesn't look like a good idea to misuse DatetruncExpr.
107  // Date will have default precision of day, but TIMESTAMP dimension would
108  // matter but while converting date through seconds
110  operand_lv, operand_ti.get_dimension(), !ti.get_notnull());
111  }
112  if (operand_ti.get_type() == kTIMESTAMP && ti.get_type() == kTIME) {
114  operand_lv, operand_ti.get_dimension(), !ti.get_notnull());
115  }
116  if ((operand_ti.get_type() == kTIMESTAMP || operand_ti.get_type() == kDATE) &&
117  ti.get_type() == kTIMESTAMP) {
118  const auto operand_dimen =
119  (operand_ti.is_timestamp()) ? operand_ti.get_dimension() : 0;
120  if (operand_dimen != ti.get_dimension()) {
122  operand_lv, operand_ti, ti, !ti.get_notnull());
123  }
124  }
125  if (ti.is_integer() || ti.is_decimal() || ti.is_time()) {
126  return codegenCastBetweenIntTypes(operand_lv, operand_ti, ti);
127  } else {
128  return codegenCastToFp(operand_lv, operand_ti, ti);
129  }
130  } else {
131  return codegenCastFromFp(operand_lv, operand_ti, ti);
132  }
133  CHECK(false);
134  return nullptr;
135 }
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:152
llvm::Value * codegenCastFromFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:517
HOST DEVICE int get_size() const
Definition: sqltypes.h:389
Definition: sqltypes.h:64
bool is_timestamp() const
Definition: sqltypes.h:995
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::Value * codegenCastToFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:474
bool byte_array_cast(const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:62
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:379
bool is_time() const
Definition: sqltypes.h:581
llvm::LLVMContext & context_
Definition: CgenState.h:439
bool is_integer() const
Definition: sqltypes.h:577
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:343
bool is_boolean() const
Definition: sqltypes.h:582
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
Definition: CastIR.cpp:157
#define AUTOMATIC_IR_METADATA(CGENSTATE)
Definition: sqltypes.h:68
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:381
llvm::Value * codegenCastBetweenTimestamps(llvm::Value *ts_lv, const SQLTypeInfo &operand_dimen, const SQLTypeInfo &target_dimen, const bool nullable)
Definition: CastIR.cpp:194
#define CHECK(condition)
Definition: Logger.h:222
llvm::Value * codegenCastTimestampToTime(llvm::Value *ts_lv, const int dimen, const bool nullable)
Definition: CastIR.cpp:137
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:226
bool is_string() const
Definition: sqltypes.h:575
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386
llvm::ArrayType * get_int_array_type(int const width, int count, llvm::LLVMContext &context)
bool is_decimal() const
Definition: sqltypes.h:578

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegenCastBetweenIntTypes ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti,
bool  upscale = true 
)

Definition at line 343 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegenCastBetweenIntTypesOverflowChecks(), CgenState::context_, CgenState::emitCall(), exp_to_scale(), get_bit_width(), get_int_type(), SQLTypeInfo::get_logical_size(), SQLTypeInfo::get_notnull(), SQLTypeInfo::get_scale(), inline_int_null_val(), CgenState::inlineIntNull(), CgenState::ir_builder_, SQLTypeInfo::is_decimal(), SQLTypeInfo::is_integer(), kBIGINT, CgenState::llInt(), and numeric_type_name().

Referenced by codegenArith(), codegenCast(), codegenDeciDiv(), codegenWidthBucketExpr(), and Executor::codegenWindowFunctionAggregateCalls().

346  {
348  if (ti.is_decimal() &&
349  (!operand_ti.is_decimal() || operand_ti.get_scale() <= ti.get_scale())) {
350  if (upscale) {
351  if (operand_ti.get_scale() < ti.get_scale()) { // scale only if needed
352  auto scale = exp_to_scale(ti.get_scale() - operand_ti.get_scale());
353  const auto scale_lv =
354  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), scale);
355  operand_lv = cgen_state_->ir_builder_.CreateSExt(
356  operand_lv, get_int_type(64, cgen_state_->context_));
357 
358  codegenCastBetweenIntTypesOverflowChecks(operand_lv, operand_ti, ti, scale);
359 
360  if (operand_ti.get_notnull()) {
361  operand_lv = cgen_state_->ir_builder_.CreateMul(operand_lv, scale_lv);
362  } else {
363  operand_lv = cgen_state_->emitCall(
364  "scale_decimal_up",
365  {operand_lv,
366  scale_lv,
367  cgen_state_->llInt(inline_int_null_val(operand_ti)),
369  }
370  }
371  }
372  } else if (operand_ti.is_decimal()) {
373  // rounded scale down
374  auto scale = (int64_t)exp_to_scale(operand_ti.get_scale() - ti.get_scale());
375  const auto scale_lv =
376  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), scale);
377 
378  const auto operand_width =
379  static_cast<llvm::IntegerType*>(operand_lv->getType())->getBitWidth();
380 
381  std::string method_name = "scale_decimal_down_nullable";
382  if (operand_ti.get_notnull()) {
383  method_name = "scale_decimal_down_not_nullable";
384  }
385 
386  CHECK(operand_width == 64);
387  operand_lv = cgen_state_->emitCall(
388  method_name,
389  {operand_lv, scale_lv, cgen_state_->llInt(inline_int_null_val(operand_ti))});
390  }
391  if (ti.is_integer() && operand_ti.is_integer() &&
392  operand_ti.get_logical_size() > ti.get_logical_size()) {
393  codegenCastBetweenIntTypesOverflowChecks(operand_lv, operand_ti, ti, 1);
394  }
395 
396  const auto operand_width =
397  static_cast<llvm::IntegerType*>(operand_lv->getType())->getBitWidth();
398  const auto target_width = get_bit_width(ti);
399  if (target_width == operand_width) {
400  return operand_lv;
401  }
402  if (operand_ti.get_notnull()) {
403  return cgen_state_->ir_builder_.CreateCast(
404  target_width > operand_width ? llvm::Instruction::CastOps::SExt
405  : llvm::Instruction::CastOps::Trunc,
406  operand_lv,
407  get_int_type(target_width, cgen_state_->context_));
408  }
409  return cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
410  numeric_type_name(ti) + "_nullable",
411  {operand_lv,
412  cgen_state_->inlineIntNull(operand_ti),
413  cgen_state_->inlineIntNull(ti)});
414 }
CgenState * cgen_state_
HOST DEVICE int get_scale() const
Definition: sqltypes.h:384
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:439
int get_logical_size() const
Definition: sqltypes.h:399
bool is_integer() const
Definition: sqltypes.h:577
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:416
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
uint64_t exp_to_scale(const unsigned exp)
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386
bool is_decimal() const
Definition: sqltypes.h:578

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void CodeGenerator::codegenCastBetweenIntTypesOverflowChecks ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti,
const int64_t  scale 
)

Definition at line 416 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CgenState::context_, CgenState::current_func_, CgenState::emitCall(), get_int_type(), SQLTypeInfo::get_logical_size(), SQLTypeInfo::get_notnull(), inline_int_null_val(), CgenState::inlineIntMaxMin(), CgenState::inlineIntNull(), CgenState::ir_builder_, kBOOLEAN, CgenState::llInt(), CgenState::needs_error_check_, numeric_type_name(), toBool(), and type_name().

Referenced by codegenCastBetweenIntTypes(), and codegenCastBetweenTimestamps().

420  {
422  llvm::Value* chosen_max{nullptr};
423  llvm::Value* chosen_min{nullptr};
424  std::tie(chosen_max, chosen_min) =
426 
428  auto cast_ok = llvm::BasicBlock::Create(
430  auto cast_fail = llvm::BasicBlock::Create(
432  auto operand_max = static_cast<llvm::ConstantInt*>(chosen_max)->getSExtValue() / scale;
433  auto operand_min = static_cast<llvm::ConstantInt*>(chosen_min)->getSExtValue() / scale;
434  const auto ti_llvm_type =
436  llvm::Value* operand_max_lv = llvm::ConstantInt::get(ti_llvm_type, operand_max);
437  llvm::Value* operand_min_lv = llvm::ConstantInt::get(ti_llvm_type, operand_min);
438  const bool is_narrowing = operand_ti.get_logical_size() > ti.get_logical_size();
439  if (is_narrowing) {
440  const auto operand_ti_llvm_type =
442  operand_max_lv =
443  cgen_state_->ir_builder_.CreateSExt(operand_max_lv, operand_ti_llvm_type);
444  operand_min_lv =
445  cgen_state_->ir_builder_.CreateSExt(operand_min_lv, operand_ti_llvm_type);
446  }
447  llvm::Value* over{nullptr};
448  llvm::Value* under{nullptr};
449  if (operand_ti.get_notnull()) {
450  over = cgen_state_->ir_builder_.CreateICmpSGT(operand_lv, operand_max_lv);
451  under = cgen_state_->ir_builder_.CreateICmpSLE(operand_lv, operand_min_lv);
452  } else {
453  const auto type_name =
454  is_narrowing ? numeric_type_name(operand_ti) : numeric_type_name(ti);
455  const auto null_operand_val = cgen_state_->llInt(inline_int_null_val(operand_ti));
456  const auto null_bool_val = cgen_state_->inlineIntNull(SQLTypeInfo(kBOOLEAN, false));
457  over = toBool(cgen_state_->emitCall(
458  "gt_" + type_name + "_nullable_lhs",
459  {operand_lv, operand_max_lv, null_operand_val, null_bool_val}));
460  under = toBool(cgen_state_->emitCall(
461  "le_" + type_name + "_nullable_lhs",
462  {operand_lv, operand_min_lv, null_operand_val, null_bool_val}));
463  }
464  const auto detected = cgen_state_->ir_builder_.CreateOr(over, under, "overflow");
465  cgen_state_->ir_builder_.CreateCondBr(detected, cast_fail, cast_ok);
466 
467  cgen_state_->ir_builder_.SetInsertPoint(cast_fail);
468  cgen_state_->ir_builder_.CreateRet(
470 
471  cgen_state_->ir_builder_.SetInsertPoint(cast_ok);
472 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::Function * current_func_
Definition: CgenState.h:433
int get_logical_size() const
Definition: sqltypes.h:399
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
static const int32_t ERR_OVERFLOW_OR_UNDERFLOW
Definition: Execute.h:1386
bool needs_error_check_
Definition: CgenState.h:461
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386
constexpr auto type_name() noexcept
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:123

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCastBetweenTimestamps ( llvm::Value *  ts_lv,
const SQLTypeInfo operand_dimen,
const SQLTypeInfo target_dimen,
const bool  nullable 
)

Definition at line 194 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, codegenCastBetweenIntTypesOverflowChecks(), CgenState::emitCall(), SQLTypeInfo::get_dimension(), DateTimeUtils::get_timestamp_precision_scale(), CgenState::inlineIntNull(), CgenState::ir_builder_, and CgenState::llInt().

Referenced by anonymous_namespace{TableFunctionCompilationContext.cpp}::cast_value(), and codegenCast().

197  {
199  const auto operand_dimen = operand_ti.get_dimension();
200  const auto target_dimen = target_ti.get_dimension();
201  if (operand_dimen == target_dimen) {
202  return ts_lv;
203  }
204  CHECK(ts_lv->getType()->isIntegerTy(64));
205  const auto scale =
206  DateTimeUtils::get_timestamp_precision_scale(abs(operand_dimen - target_dimen));
207  if (operand_dimen < target_dimen) {
208  codegenCastBetweenIntTypesOverflowChecks(ts_lv, operand_ti, target_ti, scale);
209  return nullable
210  ? cgen_state_->emitCall("mul_int64_t_nullable_lhs",
211  {ts_lv,
212  cgen_state_->llInt(static_cast<int64_t>(scale)),
213  cgen_state_->inlineIntNull(operand_ti)})
214  : cgen_state_->ir_builder_.CreateMul(
215  ts_lv, cgen_state_->llInt(static_cast<int64_t>(scale)));
216  }
217  return nullable
218  ? cgen_state_->emitCall("floor_div_nullable_lhs",
219  {ts_lv,
220  cgen_state_->llInt(static_cast<int64_t>(scale)),
221  cgen_state_->inlineIntNull(operand_ti)})
222  : cgen_state_->ir_builder_.CreateSDiv(
223  ts_lv, cgen_state_->llInt(static_cast<int64_t>(scale)));
224 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:416
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:381
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCastFromFp ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti 
)
private

Definition at line 517 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CgenState::context_, CgenState::emitCall(), get_bit_width(), get_int_type(), SQLTypeInfo::get_notnull(), SQLTypeInfo::get_type(), SQLTypeInfo::get_type_name(), CgenState::inlineFpNull(), CgenState::inlineIntNull(), CgenState::ir_builder_, SQLTypeInfo::is_decimal(), SQLTypeInfo::is_fp(), SQLTypeInfo::is_integer(), SQLTypeInfo::is_number(), kDOUBLE, kFLOAT, and numeric_type_name().

Referenced by codegenCast().

519  {
521  if (!operand_ti.is_fp() || !ti.is_number() || ti.is_decimal()) {
522  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
523  ti.get_type_name() + " not supported");
524  }
525  if (operand_ti.get_type() == ti.get_type()) {
526  // Should not have been called when both dimensions are same.
527  return operand_lv;
528  }
529  CHECK(operand_lv->getType()->isFloatTy() || operand_lv->getType()->isDoubleTy());
530  if (operand_ti.get_notnull()) {
531  if (ti.get_type() == kDOUBLE) {
532  return cgen_state_->ir_builder_.CreateFPExt(
533  operand_lv, llvm::Type::getDoubleTy(cgen_state_->context_));
534  } else if (ti.get_type() == kFLOAT) {
535  return cgen_state_->ir_builder_.CreateFPTrunc(
536  operand_lv, llvm::Type::getFloatTy(cgen_state_->context_));
537  } else if (ti.is_integer()) {
538  // Round by adding/subtracting 0.5 before fptosi.
539  auto* fp_type = operand_lv->getType()->isFloatTy()
540  ? llvm::Type::getFloatTy(cgen_state_->context_)
541  : llvm::Type::getDoubleTy(cgen_state_->context_);
542  auto* zero = llvm::ConstantFP::get(fp_type, 0.0);
543  auto* mhalf = llvm::ConstantFP::get(fp_type, -0.5);
544  auto* phalf = llvm::ConstantFP::get(fp_type, 0.5);
545  auto* is_negative = cgen_state_->ir_builder_.CreateFCmpOLT(operand_lv, zero);
546  auto* offset = cgen_state_->ir_builder_.CreateSelect(is_negative, mhalf, phalf);
547  operand_lv = cgen_state_->ir_builder_.CreateFAdd(operand_lv, offset);
548  return cgen_state_->ir_builder_.CreateFPToSI(
549  operand_lv, get_int_type(get_bit_width(ti), cgen_state_->context_));
550  } else {
551  CHECK(false);
552  }
553  } else {
554  const auto from_tname = numeric_type_name(operand_ti);
555  const auto to_tname = numeric_type_name(ti);
556  if (ti.is_fp()) {
557  return cgen_state_->emitCall("cast_" + from_tname + "_to_" + to_tname + "_nullable",
558  {operand_lv,
559  cgen_state_->inlineFpNull(operand_ti),
560  cgen_state_->inlineFpNull(ti)});
561  } else if (ti.is_integer()) {
562  return cgen_state_->emitCall("cast_" + from_tname + "_to_" + to_tname + "_nullable",
563  {operand_lv,
564  cgen_state_->inlineFpNull(operand_ti),
565  cgen_state_->inlineIntNull(ti)});
566  } else {
567  CHECK(false);
568  }
569  }
570  CHECK(false);
571  return nullptr;
572 }
CgenState * cgen_state_
bool is_fp() const
Definition: sqltypes.h:579
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:379
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
bool is_number() const
Definition: sqltypes.h:580
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:439
bool is_integer() const
Definition: sqltypes.h:577
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::string get_type_name() const
Definition: sqltypes.h:503
#define CHECK(condition)
Definition: Logger.h:222
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386
bool is_decimal() const
Definition: sqltypes.h:578
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCastFromString ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti,
const bool  operand_is_const,
const CompilationOptions co 
)
private

Definition at line 226 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, StringDictionaryTranslationMgr::codegen(), CgenState::context_, CPU, Data_Namespace::CPU_LEVEL, CompilationOptions::device_type, CgenState::emitExternalCall(), executor(), g_cluster, SQLTypeInfo::get_comp_param(), SQLTypeInfo::get_compression(), get_int_type(), SQLTypeInfo::get_type_name(), GPU, Data_Namespace::GPU_LEVEL, SQLTypeInfo::is_dict_intersection(), SQLTypeInfo::is_string(), kENCODING_DICT, kENCODING_NONE, dict_ref_t::literalsDictId, CgenState::llInt(), and CgenState::moveStringDictionaryTranslationMgr().

Referenced by codegenCast().

230  {
232  if (!ti.is_string()) {
233  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
234  ti.get_type_name() + " not supported");
235  }
236  if (operand_ti.get_compression() == kENCODING_NONE &&
238  return operand_lv;
239  }
240  if (ti.get_compression() == kENCODING_DICT &&
241  operand_ti.get_compression() == kENCODING_DICT) {
242  if (ti.get_comp_param() == operand_ti.get_comp_param()) {
243  return operand_lv;
244  }
245  if (operand_ti.get_comp_param() == DictRef::literalsDictId) {
246  // Anything being casted from a literal dictionary is not materialized at this point
247  // Should already have been kicked to CPU if it was originally a GPU query
248 
250  const int64_t source_string_proxy_handle =
251  reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
252  operand_ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
253 
254  const int64_t dest_string_proxy_handle =
255  reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
256  ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
257 
258  auto source_string_proxy_handle_lv = cgen_state_->llInt(source_string_proxy_handle);
259  auto dest_string_proxy_handle_lv = cgen_state_->llInt(dest_string_proxy_handle);
260 
261  std::vector<llvm::Value*> string_cast_lvs{
262  operand_lv, source_string_proxy_handle_lv, dest_string_proxy_handle_lv};
263  if (ti.is_dict_intersection()) {
265  "intersect_translate_string_id_to_other_dict",
267  string_cast_lvs);
268  } else {
269  return cgen_state_->emitExternalCall("union_translate_string_id_to_other_dict",
271  string_cast_lvs);
272  }
273  }
274 
275  const std::vector<StringOps_Namespace::StringOpInfo> string_op_infos;
276  auto string_dictionary_translation_mgr =
277  std::make_unique<StringDictionaryTranslationMgr>(
278  operand_ti.get_comp_param(),
279  ti.get_comp_param(),
281  ti,
282  string_op_infos,
285  executor()->deviceCount(co.device_type),
286  executor(),
287  &executor()->getCatalog()->getDataMgr(),
288  false /* delay_translation */);
289 
290  return cgen_state_
291  ->moveStringDictionaryTranslationMgr(std::move(string_dictionary_translation_mgr))
292  ->codegen(operand_lv, operand_ti, true /* add_nullcheck */, co);
293  }
294  // dictionary encode non-constant
295  if (operand_ti.get_compression() != kENCODING_DICT && !operand_is_const) {
296  if (g_cluster) {
297  throw std::runtime_error(
298  "Cast from none-encoded string to dictionary-encoded not supported for "
299  "distributed queries");
300  }
301  CHECK_EQ(kENCODING_NONE, operand_ti.get_compression());
303  CHECK(operand_lv->getType()->isIntegerTy(64));
305  throw QueryMustRunOnCpu();
306  }
308  "string_compress",
310  {operand_lv,
311  cgen_state_->llInt(int64_t(executor()->getStringDictionaryProxy(
312  ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true)))});
313  }
314  CHECK(operand_lv->getType()->isIntegerTy(32));
315  if (ti.get_compression() == kENCODING_NONE) {
316  if (g_cluster) {
317  throw std::runtime_error(
318  "Cast from dictionary-encoded string to none-encoded not "
319  "currently supported for distributed queries.");
320  }
321  // Removed watchdog check here in exchange for row cardinality based check in
322  // RelAlgExecutor
323  CHECK_EQ(kENCODING_DICT, operand_ti.get_compression());
325  throw QueryMustRunOnCpu();
326  }
327  const int64_t string_dictionary_ptr =
328  operand_ti.get_comp_param() == 0
329  ? reinterpret_cast<int64_t>(
330  executor()->getRowSetMemoryOwner()->getLiteralStringDictProxy())
331  : reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
332  operand_ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
334  "string_decompress",
336  {operand_lv, cgen_state_->llInt(string_dictionary_ptr)});
337  }
338  CHECK(operand_is_const);
340  return operand_lv;
341 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
bool is_dict_intersection() const
Definition: sqltypes.h:644
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:387
std::string get_type_name() const
Definition: sqltypes.h:503
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:388
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
bool g_cluster
const StringDictionaryTranslationMgr * moveStringDictionaryTranslationMgr(std::unique_ptr< const StringDictionaryTranslationMgr > &&str_dict_translation_mgr)
Definition: CgenState.h:196
bool is_string() const
Definition: sqltypes.h:575
llvm::Value * codegen(llvm::Value *str_id_input, const SQLTypeInfo &input_ti, const bool add_nullcheck, const CompilationOptions &co) const
static constexpr int32_t literalsDictId
Definition: DictRef.h:15
Executor * executor() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCastTimestampToDate ( llvm::Value *  ts_lv,
const int  dimen,
const bool  nullable 
)
private

Definition at line 157 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CgenState::context_, CgenState::emitExternalCall(), get_int_type(), DateTimeUtils::get_timestamp_precision_scale(), CgenState::inlineIntNull(), kBIGINT, and CgenState::llInt().

Referenced by codegenCast().

159  {
161  CHECK(ts_lv->getType()->isIntegerTy(64));
162  if (dimen > 0) {
163  if (nullable) {
165  "DateTruncateHighPrecisionToDateNullable",
167  {{ts_lv,
170  }
172  "DateTruncateHighPrecisionToDate",
174  {{ts_lv,
176  }
177  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
178  if (nullable) {
179  nullcheck_codegen =
180  std::make_unique<NullCheckCodegen>(cgen_state_,
181  executor(),
182  ts_lv,
183  SQLTypeInfo(kTIMESTAMP, dimen, 0, !nullable),
184  "cast_timestamp_nullcheck");
185  }
186  auto ret = cgen_state_->emitExternalCall(
187  "datetrunc_day", get_int_type(64, cgen_state_->context_), {ts_lv});
188  if (nullcheck_codegen) {
189  ret = nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ret);
190  }
191  return ret;
192 }
CgenState * cgen_state_
#define NULL_BIGINT
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51
Executor * executor() const

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCastTimestampToTime ( llvm::Value *  ts_lv,
const int  dimen,
const bool  nullable 
)
private

Definition at line 137 of file CastIR.cpp.

References cgen_state_, CgenState::context_, CgenState::emitExternalCall(), get_int_type(), DateTimeUtils::get_timestamp_precision_scale(), CgenState::inlineIntNull(), kBIGINT, and CgenState::llInt().

Referenced by codegenCast().

139  {
140  std::vector<llvm::Value*> datetrunc_args{ts_lv};
141  std::string hptodate_fname;
142  if (dimen > 0) {
143  hptodate_fname = "ExtractTimeFromHPTimestamp";
144  datetrunc_args.push_back(
146  } else {
147  hptodate_fname = "ExtractTimeFromLPTimestamp";
148  }
149  if (nullable) {
150  datetrunc_args.push_back(cgen_state_->inlineIntNull(SQLTypeInfo(kBIGINT, false)));
151  hptodate_fname += "Nullable";
152  }
154  hptodate_fname, get_int_type(64, cgen_state_->context_), datetrunc_args);
155 }
CgenState * cgen_state_
llvm::Value * emitExternalCall(const std::string &fname, llvm::Type *ret_type, const std::vector< llvm::Value * > args, const std::vector< llvm::Attribute::AttrKind > &fnattrs={}, const bool has_struct_return=false)
Definition: CgenState.h:217
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
constexpr int64_t get_timestamp_precision_scale(const int32_t dimen)
Definition: DateTimeUtils.h:51

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCastToFp ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti 
)
private

Definition at line 474 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CgenState::context_, CgenState::emitCall(), SQLTypeInfo::get_notnull(), SQLTypeInfo::get_scale(), SQLTypeInfo::get_type(), SQLTypeInfo::get_type_name(), CgenState::inlineFpNull(), CgenState::inlineIntNull(), CgenState::ir_builder_, SQLTypeInfo::is_fp(), kFLOAT, numeric_type_name(), and shared::power10inv().

Referenced by codegenCast().

476  {
478  if (!ti.is_fp()) {
479  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
480  ti.get_type_name() + " not supported");
481  }
482  llvm::Value* result_lv;
483  if (operand_ti.get_notnull()) {
484  auto const fp_type = ti.get_type() == kFLOAT
485  ? llvm::Type::getFloatTy(cgen_state_->context_)
486  : llvm::Type::getDoubleTy(cgen_state_->context_);
487  result_lv = cgen_state_->ir_builder_.CreateSIToFP(operand_lv, fp_type);
488  if (auto const scale = static_cast<unsigned>(operand_ti.get_scale())) {
489  double const multiplier = shared::power10inv(scale);
490  result_lv = cgen_state_->ir_builder_.CreateFMul(
491  result_lv, llvm::ConstantFP::get(result_lv->getType(), multiplier));
492  }
493  } else {
494  if (auto const scale = static_cast<unsigned>(operand_ti.get_scale())) {
495  double const multiplier = shared::power10inv(scale);
496  auto const fp_type = ti.get_type() == kFLOAT
497  ? llvm::Type::getFloatTy(cgen_state_->context_)
498  : llvm::Type::getDoubleTy(cgen_state_->context_);
499  result_lv = cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
500  numeric_type_name(ti) + "_scaled_nullable",
501  {operand_lv,
502  cgen_state_->inlineIntNull(operand_ti),
504  llvm::ConstantFP::get(fp_type, multiplier)});
505  } else {
506  result_lv = cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
507  numeric_type_name(ti) + "_nullable",
508  {operand_lv,
509  cgen_state_->inlineIntNull(operand_ti),
510  cgen_state_->inlineFpNull(ti)});
511  }
512  }
513  CHECK(result_lv);
514  return result_lv;
515 }
CgenState * cgen_state_
bool is_fp() const
Definition: sqltypes.h:579
HOST DEVICE int get_scale() const
Definition: sqltypes.h:384
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:379
llvm::LLVMContext & context_
Definition: CgenState.h:439
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
double power10inv(unsigned const x)
Definition: misc.h:276
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::string get_type_name() const
Definition: sqltypes.h:503
#define CHECK(condition)
Definition: Logger.h:222
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:386
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:106

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCmp ( const Analyzer::BinOper bin_oper,
const CompilationOptions co 
)
private

Definition at line 230 of file CompareIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, anonymous_namespace{CompareIR.cpp}::check_array_comp_cond(), CHECK_EQ, codegen(), codegenCmpDecimalConst(), codegenLogical(), codegenOverlaps(), codegenStrCmp(), Analyzer::BinOper::get_left_operand(), Analyzer::BinOper::get_optype(), Analyzer::BinOper::get_own_left_operand(), Analyzer::BinOper::get_own_right_operand(), Analyzer::BinOper::get_qualifier(), Analyzer::BinOper::get_right_operand(), IS_EQUIVALENCE, is_unnest(), kBW_EQ, kNE, kOVERLAPS, anonymous_namespace{CompareIR.cpp}::lower_bw_eq(), and anonymous_namespace{CompareIR.cpp}::lower_multicol_compare().

Referenced by codegen(), codegenCmpDecimalConst(), and codegenWidthBucketExpr().

231  {
233  const auto qualifier = bin_oper->get_qualifier();
234  const auto lhs = bin_oper->get_left_operand();
235  const auto rhs = bin_oper->get_right_operand();
236  if (dynamic_cast<const Analyzer::ExpressionTuple*>(lhs)) {
237  CHECK(dynamic_cast<const Analyzer::ExpressionTuple*>(rhs));
238  const auto lowered = lower_multicol_compare(bin_oper);
239  const auto lowered_lvs = codegen(lowered.get(), true, co);
240  CHECK_EQ(size_t(1), lowered_lvs.size());
241  return lowered_lvs.front();
242  }
243  const auto optype = bin_oper->get_optype();
244  if (optype == kBW_EQ) {
245  const auto bw_eq_oper = lower_bw_eq(bin_oper);
246  return codegenLogical(bw_eq_oper.get(), co);
247  }
248  if (optype == kOVERLAPS) {
249  return codegenOverlaps(optype,
250  qualifier,
251  bin_oper->get_own_left_operand(),
252  bin_oper->get_own_right_operand(),
253  co);
254  }
255  if (is_unnest(lhs) || is_unnest(rhs)) {
256  throw std::runtime_error("Unnest not supported in comparisons");
257  }
258  check_array_comp_cond(bin_oper);
259  const auto& lhs_ti = lhs->get_type_info();
260  const auto& rhs_ti = rhs->get_type_info();
261 
262  if (lhs_ti.is_string() && rhs_ti.is_string() &&
263  !(IS_EQUIVALENCE(optype) || optype == kNE)) {
264  auto cmp_str = codegenStrCmp(optype,
265  qualifier,
266  bin_oper->get_own_left_operand(),
267  bin_oper->get_own_right_operand(),
268  co);
269  if (cmp_str) {
270  return cmp_str;
271  }
272  }
273 
274  if (lhs_ti.is_decimal()) {
275  auto cmp_decimal_const =
276  codegenCmpDecimalConst(optype, qualifier, lhs, lhs_ti, rhs, co);
277  if (cmp_decimal_const) {
278  return cmp_decimal_const;
279  }
280  }
281  auto lhs_lvs = codegen(lhs, true, co);
282  return codegenCmp(optype, qualifier, lhs_lvs, lhs_ti, rhs, co);
283 }
void check_array_comp_cond(const Analyzer::BinOper *bin_oper)
Definition: CompareIR.cpp:182
#define CHECK_EQ(x, y)
Definition: Logger.h:230
llvm::Value * codegenStrCmp(const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
Definition: CompareIR.cpp:366
#define IS_EQUIVALENCE(X)
Definition: sqldefs.h:68
CgenState * cgen_state_
std::shared_ptr< Analyzer::BinOper > lower_multicol_compare(const Analyzer::BinOper *multicol_compare)
Definition: CompareIR.cpp:159
const Expr * get_right_operand() const
Definition: Analyzer.h:451
std::shared_ptr< Analyzer::BinOper > lower_bw_eq(const Analyzer::BinOper *bw_eq)
Definition: CompareIR.cpp:125
SQLOps get_optype() const
Definition: Analyzer.h:447
llvm::Value * codegenCmpDecimalConst(const SQLOps, const SQLQualifier, const Analyzer::Expr *, const SQLTypeInfo &, const Analyzer::Expr *, const CompilationOptions &)
Definition: CompareIR.cpp:398
llvm::Value * codegenOverlaps(const SQLOps, const SQLQualifier, const std::shared_ptr< Analyzer::Expr >, const std::shared_ptr< Analyzer::Expr >, const CompilationOptions &)
Definition: CompareIR.cpp:285
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:31
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:230
#define CHECK(condition)
Definition: Logger.h:222
Definition: sqldefs.h:30
llvm::Value * codegenLogical(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:298
const Expr * get_left_operand() const
Definition: Analyzer.h:450
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1464
const std::shared_ptr< Analyzer::Expr > get_own_right_operand() const
Definition: Analyzer.h:455
const std::shared_ptr< Analyzer::Expr > get_own_left_operand() const
Definition: Analyzer.h:452
SQLQualifier get_qualifier() const
Definition: Analyzer.h:449

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value * CodeGenerator::codegenCmp ( const SQLOps  optype,
const SQLQualifier  qualifier,
std::vector< llvm::Value * >  lhs_lvs,
const SQLTypeInfo lhs_ti,
const Analyzer::Expr rhs,
const CompilationOptions co 
)
private

Definition at line 451 of file CompareIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), codegenQualifierCmp(), CgenState::emitCall(), SQLTypeInfo::get_compression(), get_null_check_suffix(), SQLTypeInfo::get_type(), Analyzer::Expr::get_type_info(), SQLTypeInfo::is_boolean(), IS_COMPARISON, SQLTypeInfo::is_decimal(), SQLTypeInfo::is_geometry(), SQLTypeInfo::is_integer(), SQLTypeInfo::is_string(), SQLTypeInfo::is_time(), SQLTypeInfo::is_timeinterval(), kENCODING_NONE, kONE, and kOVERLAPS.

456  {
458  CHECK(IS_COMPARISON(optype));
459  const auto& rhs_ti = rhs->get_type_info();
460  if (rhs_ti.is_array()) {
461  return codegenQualifierCmp(optype, qualifier, lhs_lvs, rhs, co);
462  }
463  auto rhs_lvs = codegen(rhs, true, co);
464  CHECK_EQ(kONE, qualifier);
465  if (optype == kOVERLAPS) {
466  CHECK(lhs_ti.is_geometry());
467  CHECK(rhs_ti.is_array() ||
468  rhs_ti.is_geometry()); // allow geo col or bounds col to pass
469  } else {
470  CHECK((lhs_ti.get_type() == rhs_ti.get_type()) ||
471  (lhs_ti.is_string() && rhs_ti.is_string()));
472  }
473  const auto null_check_suffix = get_null_check_suffix(lhs_ti, rhs_ti);
474  if (lhs_ti.is_integer() || lhs_ti.is_decimal() || lhs_ti.is_time() ||
475  lhs_ti.is_boolean() || lhs_ti.is_string() || lhs_ti.is_timeinterval()) {
476  if (lhs_ti.is_string()) {
477  CHECK(rhs_ti.is_string());
478  CHECK_EQ(lhs_ti.get_compression(), rhs_ti.get_compression());
479  if (lhs_ti.get_compression() == kENCODING_NONE) {
480  // unpack pointer + length if necessary
481  if (lhs_lvs.size() != 3) {
482  CHECK_EQ(size_t(1), lhs_lvs.size());
483  lhs_lvs.push_back(cgen_state_->emitCall("extract_str_ptr", {lhs_lvs.front()}));
484  lhs_lvs.push_back(cgen_state_->emitCall("extract_str_len", {lhs_lvs.front()}));
485  }
486  if (rhs_lvs.size() != 3) {
487  CHECK_EQ(size_t(1), rhs_lvs.size());
488  rhs_lvs.push_back(cgen_state_->emitCall("extract_str_ptr", {rhs_lvs.front()}));
489  rhs_lvs.push_back(cgen_state_->emitCall("extract_str_len", {rhs_lvs.front()}));
490  }
491  std::vector<llvm::Value*> str_cmp_args{
492  lhs_lvs[1], lhs_lvs[2], rhs_lvs[1], rhs_lvs[2]};
493  if (!null_check_suffix.empty()) {
494  str_cmp_args.push_back(
496  }
497  return cgen_state_->emitCall(
498  string_cmp_func(optype) + (null_check_suffix.empty() ? "" : "_nullable"),
499  str_cmp_args);
500  } else {
501  CHECK(optype == kEQ || optype == kNE);
502  }
503  }
504 
505  if (lhs_ti.is_boolean() && rhs_ti.is_boolean()) {
506  auto& lhs_lv = lhs_lvs.front();
507  auto& rhs_lv = rhs_lvs.front();
508  CHECK(lhs_lv->getType()->isIntegerTy());
509  CHECK(rhs_lv->getType()->isIntegerTy());
510  if (lhs_lv->getType()->getIntegerBitWidth() <
511  rhs_lv->getType()->getIntegerBitWidth()) {
512  lhs_lv =
513  cgen_state_->castToTypeIn(lhs_lv, rhs_lv->getType()->getIntegerBitWidth());
514  } else {
515  rhs_lv =
516  cgen_state_->castToTypeIn(rhs_lv, lhs_lv->getType()->getIntegerBitWidth());
517  }
518  }
519 
520  return null_check_suffix.empty()
521  ? cgen_state_->ir_builder_.CreateICmp(
522  llvm_icmp_pred(optype), lhs_lvs.front(), rhs_lvs.front())
524  icmp_name(optype) + "_" + numeric_type_name(lhs_ti) +
525  null_check_suffix,
526  {lhs_lvs.front(),
527  rhs_lvs.front(),
530  }
531  if (lhs_ti.get_type() == kFLOAT || lhs_ti.get_type() == kDOUBLE) {
532  return null_check_suffix.empty()
533  ? cgen_state_->ir_builder_.CreateFCmp(
534  llvm_fcmp_pred(optype), lhs_lvs.front(), rhs_lvs.front())
536  icmp_name(optype) + "_" + numeric_type_name(lhs_ti) +
537  null_check_suffix,
538  {lhs_lvs.front(),
539  rhs_lvs.front(),
540  lhs_ti.get_type() == kFLOAT ? cgen_state_->llFp(NULL_FLOAT)
543  }
544  CHECK(false);
545  return nullptr;
546 }
#define CHECK_EQ(x, y)
Definition: Logger.h:230
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:152
#define NULL_DOUBLE
std::string icmp_name(const SQLOps op_type)
Definition: CompareIR.cpp:45
CgenState * cgen_state_
#define NULL_FLOAT
llvm::IRBuilder ir_builder_
Definition: CgenState.h:441
Definition: sqldefs.h:29
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:379
llvm::CmpInst::Predicate llvm_fcmp_pred(const SQLOps op_type)
Definition: CompareIR.cpp:83
bool is_time() const
Definition: sqltypes.h:581
bool is_integer() const
Definition: sqltypes.h:577
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
bool is_timeinterval() const
Definition: sqltypes.h:586
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:310
bool is_boolean() const
Definition: sqltypes.h:582
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::string get_null_check_suffix(const SQLTypeInfo &lhs_ti, const SQLTypeInfo &rhs_ti)
Definition: Execute.h:1448
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:82
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:219
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:70
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:387
llvm::Value * codegenQualifierCmp(const SQLOps, const SQLQualifier, std::vector< llvm::Value * >, const Analyzer::Expr *, const CompilationOptions &)
Definition: CompareIR.cpp:548
Definition: sqldefs.h:31
llvm::CmpInst::Predicate llvm_icmp_pred(const SQLOps op_type)
Definition: CompareIR.cpp:26
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:306
#define CHECK(condition)
Definition: Logger.h:222
bool is_geometry() const
Definition: sqltypes.h:587
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
bool is_string() const
Definition: sqltypes.h:575
bool is_decimal() const
Definition: sqltypes.h:578
std::string string_cmp_func(const SQLOps optype)
Definition: CompareIR.cpp:106
#define IS_COMPARISON(X)
Definition: sqldefs.h:57

+ Here is the call graph for this function:

llvm::Value * CodeGenerator::codegenCmpDecimalConst ( const SQLOps  optype,
const SQLQualifier  qualifier,
const Analyzer::Expr lhs,
const SQLTypeInfo lhs_ti,
const Analyzer::Expr rhs,
const CompilationOptions co 
)
private

Definition at line 398 of file CompareIR.cpp.

References AUTOMATIC_IR_METADATA, Datum::bigintval, cgen_state_, codegen(), codegenCast(), codegenCmp(), exp_to_scale(), SQLTypeInfo::get_scale(), Analyzer::Expr::get_type_info(), kCAST, kDECIMAL, and sql_constants::kMaxRepresentableNumericPrecision.

Referenced by codegenCmp().

403  {
405  auto u_oper = dynamic_cast<const Analyzer::UOper*>(lhs);
406  if (!u_oper || u_oper->get_optype() != kCAST) {
407  return nullptr;
408  }
409  auto rhs_constant = dynamic_cast<const Analyzer::Constant*>(rhs);
410  if (!rhs_constant) {
411  return nullptr;
412  }
413  const auto operand = u_oper->get_operand();
414  const auto& operand_ti = operand->get_type_info();
415  if (operand_ti.is_decimal() && operand_ti.get_scale() < lhs_ti.get_scale()) {
416  // lhs decimal type has smaller scale
417  } else if (operand_ti.is_integer() && 0 < lhs_ti.get_scale()) {
418  // lhs is integer, no need to scale it all the way up to the cmp expr scale
419  } else {
420  return nullptr;
421  }
422 
423  auto scale_diff = lhs_ti.get_scale() - operand_ti.get_scale() - 1;
424  int64_t bigintval = rhs_constant->get_constval().bigintval;
425  bool negative = false;
426  if (bigintval < 0) {
427  negative = true;
428  bigintval = -bigintval;
429  }
430  int64_t truncated_decimal = bigintval / exp_to_scale(scale_diff);
431  int64_t decimal_tail = bigintval % exp_to_scale(scale_diff);
432  if (truncated_decimal % 10 == 0 && decimal_tail > 0) {
433  truncated_decimal += 1;
434  }
437  lhs_ti.get_scale() - scale_diff,
438  operand_ti.get_notnull());
439  if (negative) {
440  truncated_decimal = -truncated_decimal;
441  }
442  Datum d;
443  d.bigintval = truncated_decimal;
444  const auto new_rhs_lit =
445  makeExpr<Analyzer::Constant>(new_ti, rhs_constant->get_is_null(), d);
446  const auto operand_lv = codegen(operand, true, co).front();
447  const auto lhs_lv = codegenCast(operand_lv, operand_ti, new_ti, false, co);
448  return codegenCmp(optype, qualifier, {lhs_lv}, new_ti, new_rhs_lit.get(), co);
449 }
static constexpr int32_t kMaxRepresentableNumericPrecision
Definition: sqltypes.h:48
CgenState * cgen_state_
HOST DEVICE int get_scale() const
Definition: sqltypes.h:384
Definition: sqldefs.h:48
int64_t bigintval
Definition: Datum.h:49