OmniSciDB  72180abbfe
 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 571 of file TargetExprBuilder.cpp.

References CHECK().

Referenced by GroupByAndAggregate::codegenAggCalls().

581  {
582  CHECK(group_by_and_agg);
583  CHECK(executor);
584 
585  for (const auto& target_expr_codegen : target_exprs_to_codegen) {
586  target_expr_codegen.codegen(group_by_and_agg,
587  executor,
588  query_mem_desc,
589  co,
590  gpu_smem_context,
591  agg_out_ptr_w_idx,
592  agg_out_vec,
593  output_buffer_byte_stream,
594  out_row_idx,
595  diamond_codegen);
596  }
597  if (!sample_exprs_to_codegen.empty()) {
598  codegenSampleExpressions(group_by_and_agg,
599  executor,
600  query_mem_desc,
601  co,
602  agg_out_ptr_w_idx,
603  agg_out_vec,
604  output_buffer_byte_stream,
605  out_row_idx,
606  diamond_codegen);
607  }
608 }
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
CHECK(cgen_state)
std::vector< TargetExprCodegen > sample_exprs_to_codegen

+ Here is the call graph for this function:

+ 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 672 of file TargetExprBuilder.cpp.

References 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().

681  {
682  CHECK(sample_exprs_to_codegen.size() > 1 ||
683  sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
685  const auto& first_sample_expr = sample_exprs_to_codegen.front();
686  auto target_lvs = group_by_and_agg->codegenAggArg(first_sample_expr.target_expr, co);
687  CHECK_GE(target_lvs.size(), size_t(1));
688 
689  const auto init_val =
690  get_initial_agg_val(first_sample_expr.target_info, query_mem_desc);
691 
692  llvm::Value* agg_col_ptr{nullptr};
693  if (is_group_by) {
694  const auto agg_column_size_bytes =
695  query_mem_desc.isLogicalSizedColumnsAllowed() &&
696  !first_sample_expr.target_info.sql_type.is_varlen()
697  ? first_sample_expr.target_info.sql_type.get_size()
698  : sizeof(int64_t);
699  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
700  out_row_idx,
701  agg_out_ptr_w_idx,
702  query_mem_desc,
703  agg_column_size_bytes,
704  first_sample_expr.base_slot_index,
705  first_sample_expr.target_idx);
706  } else {
707  CHECK_LT(static_cast<size_t>(first_sample_expr.base_slot_index), agg_out_vec.size());
708  agg_col_ptr =
709  executor->castToIntPtrTyIn(agg_out_vec[first_sample_expr.base_slot_index], 64);
710  }
711 
712  auto sample_cas_lv = codegenSlotEmptyKey(agg_col_ptr, target_lvs, executor, init_val);
713 
715  sample_cas_lv, executor, false, "sample_valcheck", &diamond_codegen, false);
716 
717  for (const auto& target_expr_codegen : sample_exprs_to_codegen) {
718  target_expr_codegen.codegen(group_by_and_agg,
719  executor,
720  query_mem_desc,
721  co,
722  {},
723  agg_out_ptr_w_idx,
724  agg_out_vec,
725  output_buffer_byte_stream,
726  out_row_idx,
727  diamond_codegen,
728  &sample_cfg);
729  }
730 }
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
CHECK(cgen_state)
std::vector< TargetExprCodegen > sample_exprs_to_codegen
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)
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 610 of file TargetExprBuilder.cpp.

References CHECK(), CompilationOptions::device_type, and GPU.

619  {
620  CHECK(!sample_exprs_to_codegen.empty());
622  if (sample_exprs_to_codegen.size() == 1 &&
623  !sample_exprs_to_codegen.front().target_info.sql_type.is_varlen()) {
624  codegenSingleSlotSampleExpression(group_by_and_agg,
625  executor,
626  query_mem_desc,
627  co,
628  agg_out_ptr_w_idx,
629  agg_out_vec,
630  output_buffer_byte_stream,
631  out_row_idx,
632  diamond_codegen);
633  } else {
634  codegenMultiSlotSampleExpressions(group_by_and_agg,
635  executor,
636  query_mem_desc,
637  co,
638  agg_out_ptr_w_idx,
639  agg_out_vec,
640  output_buffer_byte_stream,
641  out_row_idx,
642  diamond_codegen);
643  }
644 }
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
CHECK(cgen_state)
std::vector< TargetExprCodegen > sample_exprs_to_codegen
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

+ Here is the call graph for this function:

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 646 of file TargetExprBuilder.cpp.

References CHECK(), CHECK_EQ, CompilationOptions::device_type, and GPU.

655  {
656  CHECK_EQ(size_t(1), sample_exprs_to_codegen.size());
657  CHECK(!sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
659  // no need for the atomic if we only have one SAMPLE target
660  sample_exprs_to_codegen.front().codegen(group_by_and_agg,
661  executor,
662  query_mem_desc,
663  co,
664  {},
665  agg_out_ptr_w_idx,
666  agg_out_vec,
667  output_buffer_byte_stream,
668  out_row_idx,
669  diamond_codegen);
670 }
#define CHECK_EQ(x, y)
Definition: Logger.h:205
CHECK(cgen_state)
std::vector< TargetExprCodegen > sample_exprs_to_codegen
ExecutorDeviceType device_type

+ Here is the call graph for this function:

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 732 of file TargetExprBuilder.cpp.

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

736  {
737  const auto& first_sample_expr = sample_exprs_to_codegen.front();
738  const auto first_sample_slot_bytes =
739  first_sample_expr.target_info.sql_type.is_varlen()
740  ? sizeof(int64_t)
741  : first_sample_expr.target_info.sql_type.get_size();
742  llvm::Value* target_lv_casted{nullptr};
743  // deciding whether proper casting is required for the first sample's slot:
744  if (first_sample_expr.target_info.sql_type.is_varlen()) {
745  target_lv_casted =
746  LL_BUILDER.CreatePtrToInt(target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
747  } else if (first_sample_expr.target_info.sql_type.is_fp()) {
748  // Initialization value for SAMPLE on a float column should be 0
749  CHECK_EQ(init_val, 0);
751  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
752  target_lvs.front(),
753  first_sample_slot_bytes == sizeof(float) ? llvm::Type::getInt32Ty(LL_CONTEXT)
754  : llvm::Type::getInt64Ty(LL_CONTEXT));
755  } else {
756  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
757  target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
758  }
759  } else if (first_sample_slot_bytes != sizeof(int64_t) &&
761  target_lv_casted =
762  executor->cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
763  target_lvs.front(),
764  llvm::Type::getInt64Ty(LL_CONTEXT));
765  } else {
766  target_lv_casted = target_lvs.front();
767  }
768 
769  std::string slot_empty_cas_func_name("slotEmptyKeyCAS");
770  llvm::Value* init_val_lv{LL_INT(init_val)};
772  !first_sample_expr.target_info.sql_type.is_varlen()) {
773  // add proper suffix to the function name:
774  switch (first_sample_slot_bytes) {
775  case 1:
776  slot_empty_cas_func_name += "_int8";
777  break;
778  case 2:
779  slot_empty_cas_func_name += "_int16";
780  break;
781  case 4:
782  slot_empty_cas_func_name += "_int32";
783  break;
784  case 8:
785  break;
786  default:
787  UNREACHABLE() << "Invalid slot size for slotEmptyKeyCAS function.";
788  break;
789  }
790  if (first_sample_slot_bytes != sizeof(int64_t)) {
791  init_val_lv = llvm::ConstantInt::get(
792  get_int_type(first_sample_slot_bytes * 8, LL_CONTEXT), init_val);
793  }
794  }
795 
796  auto sample_cas_lv = executor->cgen_state_->emitExternalCall(
797  slot_empty_cas_func_name,
798  llvm::Type::getInt1Ty(executor->cgen_state_->context_),
799  {agg_col_ptr, target_lv_casted, init_val_lv});
800  return sample_cas_lv;
801 }
#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

+ 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 496 of file TargetExprBuilder.cpp.

References agg_arg(), anonymous_namespace{TargetExprBuilder.cpp}::agg_fn_base_names(), TargetInfo::agg_kind, 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, kSAMPLE, kSINGLE_VALUE, kUNNEST, NonGroupedAggregate, TargetInfo::skip_null_val, and TargetExprCodegen::target_info.

498  {
500  CHECK(!dynamic_cast<const Analyzer::AggExpr*>(target_expr));
503  return;
504  }
505  if (dynamic_cast<const Analyzer::UOper*>(target_expr) &&
506  static_cast<const Analyzer::UOper*>(target_expr)->get_optype() == kUNNEST) {
507  throw std::runtime_error("UNNEST not supported in the projection list yet.");
508  }
509  if ((executor->plan_state_->isLazyFetchColumn(target_expr) || !is_group_by) &&
511  sizeof(int64_t)) &&
513  // TODO(miyu): enable different byte width in the layout w/o padding
515  }
516 
517  auto target_info = get_target_info(target_expr, g_bigint_count);
518  auto arg_expr = agg_arg(target_expr);
519  if (arg_expr) {
520  if (target_info.agg_kind == kSINGLE_VALUE || target_info.agg_kind == kSAMPLE) {
521  target_info.skip_null_val = false;
524  !arg_expr->get_type_info().is_varlen()) {
525  // TODO: COUNT is currently not null-aware for varlen types. Need to add proper code
526  // generation for handling varlen nulls.
527  target_info.skip_null_val = true;
528  } else if (constrained_not_null(arg_expr, ra_exe_unit.quals)) {
529  target_info.skip_null_val = false;
530  }
531  }
532 
535  (co.device_type == ExecutorDeviceType::GPU) && target_info.is_agg &&
536  (target_info.agg_kind == kSAMPLE)) {
537  sample_exprs_to_codegen.emplace_back(target_expr,
538  target_info,
541  is_group_by);
542  } else {
543  target_exprs_to_codegen.emplace_back(target_expr,
544  target_info,
547  is_group_by);
548  }
549 
550  const auto agg_fn_names = agg_fn_base_names(target_info);
551  slot_index_counter += agg_fn_names.size();
552 }
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:66
std::vector< TargetExprCodegen > target_exprs_to_codegen
const QueryMemoryDescriptor & query_mem_desc
CHECK(cgen_state)
bool g_bigint_count
std::vector< TargetExprCodegen > sample_exprs_to_codegen
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
QueryDescriptionType getQueryDescriptionType() const
ExecutorDeviceType device_type
std::list< std::shared_ptr< Analyzer::Expr > > quals
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: