OmniSciDB  1dac507f6e
 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 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 64 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 65 of file TargetExprBuilder.h.

68  : query_mem_desc(query_mem_desc)
69  , 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 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 510 of file TargetExprBuilder.cpp.

References CHECK().

Referenced by GroupByAndAggregate::codegenAggCalls().

519  {
520  CHECK(group_by_and_agg);
521  CHECK(executor);
522 
523  for (const auto& target_expr_codegen : target_exprs_to_codegen) {
524  target_expr_codegen.codegen(group_by_and_agg,
525  executor,
526  query_mem_desc,
527  co,
528  agg_out_ptr_w_idx,
529  agg_out_vec,
530  output_buffer_byte_stream,
531  out_row_idx,
532  diamond_codegen);
533  }
534  if (!sample_exprs_to_codegen.empty()) {
535  codegenSampleExpressions(group_by_and_agg,
536  executor,
537  query_mem_desc,
538  co,
539  agg_out_ptr_w_idx,
540  agg_out_vec,
541  output_buffer_byte_stream,
542  out_row_idx,
543  diamond_codegen);
544  }
545 }
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 608 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().

617  {
618  CHECK(sample_exprs_to_codegen.size() > 1 ||
619  sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
621  const auto& first_sample_expr = sample_exprs_to_codegen.front();
622  auto target_lvs = group_by_and_agg->codegenAggArg(first_sample_expr.target_expr, co);
623  CHECK_GE(target_lvs.size(), size_t(1));
624 
625  const auto init_val =
626  get_initial_agg_val(first_sample_expr.target_info, query_mem_desc);
627 
628  llvm::Value* agg_col_ptr{nullptr};
629  if (is_group_by) {
630  const auto agg_column_size_bytes =
631  query_mem_desc.isLogicalSizedColumnsAllowed() &&
632  !first_sample_expr.target_info.sql_type.is_varlen()
633  ? first_sample_expr.target_info.sql_type.get_size()
634  : sizeof(int64_t);
635  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
636  out_row_idx,
637  agg_out_ptr_w_idx,
638  query_mem_desc,
639  agg_column_size_bytes,
640  first_sample_expr.base_slot_index,
641  first_sample_expr.target_idx);
642  } else {
643  CHECK_LT(static_cast<size_t>(first_sample_expr.base_slot_index), agg_out_vec.size());
644  agg_col_ptr =
645  executor->castToIntPtrTyIn(agg_out_vec[first_sample_expr.base_slot_index], 64);
646  }
647 
648  auto sample_cas_lv = codegenSlotEmptyKey(agg_col_ptr, target_lvs, executor, init_val);
649 
651  sample_cas_lv, executor, false, "sample_valcheck", &diamond_codegen, false);
652 
653  for (const auto& target_expr_codegen : sample_exprs_to_codegen) {
654  target_expr_codegen.codegen(group_by_and_agg,
655  executor,
656  query_mem_desc,
657  co,
658  agg_out_ptr_w_idx,
659  agg_out_vec,
660  output_buffer_byte_stream,
661  out_row_idx,
662  diamond_codegen,
663  &sample_cfg);
664  }
665 }
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:203
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:200
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 547 of file TargetExprBuilder.cpp.

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

556  {
557  CHECK(!sample_exprs_to_codegen.empty());
559  if (sample_exprs_to_codegen.size() == 1 &&
560  !sample_exprs_to_codegen.front().target_info.sql_type.is_varlen()) {
561  codegenSingleSlotSampleExpression(group_by_and_agg,
562  executor,
563  query_mem_desc,
564  co,
565  agg_out_ptr_w_idx,
566  agg_out_vec,
567  output_buffer_byte_stream,
568  out_row_idx,
569  diamond_codegen);
570  } else {
571  codegenMultiSlotSampleExpressions(group_by_and_agg,
572  executor,
573  query_mem_desc,
574  co,
575  agg_out_ptr_w_idx,
576  agg_out_vec,
577  output_buffer_byte_stream,
578  out_row_idx,
579  diamond_codegen);
580  }
581 }
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 583 of file TargetExprBuilder.cpp.

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

592  {
593  CHECK_EQ(size_t(1), sample_exprs_to_codegen.size());
594  CHECK(!sample_exprs_to_codegen.front().target_info.sql_type.is_varlen());
596  // no need for the atomic if we only have one SAMPLE target
597  sample_exprs_to_codegen.front().codegen(group_by_and_agg,
598  executor,
599  query_mem_desc,
600  co,
601  agg_out_ptr_w_idx,
602  agg_out_vec,
603  output_buffer_byte_stream,
604  out_row_idx,
605  diamond_codegen);
606 }
#define CHECK_EQ(x, y)
Definition: Logger.h:198
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 667 of file TargetExprBuilder.cpp.

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

671  {
672  const auto& first_sample_expr = sample_exprs_to_codegen.front();
673  const auto first_sample_slot_bytes =
674  first_sample_expr.target_info.sql_type.is_varlen()
675  ? sizeof(int64_t)
676  : first_sample_expr.target_info.sql_type.get_size();
677  llvm::Value* target_lv_casted{nullptr};
678  // deciding whether proper casting is required for the first sample's slot:
679  if (first_sample_expr.target_info.sql_type.is_varlen()) {
680  target_lv_casted =
681  LL_BUILDER.CreatePtrToInt(target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
682  } else if (first_sample_expr.target_info.sql_type.is_fp()) {
683  // Initialization value for SAMPLE on a float column should be 0
684  CHECK_EQ(init_val, 0);
686  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
687  target_lvs.front(),
688  first_sample_slot_bytes == sizeof(float) ? llvm::Type::getInt32Ty(LL_CONTEXT)
689  : llvm::Type::getInt64Ty(LL_CONTEXT));
690  } else {
691  target_lv_casted = executor->cgen_state_->ir_builder_.CreateFPToSI(
692  target_lvs.front(), llvm::Type::getInt64Ty(LL_CONTEXT));
693  }
694  } else if (first_sample_slot_bytes != sizeof(int64_t) &&
696  target_lv_casted =
697  executor->cgen_state_->ir_builder_.CreateCast(llvm::Instruction::CastOps::SExt,
698  target_lvs.front(),
699  llvm::Type::getInt64Ty(LL_CONTEXT));
700  } else {
701  target_lv_casted = target_lvs.front();
702  }
703 
704  std::string slot_empty_cas_func_name("slotEmptyKeyCAS");
705  llvm::Value* init_val_lv{LL_INT(init_val)};
707  !first_sample_expr.target_info.sql_type.is_varlen()) {
708  // add proper suffix to the function name:
709  switch (first_sample_slot_bytes) {
710  case 1:
711  slot_empty_cas_func_name += "_int8";
712  break;
713  case 2:
714  slot_empty_cas_func_name += "_int16";
715  break;
716  case 4:
717  slot_empty_cas_func_name += "_int32";
718  break;
719  case 8:
720  break;
721  default:
722  UNREACHABLE() << "Invalid slot size for slotEmptyKeyCAS function.";
723  break;
724  }
725  if (first_sample_slot_bytes != sizeof(int64_t)) {
726  init_val_lv = llvm::ConstantInt::get(
727  get_int_type(first_sample_slot_bytes * 8, LL_CONTEXT), init_val);
728  }
729  }
730 
731  auto sample_cas_lv = executor->cgen_state_->emitExternalCall(
732  slot_empty_cas_func_name,
733  llvm::Type::getInt1Ty(executor->cgen_state_->context_),
734  {agg_col_ptr, target_lv_casted, init_val_lv});
735  return sample_cas_lv;
736 }
#define LL_BUILDER
#define CHECK_EQ(x, y)
Definition: Logger.h:198
bool isLogicalSizedColumnsAllowed() const
#define UNREACHABLE()
Definition: Logger.h:234
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 435 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, kUNNEST, NonGroupedAggregate, TargetInfo::skip_null_val, and TargetExprCodegen::target_info.

437  {
439  CHECK(!dynamic_cast<const Analyzer::AggExpr*>(target_expr));
442  return;
443  }
444  if (dynamic_cast<const Analyzer::UOper*>(target_expr) &&
445  static_cast<const Analyzer::UOper*>(target_expr)->get_optype() == kUNNEST) {
446  throw std::runtime_error("UNNEST not supported in the projection list yet.");
447  }
448  if ((executor->plan_state_->isLazyFetchColumn(target_expr) || !is_group_by) &&
450  sizeof(int64_t)) &&
452  // TODO(miyu): enable different byte width in the layout w/o padding
454  }
455 
456  auto target_info = get_target_info(target_expr, g_bigint_count);
457  auto arg_expr = agg_arg(target_expr);
458  if (arg_expr) {
459  if (target_info.agg_kind == kSAMPLE) {
460  target_info.skip_null_val = false;
463  !arg_expr->get_type_info().is_varlen()) {
464  // TODO: COUNT is currently not null-aware for varlen types. Need to add proper code
465  // generation for handling varlen nulls.
466  target_info.skip_null_val = true;
467  } else if (constrained_not_null(arg_expr, ra_exe_unit.quals)) {
468  target_info.skip_null_val = false;
469  }
470  }
471 
474  (co.device_type_ == ExecutorDeviceType::GPU) && target_info.is_agg &&
475  (target_info.agg_kind == kSAMPLE)) {
476  sample_exprs_to_codegen.emplace_back(target_expr,
477  target_info,
480  is_group_by);
481  } else {
482  target_exprs_to_codegen.emplace_back(target_expr,
483  target_info,
486  is_group_by);
487  }
488 
489  const auto agg_fn_names = agg_fn_base_names(target_info);
490  slot_index_counter += agg_fn_names.size();
491 }
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:65
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
ExecutorDeviceType device_type_
QueryDescriptionType getQueryDescriptionType() const
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 133 of file TargetExprBuilder.h.

const QueryMemoryDescriptor& TargetExprCodegenBuilder::query_mem_desc

Definition at line 127 of file TargetExprBuilder.h.

const RelAlgExecutionUnit& TargetExprCodegenBuilder::ra_exe_unit

Definition at line 128 of file TargetExprBuilder.h.

std::vector<TargetExprCodegen> TargetExprCodegenBuilder::sample_exprs_to_codegen

Definition at line 131 of file TargetExprBuilder.h.

size_t TargetExprCodegenBuilder::slot_index_counter {0}

Definition at line 125 of file TargetExprBuilder.h.

std::vector<TargetExprCodegen> TargetExprCodegenBuilder::target_exprs_to_codegen

Definition at line 130 of file TargetExprBuilder.h.

size_t TargetExprCodegenBuilder::target_index_counter {0}

Definition at line 124 of file TargetExprBuilder.h.


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