OmniSciDB  085a039ca4
 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)
 

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)
 
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)
 
llvm::Value * codegenFixedLengthColVarInWindow (const Analyzer::ColumnVar *col_var, llvm::Value *col_byte_stream, llvm::Value *pos_arg)
 
llvm::Value * codegenWindowPosition (WindowFunctionContext *window_func_context, llvm::Value *pos_arg)
 
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 * 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
 
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 * 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 * 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 1483 of file NativeCodegen.cpp.

Referenced by CgenState::set_module_shallow_copy().

1483  {
1484  return func->getName() == "query_stub_hoisted_literals" ||
1485  func->getName() == "multifrag_query_hoisted_literals" ||
1486  func->getName() == "query_stub" || func->getName() == "multifrag_query" ||
1487  func->getName() == "fixed_width_int_decode" ||
1488  func->getName() == "fixed_width_unsigned_decode" ||
1489  func->getName() == "diff_fixed_width_int_decode" ||
1490  func->getName() == "fixed_width_double_decode" ||
1491  func->getName() == "fixed_width_float_decode" ||
1492  func->getName() == "fixed_width_small_date_decode" ||
1493  func->getName() == "record_error_code" || func->getName() == "get_error_code" ||
1494  func->getName() == "pos_start_impl" || func->getName() == "pos_step_impl" ||
1495  func->getName() == "group_buff_idx_impl" ||
1496  func->getName() == "init_shared_mem" ||
1497  func->getName() == "init_shared_mem_nop" || func->getName() == "write_back_nop";
1498 }

+ 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 401 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().

402  {
404  llvm::BasicBlock* args_null_bb{nullptr};
405  llvm::BasicBlock* args_notnull_bb{nullptr};
406  llvm::BasicBlock* orig_bb = cgen_state_->ir_builder_.GetInsertBlock();
407  llvm::Value* null_array_alloca{nullptr};
408  // Only generate the check if required (at least one argument must be nullable).
409  if (ext_func_call_requires_nullcheck(function_oper)) {
410  const auto func_ti = function_oper->get_type_info();
411  if (func_ti.is_buffer()) {
412  const auto arr_struct_ty = get_buffer_struct_type(
413  cgen_state_,
414  function_oper->getName(),
415  0,
417  func_ti.is_array() || func_ti.is_bytes());
418  null_array_alloca = cgen_state_->ir_builder_.CreateAlloca(arr_struct_ty);
419  }
420  const auto args_notnull_lv = cgen_state_->ir_builder_.CreateNot(
421  codegenFunctionOperNullArg(function_oper, orig_arg_lvs));
422  args_notnull_bb = llvm::BasicBlock::Create(
423  cgen_state_->context_, "args_notnull", cgen_state_->current_func_);
424  args_null_bb = llvm::BasicBlock::Create(
426  cgen_state_->ir_builder_.CreateCondBr(args_notnull_lv, args_notnull_bb, args_null_bb);
427  cgen_state_->ir_builder_.SetInsertPoint(args_notnull_bb);
428  }
429  return std::make_tuple(
430  CodeGenerator::ArgNullcheckBBs{args_null_bb, args_notnull_bb, orig_bb},
431  null_array_alloca);
432 }
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:361
bool ext_func_call_requires_nullcheck(const Analyzer::FunctionOper *function_oper)
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::Function * current_func_
Definition: CgenState.h:353
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:81
std::string getName() const
Definition: Analyzer.h:2167

+ 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 1444 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(), and OverlapsJoinHashTable::codegenKey().

1445  {
1447  if (elem_ti.get_type() == kFLOAT) {
1448  return cgen_state_->ir_builder_.CreatePointerCast(
1449  ptr, llvm::Type::getFloatPtrTy(cgen_state_->context_));
1450  }
1451  if (elem_ti.get_type() == kDOUBLE) {
1452  return cgen_state_->ir_builder_.CreatePointerCast(
1453  ptr, llvm::Type::getDoublePtrTy(cgen_state_->context_));
1454  }
1455  CHECK(elem_ti.is_integer() || elem_ti.is_boolean() ||
1456  (elem_ti.is_string() && elem_ti.get_compression() == kENCODING_DICT));
1457  switch (elem_ti.get_size()) {
1458  case 1:
1459  return cgen_state_->ir_builder_.CreatePointerCast(
1460  ptr, llvm::Type::getInt8PtrTy(cgen_state_->context_));
1461  case 2:
1462  return cgen_state_->ir_builder_.CreatePointerCast(
1463  ptr, llvm::Type::getInt16PtrTy(cgen_state_->context_));
1464  case 4:
1465  return cgen_state_->ir_builder_.CreatePointerCast(
1466  ptr, llvm::Type::getInt32PtrTy(cgen_state_->context_));
1467  case 8:
1468  return cgen_state_->ir_builder_.CreatePointerCast(
1469  ptr, llvm::Type::getInt64PtrTy(cgen_state_->context_));
1470  default:
1471  CHECK(false);
1472  }
1473  return nullptr;
1474 }
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
llvm::LLVMContext & context_
Definition: CgenState.h:359
bool is_integer() const
Definition: sqltypes.h:512
bool is_boolean() const
Definition: sqltypes.h:517
#define AUTOMATIC_IR_METADATA(CGENSTATE)
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
#define CHECK(condition)
Definition: Logger.h:223
bool is_string() const
Definition: sqltypes.h:510

+ 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:81
PlanState * plan_state_
#define CHECK(condition)
Definition: Logger.h:223
bool is_decimal() const
Definition: sqltypes.h:513
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:450
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:81
PlanState * plan_state_
#define CHECK(condition)
Definition: Logger.h:223
const Expr * get_left_operand() const
Definition: Analyzer.h:449
bool is_decimal() const
Definition: sqltypes.h:513
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(), 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:515
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:235
std::vector< llvm::Value * > codegenGeoExpr(const Analyzer::GeoExpr *, const CompilationOptions &)
Definition: GeoIR.cpp:96
#define CHECK_NE(x, y)
Definition: Logger.h:232
std::vector< llvm::Value * > codegenGeoUOper(const Analyzer::GeoUOper *, const CompilationOptions &)
Definition: GeoIR.cpp:166
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:81
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:1463
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:49
CgenState * cgen_state_
llvm::Value * addStringConstant(const std::string &str)
Definition: CgenState.h:187
int8_t boolval
Definition: sqltypes.h:211
bool get_is_null() const
Definition: Analyzer.h:341
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
float floatval
Definition: sqltypes.h:216
llvm::LLVMContext & context_
Definition: CgenState.h:359
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const int dict_id)
Definition: ConstantIR.cpp:309
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
std::string * stringval
Definition: sqltypes.h:220
SQLTypes decimal_to_int_type(const SQLTypeInfo &ti)
Definition: Datum.cpp:493
ExecutorDeviceType device_type
Definition: sqltypes.h:52
Definition: sqltypes.h:53
static llvm::ConstantInt * codegenIntConst(const Analyzer::Constant *constant, CgenState *cgen_state)
Definition: ConstantIR.cpp:89
Datum get_constval() const
Definition: Analyzer.h:342
Definition: sqltypes.h:41
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
std::vector< llvm::Value * > codegenGeoConstant(const Analyzer::GeoConstant *, const CompilationOptions &)
Definition: GeoIR.cpp:110
Definition: sqltypes.h:45
double doubleval
Definition: sqltypes.h:217
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:231
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
Definition: sqldefs.h:30
llvm::LLVMContext & context_
Definition: CgenState.h:359
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:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:71
llvm::Value * codegen(llvm::Value *needle, Executor *executor) const
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:640
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:296
#define CHECK(condition)
Definition: Logger.h:223
std::unique_ptr< InValuesBitmap > createInValuesBitmap(const Analyzer::InValues *, const CompilationOptions &)
Definition: InValuesIR.cpp:111
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1427
const Expr * get_arg() const
Definition: Analyzer.h:638
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:231
CgenState * cgen_state_
llvm::LLVMContext & context_
Definition: CgenState.h:359
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:296
#define CHECK(condition)
Definition: Logger.h:223
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1427
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:231
const Expr * get_else_expr() const
Definition: Analyzer.h:1259
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:359
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:223

+ Here is the call graph for this function:

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

Definition at line 73 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.

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

161  {
163  const auto& dateadd_expr_ti = dateadd_expr->get_type_info();
164  CHECK(dateadd_expr_ti.get_type() == kTIMESTAMP || dateadd_expr_ti.get_type() == kDATE);
165  auto datetime = codegen(dateadd_expr->get_datetime_expr(), true, co).front();
166  CHECK(datetime->getType()->isIntegerTy(64));
167  auto number = codegen(dateadd_expr->get_number_expr(), true, co).front();
168 
169  const auto& datetime_ti = dateadd_expr->get_datetime_expr()->get_type_info();
170  std::vector<llvm::Value*> dateadd_args{
171  cgen_state_->llInt(static_cast<int32_t>(dateadd_expr->get_field())),
172  number,
173  datetime};
174  std::string dateadd_fname{"DateAdd"};
175  if (is_subsecond_dateadd_field(dateadd_expr->get_field()) ||
176  dateadd_expr_ti.is_high_precision_timestamp()) {
177  dateadd_fname += "HighPrecision";
178  dateadd_args.push_back(
179  cgen_state_->llInt(static_cast<int32_t>(datetime_ti.get_dimension())));
180  }
181  if (!datetime_ti.get_notnull()) {
182  dateadd_args.push_back(cgen_state_->inlineIntNull(datetime_ti));
183  dateadd_fname += "Nullable";
184  }
185  return cgen_state_->emitExternalCall(dateadd_fname,
187  dateadd_args,
188  {llvm::Attribute::NoUnwind,
189  llvm::Attribute::ReadNone,
190  llvm::Attribute::Speculatable});
191 }
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:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqltypes.h:53
const Expr * get_datetime_expr() const
Definition: Analyzer.h:1346
DateaddField get_field() const
Definition: Analyzer.h:1344
constexpr bool is_subsecond_dateadd_field(const DateaddField field)
Definition: DateTimeUtils.h:99
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
const Expr * get_number_expr() const
Definition: Analyzer.h:1345

+ Here is the call graph for this function:

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

Definition at line 193 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().

194  {
196  auto start = codegen(datediff_expr->get_start_expr(), true, co).front();
197  CHECK(start->getType()->isIntegerTy(64));
198  auto end = codegen(datediff_expr->get_end_expr(), true, co).front();
199  CHECK(end->getType()->isIntegerTy(32) || end->getType()->isIntegerTy(64));
200  const auto& start_ti = datediff_expr->get_start_expr()->get_type_info();
201  const auto& end_ti = datediff_expr->get_end_expr()->get_type_info();
202  std::vector<llvm::Value*> datediff_args{
203  cgen_state_->llInt(static_cast<int32_t>(datediff_expr->get_field())), start, end};
204  std::string datediff_fname{"DateDiff"};
205  if (start_ti.is_high_precision_timestamp() || end_ti.is_high_precision_timestamp()) {
206  datediff_fname += "HighPrecision";
207  datediff_args.push_back(
208  cgen_state_->llInt(static_cast<int32_t>(start_ti.get_dimension())));
209  datediff_args.push_back(
210  cgen_state_->llInt(static_cast<int32_t>(end_ti.get_dimension())));
211  }
212  const auto& ret_ti = datediff_expr->get_type_info();
213  if (!start_ti.get_notnull() || !end_ti.get_notnull()) {
214  datediff_args.push_back(cgen_state_->inlineIntNull(ret_ti));
215  datediff_fname += "Nullable";
216  }
218  datediff_fname, get_int_type(64, cgen_state_->context_), datediff_args);
219 }
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:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const Expr * get_start_expr() const
Definition: Analyzer.h:1387
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
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:296
const Expr * get_end_expr() const
Definition: Analyzer.h:1388
#define CHECK(condition)
Definition: Logger.h:223
DatetruncField get_field() const
Definition: Analyzer.h:1386

+ Here is the call graph for this function:

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

Definition at line 221 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_.

222  {
224  auto from_expr = codegen(datetrunc_expr->get_from_expr(), true, co).front();
225  const auto& datetrunc_expr_ti = datetrunc_expr->get_from_expr()->get_type_info();
226  CHECK(from_expr->getType()->isIntegerTy(64));
227  DatetruncField const field = datetrunc_expr->get_field();
228  if (datetrunc_expr_ti.is_high_precision_timestamp()) {
229  return codegenDateTruncHighPrecisionTimestamps(from_expr, datetrunc_expr_ti, field);
230  }
231  static_assert(dtSECOND + 1 == dtMILLISECOND, "Please keep these consecutive.");
232  static_assert(dtMILLISECOND + 1 == dtMICROSECOND, "Please keep these consecutive.");
233  static_assert(dtMICROSECOND + 1 == dtNANOSECOND, "Please keep these consecutive.");
234  if (dtSECOND <= field && field <= dtNANOSECOND) {
235  return cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
236  from_expr,
238  }
239  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
240  const bool is_nullable = !datetrunc_expr_ti.get_notnull();
241  if (is_nullable) {
242  nullcheck_codegen = std::make_unique<NullCheckCodegen>(
243  cgen_state_, executor(), from_expr, datetrunc_expr_ti, "date_trunc_nullcheck");
244  }
245  char const* const fname = datetrunc_fname_lookup.at(field);
246  auto ret = cgen_state_->emitExternalCall(
247  fname, get_int_type(64, cgen_state_->context_), {from_expr});
248  if (is_nullable) {
249  ret = nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ret);
250  }
251  return ret;
252 }
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:361
DatetruncField get_field() const
Definition: Analyzer.h:1428
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:359
DatetruncField
Definition: DateTruncate.h:27
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
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:1429
#define CHECK(condition)
Definition: Logger.h:223
llvm::Value * codegenDateTruncHighPrecisionTimestamps(llvm::Value *, const SQLTypeInfo &, const DatetruncField &)
Definition: DateTimeIR.cpp:298
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 111 of file StringOpsIR.cpp.

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

112  {
114  auto str_lv = codegen(expr->get_arg(), true, co);
115  if (str_lv.size() != 3) {
116  CHECK_EQ(size_t(1), str_lv.size());
117  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
118  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
120  throw QueryMustRunOnCpu();
121  }
122  }
123  std::vector<llvm::Value*> charlength_args{str_lv[1], str_lv[2]};
124  std::string fn_name("char_length");
125  if (expr->get_calc_encoded_length()) {
126  fn_name += "_encoded";
127  }
128  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
129  if (is_nullable) {
130  fn_name += "_nullable";
131  charlength_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
132  }
133  return expr->get_calc_encoded_length()
135  fn_name, get_int_type(32, cgen_state_->context_), charlength_args)
136  : cgen_state_->emitCall(fn_name, charlength_args);
137 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
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:712
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:710
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336

+ Here is the call graph for this function:

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

Definition at line 139 of file StringOpsIR.cpp.

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

140  {
142  auto str_lv = codegen(expr->get_arg(), true, co);
143  CHECK_EQ(size_t(1), str_lv.size());
144  return cgen_state_->emitCall("key_for_string_encoded", str_lv);
145 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
CgenState * cgen_state_
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:759

+ 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:231
CgenState * cgen_state_
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:515
#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:804
#define CHECK(condition)
Definition: Logger.h:223
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:1043
bool is_constant_expr() const
Definition: Analyzer.h:1118
llvm::Value * codegenConstantWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:303
CgenState * cgen_state_
Definition: sqldefs.h:49
void set_constant_expr() const
Definition: Analyzer.h:1117
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:3593
std::list< std::shared_ptr< Analyzer::Expr > > getSimpleQuals() const
Definition: PlanState.h:99
const Expr * get_target_value() const
Definition: Analyzer.h:1040
#define CHECK(condition)
Definition: Logger.h:223
bool can_skip_out_of_bound_check() const
Definition: Analyzer.h:1115
void skip_out_of_bound_check() const
Definition: Analyzer.h:1116
const Expr * get_lower_bound() const
Definition: Analyzer.h:1041
const Expr * get_upper_bound() const
Definition: Analyzer.h:1042

+ Here is the call graph for this function:

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

Definition at line 236 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().

237  {
238  CHECK_GE(expr->getArity(), 1UL);
239  if (expr->hasNoneEncodedTextArg()) {
240  return codegenPerRowStringOper(expr, co);
241  }
243 
244  const auto& expr_ti = expr->get_type_info();
245  auto string_dictionary_translation_mgr =
247 
248  auto str_id_lv = codegen(expr->getArg(0), true, co);
249  CHECK_EQ(size_t(1), str_id_lv.size());
250 
251  return cgen_state_
252  ->moveStringDictionaryTranslationMgr(std::move(string_dictionary_translation_mgr))
253  ->codegen(str_id_lv[0], expr_ti, true /* add_nullcheck */, co);
254 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
llvm::Value * codegenPerRowStringOper(const Analyzer::StringOper *string_oper, const CompilationOptions &co)
bool hasNoneEncodedTextArg() const
Definition: Analyzer.h:1541
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:236
size_t getArity() const
Definition: Analyzer.h:1506
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
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:1518
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 288 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.

289  {
291  if (is_unnest(extract_cast_arg(expr->get_arg()))) {
292  throw std::runtime_error("LIKE not supported for unnested expressions");
293  }
294  char escape_char{'\\'};
295  if (expr->get_escape_expr()) {
296  auto escape_char_expr =
297  dynamic_cast<const Analyzer::Constant*>(expr->get_escape_expr());
298  CHECK(escape_char_expr);
299  CHECK(escape_char_expr->get_type_info().is_string());
300  CHECK_EQ(size_t(1), escape_char_expr->get_constval().stringval->size());
301  escape_char = (*escape_char_expr->get_constval().stringval)[0];
302  }
303  auto pattern = dynamic_cast<const Analyzer::Constant*>(expr->get_like_expr());
304  CHECK(pattern);
305  auto fast_dict_like_lv = codegenDictLike(expr->get_own_arg(),
306  pattern,
307  expr->get_is_ilike(),
308  expr->get_is_simple(),
309  escape_char,
310  co);
311  if (fast_dict_like_lv) {
312  return fast_dict_like_lv;
313  }
314  const auto& ti = expr->get_arg()->get_type_info();
315  CHECK(ti.is_string());
316  if (g_enable_watchdog && ti.get_compression() != kENCODING_NONE) {
317  throw WatchdogException(
318  "Cannot do LIKE / ILIKE on this dictionary encoded column, its cardinality is "
319  "too high");
320  }
321  auto str_lv = codegen(expr->get_arg(), true, co);
322  if (str_lv.size() != 3) {
323  CHECK_EQ(size_t(1), str_lv.size());
324  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
325  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
327  throw QueryMustRunOnCpu();
328  }
329  }
330  auto like_expr_arg_lvs = codegen(expr->get_like_expr(), true, co);
331  CHECK_EQ(size_t(3), like_expr_arg_lvs.size());
332  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
333  std::vector<llvm::Value*> str_like_args{
334  str_lv[1], str_lv[2], like_expr_arg_lvs[1], like_expr_arg_lvs[2]};
335  std::string fn_name{expr->get_is_ilike() ? "string_ilike" : "string_like"};
336  if (expr->get_is_simple()) {
337  fn_name += "_simple";
338  } else {
339  str_like_args.push_back(cgen_state_->llInt(int8_t(escape_char)));
340  }
341  if (is_nullable) {
342  fn_name += "_nullable";
343  str_like_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
344  }
345  return cgen_state_->emitCall(fn_name, str_like_args);
346 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
const Expr * get_escape_expr() const
Definition: Analyzer.h:906
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:903
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
bool g_enable_watchdog
bool get_is_simple() const
Definition: Analyzer.h:908
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:905
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:904
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1427
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
bool get_is_ilike() const
Definition: Analyzer.h:907
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 541 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.

542  {
544  if (is_unnest(extract_cast_arg(expr->get_arg()))) {
545  throw std::runtime_error("REGEXP not supported for unnested expressions");
546  }
547  char escape_char{'\\'};
548  if (expr->get_escape_expr()) {
549  auto escape_char_expr =
550  dynamic_cast<const Analyzer::Constant*>(expr->get_escape_expr());
551  CHECK(escape_char_expr);
552  CHECK(escape_char_expr->get_type_info().is_string());
553  CHECK_EQ(size_t(1), escape_char_expr->get_constval().stringval->size());
554  escape_char = (*escape_char_expr->get_constval().stringval)[0];
555  }
556  auto pattern = dynamic_cast<const Analyzer::Constant*>(expr->get_pattern_expr());
557  CHECK(pattern);
558  auto fast_dict_pattern_lv =
559  codegenDictRegexp(expr->get_own_arg(), pattern, escape_char, co);
560  if (fast_dict_pattern_lv) {
561  return fast_dict_pattern_lv;
562  }
563  const auto& ti = expr->get_arg()->get_type_info();
564  CHECK(ti.is_string());
565  if (g_enable_watchdog && ti.get_compression() != kENCODING_NONE) {
566  throw WatchdogException(
567  "Cannot do REGEXP_LIKE on this dictionary encoded column, its cardinality is too "
568  "high");
569  }
570  // Now we know we are working on NONE ENCODED column. So switch back to CPU
572  throw QueryMustRunOnCpu();
573  }
574  auto str_lv = codegen(expr->get_arg(), true, co);
575  if (str_lv.size() != 3) {
576  CHECK_EQ(size_t(1), str_lv.size());
577  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
578  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
579  }
580  auto regexp_expr_arg_lvs = codegen(expr->get_pattern_expr(), true, co);
581  CHECK_EQ(size_t(3), regexp_expr_arg_lvs.size());
582  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
583  std::vector<llvm::Value*> regexp_args{
584  str_lv[1], str_lv[2], regexp_expr_arg_lvs[1], regexp_expr_arg_lvs[2]};
585  std::string fn_name("regexp_like");
586  regexp_args.push_back(cgen_state_->llInt(int8_t(escape_char)));
587  if (is_nullable) {
588  fn_name += "_nullable";
589  regexp_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
591  fn_name, get_int_type(8, cgen_state_->context_), regexp_args);
592  }
594  fn_name, get_int_type(1, cgen_state_->context_), regexp_args);
595 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:976
CgenState * cgen_state_
const Expr * get_escape_expr() const
Definition: Analyzer.h:978
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:975
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:359
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:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:977
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1427
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336

+ 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:515
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
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:296
#define CHECK(condition)
Definition: Logger.h:223
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
const Expr * get_arg() const
Definition: Analyzer.h:849

+ 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:61
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:446
#define AUTOMATIC_IR_METADATA(CGENSTATE)
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:62
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:58

+ 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:267
Definition: sqldefs.h:49
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:39
SQLOps get_optype() const
Definition: Analyzer.h:377

+ 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:231
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::Function * current_func_
Definition: CgenState.h:353
bool is_integer() const
Definition: sqltypes.h:512
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:1349
bool is_timeinterval() const
Definition: sqltypes.h:521
bool needs_error_check_
Definition: CgenState.h:381
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
ExecutorDeviceType device_type
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
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:513
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:119

+ 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:231
CgenState * cgen_state_
const Expr * get_right_operand() const
Definition: Analyzer.h:450
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:446
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:318
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
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:62
#define CHECK(condition)
Definition: Logger.h:223
const Expr * get_left_operand() const
Definition: Analyzer.h:449

+ 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:231
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:450
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:515
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::string to_string(char const *&&v)
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define CHECK(condition)
Definition: Logger.h:223
const Expr * get_left_operand() const
Definition: Analyzer.h:449
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:102

+ 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 655 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().

659  {
661  const auto elem_ti =
662  SQLTypeInfo(
663  SQLTypes::kARRAY, 0, 0, false, EncodingType::kENCODING_NONE, 0, array_type)
664  .get_elem_type();
665 
666  auto buff = cgen_state_->emitExternalCall(
667  "array_buff", llvm::Type::getInt32PtrTy(cgen_state_->context_), {chunk, row_pos});
668 
669  auto len = cgen_state_->emitExternalCall(
670  "array_size",
672  {chunk, row_pos, cgen_state_->llInt(log2_bytes(elem_ti.get_logical_size()))});
673 
674  if (cast_and_extend) {
675  buff = castArrayPointer(buff, elem_ti);
676  len =
677  cgen_state_->ir_builder_.CreateZExt(len, get_int_type(64, cgen_state_->context_));
678  }
679 
680  return std::make_pair(buff, len);
681 }
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:361
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:359
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
SQLTypeInfo get_elem_type() const
Definition: sqltypes.h:865

+ 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:361
#define UNREACHABLE()
Definition: Logger.h:267
bool isNull() const
Definition: Analyzer.h:2304
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:300
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
size_t getElementCount() const
Definition: Analyzer.h:2302
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:2303
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
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:2306
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:361
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::Function * current_func_
Definition: CgenState.h:353
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:1349
bool needs_error_check_
Definition: CgenState.h:381
#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:296
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 683 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().

688  {
690  CHECK(buffer_buf);
691  CHECK(buffer_size);
692 
693  auto buffer_abstraction = get_buffer_struct_type(
694  cgen_state_, ext_func_name, param_num, buffer_buf->getType(), !!(buffer_null));
695  auto alloc_mem = cgen_state_->ir_builder_.CreateAlloca(buffer_abstraction);
696 
697  auto buffer_buf_ptr =
698  cgen_state_->ir_builder_.CreateStructGEP(buffer_abstraction, alloc_mem, 0);
699  cgen_state_->ir_builder_.CreateStore(buffer_buf, buffer_buf_ptr);
700 
701  auto buffer_size_ptr =
702  cgen_state_->ir_builder_.CreateStructGEP(buffer_abstraction, alloc_mem, 1);
703  cgen_state_->ir_builder_.CreateStore(buffer_size, buffer_size_ptr);
704 
705  if (buffer_null) {
706  auto bool_extended_type = llvm::Type::getInt8Ty(cgen_state_->context_);
707  auto buffer_null_extended =
708  cgen_state_->ir_builder_.CreateZExt(buffer_null, bool_extended_type);
709  auto buffer_is_null_ptr =
710  cgen_state_->ir_builder_.CreateStructGEP(buffer_abstraction, alloc_mem, 2);
711  cgen_state_->ir_builder_.CreateStore(buffer_null_extended, buffer_is_null_ptr);
712  }
713  output_args.push_back(alloc_mem);
714 }
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:361
llvm::LLVMContext & context_
Definition: CgenState.h:359
#define AUTOMATIC_IR_METADATA(CGENSTATE)
#define CHECK(condition)
Definition: Logger.h:223

+ 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:231
const Expr * get_else_expr() const
Definition: Analyzer.h:1259
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::Function * current_func_
Definition: CgenState.h:353
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:223
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1256

+ 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:231
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
Definition: sqldefs.h:49
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:359
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
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:378
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:21
SQLOps get_optype() const
Definition: Analyzer.h:377

+ 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(), 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, 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 || operand_ti.get_type() == kDATE) &&
113  ti.get_type() == kTIMESTAMP) {
114  const auto operand_dimen =
115  (operand_ti.is_timestamp()) ? operand_ti.get_dimension() : 0;
116  if (operand_dimen != ti.get_dimension()) {
118  operand_lv, operand_ti, ti, !ti.get_notnull());
119  }
120  }
121  if (ti.is_integer() || ti.is_decimal() || ti.is_time()) {
122  return codegenCastBetweenIntTypes(operand_lv, operand_ti, ti);
123  } else {
124  return codegenCastToFp(operand_lv, operand_ti, ti);
125  }
126  } else {
127  return codegenCastFromFp(operand_lv, operand_ti, ti);
128  }
129  CHECK(false);
130  return nullptr;
131 }
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:148
llvm::Value * codegenCastFromFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:492
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
bool is_timestamp() const
Definition: sqltypes.h:895
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
llvm::Value * codegenCastToFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:449
bool byte_array_cast(const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:62
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
bool is_time() const
Definition: sqltypes.h:516
llvm::LLVMContext & context_
Definition: CgenState.h:359
bool is_integer() const
Definition: sqltypes.h:512
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:318
bool is_boolean() const
Definition: sqltypes.h:517
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
Definition: CastIR.cpp:133
#define AUTOMATIC_IR_METADATA(CGENSTATE)
Definition: sqltypes.h:53
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
llvm::Value * codegenCastBetweenTimestamps(llvm::Value *ts_lv, const SQLTypeInfo &operand_dimen, const SQLTypeInfo &target_dimen, const bool nullable)
Definition: CastIR.cpp:170
#define CHECK(condition)
Definition: Logger.h:223
llvm::Value * codegenCastFromString(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const bool operand_is_const, const CompilationOptions &co)
Definition: CastIR.cpp:202
bool is_string() const
Definition: sqltypes.h:510
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
llvm::ArrayType * get_int_array_type(int const width, int count, llvm::LLVMContext &context)
bool is_decimal() const
Definition: sqltypes.h:513

+ 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 318 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().

321  {
323  if (ti.is_decimal() &&
324  (!operand_ti.is_decimal() || operand_ti.get_scale() <= ti.get_scale())) {
325  if (upscale) {
326  if (operand_ti.get_scale() < ti.get_scale()) { // scale only if needed
327  auto scale = exp_to_scale(ti.get_scale() - operand_ti.get_scale());
328  const auto scale_lv =
329  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), scale);
330  operand_lv = cgen_state_->ir_builder_.CreateSExt(
331  operand_lv, get_int_type(64, cgen_state_->context_));
332 
333  codegenCastBetweenIntTypesOverflowChecks(operand_lv, operand_ti, ti, scale);
334 
335  if (operand_ti.get_notnull()) {
336  operand_lv = cgen_state_->ir_builder_.CreateMul(operand_lv, scale_lv);
337  } else {
338  operand_lv = cgen_state_->emitCall(
339  "scale_decimal_up",
340  {operand_lv,
341  scale_lv,
342  cgen_state_->llInt(inline_int_null_val(operand_ti)),
344  }
345  }
346  }
347  } else if (operand_ti.is_decimal()) {
348  // rounded scale down
349  auto scale = (int64_t)exp_to_scale(operand_ti.get_scale() - ti.get_scale());
350  const auto scale_lv =
351  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), scale);
352 
353  const auto operand_width =
354  static_cast<llvm::IntegerType*>(operand_lv->getType())->getBitWidth();
355 
356  std::string method_name = "scale_decimal_down_nullable";
357  if (operand_ti.get_notnull()) {
358  method_name = "scale_decimal_down_not_nullable";
359  }
360 
361  CHECK(operand_width == 64);
362  operand_lv = cgen_state_->emitCall(
363  method_name,
364  {operand_lv, scale_lv, cgen_state_->llInt(inline_int_null_val(operand_ti))});
365  }
366  if (ti.is_integer() && operand_ti.is_integer() &&
367  operand_ti.get_logical_size() > ti.get_logical_size()) {
368  codegenCastBetweenIntTypesOverflowChecks(operand_lv, operand_ti, ti, 1);
369  }
370 
371  const auto operand_width =
372  static_cast<llvm::IntegerType*>(operand_lv->getType())->getBitWidth();
373  const auto target_width = get_bit_width(ti);
374  if (target_width == operand_width) {
375  return operand_lv;
376  }
377  if (operand_ti.get_notnull()) {
378  return cgen_state_->ir_builder_.CreateCast(
379  target_width > operand_width ? llvm::Instruction::CastOps::SExt
380  : llvm::Instruction::CastOps::Trunc,
381  operand_lv,
382  get_int_type(target_width, cgen_state_->context_));
383  }
384  return cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
385  numeric_type_name(ti) + "_nullable",
386  {operand_lv,
387  cgen_state_->inlineIntNull(operand_ti),
388  cgen_state_->inlineIntNull(ti)});
389 }
CgenState * cgen_state_
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
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:359
int get_logical_size() const
Definition: sqltypes.h:349
bool is_integer() const
Definition: sqltypes.h:512
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:215
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:391
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
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:336
bool is_decimal() const
Definition: sqltypes.h:513

+ 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 391 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().

395  {
397  llvm::Value* chosen_max{nullptr};
398  llvm::Value* chosen_min{nullptr};
399  std::tie(chosen_max, chosen_min) =
401 
403  auto cast_ok = llvm::BasicBlock::Create(
405  auto cast_fail = llvm::BasicBlock::Create(
407  auto operand_max = static_cast<llvm::ConstantInt*>(chosen_max)->getSExtValue() / scale;
408  auto operand_min = static_cast<llvm::ConstantInt*>(chosen_min)->getSExtValue() / scale;
409  const auto ti_llvm_type =
411  llvm::Value* operand_max_lv = llvm::ConstantInt::get(ti_llvm_type, operand_max);
412  llvm::Value* operand_min_lv = llvm::ConstantInt::get(ti_llvm_type, operand_min);
413  const bool is_narrowing = operand_ti.get_logical_size() > ti.get_logical_size();
414  if (is_narrowing) {
415  const auto operand_ti_llvm_type =
417  operand_max_lv =
418  cgen_state_->ir_builder_.CreateSExt(operand_max_lv, operand_ti_llvm_type);
419  operand_min_lv =
420  cgen_state_->ir_builder_.CreateSExt(operand_min_lv, operand_ti_llvm_type);
421  }
422  llvm::Value* over{nullptr};
423  llvm::Value* under{nullptr};
424  if (operand_ti.get_notnull()) {
425  over = cgen_state_->ir_builder_.CreateICmpSGT(operand_lv, operand_max_lv);
426  under = cgen_state_->ir_builder_.CreateICmpSLE(operand_lv, operand_min_lv);
427  } else {
428  const auto type_name =
429  is_narrowing ? numeric_type_name(operand_ti) : numeric_type_name(ti);
430  const auto null_operand_val = cgen_state_->llInt(inline_int_null_val(operand_ti));
431  const auto null_bool_val = cgen_state_->inlineIntNull(SQLTypeInfo(kBOOLEAN, false));
432  over = toBool(cgen_state_->emitCall(
433  "gt_" + type_name + "_nullable_lhs",
434  {operand_lv, operand_max_lv, null_operand_val, null_bool_val}));
435  under = toBool(cgen_state_->emitCall(
436  "le_" + type_name + "_nullable_lhs",
437  {operand_lv, operand_min_lv, null_operand_val, null_bool_val}));
438  }
439  const auto detected = cgen_state_->ir_builder_.CreateOr(over, under, "overflow");
440  cgen_state_->ir_builder_.CreateCondBr(detected, cast_fail, cast_ok);
441 
442  cgen_state_->ir_builder_.SetInsertPoint(cast_fail);
443  cgen_state_->ir_builder_.CreateRet(
445 
446  cgen_state_->ir_builder_.SetInsertPoint(cast_ok);
447 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::Function * current_func_
Definition: CgenState.h:353
int get_logical_size() const
Definition: sqltypes.h:349
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
static const int32_t ERR_OVERFLOW_OR_UNDERFLOW
Definition: Execute.h:1349
bool needs_error_check_
Definition: CgenState.h:381
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
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:336
constexpr auto type_name() noexcept
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:119

+ 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 170 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().

173  {
175  const auto operand_dimen = operand_ti.get_dimension();
176  const auto target_dimen = target_ti.get_dimension();
177  if (operand_dimen == target_dimen) {
178  return ts_lv;
179  }
180  CHECK(ts_lv->getType()->isIntegerTy(64));
181  const auto scale =
182  DateTimeUtils::get_timestamp_precision_scale(abs(operand_dimen - target_dimen));
183  if (operand_dimen < target_dimen) {
184  codegenCastBetweenIntTypesOverflowChecks(ts_lv, operand_ti, target_ti, scale);
185  return nullable
186  ? cgen_state_->emitCall("mul_int64_t_nullable_lhs",
187  {ts_lv,
188  cgen_state_->llInt(static_cast<int64_t>(scale)),
189  cgen_state_->inlineIntNull(operand_ti)})
190  : cgen_state_->ir_builder_.CreateMul(
191  ts_lv, cgen_state_->llInt(static_cast<int64_t>(scale)));
192  }
193  return nullable
194  ? cgen_state_->emitCall("floor_div_nullable_lhs",
195  {ts_lv,
196  cgen_state_->llInt(static_cast<int64_t>(scale)),
197  cgen_state_->inlineIntNull(operand_ti)})
198  : cgen_state_->ir_builder_.CreateSDiv(
199  ts_lv, cgen_state_->llInt(static_cast<int64_t>(scale)));
200 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
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:215
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:391
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
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 492 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().

494  {
496  if (!operand_ti.is_fp() || !ti.is_number() || ti.is_decimal()) {
497  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
498  ti.get_type_name() + " not supported");
499  }
500  if (operand_ti.get_type() == ti.get_type()) {
501  // Should not have been called when both dimensions are same.
502  return operand_lv;
503  }
504  CHECK(operand_lv->getType()->isFloatTy() || operand_lv->getType()->isDoubleTy());
505  if (operand_ti.get_notnull()) {
506  if (ti.get_type() == kDOUBLE) {
507  return cgen_state_->ir_builder_.CreateFPExt(
508  operand_lv, llvm::Type::getDoubleTy(cgen_state_->context_));
509  } else if (ti.get_type() == kFLOAT) {
510  return cgen_state_->ir_builder_.CreateFPTrunc(
511  operand_lv, llvm::Type::getFloatTy(cgen_state_->context_));
512  } else if (ti.is_integer()) {
513  // Round by adding/subtracting 0.5 before fptosi.
514  auto* fp_type = operand_lv->getType()->isFloatTy()
515  ? llvm::Type::getFloatTy(cgen_state_->context_)
516  : llvm::Type::getDoubleTy(cgen_state_->context_);
517  auto* zero = llvm::ConstantFP::get(fp_type, 0.0);
518  auto* mhalf = llvm::ConstantFP::get(fp_type, -0.5);
519  auto* phalf = llvm::ConstantFP::get(fp_type, 0.5);
520  auto* is_negative = cgen_state_->ir_builder_.CreateFCmpOLT(operand_lv, zero);
521  auto* offset = cgen_state_->ir_builder_.CreateSelect(is_negative, mhalf, phalf);
522  operand_lv = cgen_state_->ir_builder_.CreateFAdd(operand_lv, offset);
523  return cgen_state_->ir_builder_.CreateFPToSI(
524  operand_lv, get_int_type(get_bit_width(ti), cgen_state_->context_));
525  } else {
526  CHECK(false);
527  }
528  } else {
529  const auto from_tname = numeric_type_name(operand_ti);
530  const auto to_tname = numeric_type_name(ti);
531  if (ti.is_fp()) {
532  return cgen_state_->emitCall("cast_" + from_tname + "_to_" + to_tname + "_nullable",
533  {operand_lv,
534  cgen_state_->inlineFpNull(operand_ti),
535  cgen_state_->inlineFpNull(ti)});
536  } else if (ti.is_integer()) {
537  return cgen_state_->emitCall("cast_" + from_tname + "_to_" + to_tname + "_nullable",
538  {operand_lv,
539  cgen_state_->inlineFpNull(operand_ti),
540  cgen_state_->inlineIntNull(ti)});
541  } else {
542  CHECK(false);
543  }
544  }
545  CHECK(false);
546  return nullptr;
547 }
CgenState * cgen_state_
bool is_fp() const
Definition: sqltypes.h:514
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
bool is_number() const
Definition: sqltypes.h:515
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:359
bool is_integer() const
Definition: sqltypes.h:512
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:215
std::string get_type_name() const
Definition: sqltypes.h:443
#define CHECK(condition)
Definition: Logger.h:223
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
bool is_decimal() const
Definition: sqltypes.h:513
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:102

+ 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 202 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().

206  {
208  if (!ti.is_string()) {
209  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
210  ti.get_type_name() + " not supported");
211  }
212  if (operand_ti.get_compression() == kENCODING_NONE &&
214  return operand_lv;
215  }
216  if (ti.get_compression() == kENCODING_DICT &&
217  operand_ti.get_compression() == kENCODING_DICT) {
218  if (ti.get_comp_param() == operand_ti.get_comp_param()) {
219  return operand_lv;
220  }
221  if (operand_ti.get_comp_param() == DictRef::literalsDictId) {
222  // Anything being casted from a literal dictionary is not materialized at this point
223  // Should already have been kicked to CPU if it was originally a GPU query
224 
226  const int64_t source_string_proxy_handle =
227  reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
228  operand_ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
229 
230  const int64_t dest_string_proxy_handle =
231  reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
232  ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
233 
234  auto source_string_proxy_handle_lv = cgen_state_->llInt(source_string_proxy_handle);
235  auto dest_string_proxy_handle_lv = cgen_state_->llInt(dest_string_proxy_handle);
236 
237  std::vector<llvm::Value*> string_cast_lvs{
238  operand_lv, source_string_proxy_handle_lv, dest_string_proxy_handle_lv};
239  if (ti.is_dict_intersection()) {
241  "intersect_translate_string_id_to_other_dict",
243  string_cast_lvs);
244  } else {
245  return cgen_state_->emitExternalCall("union_translate_string_id_to_other_dict",
247  string_cast_lvs);
248  }
249  }
250 
251  const std::vector<StringOps_Namespace::StringOpInfo> string_op_infos;
252  auto string_dictionary_translation_mgr =
253  std::make_unique<StringDictionaryTranslationMgr>(
254  operand_ti.get_comp_param(),
255  ti.get_comp_param(),
257  string_op_infos,
260  executor()->deviceCount(co.device_type),
261  executor(),
262  &executor()->getCatalog()->getDataMgr(),
263  false /* delay_translation */);
264 
265  return cgen_state_
266  ->moveStringDictionaryTranslationMgr(std::move(string_dictionary_translation_mgr))
267  ->codegen(operand_lv, operand_ti, true /* add_nullcheck */, co);
268  }
269  // dictionary encode non-constant
270  if (operand_ti.get_compression() != kENCODING_DICT && !operand_is_const) {
271  if (g_cluster) {
272  throw std::runtime_error(
273  "Cast from none-encoded string to dictionary-encoded not supported for "
274  "distributed queries");
275  }
276  CHECK_EQ(kENCODING_NONE, operand_ti.get_compression());
278  CHECK(operand_lv->getType()->isIntegerTy(64));
280  throw QueryMustRunOnCpu();
281  }
283  "string_compress",
285  {operand_lv,
286  cgen_state_->llInt(int64_t(executor()->getStringDictionaryProxy(
287  ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true)))});
288  }
289  CHECK(operand_lv->getType()->isIntegerTy(32));
290  if (ti.get_compression() == kENCODING_NONE) {
291  if (g_cluster) {
292  throw std::runtime_error(
293  "Cast from dictionary-encoded string to none-encoded not "
294  "currently supported for distributed queries.");
295  }
296  // Removed watchdog check here in exchange for row cardinality based check in
297  // RelAlgExecutor
298  CHECK_EQ(kENCODING_DICT, operand_ti.get_compression());
300  throw QueryMustRunOnCpu();
301  }
302  const int64_t string_dictionary_ptr =
303  operand_ti.get_comp_param() == 0
304  ? reinterpret_cast<int64_t>(
305  executor()->getRowSetMemoryOwner()->getLiteralStringDictProxy())
306  : reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
307  operand_ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
309  "string_decompress",
311  {operand_lv, cgen_state_->llInt(string_dictionary_ptr)});
312  }
313  CHECK(operand_is_const);
315  return operand_lv;
316 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
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:359
bool is_dict_intersection() const
Definition: sqltypes.h:565
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
std::string get_type_name() const
Definition: sqltypes.h:443
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:338
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
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:510
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 133 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().

135  {
137  CHECK(ts_lv->getType()->isIntegerTy(64));
138  if (dimen > 0) {
139  if (nullable) {
141  "DateTruncateHighPrecisionToDateNullable",
143  {{ts_lv,
146  }
148  "DateTruncateHighPrecisionToDate",
150  {{ts_lv,
152  }
153  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
154  if (nullable) {
155  nullcheck_codegen =
156  std::make_unique<NullCheckCodegen>(cgen_state_,
157  executor(),
158  ts_lv,
159  SQLTypeInfo(kTIMESTAMP, dimen, 0, !nullable),
160  "cast_timestamp_nullcheck");
161  }
162  auto ret = cgen_state_->emitExternalCall(
163  "datetrunc_day", get_int_type(64, cgen_state_->context_), {ts_lv});
164  if (nullcheck_codegen) {
165  ret = nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ret);
166  }
167  return ret;
168 }
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:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
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::codegenCastToFp ( llvm::Value *  operand_lv,
const SQLTypeInfo operand_ti,
const SQLTypeInfo ti 
)
private

Definition at line 449 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().

451  {
453  if (!ti.is_fp()) {
454  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
455  ti.get_type_name() + " not supported");
456  }
457  llvm::Value* result_lv;
458  if (operand_ti.get_notnull()) {
459  auto const fp_type = ti.get_type() == kFLOAT
460  ? llvm::Type::getFloatTy(cgen_state_->context_)
461  : llvm::Type::getDoubleTy(cgen_state_->context_);
462  result_lv = cgen_state_->ir_builder_.CreateSIToFP(operand_lv, fp_type);
463  if (auto const scale = static_cast<unsigned>(operand_ti.get_scale())) {
464  double const multiplier = shared::power10inv(scale);
465  result_lv = cgen_state_->ir_builder_.CreateFMul(
466  result_lv, llvm::ConstantFP::get(result_lv->getType(), multiplier));
467  }
468  } else {
469  if (auto const scale = static_cast<unsigned>(operand_ti.get_scale())) {
470  double const multiplier = shared::power10inv(scale);
471  auto const fp_type = ti.get_type() == kFLOAT
472  ? llvm::Type::getFloatTy(cgen_state_->context_)
473  : llvm::Type::getDoubleTy(cgen_state_->context_);
474  result_lv = cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
475  numeric_type_name(ti) + "_scaled_nullable",
476  {operand_lv,
477  cgen_state_->inlineIntNull(operand_ti),
479  llvm::ConstantFP::get(fp_type, multiplier)});
480  } else {
481  result_lv = cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
482  numeric_type_name(ti) + "_nullable",
483  {operand_lv,
484  cgen_state_->inlineIntNull(operand_ti),
485  cgen_state_->inlineFpNull(ti)});
486  }
487  }
488  CHECK(result_lv);
489  return result_lv;
490 }
CgenState * cgen_state_
bool is_fp() const
Definition: sqltypes.h:514
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
llvm::IRBuilder ir_builder_
Definition: CgenState.h:361
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
llvm::LLVMContext & context_
Definition: CgenState.h:359
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
#define AUTOMATIC_IR_METADATA(CGENSTATE)
double power10inv(unsigned const x)
Definition: misc.h:277
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
std::string get_type_name() const
Definition: sqltypes.h:443
#define CHECK(condition)
Definition: Logger.h:223
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:209
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:102

+ 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:231
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:69
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:450
std::shared_ptr< Analyzer::BinOper > lower_bw_eq(const Analyzer::BinOper *bw_eq)
Definition: CompareIR.cpp:125
SQLOps get_optype() const
Definition: Analyzer.h:446
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:32
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:230
#define CHECK(condition)
Definition: Logger.h:223
Definition: sqldefs.h:31
llvm::Value * codegenLogical(const Analyzer::BinOper *, const CompilationOptions &)
Definition: LogicalIR.cpp:298
const Expr * get_left_operand() const
Definition: Analyzer.h:449
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1427
const std::shared_ptr< Analyzer::Expr > get_own_right_operand() const
Definition: Analyzer.h:454
const std::shared_ptr< Analyzer::Expr > get_own_left_operand() const
Definition: Analyzer.h:451
SQLQualifier get_qualifier() const
Definition: Analyzer.h:448

+ 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 449 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.

454  {
456  CHECK(IS_COMPARISON(optype));
457  const auto& rhs_ti = rhs->get_type_info();
458  if (rhs_ti.is_array()) {
459  return codegenQualifierCmp(optype, qualifier, lhs_lvs, rhs, co);
460  }
461  auto rhs_lvs = codegen(rhs, true, co);
462  CHECK_EQ(kONE, qualifier);
463  if (optype == kOVERLAPS) {
464  CHECK(lhs_ti.is_geometry());
465  CHECK(rhs_ti.is_array() ||
466  rhs_ti.is_geometry()); // allow geo col or bounds col to pass
467  } else {
468  CHECK((lhs_ti.get_type() == rhs_ti.get_type()) ||
469  (lhs_ti.is_string() && rhs_ti.is_string()));
470  }
471  const auto null_check_suffix = get_null_check_suffix(lhs_ti, rhs_ti);
472  if (lhs_ti.is_integer() || lhs_ti.is_decimal() || lhs_ti.is_time() ||
473  lhs_ti.is_boolean() || lhs_ti.is_string() || lhs_ti.is_timeinterval()) {
474  if (lhs_ti.is_string()) {
475  CHECK(rhs_ti.is_string());
476  CHECK_EQ(lhs_ti.get_compression(), rhs_ti.get_compression());
477  if (lhs_ti.get_compression() == kENCODING_NONE) {
478  // unpack pointer + length if necessary
479  if (lhs_lvs.size() != 3) {
480  CHECK_EQ(size_t(1), lhs_lvs.size());
481  lhs_lvs.push_back(cgen_state_->emitCall("extract_str_ptr", {lhs_lvs.front()}));
482  lhs_lvs.push_back(cgen_state_->emitCall("extract_str_len", {lhs_lvs.front()}));
483  }
484  if (rhs_lvs.size() != 3) {
485  CHECK_EQ(size_t(1), rhs_lvs.size());
486  rhs_lvs.push_back(cgen_state_->emitCall("extract_str_ptr", {rhs_lvs.front()}));
487  rhs_lvs.push_back(cgen_state_->emitCall("extract_str_len", {rhs_lvs.front()}));
488  }
489  std::vector<llvm::Value*> str_cmp_args{
490  lhs_lvs[1], lhs_lvs[2], rhs_lvs[1], rhs_lvs[2]};
491  if (!null_check_suffix.empty()) {
492  str_cmp_args.push_back(
494  }
495  return cgen_state_->emitCall(
496  string_cmp_func(optype) + (null_check_suffix.empty() ? "" : "_nullable"),
497  str_cmp_args);
498  } else {
499  CHECK(optype == kEQ || optype == kNE);
500  }
501  }
502 
503  if (lhs_ti.is_boolean() && rhs_ti.is_boolean()) {
504  auto& lhs_lv = lhs_lvs.front();
505  auto& rhs_lv = rhs_lvs.front();
506  CHECK(lhs_lv->getType()->isIntegerTy());
507  CHECK(rhs_lv->getType()->isIntegerTy());
508  if (lhs_lv->getType()->getIntegerBitWidth() <
509  rhs_lv->getType()->getIntegerBitWidth()) {
510  lhs_lv =
511  cgen_state_->castToTypeIn(lhs_lv, rhs_lv->getType()->getIntegerBitWidth());
512  } else {
513  rhs_lv =
514  cgen_state_->castToTypeIn(rhs_lv, lhs_lv->getType()->getIntegerBitWidth());
515  }
516  }
517 
518  return null_check_suffix.empty()
519  ? cgen_state_->ir_builder_.CreateICmp(
520  llvm_icmp_pred(optype), lhs_lvs.front(), rhs_lvs.front())
522  icmp_name(optype) + "_" + numeric_type_name(lhs_ti) +
523  null_check_suffix,
524  {lhs_lvs.front(),
525  rhs_lvs.front(),
528  }
529  if (lhs_ti.get_type() == kFLOAT || lhs_ti.get_type() == kDOUBLE) {
530  return null_check_suffix.empty()
531  ? cgen_state_->ir_builder_.CreateFCmp(
532  llvm_fcmp_pred(optype), lhs_lvs.front(), rhs_lvs.front())
534  icmp_name(optype) + "_" + numeric_type_name(lhs_ti) +
535  null_check_suffix,
536  {lhs_lvs.front(),
537  rhs_lvs.front(),
538  lhs_ti.get_type() == kFLOAT ? cgen_state_->llFp(NULL_FLOAT)
541  }
542  CHECK(false);
543  return nullptr;
544 }
#define CHECK_EQ(x, y)
Definition: Logger.h:231
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:148
#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:361
Definition: sqldefs.h:30
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
llvm::CmpInst::Predicate llvm_fcmp_pred(const SQLOps op_type)
Definition: CompareIR.cpp:83
bool is_time() const
Definition: sqltypes.h:516
bool is_integer() const
Definition: sqltypes.h:512
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:64
bool is_timeinterval() const
Definition: sqltypes.h:521
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:300
bool is_boolean() const
Definition: sqltypes.h:517
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::string get_null_check_suffix(const SQLTypeInfo &lhs_ti, const SQLTypeInfo &rhs_ti)
Definition: Execute.h:1411
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:215
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:71
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
llvm::Value * codegenQualifierCmp(const SQLOps, const SQLQualifier, std::vector< llvm::Value * >, const Analyzer::Expr *, const CompilationOptions &)
Definition: CompareIR.cpp:546
Definition: sqldefs.h:32
llvm::CmpInst::Predicate llvm_icmp_pred(const SQLOps op_type)
Definition: CompareIR.cpp:26
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:296
#define CHECK(condition)
Definition: Logger.h:223
bool is_geometry() const
Definition: sqltypes.h:522
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:510
bool is_decimal() const
Definition: sqltypes.h:513
std::string string_cmp_func(const SQLOps optype)
Definition: CompareIR.cpp:106
#define IS_COMPARISON(X)
Definition: sqldefs.h:58

+ 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, and kDECIMAL.

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  }
435  SQLTypeInfo new_ti = SQLTypeInfo(
436  kDECIMAL, 19, lhs_ti.get_scale() - scale_diff, operand_ti.get_notnull());
437  if (negative) {
438  truncated_decimal = -truncated_decimal;
439  }
440  Datum d;
441  d.bigintval = truncated_decimal;
442  const auto new_rhs_lit =
443  makeExpr<Analyzer::Constant>(new_ti, rhs_constant->get_is_null(), d);
444  const auto operand_lv = codegen(operand, true, co).front();
445  const auto lhs_lv = codegenCast(operand_lv, operand_ti, new_ti, false, co);
446  return codegenCmp(optype, qualifier, {lhs_lv}, new_ti, new_rhs_lit.get(), co);
447 }
CgenState * cgen_state_
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
Definition: sqldefs.h:49
int64_t bigintval
Definition: sqltypes.h:215
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:81
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
llvm::Value * codegenCmp(const Analyzer::BinOper *, const CompilationOptions &)
Definition: CompareIR.cpp:230
uint64_t exp_to_scale(const unsigned exp)
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:21

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

std::vector< llvm::Value * > CodeGenerator::codegenColumn ( const Analyzer::ColumnVar col_var,
const bool  fetch_column,
const CompilationOptions co 
)
privatevirtual

Reimplemented in ScalarCodeGenerator.

Definition at line 92 of file ColumnIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, codegenColVar(), codegenOuterJoinNullPlaceholder(), foundOuterJoinMatch(), Analyzer::ColumnVar::get_rte_idx(), and CgenState::outer_join_match_found_per_level_.

Referenced by codegen().

94  {
96  if (col_var->get_rte_idx() <= 0 ||
98  !foundOuterJoinMatch(col_var->get_rte_idx())) {
99  return codegenColVar(col_var, fetch_column, true, co);
100  }
101  return codegenOuterJoinNullPlaceholder(col_var, fetch_column, co);
102 }
std::vector< llvm::Value * > outer_join_match_found_per_level_
Definition: CgenState.h:372
CgenState * cgen_state_
std::vector< llvm::Value * > codegenOuterJoinNullPlaceholder(const Analyzer::ColumnVar *col_var, const bool fetch_column, const CompilationOptions &co)
Definition: ColumnIR.cpp:425
llvm::Value * foundOuterJoinMatch(const size_t nesting_level) const
Definition: ColumnIR.cpp:418
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::vector< llvm::Value * > codegenColVar(const Analyzer::ColumnVar *, const bool fetch_column, const bool update_query_plan, const CompilationOptions &)
Definition: ColumnIR.cpp:104
int get_rte_idx() const
Definition: Analyzer.h:202

+ Here is the call graph for this function:

+ Here is the caller graph for this function: