OmniSciDB  a667adc9c8
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
TargetExprCodegenBuilder Struct Reference

#include <TargetExprBuilder.h>

+ Collaboration diagram for TargetExprCodegenBuilder:

Public Member Functions

 TargetExprCodegenBuilder (const QueryMemoryDescriptor &query_mem_desc, const RelAlgExecutionUnit &ra_exe_unit, const bool is_group_by)
 
void operator() (const Analyzer::Expr *target_expr, const Executor *executor, const CompilationOptions &co)
 
void codegen (GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const GpuSharedMemoryContext &gpu_smem_context, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
 
void codegenSampleExpressions (GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
 
void codegenSingleSlotSampleExpression (GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
 
void codegenMultiSlotSampleExpressions (GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
 
llvm::Value * codegenSlotEmptyKey (llvm::Value *agg_col_ptr, std::vector< llvm::Value * > &target_lvs, Executor *executor, const int64_t init_val) const
 

Public Attributes

size_t target_index_counter {0}
 
size_t slot_index_counter {0}
 
const QueryMemoryDescriptorquery_mem_desc
 
const RelAlgExecutionUnitra_exe_unit
 
std::vector< TargetExprCodegentarget_exprs_to_codegen
 
std::vector< TargetExprCodegensample_exprs_to_codegen
 
bool is_group_by
 

Detailed Description

Definition at line 76 of file TargetExprBuilder.h.

Constructor & Destructor Documentation

TargetExprCodegenBuilder::TargetExprCodegenBuilder ( const QueryMemoryDescriptor query_mem_desc,
const RelAlgExecutionUnit ra_exe_unit,
const bool  is_group_by 
)
inline

Definition at line 77 of file TargetExprBuilder.h.

80  : query_mem_desc(query_mem_desc)
81  , ra_exe_unit(ra_exe_unit)
const RelAlgExecutionUnit & ra_exe_unit
const QueryMemoryDescriptor & query_mem_desc

Member Function Documentation

void TargetExprCodegenBuilder::codegen ( GroupByAndAggregate group_by_and_agg,
Executor executor,
const QueryMemoryDescriptor query_mem_desc,
const CompilationOptions co,
const GpuSharedMemoryContext gpu_smem_context,
const std::tuple< llvm::Value *, llvm::Value * > &  agg_out_ptr_w_idx,
const std::vector< llvm::Value * > &  agg_out_vec,
llvm::Value *  output_buffer_byte_stream,
llvm::Value *  out_row_idx,
GroupByAndAggregate::DiamondCodegen diamond_codegen 
) const

Definition at line 581 of file TargetExprBuilder.cpp.

References AUTOMATIC_IR_METADATA, and CHECK.

Referenced by GroupByAndAggregate::codegenAggCalls().

591  {
592  CHECK(group_by_and_agg);
593  CHECK(executor);
594  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
595 
596  for (const auto& target_expr_codegen : target_exprs_to_codegen) {
597  target_expr_codegen.codegen(group_by_and_agg,
598  executor,
599  query_mem_desc,
600  co,
601  gpu_smem_context,
602  agg_out_ptr_w_idx,
603  agg_out_vec,
604  output_buffer_byte_stream,
605  out_row_idx,
606  diamond_codegen);
607  }
608  if (!sample_exprs_to_codegen.empty()) {
609  codegenSampleExpressions(group_by_and_agg,
610  executor,
611  query_mem_desc,
612  co,
613  agg_out_ptr_w_idx,
614  agg_out_vec,
615  output_buffer_byte_stream,
616  out_row_idx,
617  diamond_codegen);
618  }
619 }
void codegenSampleExpressions(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
std::vector< TargetExprCodegen > target_exprs_to_codegen
std::vector< TargetExprCodegen > sample_exprs_to_codegen
#define AUTOMATIC_IR_METADATA(CGENSTATE)
#define CHECK(condition)
Definition: Logger.h:197

+ Here is the caller graph for this function:

void TargetExprCodegenBuilder::codegenMultiSlotSampleExpressions ( GroupByAndAggregate group_by_and_agg,
Executor executor,
const QueryMemoryDescriptor query_mem_desc,
const CompilationOptions co,
const std::tuple< llvm::Value *, llvm::Value * > &  agg_out_ptr_w_idx,
const std::vector< llvm::Value * > &  agg_out_vec,
llvm::Value *  output_buffer_byte_stream,
llvm::Value *  out_row_idx,
GroupByAndAggregate::DiamondCodegen diamond_codegen 
) const

Definition at line 685 of file TargetExprBuilder.cpp.

References AUTOMATIC_IR_METADATA, CHECK, CHECK_GE, CHECK_LT, GroupByAndAggregate::codegenAggArg(), GroupByAndAggregate::codegenAggColumnPtr(), CompilationOptions::device_type, anonymous_namespace{TargetExprBuilder.cpp}::get_initial_agg_val(), GPU, TargetExprCodegen::is_group_by, and QueryMemoryDescriptor::isLogicalSizedColumnsAllowed().

694  {
695  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
696  CHECK(sample_exprs_to_codegen.size() > 1 ||
697  sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
699  const auto& first_sample_expr = sample_exprs_to_codegen.front();
700  auto target_lvs = group_by_and_agg->codegenAggArg(first_sample_expr.target_expr, co);
701  CHECK_GE(target_lvs.size(), size_t(1));
702 
703  const auto init_val =
704  get_initial_agg_val(first_sample_expr.target_info, query_mem_desc);
705 
706  llvm::Value* agg_col_ptr{nullptr};
707  if (is_group_by) {
708  const auto agg_column_size_bytes =
709  query_mem_desc.isLogicalSizedColumnsAllowed() &&
710  !first_sample_expr.target_info.sql_type.is_varlen()
711  ? first_sample_expr.target_info.sql_type.get_size()
712  : sizeof(int64_t);
713  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
714  out_row_idx,
715  agg_out_ptr_w_idx,
716  query_mem_desc,
717  agg_column_size_bytes,
718  first_sample_expr.base_slot_index,
719  first_sample_expr.target_idx);
720  } else {
721  CHECK_LT(static_cast<size_t>(first_sample_expr.base_slot_index), agg_out_vec.size());
722  agg_col_ptr =
723  executor->castToIntPtrTyIn(agg_out_vec[first_sample_expr.base_slot_index], 64);
724  }
725 
726  auto sample_cas_lv = codegenSlotEmptyKey(agg_col_ptr, target_lvs, executor, init_val);
727 
729  sample_cas_lv, executor, false, "sample_valcheck", &diamond_codegen, false);
730 
731  for (const auto& target_expr_codegen : sample_exprs_to_codegen) {
732  target_expr_codegen.codegen(group_by_and_agg,
733  executor,
734  query_mem_desc,
735  co,
736  {},
737  agg_out_ptr_w_idx,
738  agg_out_vec,
739  output_buffer_byte_stream,
740  out_row_idx,
741  diamond_codegen,
742  &sample_cfg);
743  }
744 }
llvm::Value * codegenAggColumnPtr(llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const QueryMemoryDescriptor &query_mem_desc, const size_t chosen_bytes, const size_t agg_out_off, const size_t target_idx)
: returns the pointer to where the aggregation should be stored.
bool isLogicalSizedColumnsAllowed() const
#define CHECK_GE(x, y)
Definition: Logger.h:210
llvm::Value * codegenSlotEmptyKey(llvm::Value *agg_col_ptr, std::vector< llvm::Value * > &target_lvs, Executor *executor, const int64_t init_val) const
std::vector< TargetExprCodegen > sample_exprs_to_codegen
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
#define CHECK_LT(x, y)
Definition: Logger.h:207
std::vector< llvm::Value * > codegenAggArg(const Analyzer::Expr *target_expr, const CompilationOptions &co)
#define CHECK(condition)
Definition: Logger.h:197
int64_t get_initial_agg_val(const TargetInfo &target_info, const QueryMemoryDescriptor &query_mem_desc)

+ Here is the call graph for this function:

void TargetExprCodegenBuilder::codegenSampleExpressions ( GroupByAndAggregate group_by_and_agg,
Executor executor,
const QueryMemoryDescriptor query_mem_desc,
const CompilationOptions co,
const std::tuple< llvm::Value *, llvm::Value * > &  agg_out_ptr_w_idx,
const std::vector< llvm::Value * > &  agg_out_vec,
llvm::Value *  output_buffer_byte_stream,
llvm::Value *  out_row_idx,
GroupByAndAggregate::DiamondCodegen diamond_codegen 
) const

Definition at line 621 of file TargetExprBuilder.cpp.

References AUTOMATIC_IR_METADATA, CHECK, CompilationOptions::device_type, and GPU.

630  {
631  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
632  CHECK(!sample_exprs_to_codegen.empty());
634  if (sample_exprs_to_codegen.size() == 1 &&
635  !sample_exprs_to_codegen.front().target_info.sql_type.is_varlen()) {
636  codegenSingleSlotSampleExpression(group_by_and_agg,
637  executor,
638  query_mem_desc,
639  co,
640  agg_out_ptr_w_idx,
641  agg_out_vec,
642  output_buffer_byte_stream,
643  out_row_idx,
644  diamond_codegen);
645  } else {
646  codegenMultiSlotSampleExpressions(group_by_and_agg,
647  executor,
648  query_mem_desc,
649  co,
650  agg_out_ptr_w_idx,
651  agg_out_vec,
652  output_buffer_byte_stream,
653  out_row_idx,
654  diamond_codegen);
655  }
656 }
void codegenSingleSlotSampleExpression(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
std::vector< TargetExprCodegen > sample_exprs_to_codegen
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
void codegenMultiSlotSampleExpressions(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::tuple< llvm::Value *, llvm::Value * > &agg_out_ptr_w_idx, const std::vector< llvm::Value * > &agg_out_vec, llvm::Value *output_buffer_byte_stream, llvm::Value *out_row_idx, GroupByAndAggregate::DiamondCodegen &diamond_codegen) const
#define CHECK(condition)
Definition: Logger.h:197
void TargetExprCodegenBuilder::codegenSingleSlotSampleExpression ( GroupByAndAggregate group_by_and_agg,
Executor executor,
const QueryMemoryDescriptor query_mem_desc,
const CompilationOptions co,
const std::tuple< llvm::Value *, llvm::Value * > &  agg_out_ptr_w_idx,
const std::vector< llvm::Value * > &  agg_out_vec,
llvm::Value *  output_buffer_byte_stream,
llvm::Value *  out_row_idx,
GroupByAndAggregate::DiamondCodegen diamond_codegen 
) const

Definition at line 658 of file TargetExprBuilder.cpp.

References AUTOMATIC_IR_METADATA, CHECK, CHECK_EQ, CompilationOptions::device_type, and GPU.

667  {
668  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
669  CHECK_EQ(size_t(1), sample_exprs_to_codegen.size());
670  CHECK(!sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
672  // no need for the atomic if we only have one SAMPLE target
673  sample_exprs_to_codegen.front().codegen(group_by_and_agg,
674  executor,
675  query_mem_desc,
676  co,
677  {},
678  agg_out_ptr_w_idx,
679  agg_out_vec,
680  output_buffer_byte_stream,
681  out_row_idx,
682  diamond_codegen);
683 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
std::vector< TargetExprCodegen > sample_exprs_to_codegen
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
#define CHECK(condition)
Definition: Logger.h:197
llvm::Value * TargetExprCodegenBuilder::codegenSlotEmptyKey ( llvm::Value *  agg_col_ptr,
std::vector< llvm::Value * > &  target_lvs,
Executor executor,
const int64_t  init_val 
) const

Definition at line 746 of file TargetExprBuilder.cpp.

References AUTOMATIC_IR_METADATA, CHECK_EQ, get_int_type(), QueryMemoryDescriptor::isLogicalSizedColumnsAllowed(), LL_BUILDER, LL_CONTEXT, LL_INT, and UNREACHABLE.

750  {
751  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
752  const auto& first_sample_expr = sample_exprs_to_codegen.front();
753  const auto first_sample_slot_bytes =
754  first_sample_expr.target_info.sql_type.is_varlen()
755  ? sizeof(int64_t)
756  : first_sample_expr.target_info.sql_type.get_size();
757  llvm::Value* target_lv_casted{nullptr};
758  // deciding whether proper casting is required for the first sample's slot:
759  if (first_sample_expr.target_info.sql_type.is_varlen()) {
760  target_lv_casted =
761  LL_BUILDER.CreatePtrToInt(target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
762  } else if (first_sample_expr.target_info.sql_type.is_fp()) {
763  // Initialization value for SAMPLE on a float column should be 0
764  CHECK_EQ(init_val, 0);
766  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
767  target_lvs.front(),
768  first_sample_slot_bytes == sizeof(float) ? llvm::Type::getInt32Ty(LL_CONTEXT)
769  : llvm::Type::getInt64Ty(LL_CONTEXT));
770  } else {
771  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
772  target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
773  }
774  } else if (first_sample_slot_bytes != sizeof(int64_t) &&
776  target_lv_casted =
777  executor->cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
778  target_lvs.front(),
779  llvm::Type::getInt64Ty(LL_CONTEXT));
780  } else {
781  target_lv_casted = target_lvs.front();
782  }
783 
784  std::string slot_empty_cas_func_name("slotEmptyKeyCAS");
785  llvm::Value* init_val_lv{LL_INT(init_val)};
787  !first_sample_expr.target_info.sql_type.is_varlen()) {
788  // add proper suffix to the function name:
789  switch (first_sample_slot_bytes) {
790  case 1:
791  slot_empty_cas_func_name += "_int8";
792  break;
793  case 2:
794  slot_empty_cas_func_name += "_int16";
795  break;
796  case 4:
797  slot_empty_cas_func_name += "_int32";
798  break;
799  case 8:
800  break;
801  default:
802  UNREACHABLE() << "Invalid slot size for slotEmptyKeyCAS function.";
803  break;
804  }
805  if (first_sample_slot_bytes != sizeof(int64_t)) {
806  init_val_lv = llvm::ConstantInt::get(
807  get_int_type(first_sample_slot_bytes * 8, LL_CONTEXT), init_val);
808  }
809  }
810 
811  auto sample_cas_lv = executor->cgen_state_->emitExternalCall(
812  slot_empty_cas_func_name,
813  llvm::Type::getInt1Ty(executor->cgen_state_->context_),
814  {agg_col_ptr, target_lv_casted, init_val_lv});
815  return sample_cas_lv;
816 }
#define LL_BUILDER
#define CHECK_EQ(x, y)
Definition: Logger.h:205
bool isLogicalSizedColumnsAllowed() const
#define UNREACHABLE()
Definition: Logger.h:241
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
const QueryMemoryDescriptor & query_mem_desc
#define LL_INT(v)
std::vector< TargetExprCodegen > sample_exprs_to_codegen
#define LL_CONTEXT
#define AUTOMATIC_IR_METADATA(CGENSTATE)

+ Here is the call graph for this function:

void TargetExprCodegenBuilder::operator() ( const Analyzer::Expr target_expr,
const Executor executor,
const CompilationOptions co 
)

Definition at line 504 of file TargetExprBuilder.cpp.

References agg_arg(), anonymous_namespace{TargetExprBuilder.cpp}::agg_fn_base_names(), TargetInfo::agg_kind, AUTOMATIC_IR_METADATA, CHECK, constrained_not_null(), CompilationOptions::device_type, g_bigint_count, get_target_info(), QueryMemoryDescriptor::getPaddedSlotWidthBytes(), QueryMemoryDescriptor::getQueryDescriptionType(), GPU, TargetInfo::is_agg, anonymous_namespace{TargetExprBuilder.cpp}::is_columnar_projection(), TargetExprCodegen::is_group_by, kAPPROX_MEDIAN, kSAMPLE, kSINGLE_VALUE, kUNNEST, NonGroupedAggregate, TargetInfo::skip_null_val, and TargetExprCodegen::target_info.

506  {
507  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
509  CHECK(!dynamic_cast<const Analyzer::AggExpr*>(target_expr));
512  return;
513  }
514  if (dynamic_cast<const Analyzer::UOper*>(target_expr) &&
515  static_cast<const Analyzer::UOper*>(target_expr)->get_optype() == kUNNEST) {
516  throw std::runtime_error("UNNEST not supported in the projection list yet.");
517  }
518  if ((executor->plan_state_->isLazyFetchColumn(target_expr) || !is_group_by) &&
520  sizeof(int64_t)) &&
522  // TODO(miyu): enable different byte width in the layout w/o padding
524  }
525 
526  auto target_info = get_target_info(target_expr, g_bigint_count);
527  auto arg_expr = agg_arg(target_expr);
528  if (arg_expr) {
529  if (target_info.agg_kind == kSINGLE_VALUE || target_info.agg_kind == kSAMPLE ||
530  target_info.agg_kind == kAPPROX_MEDIAN) {
531  target_info.skip_null_val = false;
534  !arg_expr->get_type_info().is_varlen()) {
535  // TODO: COUNT is currently not null-aware for varlen types. Need to add proper code
536  // generation for handling varlen nulls.
537  target_info.skip_null_val = true;
538  } else if (constrained_not_null(arg_expr, ra_exe_unit.quals)) {
539  target_info.skip_null_val = false;
540  }
541  }
542 
545  (co.device_type == ExecutorDeviceType::GPU) && target_info.is_agg &&
546  (target_info.agg_kind == kSAMPLE)) {
547  sample_exprs_to_codegen.emplace_back(target_expr,
548  target_info,
551  is_group_by);
552  } else {
553  target_exprs_to_codegen.emplace_back(target_expr,
554  target_info,
557  is_group_by);
558  }
559 
560  const auto agg_fn_names = agg_fn_base_names(target_info);
561  slot_index_counter += agg_fn_names.size();
562 }
const RelAlgExecutionUnit & ra_exe_unit
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
bool constrained_not_null(const Analyzer::Expr *expr, const std::list< std::shared_ptr< Analyzer::Expr >> &quals)
std::vector< std::string > agg_fn_base_names(const TargetInfo &target_info)
TargetInfo get_target_info(const PointerType target_expr, const bool bigint_count)
Definition: TargetInfo.h:79
std::vector< TargetExprCodegen > target_exprs_to_codegen
const QueryMemoryDescriptor & query_mem_desc
bool g_bigint_count
std::vector< TargetExprCodegen > sample_exprs_to_codegen
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
#define AUTOMATIC_IR_METADATA(CGENSTATE)
QueryDescriptionType getQueryDescriptionType() const
ExecutorDeviceType device_type
std::list< std::shared_ptr< Analyzer::Expr > > quals
#define CHECK(condition)
Definition: Logger.h:197
bool is_columnar_projection(const QueryMemoryDescriptor &query_mem_desc)

+ Here is the call graph for this function:

Member Data Documentation

bool TargetExprCodegenBuilder::is_group_by

Definition at line 146 of file TargetExprBuilder.h.

const QueryMemoryDescriptor& TargetExprCodegenBuilder::query_mem_desc

Definition at line 140 of file TargetExprBuilder.h.

const RelAlgExecutionUnit& TargetExprCodegenBuilder::ra_exe_unit

Definition at line 141 of file TargetExprBuilder.h.

std::vector<TargetExprCodegen> TargetExprCodegenBuilder::sample_exprs_to_codegen

Definition at line 144 of file TargetExprBuilder.h.

size_t TargetExprCodegenBuilder::slot_index_counter {0}

Definition at line 138 of file TargetExprBuilder.h.

std::vector<TargetExprCodegen> TargetExprCodegenBuilder::target_exprs_to_codegen

Definition at line 143 of file TargetExprBuilder.h.

size_t TargetExprCodegenBuilder::target_index_counter {0}

Definition at line 137 of file TargetExprBuilder.h.


The documentation for this struct was generated from the following files: