OmniSciDB  d2f719934e
 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 &)
 
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)
 

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 std::shared_ptr
< GpuCompilationContext
generateNativeGPUCode (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::LowerExpr *, 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 * codegenCastBetweenTimestamps (llvm::Value *ts_lv, const SQLTypeInfo &operand_dimen, const SQLTypeInfo &target_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 1404 of file NativeCodegen.cpp.

1404  {
1405  return func->getName() == "query_stub_hoisted_literals" ||
1406  func->getName() == "multifrag_query_hoisted_literals" ||
1407  func->getName() == "query_stub" || func->getName() == "multifrag_query" ||
1408  func->getName() == "fixed_width_int_decode" ||
1409  func->getName() == "fixed_width_unsigned_decode" ||
1410  func->getName() == "diff_fixed_width_int_decode" ||
1411  func->getName() == "fixed_width_double_decode" ||
1412  func->getName() == "fixed_width_float_decode" ||
1413  func->getName() == "fixed_width_small_date_decode" ||
1414  func->getName() == "record_error_code" || func->getName() == "get_error_code" ||
1415  func->getName() == "pos_start_impl" || func->getName() == "pos_step_impl" ||
1416  func->getName() == "group_buff_idx_impl" ||
1417  func->getName() == "init_shared_mem" ||
1418  func->getName() == "init_shared_mem_nop" || func->getName() == "write_back_nop";
1419 }
std::tuple< CodeGenerator::ArgNullcheckBBs, llvm::Value * > CodeGenerator::beginArgsNullcheck ( const Analyzer::FunctionOper function_oper,
const std::vector< llvm::Value * > &  orig_arg_lvs 
)
private

Definition at line 400 of file ExtensionsIR.cpp.

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

Referenced by codegenFunctionOper(), and codegenFunctionOperWithCustomTypeHandling().

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

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

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

+ 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:77
PlanState * plan_state_
#define CHECK(condition)
Definition: Logger.h:211
bool is_decimal() const
Definition: sqltypes.h:522
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:442
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:77
PlanState * plan_state_
#define CHECK(condition)
Definition: Logger.h:211
const Expr * get_left_operand() const
Definition: Analyzer.h:441
bool is_decimal() const
Definition: sqltypes.h:522
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(), codegenColVar(), codegenConstantWidthBucketExpr(), codegenDeciDiv(), codegenDictLike(), codegenDictRegexp(), codegenDictStrCmp(), codegenFunctionOper(), codegenFunctionOperWithCustomTypeHandling(), codegenGeoArgs(), codegenGeoColumnVar(), codegenGeoConstant(), codegenGeoOperator(), codegenIsNull(), RangeJoinHashTable::codegenKey(), BaselineJoinHashTable::codegenKey(), OverlapsJoinHashTable::codegenKey(), codegenLogical(), codegenLogicalShortCircuit(), OverlapsJoinHashTable::codegenManyKey(), codegenOuterJoinNullPlaceholder(), GroupByAndAggregate::codegenOutputSlot(), codegenQualifierCmp(), codegenRowId(), PerfectJoinHashTable::codegenSlot(), 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 lower_expr = dynamic_cast<const Analyzer::LowerExpr*>(expr);
107  if (lower_expr) {
108  return {codegen(lower_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:512
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:220
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:77
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Expression class for the LOWER (lowercase) string function. The &quot;arg&quot; constructor parameter must be a...
Definition: Analyzer.h:837
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 &)
ALWAYS_INLINE DEVICE double 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:209
int8_t boolval
Definition: sqltypes.h:211
bool get_is_null() const
Definition: Analyzer.h:333
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
float floatval
Definition: sqltypes.h:216
llvm::LLVMContext & context_
Definition: CgenState.h:338
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const int dict_id)
Definition: ConstantIR.cpp:285
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
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:334
Definition: sqltypes.h:41
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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:219
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
Definition: sqldefs.h:30
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
const InValuesBitmap * addInValuesBitmap(std::unique_ptr< InValuesBitmap > &in_values_bitmap)
Definition: CgenState.h:218
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:69
llvm::Value * codegen(llvm::Value *needle, Executor *executor) const
const std::list< std::shared_ptr< Analyzer::Expr > > & get_value_list() const
Definition: Analyzer.h:632
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:307
#define CHECK(condition)
Definition: Logger.h:211
std::unique_ptr< InValuesBitmap > createInValuesBitmap(const Analyzer::InValues *, const CompilationOptions &)
Definition: InValuesIR.cpp:111
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1223
const Expr * get_arg() const
Definition: Analyzer.h:630
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:219
CgenState * cgen_state_
llvm::LLVMContext & context_
Definition: CgenState.h:338
const InValuesBitmap * addInValuesBitmap(std::unique_ptr< InValuesBitmap > &in_values_bitmap)
Definition: CgenState.h:218
#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:307
#define CHECK(condition)
Definition: Logger.h:211
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1223
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:219
const Expr * get_else_expr() const
Definition: Analyzer.h:1307
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:338
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:211

+ 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:1352
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:1351
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:228
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
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:338
llvm::Function * current_func_
Definition: CgenState.h:332
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:307
#define CHECK(condition)
Definition: Logger.h:211
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:228
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
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:1394
DateaddField get_field() const
Definition: Analyzer.h:1392
constexpr bool is_subsecond_dateadd_field(const DateaddField field)
Definition: DateTimeUtils.h:99
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
const Expr * get_number_expr() const
Definition: Analyzer.h:1393

+ 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:228
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const Expr * get_start_expr() const
Definition: Analyzer.h:1435
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
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:307
const Expr * get_end_expr() const
Definition: Analyzer.h:1436
#define CHECK(condition)
Definition: Logger.h:211
DatetruncField get_field() const
Definition: Analyzer.h:1434

+ 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:228
llvm::ConstantInt * ll_int(const T v, llvm::LLVMContext &context)
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
DatetruncField get_field() const
Definition: Analyzer.h:1476
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:338
DatetruncField
Definition: DateTruncate.h:27
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
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:1477
#define CHECK(condition)
Definition: Logger.h:211
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 67 of file StringOpsIR.cpp.

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

68  {
70  auto str_lv = codegen(expr->get_arg(), true, co);
71  if (str_lv.size() != 3) {
72  CHECK_EQ(size_t(1), str_lv.size());
73  if (g_enable_watchdog) {
74  throw WatchdogException(
75  "LENGTH / CHAR_LENGTH on dictionary-encoded strings would be slow");
76  }
77  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
78  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
80  throw QueryMustRunOnCpu();
81  }
82  }
83  std::vector<llvm::Value*> charlength_args{str_lv[1], str_lv[2]};
84  std::string fn_name("char_length");
85  if (expr->get_calc_encoded_length()) {
86  fn_name += "_encoded";
87  }
88  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
89  if (is_nullable) {
90  fn_name += "_nullable";
91  charlength_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
92  }
93  return expr->get_calc_encoded_length()
95  fn_name, get_int_type(32, cgen_state_->context_), charlength_args)
96  : cgen_state_->emitCall(fn_name, charlength_args);
97 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
bool g_enable_watchdog
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:228
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
bool get_calc_encoded_length() const
Definition: Analyzer.h:704
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:702
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 99 of file StringOpsIR.cpp.

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

100  {
102  auto str_lv = codegen(expr->get_arg(), true, co);
103  CHECK_EQ(size_t(1), str_lv.size());
104  return cgen_state_->emitCall("key_for_string_encoded", str_lv);
105 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
CgenState * cgen_state_
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:751

+ 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:219
CgenState * cgen_state_
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:512
#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:796
#define CHECK(condition)
Definition: Logger.h:211
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(), CgenState::context_, 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, CgenState::ir_builder_, 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  llvm::Value* computed_bucket_lv{nullptr};
257  auto is_constant_expr = [](const Analyzer::Expr* expr) {
258  auto target_expr = expr;
259  if (auto cast_expr = dynamic_cast<const Analyzer::UOper*>(expr)) {
260  if (cast_expr->get_optype() == SQLOps::kCAST) {
261  target_expr = cast_expr->get_operand();
262  }
263  }
264  // there are more complex constant expr like 1+2, 1/2*3, and so on
265  // but when considering a typical usage of width_bucket function
266  // it is sufficient to consider a singleton constant expr
267  auto constant_expr = dynamic_cast<const Analyzer::Constant*>(target_expr);
268  if (constant_expr) {
269  return true;
270  }
271  return false;
272  };
273  if (is_constant_expr(lower_bound_expr) && is_constant_expr(upper_bound_expr) &&
274  is_constant_expr(partition_count_expr)) {
275  expr->set_constant_expr();
276  }
277  // compute width_bucket's expresn range and check the possibility of avoiding oob check
278  auto col_range =
279  getExpressionRange(expr,
281  executor_,
282  boost::make_optional(plan_state_->getSimpleQuals()));
283  // check whether target_expr is valid
284  if (col_range.getType() == ExpressionRangeType::Integer &&
285  !expr->can_skip_out_of_bound_check() && col_range.getIntMin() > 0 &&
286  col_range.getIntMax() <= expr->get_partition_count_val()) {
287  // check whether target_col is not-nullable or has filter expr on it
288  if (!col_range.hasNulls()) {
289  // Even if the target_expr has its filter expression, target_col_range may exactly
290  // the same with the col_range of the target_expr's operand col,
291  // i.e., SELECT WIDTH_BUCKET(v1, 1, 10, 10) FROM T WHERE v1 != 1;
292  // In that query, col_range of v1 with/without considering the filter expression
293  // v1 != 1 have exactly the same col ranges, so we cannot recognize the existence
294  // of the filter expression based on them. Also, is (not) null is located in
295  // FilterNode, so we cannot trace it in here.
296  // todo (yoonmin): relax this to allow skipping oob check more cases
297  expr->skip_out_of_bound_check();
298  }
299  }
300  if (expr->is_constant_expr()) {
301  computed_bucket_lv = codegenConstantWidthBucketExpr(expr, co);
302  } else {
303  computed_bucket_lv = codegenWidthBucketExpr(expr, co);
304  }
305  CHECK(computed_bucket_lv);
306  // return the largest integer equal to or less than the computed bucket number
307  // truncate double type computed bucket number
308  // the reason of casting it to float is the restriction of fptrunc func
309  // fptrunc value to ty2 --> The size of value must be larger than the size of ty2
310  auto truncated = cgen_state_->ir_builder_.CreateFPTrunc(
311  computed_bucket_lv, llvm::Type::getFloatTy(cgen_state_->context_), "truncated");
312  // cast 4-byte fp type to int32_t type
313  return cgen_state_->ir_builder_.CreateFPToSI(
314  truncated, llvm::Type::getInt32Ty(cgen_state_->context_), "bucket_number");
315 }
const Expr * get_partition_count() const
Definition: Analyzer.h:1091
bool is_constant_expr() const
Definition: Analyzer.h:1166
llvm::Value * codegenConstantWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:317
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
Definition: sqldefs.h:49
void set_constant_expr() const
Definition: Analyzer.h:1165
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::Value * codegenWidthBucketExpr(const Analyzer::WidthBucketExpr *, const CompilationOptions &)
Definition: IRCodegen.cpp:398
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:3430
std::list< std::shared_ptr< Analyzer::Expr > > getSimpleQuals() const
Definition: PlanState.h:99
const Expr * get_target_value() const
Definition: Analyzer.h:1088
#define CHECK(condition)
Definition: Logger.h:211
bool can_skip_out_of_bound_check() const
Definition: Analyzer.h:1163
void skip_out_of_bound_check() const
Definition: Analyzer.h:1164
const Expr * get_lower_bound() const
Definition: Analyzer.h:1089
const Expr * get_upper_bound() const
Definition: Analyzer.h:1090

+ Here is the call graph for this function:

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

Definition at line 107 of file StringOpsIR.cpp.

References run_benchmark_import::args, AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, codegen(), CgenState::context_, CompilationOptions::device_type, CgenState::emitExternalCall(), executor(), Analyzer::LowerExpr::get_arg(), SQLTypeInfo::get_comp_param(), get_int_type(), Analyzer::Expr::get_type_info(), GPU, and CgenState::llInt().

108  {
111  throw QueryMustRunOnCpu();
112  }
113 
114  auto str_id_lv = codegen(expr->get_arg(), true, co);
115  CHECK_EQ(size_t(1), str_id_lv.size());
116 
117  const auto string_dictionary_proxy = executor()->getStringDictionaryProxy(
118  expr->get_type_info().get_comp_param(), executor()->getRowSetMemoryOwner(), true);
119  CHECK(string_dictionary_proxy);
120 
121  std::vector<llvm::Value*> args{
122  str_id_lv[0],
123  cgen_state_->llInt(reinterpret_cast<int64_t>(string_dictionary_proxy))};
124 
126  "lower_encoded", get_int_type(32, cgen_state_->context_), args);
127 }
#define CHECK_EQ(x, y)
Definition: Logger.h: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:228
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
ExecutorDeviceType device_type
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:338
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
const Expr * get_arg() const
Definition: Analyzer.h:841
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 129 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.

130  {
132  if (is_unnest(extract_cast_arg(expr->get_arg()))) {
133  throw std::runtime_error("LIKE not supported for unnested expressions");
134  }
135  char escape_char{'\\'};
136  if (expr->get_escape_expr()) {
137  auto escape_char_expr =
138  dynamic_cast<const Analyzer::Constant*>(expr->get_escape_expr());
139  CHECK(escape_char_expr);
140  CHECK(escape_char_expr->get_type_info().is_string());
141  CHECK_EQ(size_t(1), escape_char_expr->get_constval().stringval->size());
142  escape_char = (*escape_char_expr->get_constval().stringval)[0];
143  }
144  auto pattern = dynamic_cast<const Analyzer::Constant*>(expr->get_like_expr());
145  CHECK(pattern);
146  auto fast_dict_like_lv = codegenDictLike(expr->get_own_arg(),
147  pattern,
148  expr->get_is_ilike(),
149  expr->get_is_simple(),
150  escape_char,
151  co);
152  if (fast_dict_like_lv) {
153  return fast_dict_like_lv;
154  }
155  const auto& ti = expr->get_arg()->get_type_info();
156  CHECK(ti.is_string());
157  if (g_enable_watchdog && ti.get_compression() != kENCODING_NONE) {
158  throw WatchdogException(
159  "Cannot do LIKE / ILIKE on this dictionary encoded column, its cardinality is "
160  "too high");
161  }
162  auto str_lv = codegen(expr->get_arg(), true, co);
163  if (str_lv.size() != 3) {
164  CHECK_EQ(size_t(1), str_lv.size());
165  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
166  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
168  throw QueryMustRunOnCpu();
169  }
170  }
171  auto like_expr_arg_lvs = codegen(expr->get_like_expr(), true, co);
172  CHECK_EQ(size_t(3), like_expr_arg_lvs.size());
173  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
174  std::vector<llvm::Value*> str_like_args{
175  str_lv[1], str_lv[2], like_expr_arg_lvs[1], like_expr_arg_lvs[2]};
176  std::string fn_name{expr->get_is_ilike() ? "string_ilike" : "string_like"};
177  if (expr->get_is_simple()) {
178  fn_name += "_simple";
179  } else {
180  str_like_args.push_back(cgen_state_->llInt(int8_t(escape_char)));
181  }
182  if (is_nullable) {
183  fn_name += "_nullable";
184  str_like_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
185  }
186  return cgen_state_->emitCall(fn_name, str_like_args);
187 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
bool g_enable_watchdog
const Expr * get_escape_expr() const
Definition: Analyzer.h:954
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:951
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
bool get_is_simple() const
Definition: Analyzer.h:956
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:953
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:952
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1223
HOST DEVICE bool get_notnull() const
Definition: sqltypes.h:336
bool get_is_ilike() const
Definition: Analyzer.h:955
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 346 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.

347  {
349  if (is_unnest(extract_cast_arg(expr->get_arg()))) {
350  throw std::runtime_error("REGEXP not supported for unnested expressions");
351  }
352  char escape_char{'\\'};
353  if (expr->get_escape_expr()) {
354  auto escape_char_expr =
355  dynamic_cast<const Analyzer::Constant*>(expr->get_escape_expr());
356  CHECK(escape_char_expr);
357  CHECK(escape_char_expr->get_type_info().is_string());
358  CHECK_EQ(size_t(1), escape_char_expr->get_constval().stringval->size());
359  escape_char = (*escape_char_expr->get_constval().stringval)[0];
360  }
361  auto pattern = dynamic_cast<const Analyzer::Constant*>(expr->get_pattern_expr());
362  CHECK(pattern);
363  auto fast_dict_pattern_lv =
364  codegenDictRegexp(expr->get_own_arg(), pattern, escape_char, co);
365  if (fast_dict_pattern_lv) {
366  return fast_dict_pattern_lv;
367  }
368  const auto& ti = expr->get_arg()->get_type_info();
369  CHECK(ti.is_string());
370  if (g_enable_watchdog && ti.get_compression() != kENCODING_NONE) {
371  throw WatchdogException(
372  "Cannot do REGEXP_LIKE on this dictionary encoded column, its cardinality is too "
373  "high");
374  }
375  // Now we know we are working on NONE ENCODED column. So switch back to CPU
377  throw QueryMustRunOnCpu();
378  }
379  auto str_lv = codegen(expr->get_arg(), true, co);
380  if (str_lv.size() != 3) {
381  CHECK_EQ(size_t(1), str_lv.size());
382  str_lv.push_back(cgen_state_->emitCall("extract_str_ptr", {str_lv.front()}));
383  str_lv.push_back(cgen_state_->emitCall("extract_str_len", {str_lv.front()}));
384  }
385  auto regexp_expr_arg_lvs = codegen(expr->get_pattern_expr(), true, co);
386  CHECK_EQ(size_t(3), regexp_expr_arg_lvs.size());
387  const bool is_nullable{!expr->get_arg()->get_type_info().get_notnull()};
388  std::vector<llvm::Value*> regexp_args{
389  str_lv[1], str_lv[2], regexp_expr_arg_lvs[1], regexp_expr_arg_lvs[2]};
390  std::string fn_name("regexp_like");
391  regexp_args.push_back(cgen_state_->llInt(int8_t(escape_char)));
392  if (is_nullable) {
393  fn_name += "_nullable";
394  regexp_args.push_back(cgen_state_->inlineIntNull(expr->get_type_info()));
396  fn_name, get_int_type(8, cgen_state_->context_), regexp_args);
397  }
399  fn_name, get_int_type(1, cgen_state_->context_), regexp_args);
400 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
const std::shared_ptr< Analyzer::Expr > get_own_arg() const
Definition: Analyzer.h:1024
bool g_enable_watchdog
CgenState * cgen_state_
const Expr * get_escape_expr() const
Definition: Analyzer.h:1026
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:228
const Expr * get_arg() const
Definition: Analyzer.h:1023
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:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
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:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:1025
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1223
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:228
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:512
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
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:307
#define CHECK(condition)
Definition: Logger.h:211
uint32_t log2_bytes(const uint32_t bytes)
Definition: Execute.h:176
const Expr * get_arg() const
Definition: Analyzer.h:897

+ Here is the call graph for this function:

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

Definition at line 173 of file IRCodegen.cpp.

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

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

+ Here is the call graph for this function:

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

Definition at line 192 of file IRCodegen.cpp.

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

193  {
195  const auto optype = u_oper->get_optype();
196  switch (optype) {
197  case kNOT: {
198  return codegenLogical(u_oper, co);
199  }
200  case kCAST: {
201  return codegenCast(u_oper, co);
202  }
203  case kUMINUS: {
204  return codegenUMinus(u_oper, co);
205  }
206  case kISNULL: {
207  return codegenIsNull(u_oper, co);
208  }
209  case kUNNEST:
210  return codegenUnnest(u_oper, co);
211  default:
212  UNREACHABLE();
213  }
214  return nullptr;
215 }
CgenState * cgen_state_
#define UNREACHABLE()
Definition: Logger.h:255
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:20
Definition: sqldefs.h:39
SQLOps get_optype() const
Definition: Analyzer.h:369

+ 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:219
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::Function * current_func_
Definition: CgenState.h:332
bool is_integer() const
Definition: sqltypes.h:521
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:1163
bool is_timeinterval() const
Definition: sqltypes.h:530
bool needs_error_check_
Definition: CgenState.h:358
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
ExecutorDeviceType device_type
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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:522
std::pair< llvm::ConstantInt *, llvm::ConstantInt * > inlineIntMaxMin(const size_t byte_width, const bool is_signed)
Definition: CgenState.cpp:84

+ 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:219
CgenState * cgen_state_
const Expr * get_right_operand() const
Definition: Analyzer.h:442
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:438
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:256
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define IS_ARITHMETIC(X)
Definition: sqldefs.h:61
#define CHECK(condition)
Definition: Logger.h:211
const Expr * get_left_operand() const
Definition: Analyzer.h:441

+ 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: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:228
const Expr * get_right_operand() const
Definition: Analyzer.h:442
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
llvm::Value * posArg(const Analyzer::Expr *) const
Definition: ColumnIR.cpp:512
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::string to_string(char const *&&v)
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
#define CHECK(condition)
Definition: Logger.h:211
const Expr * get_left_operand() const
Definition: Analyzer.h:441
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:67

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 654 of file ExtensionsIR.cpp.

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

Referenced by codegenFunctionOperCastArgs().

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

+ 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, i, 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:228
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
#define UNREACHABLE()
Definition: Logger.h:255
bool isNull() const
Definition: Analyzer.h:1650
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:311
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
size_t getElementCount() const
Definition: Analyzer.h:1648
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:1649
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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:1652
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:340
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::Function * current_func_
Definition: CgenState.h:332
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:1163
bool needs_error_check_
Definition: CgenState.h:358
#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:307
int64_t inline_int_null_val(const SQL_TYPE_INFO &ti)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

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

Definition at line 682 of file ExtensionsIR.cpp.

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

Referenced by codegenFunctionOperCastArgs().

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

+ 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:219
const Expr * get_else_expr() const
Definition: Analyzer.h:1307
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::Function * current_func_
Definition: CgenState.h:332
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:211
const std::list< std::pair< std::shared_ptr< Analyzer::Expr >, std::shared_ptr< Analyzer::Expr > > > & get_expr_pair_list() const
Definition: Analyzer.h:1304

+ 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 20 of file CastIR.cpp.

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

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

21  {
23  CHECK_EQ(uoper->get_optype(), kCAST);
24  const auto& ti = uoper->get_type_info();
25  const auto operand = uoper->get_operand();
26  const auto operand_as_const = dynamic_cast<const Analyzer::Constant*>(operand);
27  // For dictionary encoded constants, the cast holds the dictionary id
28  // information as the compression parameter; handle this case separately.
29  llvm::Value* operand_lv{nullptr};
30  if (operand_as_const) {
31  const auto operand_lvs =
32  codegen(operand_as_const, ti.get_compression(), ti.get_comp_param(), co);
33  if (operand_lvs.size() == 3) {
34  operand_lv = cgen_state_->emitCall("string_pack", {operand_lvs[1], operand_lvs[2]});
35  } else {
36  operand_lv = operand_lvs.front();
37  }
38  } else {
39  operand_lv = codegen(operand, true, co).front();
40  }
41  const auto& operand_ti = operand->get_type_info();
42  return codegenCast(operand_lv, operand_ti, ti, operand_as_const, co);
43 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
CgenState * cgen_state_
Definition: sqldefs.h:49
#define AUTOMATIC_IR_METADATA(CGENSTATE)
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
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:370
llvm::Value * codegenCast(const Analyzer::UOper *, const CompilationOptions &)
Definition: CastIR.cpp:20
SQLOps get_optype() const
Definition: Analyzer.h:369

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

58  {
60  if (byte_array_cast(operand_ti, ti)) {
61  auto* byte_array_type = get_int_array_type(8, ti.get_size(), cgen_state_->context_);
62  return cgen_state_->ir_builder_.CreatePointerCast(operand_lv,
63  byte_array_type->getPointerTo());
64  }
65  if (operand_lv->getType()->isIntegerTy()) {
66  if (operand_ti.is_string()) {
67  return codegenCastFromString(operand_lv, operand_ti, ti, operand_is_const, co);
68  }
69  CHECK(operand_ti.is_integer() || operand_ti.is_decimal() || operand_ti.is_time() ||
70  operand_ti.is_boolean());
71  if (operand_ti.is_boolean()) {
72  // cast boolean to int8
73  CHECK(operand_lv->getType()->isIntegerTy(1) ||
74  operand_lv->getType()->isIntegerTy(8));
75  if (operand_lv->getType()->isIntegerTy(1)) {
76  operand_lv = cgen_state_->castToTypeIn(operand_lv, 8);
77  }
78  if (ti.is_boolean()) {
79  return operand_lv;
80  }
81  }
82  if (operand_ti.is_integer() && operand_lv->getType()->isIntegerTy(8) &&
83  ti.is_boolean()) {
84  // cast int8 to boolean
85  return codegenCastBetweenIntTypes(operand_lv, operand_ti, ti);
86  }
87  if (operand_ti.get_type() == kTIMESTAMP && ti.get_type() == kDATE) {
88  // Maybe we should instead generate DatetruncExpr directly from RelAlgTranslator
89  // for this pattern. However, DatetruncExpr is supposed to return a timestamp,
90  // whereas this cast returns a date. The underlying type for both is still the same,
91  // but it still doesn't look like a good idea to misuse DatetruncExpr.
92  // Date will have default precision of day, but TIMESTAMP dimension would
93  // matter but while converting date through seconds
95  operand_lv, operand_ti.get_dimension(), !ti.get_notnull());
96  }
97  if ((operand_ti.get_type() == kTIMESTAMP || operand_ti.get_type() == kDATE) &&
98  ti.get_type() == kTIMESTAMP) {
99  const auto operand_dimen =
100  (operand_ti.is_timestamp()) ? operand_ti.get_dimension() : 0;
101  if (operand_dimen != ti.get_dimension()) {
103  operand_lv, operand_ti, ti, !ti.get_notnull());
104  }
105  }
106  if (ti.is_integer() || ti.is_decimal() || ti.is_time()) {
107  return codegenCastBetweenIntTypes(operand_lv, operand_ti, ti);
108  } else {
109  return codegenCastToFp(operand_lv, operand_ti, ti);
110  }
111  } else {
112  return codegenCastFromFp(operand_lv, operand_ti, ti);
113  }
114  CHECK(false);
115  return nullptr;
116 }
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:113
llvm::Value * codegenCastFromFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:430
HOST DEVICE int get_size() const
Definition: sqltypes.h:339
bool is_timestamp() const
Definition: sqltypes.h:891
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
llvm::Value * codegenCastToFp(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:387
bool byte_array_cast(const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti)
Definition: CastIR.cpp:47
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
bool is_time() const
Definition: sqltypes.h:525
llvm::LLVMContext & context_
Definition: CgenState.h:338
bool is_integer() const
Definition: sqltypes.h:521
llvm::Value * codegenCastBetweenIntTypes(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, bool upscale=true)
Definition: CastIR.cpp:256
bool is_boolean() const
Definition: sqltypes.h:526
llvm::Value * codegenCastTimestampToDate(llvm::Value *ts_lv, const int dimen, const bool nullable)
Definition: CastIR.cpp:118
#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:155
#define CHECK(condition)
Definition: Logger.h:211
llvm::Value * codegenCastFromString(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const bool operand_is_const, const CompilationOptions &co)
Definition: CastIR.cpp:187
bool is_string() const
Definition: sqltypes.h:519
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:522

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

259  {
261  if (ti.is_decimal() &&
262  (!operand_ti.is_decimal() || operand_ti.get_scale() <= ti.get_scale())) {
263  if (upscale) {
264  if (operand_ti.get_scale() < ti.get_scale()) { // scale only if needed
265  auto scale = exp_to_scale(ti.get_scale() - operand_ti.get_scale());
266  const auto scale_lv =
267  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), scale);
268  operand_lv = cgen_state_->ir_builder_.CreateSExt(
269  operand_lv, get_int_type(64, cgen_state_->context_));
270 
271  codegenCastBetweenIntTypesOverflowChecks(operand_lv, operand_ti, ti, scale);
272 
273  if (operand_ti.get_notnull()) {
274  operand_lv = cgen_state_->ir_builder_.CreateMul(operand_lv, scale_lv);
275  } else {
276  operand_lv = cgen_state_->emitCall(
277  "scale_decimal_up",
278  {operand_lv,
279  scale_lv,
280  cgen_state_->llInt(inline_int_null_val(operand_ti)),
282  }
283  }
284  }
285  } else if (operand_ti.is_decimal()) {
286  // rounded scale down
287  auto scale = (int64_t)exp_to_scale(operand_ti.get_scale() - ti.get_scale());
288  const auto scale_lv =
289  llvm::ConstantInt::get(get_int_type(64, cgen_state_->context_), scale);
290 
291  const auto operand_width =
292  static_cast<llvm::IntegerType*>(operand_lv->getType())->getBitWidth();
293 
294  std::string method_name = "scale_decimal_down_nullable";
295  if (operand_ti.get_notnull()) {
296  method_name = "scale_decimal_down_not_nullable";
297  }
298 
299  CHECK(operand_width == 64);
300  operand_lv = cgen_state_->emitCall(
301  method_name,
302  {operand_lv, scale_lv, cgen_state_->llInt(inline_int_null_val(operand_ti))});
303  }
304  if (ti.is_integer() && operand_ti.is_integer() &&
305  operand_ti.get_logical_size() > ti.get_logical_size()) {
306  codegenCastBetweenIntTypesOverflowChecks(operand_lv, operand_ti, ti, 1);
307  }
308 
309  const auto operand_width =
310  static_cast<llvm::IntegerType*>(operand_lv->getType())->getBitWidth();
311  const auto target_width = get_bit_width(ti);
312  if (target_width == operand_width) {
313  return operand_lv;
314  }
315  if (operand_ti.get_notnull()) {
316  return cgen_state_->ir_builder_.CreateCast(
317  target_width > operand_width ? llvm::Instruction::CastOps::SExt
318  : llvm::Instruction::CastOps::Trunc,
319  operand_lv,
320  get_int_type(target_width, cgen_state_->context_));
321  }
322  return cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
323  numeric_type_name(ti) + "_nullable",
324  {operand_lv,
325  cgen_state_->inlineIntNull(operand_ti),
326  cgen_state_->inlineIntNull(ti)});
327 }
CgenState * cgen_state_
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
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:338
int get_logical_size() const
Definition: sqltypes.h:349
bool is_integer() const
Definition: sqltypes.h:521
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:329
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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:522

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

333  {
335  llvm::Value* chosen_max{nullptr};
336  llvm::Value* chosen_min{nullptr};
337  std::tie(chosen_max, chosen_min) =
339 
341  auto cast_ok = llvm::BasicBlock::Create(
343  auto cast_fail = llvm::BasicBlock::Create(
345  auto operand_max = static_cast<llvm::ConstantInt*>(chosen_max)->getSExtValue() / scale;
346  auto operand_min = static_cast<llvm::ConstantInt*>(chosen_min)->getSExtValue() / scale;
347  const auto ti_llvm_type =
349  llvm::Value* operand_max_lv = llvm::ConstantInt::get(ti_llvm_type, operand_max);
350  llvm::Value* operand_min_lv = llvm::ConstantInt::get(ti_llvm_type, operand_min);
351  const bool is_narrowing = operand_ti.get_logical_size() > ti.get_logical_size();
352  if (is_narrowing) {
353  const auto operand_ti_llvm_type =
355  operand_max_lv =
356  cgen_state_->ir_builder_.CreateSExt(operand_max_lv, operand_ti_llvm_type);
357  operand_min_lv =
358  cgen_state_->ir_builder_.CreateSExt(operand_min_lv, operand_ti_llvm_type);
359  }
360  llvm::Value* over{nullptr};
361  llvm::Value* under{nullptr};
362  if (operand_ti.get_notnull()) {
363  over = cgen_state_->ir_builder_.CreateICmpSGT(operand_lv, operand_max_lv);
364  under = cgen_state_->ir_builder_.CreateICmpSLE(operand_lv, operand_min_lv);
365  } else {
366  const auto type_name =
367  is_narrowing ? numeric_type_name(operand_ti) : numeric_type_name(ti);
368  const auto null_operand_val = cgen_state_->llInt(inline_int_null_val(operand_ti));
369  const auto null_bool_val = cgen_state_->inlineIntNull(SQLTypeInfo(kBOOLEAN, false));
370  over = toBool(cgen_state_->emitCall(
371  "gt_" + type_name + "_nullable_lhs",
372  {operand_lv, operand_max_lv, null_operand_val, null_bool_val}));
373  under = toBool(cgen_state_->emitCall(
374  "le_" + type_name + "_nullable_lhs",
375  {operand_lv, operand_min_lv, null_operand_val, null_bool_val}));
376  }
377  const auto detected = cgen_state_->ir_builder_.CreateOr(over, under, "overflow");
378  cgen_state_->ir_builder_.CreateCondBr(detected, cast_fail, cast_ok);
379 
380  cgen_state_->ir_builder_.SetInsertPoint(cast_fail);
381  cgen_state_->ir_builder_.CreateRet(
383 
384  cgen_state_->ir_builder_.SetInsertPoint(cast_ok);
385 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::Function * current_func_
Definition: CgenState.h:332
int get_logical_size() const
Definition: sqltypes.h:349
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
static const int32_t ERR_OVERFLOW_OR_UNDERFLOW
Definition: Execute.h:1163
bool needs_error_check_
Definition: CgenState.h:358
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
llvm::Value * toBool(llvm::Value *)
Definition: LogicalIR.cpp:343
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
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:84

+ 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 
)
private

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

158  {
160  const auto operand_dimen = operand_ti.get_dimension();
161  const auto target_dimen = target_ti.get_dimension();
162  if (operand_dimen == target_dimen) {
163  return ts_lv;
164  }
165  CHECK(ts_lv->getType()->isIntegerTy(64));
166  const auto scale =
167  DateTimeUtils::get_timestamp_precision_scale(abs(operand_dimen - target_dimen));
168  if (operand_dimen < target_dimen) {
169  codegenCastBetweenIntTypesOverflowChecks(ts_lv, operand_ti, target_ti, scale);
170  return nullable
171  ? cgen_state_->emitCall("mul_int64_t_nullable_lhs",
172  {ts_lv,
173  cgen_state_->llInt(static_cast<int64_t>(scale)),
174  cgen_state_->inlineIntNull(operand_ti)})
175  : cgen_state_->ir_builder_.CreateMul(
176  ts_lv, cgen_state_->llInt(static_cast<int64_t>(scale)));
177  }
178  return nullable
179  ? cgen_state_->emitCall("floor_div_nullable_lhs",
180  {ts_lv,
181  cgen_state_->llInt(static_cast<int64_t>(scale)),
182  cgen_state_->inlineIntNull(operand_ti)})
183  : cgen_state_->ir_builder_.CreateSDiv(
184  ts_lv, cgen_state_->llInt(static_cast<int64_t>(scale)));
185 }
CgenState * cgen_state_
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
void codegenCastBetweenIntTypesOverflowChecks(llvm::Value *operand_lv, const SQLTypeInfo &operand_ti, const SQLTypeInfo &ti, const int64_t scale)
Definition: CastIR.cpp:329
HOST DEVICE int get_dimension() const
Definition: sqltypes.h:331
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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 430 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().

432  {
434  if (!operand_ti.is_fp() || !ti.is_number() || ti.is_decimal()) {
435  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
436  ti.get_type_name() + " not supported");
437  }
438  if (operand_ti.get_type() == ti.get_type()) {
439  // Should not have been called when both dimensions are same.
440  return operand_lv;
441  }
442  CHECK(operand_lv->getType()->isFloatTy() || operand_lv->getType()->isDoubleTy());
443  if (operand_ti.get_notnull()) {
444  if (ti.get_type() == kDOUBLE) {
445  return cgen_state_->ir_builder_.CreateFPExt(
446  operand_lv, llvm::Type::getDoubleTy(cgen_state_->context_));
447  } else if (ti.get_type() == kFLOAT) {
448  return cgen_state_->ir_builder_.CreateFPTrunc(
449  operand_lv, llvm::Type::getFloatTy(cgen_state_->context_));
450  } else if (ti.is_integer()) {
451  // Round by adding/subtracting 0.5 before fptosi.
452  auto* fp_type = operand_lv->getType()->isFloatTy()
453  ? llvm::Type::getFloatTy(cgen_state_->context_)
454  : llvm::Type::getDoubleTy(cgen_state_->context_);
455  auto* zero = llvm::ConstantFP::get(fp_type, 0.0);
456  auto* mhalf = llvm::ConstantFP::get(fp_type, -0.5);
457  auto* phalf = llvm::ConstantFP::get(fp_type, 0.5);
458  auto* is_negative = cgen_state_->ir_builder_.CreateFCmpOLT(operand_lv, zero);
459  auto* offset = cgen_state_->ir_builder_.CreateSelect(is_negative, mhalf, phalf);
460  operand_lv = cgen_state_->ir_builder_.CreateFAdd(operand_lv, offset);
461  return cgen_state_->ir_builder_.CreateFPToSI(
462  operand_lv, get_int_type(get_bit_width(ti), cgen_state_->context_));
463  } else {
464  CHECK(false);
465  }
466  } else {
467  const auto from_tname = numeric_type_name(operand_ti);
468  const auto to_tname = numeric_type_name(ti);
469  if (ti.is_fp()) {
470  return cgen_state_->emitCall("cast_" + from_tname + "_to_" + to_tname + "_nullable",
471  {operand_lv,
472  cgen_state_->inlineFpNull(operand_ti),
473  cgen_state_->inlineFpNull(ti)});
474  } else if (ti.is_integer()) {
475  return cgen_state_->emitCall("cast_" + from_tname + "_to_" + to_tname + "_nullable",
476  {operand_lv,
477  cgen_state_->inlineFpNull(operand_ti),
478  cgen_state_->inlineIntNull(ti)});
479  } else {
480  CHECK(false);
481  }
482  }
483  CHECK(false);
484  return nullptr;
485 }
CgenState * cgen_state_
bool is_fp() const
Definition: sqltypes.h:523
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
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:524
size_t get_bit_width(const SQLTypeInfo &ti)
llvm::LLVMContext & context_
Definition: CgenState.h:338
bool is_integer() const
Definition: sqltypes.h:521
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
std::string get_type_name() const
Definition: sqltypes.h:442
#define CHECK(condition)
Definition: Logger.h:211
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:522
llvm::ConstantFP * inlineFpNull(const SQLTypeInfo &)
Definition: CgenState.cpp:67

+ 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 187 of file CastIR.cpp.

References AUTOMATIC_IR_METADATA, cgen_state_, CHECK, CHECK_EQ, CgenState::context_, CompilationOptions::device_type, CgenState::emitExternalCall(), executor(), g_cluster, g_enable_watchdog, SQLTypeInfo::get_comp_param(), SQLTypeInfo::get_compression(), get_int_type(), SQLTypeInfo::get_type_name(), GPU, SQLTypeInfo::is_string(), kENCODING_DICT, kENCODING_NONE, and CgenState::llInt().

Referenced by codegenCast().

191  {
193  if (!ti.is_string()) {
194  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
195  ti.get_type_name() + " not supported");
196  }
197  if (operand_ti.get_compression() == kENCODING_NONE &&
199  return operand_lv;
200  }
201  // dictionary encode non-constant
202  if (operand_ti.get_compression() != kENCODING_DICT && !operand_is_const) {
203  if (g_cluster) {
204  throw std::runtime_error(
205  "Cast from none-encoded string to dictionary-encoded not supported for "
206  "distributed queries");
207  }
208  if (g_enable_watchdog) {
209  throw WatchdogException(
210  "Cast from none-encoded string to dictionary-encoded would be slow");
211  }
212  CHECK_EQ(kENCODING_NONE, operand_ti.get_compression());
214  CHECK(operand_lv->getType()->isIntegerTy(64));
216  throw QueryMustRunOnCpu();
217  }
219  "string_compress",
221  {operand_lv,
222  cgen_state_->llInt(int64_t(executor()->getStringDictionaryProxy(
223  ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true)))});
224  }
225  CHECK(operand_lv->getType()->isIntegerTy(32));
226  if (ti.get_compression() == kENCODING_NONE) {
227  if (g_cluster) {
228  throw std::runtime_error(
229  "Cast from dictionary-encoded string to none-encoded not supported for "
230  "distributed queries");
231  }
232  if (g_enable_watchdog) {
233  throw WatchdogException(
234  "Cast from dictionary-encoded string to none-encoded would be slow");
235  }
236  CHECK_EQ(kENCODING_DICT, operand_ti.get_compression());
238  throw QueryMustRunOnCpu();
239  }
240  const int64_t string_dictionary_ptr =
241  operand_ti.get_comp_param() == 0
242  ? reinterpret_cast<int64_t>(
243  executor()->getRowSetMemoryOwner()->getLiteralStringDictProxy())
244  : reinterpret_cast<int64_t>(executor()->getStringDictionaryProxy(
245  operand_ti.get_comp_param(), executor()->getRowSetMemoryOwner(), true));
247  "string_decompress",
249  {operand_lv, cgen_state_->llInt(string_dictionary_ptr)});
250  }
251  CHECK(operand_is_const);
253  return operand_lv;
254 }
#define CHECK_EQ(x, y)
Definition: Logger.h:219
bool g_enable_watchdog
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:228
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
llvm::LLVMContext & context_
Definition: CgenState.h:338
#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:442
HOST DEVICE int get_comp_param() const
Definition: sqltypes.h:338
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
bool g_cluster
bool is_string() const
Definition: sqltypes.h:519
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 118 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().

120  {
122  CHECK(ts_lv->getType()->isIntegerTy(64));
123  if (dimen > 0) {
124  if (nullable) {
126  "DateTruncateHighPrecisionToDateNullable",
128  {{ts_lv,
131  }
133  "DateTruncateHighPrecisionToDate",
135  {{ts_lv,
137  }
138  std::unique_ptr<CodeGenerator::NullCheckCodegen> nullcheck_codegen;
139  if (nullable) {
140  nullcheck_codegen =
141  std::make_unique<NullCheckCodegen>(cgen_state_,
142  executor(),
143  ts_lv,
144  SQLTypeInfo(kTIMESTAMP, dimen, 0, !nullable),
145  "cast_timestamp_nullcheck");
146  }
147  auto ret = cgen_state_->emitExternalCall(
148  "datetrunc_day", get_int_type(64, cgen_state_->context_), {ts_lv});
149  if (nullcheck_codegen) {
150  ret = nullcheck_codegen->finalize(ll_int(NULL_BIGINT, cgen_state_->context_), ret);
151  }
152  return ret;
153 }
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:228
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:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
llvm::ConstantInt * llInt(const T v) const
Definition: CgenState.h:307
#define CHECK(condition)
Definition: Logger.h:211
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 387 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().

389  {
391  if (!ti.is_fp()) {
392  throw std::runtime_error("Cast from " + operand_ti.get_type_name() + " to " +
393  ti.get_type_name() + " not supported");
394  }
395  llvm::Value* result_lv;
396  if (operand_ti.get_notnull()) {
397  auto const fp_type = ti.get_type() == kFLOAT
398  ? llvm::Type::getFloatTy(cgen_state_->context_)
399  : llvm::Type::getDoubleTy(cgen_state_->context_);
400  result_lv = cgen_state_->ir_builder_.CreateSIToFP(operand_lv, fp_type);
401  if (auto const scale = static_cast<unsigned>(operand_ti.get_scale())) {
402  double const multiplier = shared::power10inv(scale);
403  result_lv = cgen_state_->ir_builder_.CreateFMul(
404  result_lv, llvm::ConstantFP::get(result_lv->getType(), multiplier));
405  }
406  } else {
407  if (auto const scale = static_cast<unsigned>(operand_ti.get_scale())) {
408  double const multiplier = shared::power10inv(scale);
409  auto const fp_type = ti.get_type() == kFLOAT
410  ? llvm::Type::getFloatTy(cgen_state_->context_)
411  : llvm::Type::getDoubleTy(cgen_state_->context_);
412  result_lv = cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
413  numeric_type_name(ti) + "_scaled_nullable",
414  {operand_lv,
415  cgen_state_->inlineIntNull(operand_ti),
417  llvm::ConstantFP::get(fp_type, multiplier)});
418  } else {
419  result_lv = cgen_state_->emitCall("cast_" + numeric_type_name(operand_ti) + "_to_" +
420  numeric_type_name(ti) + "_nullable",
421  {operand_lv,
422  cgen_state_->inlineIntNull(operand_ti),
423  cgen_state_->inlineFpNull(ti)});
424  }
425  }
426  CHECK(result_lv);
427  return result_lv;
428 }
CgenState * cgen_state_
bool is_fp() const
Definition: sqltypes.h:523
HOST DEVICE int get_scale() const
Definition: sqltypes.h:334
llvm::IRBuilder ir_builder_
Definition: CgenState.h:340
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
llvm::LLVMContext & context_
Definition: CgenState.h:338
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
#define AUTOMATIC_IR_METADATA(CGENSTATE)
double power10inv(unsigned const x)
Definition: misc.h:268
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
std::string get_type_name() const
Definition: sqltypes.h:442
#define CHECK(condition)
Definition: Logger.h:211
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:67

+ 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:219
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:67
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:442
std::shared_ptr< Analyzer::BinOper > lower_bw_eq(const Analyzer::BinOper *bw_eq)
Definition: CompareIR.cpp:125
SQLOps get_optype() const
Definition: Analyzer.h:438
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:211
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:441
bool is_unnest(const Analyzer::Expr *expr)
Definition: Execute.h:1223
const std::shared_ptr< Analyzer::Expr > get_own_right_operand() const
Definition: Analyzer.h:446
const std::shared_ptr< Analyzer::Expr > get_own_left_operand() const
Definition: Analyzer.h:443
SQLQualifier get_qualifier() const
Definition: Analyzer.h:440

+ 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:219
llvm::Value * castToTypeIn(llvm::Value *val, const size_t bit_width)
Definition: CgenState.cpp:113
#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:340
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:525
bool is_integer() const
Definition: sqltypes.h:521
llvm::ConstantInt * inlineIntNull(const SQLTypeInfo &)
Definition: CgenState.cpp:29
bool is_timeinterval() const
Definition: sqltypes.h:530
llvm::ConstantFP * llFp(const float v) const
Definition: CgenState.h:311
bool is_boolean() const
Definition: sqltypes.h:526
#define AUTOMATIC_IR_METADATA(CGENSTATE)
std::string get_null_check_suffix(const SQLTypeInfo &lhs_ti, const SQLTypeInfo &rhs_ti)
Definition: Execute.h:1207
const SQLTypeInfo & get_type_info() const
Definition: Analyzer.h:77
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
Definition: CgenState.cpp:180
std::vector< llvm::Value * > codegen(const Analyzer::Expr *, const bool fetch_columns, const CompilationOptions &)
Definition: IRCodegen.cpp:30
Definition: sqldefs.h:69
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:307
#define CHECK(condition)
Definition: Logger.h:211
bool is_geometry() const
Definition: sqltypes.h:531
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:519
bool is_decimal() const
Definition: sqltypes.h:522
std::string string_cmp_func(const SQLOps optype)
Definition: CompareIR.cpp:106
#define IS_COMPARISON(X)
Definition: sqldefs.h:57

+ Here is the call graph for this function:

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

Definition at line 398 of file CompareIR.cpp.

References AUTOMATIC_IR_METADATA, Datum::bigintval, cgen_state_, codegen(), codegenCast(), codegenCmp(), exp_to_scale(), SQLTypeInfo::get_scale(), Analyzer::Expr::get_type_info(), kCAST, 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<