OmniSciDB  16c4e035a1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
TargetExprCodegen Struct Reference

#include <TargetExprBuilder.h>

+ Collaboration diagram for TargetExprCodegen:

Public Member Functions

 TargetExprCodegen (const Analyzer::Expr *target_expr, TargetInfo &target_info, const int32_t base_slot_index, const size_t target_idx, const bool is_group_by)
 
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, llvm::Value *varlen_output_buffer, DiamondCodegen &diamond_codegen, DiamondCodegen *sample_cfg=nullptr) const
 
void codegenAggregate (GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::vector< llvm::Value * > &target_lvs, 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, llvm::Value *varlen_output_buffer, int32_t slot_index) const
 

Public Attributes

const Analyzer::Exprtarget_expr
 
TargetInfo target_info
 
int32_t base_slot_index
 
size_t target_idx
 
bool is_group_by
 

Detailed Description

Definition at line 33 of file TargetExprBuilder.h.

Constructor & Destructor Documentation

TargetExprCodegen::TargetExprCodegen ( const Analyzer::Expr target_expr,
TargetInfo target_info,
const int32_t  base_slot_index,
const size_t  target_idx,
const bool  is_group_by 
)
inline

Definition at line 34 of file TargetExprBuilder.h.

39  : target_expr(target_expr)
40  , target_info(target_info)
const Analyzer::Expr * target_expr

Member Function Documentation

void TargetExprCodegen::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,
llvm::Value *  varlen_output_buffer,
DiamondCodegen diamond_codegen,
DiamondCodegen sample_cfg = nullptr 
) const

Definition at line 109 of file TargetExprBuilder.cpp.

References agg_arg(), anonymous_namespace{TargetExprBuilder.cpp}::agg_fn_base_names(), AUTOMATIC_IR_METADATA, base_slot_index, CHECK, CHECK_EQ, CHECK_GE, GroupByAndAggregate::codegenAggArg(), codegenAggregate(), GroupByAndAggregate::codegenWindowRowPointer(), CompilationOptions::device_type, QueryMemoryDescriptor::didOutputColumnar(), GroupByAndAggregate::emitCall(), g_bigint_count, get_int_type(), SQLTypeInfo::get_physical_coord_cols(), QueryMemoryDescriptor::getColOffInBytes(), QueryMemoryDescriptor::getColOnlyOffInBytes(), QueryMemoryDescriptor::getPaddedSlotWidthBytes(), GPU, i, TargetInfo::is_agg, SQLTypeInfo::is_geometry(), is_group_by, anonymous_namespace{TargetExprBuilder.cpp}::is_simple_count(), anonymous_namespace{TargetExprBuilder.cpp}::is_varlen_projection(), GpuSharedMemoryContext::isSharedMemoryUsed(), LL_BUILDER, LL_CONTEXT, LL_INT, LLVM_ALIGN, WindowProjectNodeContext::resetWindowFunctionContext(), TargetInfo::sql_type, target_expr, anonymous_namespace{TargetExprBuilder.cpp}::target_has_geo(), target_idx, target_info, QueryMemoryDescriptor::threadsShareMemory(), and window_function_is_aggregate().

121  {
122  CHECK(group_by_and_agg);
123  CHECK(executor);
124  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
125 
126  auto agg_out_ptr_w_idx = agg_out_ptr_w_idx_in;
127  const auto arg_expr = agg_arg(target_expr);
128 
129  const bool varlen_projection = is_varlen_projection(target_expr, target_info.sql_type);
130  const auto agg_fn_names = agg_fn_base_names(target_info, varlen_projection);
131  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(target_expr);
133  auto target_lvs =
134  window_func
135  ? std::vector<llvm::Value*>{executor->codegenWindowFunction(target_idx, co)}
136  : group_by_and_agg->codegenAggArg(target_expr, co);
137  const auto window_row_ptr = window_func
138  ? group_by_and_agg->codegenWindowRowPointer(
139  window_func, query_mem_desc, co, diamond_codegen)
140  : nullptr;
141  if (window_row_ptr) {
142  agg_out_ptr_w_idx =
143  std::make_tuple(window_row_ptr, std::get<1>(agg_out_ptr_w_idx_in));
144  if (window_function_is_aggregate(window_func->getKind())) {
145  out_row_idx = window_row_ptr;
146  }
147  }
148 
149  llvm::Value* str_target_lv{nullptr};
150  if (target_lvs.size() == 3 && !target_has_geo(target_info)) {
151  // none encoding string, pop the packed pointer + length since
152  // it's only useful for IS NULL checks and assumed to be only
153  // two components (pointer and length) for the purpose of projection
154  str_target_lv = target_lvs.front();
155  target_lvs.erase(target_lvs.begin());
156  }
157  if (target_info.sql_type.is_geometry() && !varlen_projection) {
158  // Geo cols are expanded to the physical coord cols. Each physical coord col is an
159  // array. Ensure that the target values generated match the number of agg
160  // functions before continuing
161  if (target_lvs.size() < agg_fn_names.size()) {
162  CHECK_EQ(target_lvs.size(), agg_fn_names.size() / 2);
163  std::vector<llvm::Value*> new_target_lvs;
164  new_target_lvs.reserve(agg_fn_names.size());
165  for (const auto& target_lv : target_lvs) {
166  new_target_lvs.push_back(target_lv);
167  new_target_lvs.push_back(target_lv);
168  }
169  target_lvs = new_target_lvs;
170  }
171  }
172  if (target_lvs.size() < agg_fn_names.size()) {
173  CHECK_EQ(size_t(1), target_lvs.size());
174  CHECK_EQ(size_t(2), agg_fn_names.size());
175  for (size_t i = 1; i < agg_fn_names.size(); ++i) {
176  target_lvs.push_back(target_lvs.front());
177  }
178  } else {
180  if (!target_info.is_agg && !varlen_projection) {
181  CHECK_EQ(static_cast<size_t>(2 * target_info.sql_type.get_physical_coord_cols()),
182  target_lvs.size());
183  CHECK_EQ(agg_fn_names.size(), target_lvs.size());
184  }
185  } else {
186  CHECK(str_target_lv || (agg_fn_names.size() == target_lvs.size()));
187  CHECK(target_lvs.size() == 1 || target_lvs.size() == 2);
188  }
189  }
190 
191  int32_t slot_index = base_slot_index;
192  CHECK_GE(slot_index, 0);
193  CHECK(is_group_by || static_cast<size_t>(slot_index) < agg_out_vec.size());
194 
195  uint32_t col_off{0};
196  if (co.device_type == ExecutorDeviceType::GPU && query_mem_desc.threadsShareMemory() &&
198  (!arg_expr || arg_expr->get_type_info().get_notnull())) {
199  CHECK_EQ(size_t(1), agg_fn_names.size());
200  const auto chosen_bytes = query_mem_desc.getPaddedSlotWidthBytes(slot_index);
201  llvm::Value* agg_col_ptr{nullptr};
202  if (is_group_by) {
203  if (query_mem_desc.didOutputColumnar()) {
204  col_off = query_mem_desc.getColOffInBytes(slot_index);
205  CHECK_EQ(size_t(0), col_off % chosen_bytes);
206  col_off /= chosen_bytes;
207  CHECK(std::get<1>(agg_out_ptr_w_idx));
208  auto offset =
209  LL_BUILDER.CreateAdd(std::get<1>(agg_out_ptr_w_idx), LL_INT(col_off));
210  auto* bit_cast = LL_BUILDER.CreateBitCast(
211  std::get<0>(agg_out_ptr_w_idx),
212  llvm::PointerType::get(get_int_type((chosen_bytes << 3), LL_CONTEXT), 0));
213  agg_col_ptr = LL_BUILDER.CreateGEP(
214  bit_cast->getType()->getScalarType()->getPointerElementType(),
215  bit_cast,
216  offset);
217  } else {
218  col_off = query_mem_desc.getColOnlyOffInBytes(slot_index);
219  CHECK_EQ(size_t(0), col_off % chosen_bytes);
220  col_off /= chosen_bytes;
221  auto* bit_cast = LL_BUILDER.CreateBitCast(
222  std::get<0>(agg_out_ptr_w_idx),
223  llvm::PointerType::get(get_int_type((chosen_bytes << 3), LL_CONTEXT), 0));
224  agg_col_ptr = LL_BUILDER.CreateGEP(
225  bit_cast->getType()->getScalarType()->getPointerElementType(),
226  bit_cast,
227  LL_INT(col_off));
228  }
229  }
230 
231  if (chosen_bytes != sizeof(int32_t)) {
232  CHECK_EQ(8, chosen_bytes);
233  if (g_bigint_count) {
234  const auto acc_i64 = LL_BUILDER.CreateBitCast(
235  is_group_by ? agg_col_ptr : agg_out_vec[slot_index],
236  llvm::PointerType::get(get_int_type(64, LL_CONTEXT), 0));
237  if (gpu_smem_context.isSharedMemoryUsed()) {
238  group_by_and_agg->emitCall(
239  "agg_count_shared", std::vector<llvm::Value*>{acc_i64, LL_INT(int64_t(1))});
240  } else {
241  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
242  acc_i64,
243  LL_INT(int64_t(1)),
244 #if LLVM_VERSION_MAJOR > 12
245  LLVM_ALIGN(8),
246 #endif
247  llvm::AtomicOrdering::Monotonic);
248  }
249  } else {
250  auto acc_i32 = LL_BUILDER.CreateBitCast(
251  is_group_by ? agg_col_ptr : agg_out_vec[slot_index],
252  llvm::PointerType::get(get_int_type(32, LL_CONTEXT), 0));
253  if (gpu_smem_context.isSharedMemoryUsed()) {
254  acc_i32 = LL_BUILDER.CreatePointerCast(
255  acc_i32, llvm::Type::getInt32PtrTy(LL_CONTEXT, 3));
256  }
257  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
258  acc_i32,
259  LL_INT(1),
260 #if LLVM_VERSION_MAJOR > 12
261  LLVM_ALIGN(4),
262 #endif
263  llvm::AtomicOrdering::Monotonic);
264  }
265  } else {
266  const auto acc_i32 = (is_group_by ? agg_col_ptr : agg_out_vec[slot_index]);
267  if (gpu_smem_context.isSharedMemoryUsed()) {
268  // Atomic operation on address space level 3 (Shared):
269  const auto shared_acc_i32 = LL_BUILDER.CreatePointerCast(
270  acc_i32, llvm::Type::getInt32PtrTy(LL_CONTEXT, 3));
271  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
272  shared_acc_i32,
273  LL_INT(1),
274 #if LLVM_VERSION_MAJOR > 12
275  LLVM_ALIGN(4),
276 #endif
277  llvm::AtomicOrdering::Monotonic);
278  } else {
279  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
280  acc_i32,
281  LL_INT(1),
282 #if LLVM_VERSION_MAJOR > 12
283  LLVM_ALIGN(4),
284 #endif
285  llvm::AtomicOrdering::Monotonic);
286  }
287  }
288  return;
289  }
290 
291  codegenAggregate(group_by_and_agg,
292  executor,
293  query_mem_desc,
294  co,
295  target_lvs,
296  agg_out_ptr_w_idx,
297  agg_out_vec,
298  output_buffer_byte_stream,
299  out_row_idx,
300  varlen_output_buffer,
301  slot_index);
302 }
#define LL_BUILDER
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
#define CHECK_EQ(x, y)
Definition: Logger.h:219
bool target_has_geo(const TargetInfo &target_info)
std::vector< std::string > agg_fn_base_names(const TargetInfo &target_info, const bool is_varlen_projection)
SQLTypeInfo sql_type
Definition: TargetInfo.h:51
#define CHECK_GE(x, y)
Definition: Logger.h:224
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
#define LLVM_ALIGN(alignment)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
size_t getColOnlyOffInBytes(const size_t col_idx) const
bool is_varlen_projection(const Analyzer::Expr *target_expr, const SQLTypeInfo &ti)
bool is_agg
Definition: TargetInfo.h:49
#define LL_INT(v)
bool g_bigint_count
#define LL_CONTEXT
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
#define AUTOMATIC_IR_METADATA(CGENSTATE)
ExecutorDeviceType device_type
bool window_function_is_aggregate(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:42
const Analyzer::Expr * target_expr
std::vector< llvm::Value * > codegenAggArg(const Analyzer::Expr *target_expr, const CompilationOptions &co)
llvm::Value * codegenWindowRowPointer(const Analyzer::WindowFunction *window_func, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, DiamondCodegen &diamond_codegen)
bool is_simple_count(const TargetInfo &target_info)
#define CHECK(condition)
Definition: Logger.h:211
bool is_geometry() const
Definition: sqltypes.h:531
static void resetWindowFunctionContext(Executor *executor)
int get_physical_coord_cols() const
Definition: sqltypes.h:375
size_t getColOffInBytes(const size_t col_idx) const
void codegenAggregate(GroupByAndAggregate *group_by_and_agg, Executor *executor, const QueryMemoryDescriptor &query_mem_desc, const CompilationOptions &co, const std::vector< llvm::Value * > &target_lvs, 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, llvm::Value *varlen_output_buffer, int32_t slot_index) const

+ Here is the call graph for this function:

void TargetExprCodegen::codegenAggregate ( GroupByAndAggregate group_by_and_agg,
Executor executor,
const QueryMemoryDescriptor query_mem_desc,
const CompilationOptions co,
const std::vector< llvm::Value * > &  target_lvs,
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,
llvm::Value *  varlen_output_buffer,
int32_t  slot_index 
) const

Definition at line 304 of file TargetExprBuilder.cpp.

References agg_arg(), TargetInfo::agg_arg_type, anonymous_namespace{TargetExprBuilder.cpp}::agg_fn_base_names(), TargetInfo::agg_kind, AUTOMATIC_IR_METADATA, CHECK, CHECK_EQ, CHECK_GE, CHECK_LT, GroupByAndAggregate::checkErrorCode(), GroupByAndAggregate::codegenAggColumnPtr(), GroupByAndAggregate::codegenApproxQuantile(), GroupByAndAggregate::codegenCountDistinct(), DiamondCodegen::cond_false_, DiamondCodegen::cond_true_, GroupByAndAggregate::convertNullIfAny(), CompilationOptions::device_type, QueryMemoryDescriptor::didOutputColumnar(), GroupByAndAggregate::emitCall(), get_arg_by_name(), get_compact_type(), SQLTypeInfo::get_compression(), get_int_type(), SQLTypeInfo::get_type(), WindowProjectNodeContext::getActiveWindowFunctionContext(), QueryMemoryDescriptor::getColOnlyOffInBytes(), QueryMemoryDescriptor::getLogicalSlotWidthBytes(), QueryMemoryDescriptor::getPaddedSlotWidthBytes(), GPU, is_agg_domain_range_equivalent(), TargetInfo::is_distinct, is_distinct_target(), is_group_by, anonymous_namespace{TargetExprBuilder.cpp}::is_simple_count(), anonymous_namespace{TargetExprBuilder.cpp}::is_varlen_projection(), QueryMemoryDescriptor::isLogicalSizedColumnsAllowed(), kAPPROX_QUANTILE, kAVG, kDOUBLE, kENCODING_GEOINT, kFLOAT, kNULLT, kPOINT, kSINGLE_VALUE, LL_BUILDER, LL_CONTEXT, LL_INT, GroupByAndAggregate::needsUnnestDoublePatch(), numeric_type_name(), patch_agg_fname(), CodeGenerator::posArg(), ROW_FUNC, TargetInfo::skip_null_val, TargetInfo::sql_type, takes_float_argument(), target_expr, anonymous_namespace{TargetExprBuilder.cpp}::target_has_geo(), target_idx, target_info, QueryMemoryDescriptor::threadsShareMemory(), to_string(), QueryMemoryDescriptor::varlenOutputBufferElemSize(), QueryMemoryDescriptor::varlenOutputRowSizeToSlot(), and window_function_requires_peer_handling().

Referenced by codegen().

315  {
316  AUTOMATIC_IR_METADATA(executor->cgen_state_.get());
317  size_t target_lv_idx = 0;
318  const bool lazy_fetched{executor->plan_state_->isLazyFetchColumn(target_expr)};
319 
320  CodeGenerator code_generator(executor);
321 
322  const auto agg_fn_names = agg_fn_base_names(
324  auto arg_expr = agg_arg(target_expr);
325 
326  for (const auto& agg_base_name : agg_fn_names) {
327  if (target_info.is_distinct && arg_expr->get_type_info().is_array()) {
328  CHECK_EQ(static_cast<size_t>(query_mem_desc.getLogicalSlotWidthBytes(slot_index)),
329  sizeof(int64_t));
330  // TODO(miyu): check if buffer may be columnar here
331  CHECK(!query_mem_desc.didOutputColumnar());
332  const auto& elem_ti = arg_expr->get_type_info().get_elem_type();
333  uint32_t col_off{0};
334  if (is_group_by) {
335  const auto col_off_in_bytes = query_mem_desc.getColOnlyOffInBytes(slot_index);
336  CHECK_EQ(size_t(0), col_off_in_bytes % sizeof(int64_t));
337  col_off /= sizeof(int64_t);
338  }
339  executor->cgen_state_->emitExternalCall(
340  "agg_count_distinct_array_" + numeric_type_name(elem_ti),
341  llvm::Type::getVoidTy(LL_CONTEXT),
342  {is_group_by ? LL_BUILDER.CreateGEP(std::get<0>(agg_out_ptr_w_idx)
343  ->getType()
344  ->getScalarType()
345  ->getPointerElementType(),
346  std::get<0>(agg_out_ptr_w_idx),
347  LL_INT(col_off))
348  : agg_out_vec[slot_index],
349  target_lvs[target_lv_idx],
350  code_generator.posArg(arg_expr),
351  elem_ti.is_fp()
352  ? static_cast<llvm::Value*>(executor->cgen_state_->inlineFpNull(elem_ti))
353  : static_cast<llvm::Value*>(
354  executor->cgen_state_->inlineIntNull(elem_ti))});
355  ++slot_index;
356  ++target_lv_idx;
357  continue;
358  }
359 
360  llvm::Value* agg_col_ptr{nullptr};
361  const auto chosen_bytes =
362  static_cast<size_t>(query_mem_desc.getPaddedSlotWidthBytes(slot_index));
363  const auto& chosen_type = get_compact_type(target_info);
364  const auto& arg_type =
365  ((arg_expr && arg_expr->get_type_info().get_type() != kNULLT) &&
369  const bool is_fp_arg =
370  !lazy_fetched && arg_type.get_type() != kNULLT && arg_type.is_fp();
371  if (is_group_by) {
372  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
373  out_row_idx,
374  agg_out_ptr_w_idx,
375  query_mem_desc,
376  chosen_bytes,
377  slot_index,
378  target_idx);
379  CHECK(agg_col_ptr);
380  agg_col_ptr->setName("agg_col_ptr");
381  }
382 
384  CHECK(!query_mem_desc.didOutputColumnar());
385 
387  CHECK_LT(target_lv_idx, target_lvs.size());
388  CHECK(varlen_output_buffer);
389  auto target_lv = target_lvs[target_lv_idx];
390 
391  std::string agg_fname_suffix = "";
393  query_mem_desc.threadsShareMemory()) {
394  agg_fname_suffix += "_shared";
395  }
396 
397  // first write the varlen data into the varlen buffer and get the pointer location
398  // into the varlen buffer
399  auto& builder = executor->cgen_state_->ir_builder_;
400  auto orig_bb = builder.GetInsertBlock();
401  auto target_ptr_type = llvm::dyn_cast<llvm::PointerType>(target_lv->getType());
402  CHECK(target_ptr_type) << "Varlen projections expect a pointer input.";
403  auto is_nullptr =
404  builder.CreateICmp(llvm::CmpInst::ICMP_EQ,
405  target_lv,
406  llvm::ConstantPointerNull::get(llvm::PointerType::get(
407  target_ptr_type->getPointerElementType(), 0)));
408  llvm::BasicBlock* true_bb{nullptr};
409  {
410  DiamondCodegen nullcheck_diamond(
411  is_nullptr, executor, false, "varlen_null_check", nullptr, false);
412  // maintain a reference to the true bb, overriding the diamond codegen destructor
413  true_bb = nullcheck_diamond.cond_true_;
414  // if not null, process the pointer and insert it into the varlen buffer
415  builder.SetInsertPoint(nullcheck_diamond.cond_false_);
416  auto arr_ptr_lv = executor->cgen_state_->ir_builder_.CreateBitCast(
417  target_lv,
418  llvm::PointerType::get(get_int_type(8, executor->cgen_state_->context_), 0));
419  const int64_t chosen_bytes =
421  auto* arg = get_arg_by_name(ROW_FUNC, "old_total_matched");
422  const auto output_buffer_slot = LL_BUILDER.CreateZExt(
423  LL_BUILDER.CreateLoad(arg->getType()->getPointerElementType(), arg),
424  llvm::Type::getInt64Ty(LL_CONTEXT));
425  const auto varlen_buffer_row_sz = query_mem_desc.varlenOutputBufferElemSize();
426  CHECK(varlen_buffer_row_sz);
427  const auto output_buffer_slot_bytes = LL_BUILDER.CreateAdd(
428  LL_BUILDER.CreateMul(output_buffer_slot,
429  executor->cgen_state_->llInt(
430  static_cast<int64_t>(*varlen_buffer_row_sz))),
431  executor->cgen_state_->llInt(static_cast<int64_t>(
432  query_mem_desc.varlenOutputRowSizeToSlot(slot_index))));
433 
434  std::vector<llvm::Value*> varlen_agg_args{
435  executor->castToIntPtrTyIn(varlen_output_buffer, 8),
436  output_buffer_slot_bytes,
437  arr_ptr_lv,
438  executor->cgen_state_->llInt(chosen_bytes)};
439  auto varlen_offset_ptr =
440  group_by_and_agg->emitCall(agg_base_name + agg_fname_suffix, varlen_agg_args);
441 
442  // then write that pointer location into the 64 bit slot in the output buffer
443  auto varlen_offset_int = LL_BUILDER.CreatePtrToInt(
444  varlen_offset_ptr, llvm::Type::getInt64Ty(LL_CONTEXT));
445  builder.CreateBr(nullcheck_diamond.cond_true_);
446 
447  // use the true block to do the output buffer insertion regardless of nullness
448  builder.SetInsertPoint(nullcheck_diamond.cond_true_);
449  auto output_phi =
450  builder.CreatePHI(llvm::Type::getInt64Ty(executor->cgen_state_->context_), 2);
451  output_phi->addIncoming(varlen_offset_int, nullcheck_diamond.cond_false_);
452  output_phi->addIncoming(executor->cgen_state_->llInt(static_cast<int64_t>(0)),
453  orig_bb);
454 
455  std::vector<llvm::Value*> agg_args{agg_col_ptr, output_phi};
456  group_by_and_agg->emitCall("agg_id" + agg_fname_suffix, agg_args);
457  }
458  CHECK(true_bb);
459  builder.SetInsertPoint(true_bb);
460 
461  ++slot_index;
462  ++target_lv_idx;
463  continue;
464  }
465 
466  const bool float_argument_input = takes_float_argument(target_info);
467  const bool is_count_in_avg = target_info.agg_kind == kAVG && target_lv_idx == 1;
468  // The count component of an average should never be compacted.
469  const auto agg_chosen_bytes =
470  float_argument_input && !is_count_in_avg ? sizeof(float) : chosen_bytes;
471  if (float_argument_input) {
472  CHECK_GE(chosen_bytes, sizeof(float));
473  }
474 
475  auto target_lv = target_lvs[target_lv_idx];
476  const auto needs_unnest_double_patch = group_by_and_agg->needsUnnestDoublePatch(
477  target_lv, agg_base_name, query_mem_desc.threadsShareMemory(), co);
478  const auto need_skip_null = !needs_unnest_double_patch && target_info.skip_null_val;
479  if (!needs_unnest_double_patch) {
480  if (need_skip_null && !is_agg_domain_range_equivalent(target_info.agg_kind)) {
481  target_lv = group_by_and_agg->convertNullIfAny(arg_type, target_info, target_lv);
482  } else if (is_fp_arg) {
483  target_lv = executor->castToFP(target_lv, arg_type, target_info.sql_type);
484  }
485  if (!dynamic_cast<const Analyzer::AggExpr*>(target_expr) || arg_expr) {
486  target_lv =
487  executor->cgen_state_->castToTypeIn(target_lv, (agg_chosen_bytes << 3));
488  }
489  }
490 
491  const bool is_simple_count_target = is_simple_count(target_info);
492  llvm::Value* str_target_lv{nullptr};
493  if (target_lvs.size() == 3 && !target_has_geo(target_info)) {
494  // none encoding string
495  str_target_lv = target_lvs.front();
496  }
497  std::vector<llvm::Value*> agg_args{
498  executor->castToIntPtrTyIn((is_group_by ? agg_col_ptr : agg_out_vec[slot_index]),
499  (agg_chosen_bytes << 3)),
500  (is_simple_count_target && !arg_expr)
501  ? (agg_chosen_bytes == sizeof(int32_t) ? LL_INT(int32_t(0))
502  : LL_INT(int64_t(0)))
503  : (is_simple_count_target && arg_expr && str_target_lv ? str_target_lv
504  : target_lv)};
505  if (query_mem_desc.isLogicalSizedColumnsAllowed()) {
506  if (is_simple_count_target && arg_expr && str_target_lv) {
507  agg_args[1] =
508  agg_chosen_bytes == sizeof(int32_t) ? LL_INT(int32_t(0)) : LL_INT(int64_t(0));
509  }
510  }
511  std::string agg_fname{agg_base_name};
512  if (is_fp_arg) {
513  if (!lazy_fetched) {
514  if (agg_chosen_bytes == sizeof(float)) {
515  CHECK_EQ(arg_type.get_type(), kFLOAT);
516  agg_fname += "_float";
517  } else {
518  CHECK_EQ(agg_chosen_bytes, sizeof(double));
519  agg_fname += "_double";
520  }
521  }
522  } else if (agg_chosen_bytes == sizeof(int32_t)) {
523  agg_fname += "_int32";
524  } else if (agg_chosen_bytes == sizeof(int16_t) &&
525  query_mem_desc.didOutputColumnar()) {
526  agg_fname += "_int16";
527  } else if (agg_chosen_bytes == sizeof(int8_t) && query_mem_desc.didOutputColumnar()) {
528  agg_fname += "_int8";
529  }
530 
532  CHECK_EQ(agg_chosen_bytes, sizeof(int64_t));
533  CHECK(!chosen_type.is_fp());
534  group_by_and_agg->codegenCountDistinct(
535  target_idx, target_expr, agg_args, query_mem_desc, co.device_type);
536  } else if (target_info.agg_kind == kAPPROX_QUANTILE) {
537  CHECK_EQ(agg_chosen_bytes, sizeof(int64_t));
538  group_by_and_agg->codegenApproxQuantile(
539  target_idx, target_expr, agg_args, query_mem_desc, co.device_type);
540  } else {
541  const auto& arg_ti = target_info.agg_arg_type;
542  if (need_skip_null && !arg_ti.is_geometry()) {
543  agg_fname += "_skip_val";
544  }
545 
547  (need_skip_null && !arg_ti.is_geometry())) {
548  llvm::Value* null_in_lv{nullptr};
549  if (arg_ti.is_fp()) {
550  null_in_lv =
551  static_cast<llvm::Value*>(executor->cgen_state_->inlineFpNull(arg_ti));
552  } else {
553  null_in_lv = static_cast<llvm::Value*>(executor->cgen_state_->inlineIntNull(
555  ? arg_ti
556  : target_info.sql_type));
557  }
558  CHECK(null_in_lv);
559  auto null_lv =
560  executor->cgen_state_->castToTypeIn(null_in_lv, (agg_chosen_bytes << 3));
561  agg_args.push_back(null_lv);
562  }
563  if (!target_info.is_distinct) {
565  query_mem_desc.threadsShareMemory()) {
566  agg_fname += "_shared";
567  if (needs_unnest_double_patch) {
568  agg_fname = patch_agg_fname(agg_fname);
569  }
570  }
571  auto agg_fname_call_ret_lv = group_by_and_agg->emitCall(agg_fname, agg_args);
572 
573  if (agg_fname.find("checked") != std::string::npos) {
574  group_by_and_agg->checkErrorCode(agg_fname_call_ret_lv);
575  }
576  }
577  }
578  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(target_expr);
579  if (window_func && window_function_requires_peer_handling(window_func)) {
580  const auto window_func_context =
582  const auto pending_outputs =
583  LL_INT(window_func_context->aggregateStatePendingOutputs());
584  executor->cgen_state_->emitExternalCall("add_window_pending_output",
585  llvm::Type::getVoidTy(LL_CONTEXT),
586  {agg_args.front(), pending_outputs});
587  const auto& window_func_ti = window_func->get_type_info();
588  std::string apply_window_pending_outputs_name = "apply_window_pending_outputs";
589  switch (window_func_ti.get_type()) {
590  case kFLOAT: {
591  apply_window_pending_outputs_name += "_float";
592  if (query_mem_desc.didOutputColumnar()) {
593  apply_window_pending_outputs_name += "_columnar";
594  }
595  break;
596  }
597  case kDOUBLE: {
598  apply_window_pending_outputs_name += "_double";
599  break;
600  }
601  default: {
602  apply_window_pending_outputs_name += "_int";
603  if (query_mem_desc.didOutputColumnar()) {
604  apply_window_pending_outputs_name +=
605  std::to_string(window_func_ti.get_size() * 8);
606  } else {
607  apply_window_pending_outputs_name += "64";
608  }
609  break;
610  }
611  }
612  const auto partition_end =
613  LL_INT(reinterpret_cast<int64_t>(window_func_context->partitionEnd()));
614  executor->cgen_state_->emitExternalCall(apply_window_pending_outputs_name,
615  llvm::Type::getVoidTy(LL_CONTEXT),
616  {pending_outputs,
617  target_lvs.front(),
618  partition_end,
619  code_generator.posArg(nullptr)});
620  }
621 
622  ++slot_index;
623  ++target_lv_idx;
624  }
625 }
size_t varlenOutputRowSizeToSlot(const size_t slot_idx) const
#define LL_BUILDER
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
#define CHECK_EQ(x, y)
Definition: Logger.h:219
bool target_has_geo(const TargetInfo &target_info)
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.
std::vector< std::string > agg_fn_base_names(const TargetInfo &target_info, const bool is_varlen_projection)
bool isLogicalSizedColumnsAllowed() const
SQLTypeInfo sql_type
Definition: TargetInfo.h:51
#define CHECK_GE(x, y)
Definition: Logger.h:224
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
void codegenApproxQuantile(const size_t target_idx, const Analyzer::Expr *target_expr, std::vector< llvm::Value * > &agg_args, const QueryMemoryDescriptor &query_mem_desc, const ExecutorDeviceType device_type)
void checkErrorCode(llvm::Value *retCode)
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:157
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:329
bool needsUnnestDoublePatch(llvm::Value const *val_ptr, const std::string &agg_base_name, const bool threads_share_memory, const CompilationOptions &co) const
bool skip_null_val
Definition: TargetInfo.h:53
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
static WindowFunctionContext * getActiveWindowFunctionContext(Executor *executor)
std::string to_string(char const *&&v)
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:52
std::string patch_agg_fname(const std::string &agg_name)
size_t getColOnlyOffInBytes(const size_t col_idx) const
const SQLTypeInfo get_compact_type(const TargetInfo &target)
bool is_varlen_projection(const Analyzer::Expr *target_expr, const SQLTypeInfo &ti)
llvm::Value * get_arg_by_name(llvm::Function *func, const std::string &name)
Definition: Execute.h:167
#define LL_INT(v)
llvm::Value * convertNullIfAny(const SQLTypeInfo &arg_type, const TargetInfo &agg_info, llvm::Value *target)
#define LL_CONTEXT
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:153
void codegenCountDistinct(const size_t target_idx, const Analyzer::Expr *target_expr, std::vector< llvm::Value * > &agg_args, const QueryMemoryDescriptor &, const ExecutorDeviceType)
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
#define AUTOMATIC_IR_METADATA(CGENSTATE)
SQLAgg agg_kind
Definition: TargetInfo.h:50
ExecutorDeviceType device_type
std::optional< size_t > varlenOutputBufferElemSize() const
#define CHECK_LT(x, y)
Definition: Logger.h:221
HOST DEVICE EncodingType get_compression() const
Definition: sqltypes.h:337
const Analyzer::Expr * target_expr
bool window_function_requires_peer_handling(const Analyzer::WindowFunction *window_func)
bool is_simple_count(const TargetInfo &target_info)
#define CHECK(condition)
Definition: Logger.h:211
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:210
bool is_distinct
Definition: TargetInfo.h:54
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
Definition: sqldefs.h:72
#define ROW_FUNC
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:78

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

Member Data Documentation

int32_t TargetExprCodegen::base_slot_index

Definition at line 73 of file TargetExprBuilder.h.

Referenced by codegen().

const Analyzer::Expr* TargetExprCodegen::target_expr

Definition at line 70 of file TargetExprBuilder.h.

Referenced by codegen(), and codegenAggregate().

size_t TargetExprCodegen::target_idx

Definition at line 74 of file TargetExprBuilder.h.

Referenced by codegen(), and codegenAggregate().

TargetInfo TargetExprCodegen::target_info

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