OmniSciDB  b24e664e58
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros 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 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, GroupByAndAggregate::DiamondCodegen *sample_cfg=nullptr) 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 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,
GroupByAndAggregate::DiamondCodegen sample_cfg = nullptr 
) const

Definition at line 81 of file TargetExprBuilder.cpp.

References agg_arg(), TargetInfo::agg_arg_type, anonymous_namespace{TargetExprBuilder.cpp}::agg_fn_base_names(), TargetInfo::agg_kind, base_slot_index, CHECK(), CHECK_EQ, CHECK_GE, GroupByAndAggregate::codegenAggArg(), GroupByAndAggregate::codegenAggColumnPtr(), GroupByAndAggregate::codegenCountDistinct(), GroupByAndAggregate::codegenWindowRowPointer(), GroupByAndAggregate::convertNullIfAny(), CompilationOptions::device_type_, QueryMemoryDescriptor::didOutputColumnar(), GroupByAndAggregate::emitCall(), g_bigint_count, get_compact_type(), get_int_type(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_physical_coord_cols(), SQLTypeInfoCore< TYPE_FACET_PACK >::get_type(), WindowProjectNodeContext::getActiveWindowFunctionContext(), QueryMemoryDescriptor::getColOffInBytes(), QueryMemoryDescriptor::getColOnlyOffInBytes(), QueryMemoryDescriptor::getGpuMemSharing(), QueryMemoryDescriptor::getLogicalSlotWidthBytes(), QueryMemoryDescriptor::getPaddedSlotWidthBytes(), GPU, TargetInfo::is_agg, is_agg_domain_range_equivalent(), TargetInfo::is_distinct, is_distinct_target(), SQLTypeInfoCore< TYPE_FACET_PACK >::is_geometry(), is_group_by, QueryMemoryDescriptor::isLogicalSizedColumnsAllowed(), kAVG, kCOUNT, kDOUBLE, kFLOAT, kNULLT, LL_BUILDER, LL_CONTEXT, LL_INT, GroupByAndAggregate::needsUnnestDoublePatch(), numeric_type_name(), patch_agg_fname(), CodeGenerator::posArg(), WindowProjectNodeContext::resetWindowFunctionContext(), SharedForKeylessOneColumnKnownRange, TargetInfo::skip_null_val, TargetInfo::sql_type, takes_float_argument(), target_expr, target_idx, target_info, QueryMemoryDescriptor::threadsShareMemory(), to_string(), window_function_is_aggregate(), and window_function_requires_peer_handling().

91  {
92  CHECK(group_by_and_agg);
93  CHECK(executor);
94 
95  auto agg_out_ptr_w_idx = agg_out_ptr_w_idx_in;
96  const auto arg_expr = agg_arg(target_expr);
97 
98  const auto agg_fn_names = agg_fn_base_names(target_info);
99  const auto window_func = dynamic_cast<const Analyzer::WindowFunction*>(target_expr);
101  auto target_lvs =
102  window_func
103  ? std::vector<llvm::Value*>{executor->codegenWindowFunction(target_idx, co)}
104  : group_by_and_agg->codegenAggArg(target_expr, co);
105  const auto window_row_ptr = window_func
106  ? group_by_and_agg->codegenWindowRowPointer(
107  window_func, query_mem_desc, co, diamond_codegen)
108  : nullptr;
109  if (window_row_ptr) {
110  agg_out_ptr_w_idx =
111  std::make_tuple(window_row_ptr, std::get<1>(agg_out_ptr_w_idx_in));
112  if (window_function_is_aggregate(window_func->getKind())) {
113  out_row_idx = window_row_ptr;
114  }
115  }
116 
117  llvm::Value* str_target_lv{nullptr};
118  const bool target_has_geo = target_info.is_agg ? target_info.agg_arg_type.is_geometry()
120  if (target_lvs.size() == 3 && !target_has_geo) {
121  // none encoding string, pop the packed pointer + length since
122  // it's only useful for IS NULL checks and assumed to be only
123  // two components (pointer and length) for the purpose of projection
124  str_target_lv = target_lvs.front();
125  target_lvs.erase(target_lvs.begin());
126  }
128  // Geo cols are expanded to the physical coord cols. Each physical coord col is an
129  // array. Ensure that the target values generated match the number of agg
130  // functions before continuing
131  if (target_lvs.size() < agg_fn_names.size()) {
132  CHECK_EQ(target_lvs.size(), agg_fn_names.size() / 2);
133  std::vector<llvm::Value*> new_target_lvs;
134  new_target_lvs.reserve(agg_fn_names.size());
135  for (const auto& target_lv : target_lvs) {
136  new_target_lvs.push_back(target_lv);
137  new_target_lvs.push_back(target_lv);
138  }
139  target_lvs = new_target_lvs;
140  }
141  }
142  if (target_lvs.size() < agg_fn_names.size()) {
143  CHECK_EQ(size_t(1), target_lvs.size());
144  CHECK_EQ(size_t(2), agg_fn_names.size());
145  for (size_t i = 1; i < agg_fn_names.size(); ++i) {
146  target_lvs.push_back(target_lvs.front());
147  }
148  } else {
149  if (target_has_geo) {
150  if (!target_info.is_agg) {
151  CHECK_EQ(static_cast<size_t>(2 * target_info.sql_type.get_physical_coord_cols()),
152  target_lvs.size());
153  CHECK_EQ(agg_fn_names.size(), target_lvs.size());
154  }
155  } else {
156  CHECK(str_target_lv || (agg_fn_names.size() == target_lvs.size()));
157  CHECK(target_lvs.size() == 1 || target_lvs.size() == 2);
158  }
159  }
160 
161  int32_t slot_index = base_slot_index;
162  CHECK_GE(slot_index, 0);
163  CHECK(is_group_by || static_cast<size_t>(slot_index) < agg_out_vec.size());
164 
165  uint32_t col_off{0};
166  const bool is_simple_count =
168  if (co.device_type_ == ExecutorDeviceType::GPU && query_mem_desc.threadsShareMemory() &&
169  is_simple_count && (!arg_expr || arg_expr->get_type_info().get_notnull())) {
170  CHECK_EQ(size_t(1), agg_fn_names.size());
171  const auto chosen_bytes = query_mem_desc.getPaddedSlotWidthBytes(slot_index);
172  llvm::Value* agg_col_ptr{nullptr};
173  if (is_group_by) {
174  if (query_mem_desc.didOutputColumnar()) {
175  col_off = query_mem_desc.getColOffInBytes(slot_index);
176  CHECK_EQ(size_t(0), col_off % chosen_bytes);
177  col_off /= chosen_bytes;
178  CHECK(std::get<1>(agg_out_ptr_w_idx));
179  auto offset =
180  LL_BUILDER.CreateAdd(std::get<1>(agg_out_ptr_w_idx), LL_INT(col_off));
181  agg_col_ptr = LL_BUILDER.CreateGEP(
182  LL_BUILDER.CreateBitCast(
183  std::get<0>(agg_out_ptr_w_idx),
184  llvm::PointerType::get(get_int_type((chosen_bytes << 3), LL_CONTEXT), 0)),
185  offset);
186  } else {
187  col_off = query_mem_desc.getColOnlyOffInBytes(slot_index);
188  CHECK_EQ(size_t(0), col_off % chosen_bytes);
189  col_off /= chosen_bytes;
190  agg_col_ptr = LL_BUILDER.CreateGEP(
191  LL_BUILDER.CreateBitCast(
192  std::get<0>(agg_out_ptr_w_idx),
193  llvm::PointerType::get(get_int_type((chosen_bytes << 3), LL_CONTEXT), 0)),
194  LL_INT(col_off));
195  }
196  }
197 
198  if (chosen_bytes != sizeof(int32_t)) {
199  CHECK_EQ(8, chosen_bytes);
200  if (g_bigint_count) {
201  const auto acc_i64 = LL_BUILDER.CreateBitCast(
202  is_group_by ? agg_col_ptr : agg_out_vec[slot_index],
203  llvm::PointerType::get(get_int_type(64, LL_CONTEXT), 0));
204  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
205  acc_i64,
206  LL_INT(int64_t(1)),
207  llvm::AtomicOrdering::Monotonic);
208  } else {
209  const auto acc_i32 = LL_BUILDER.CreateBitCast(
210  is_group_by ? agg_col_ptr : agg_out_vec[slot_index],
211  llvm::PointerType::get(get_int_type(32, LL_CONTEXT), 0));
212  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
213  acc_i32,
214  LL_INT(1),
215  llvm::AtomicOrdering::Monotonic);
216  }
217  } else {
218  const auto acc_i32 = (is_group_by ? agg_col_ptr : agg_out_vec[slot_index]);
219  if (query_mem_desc.getGpuMemSharing() ==
221  // Atomic operation on address space level 3 (Shared):
222  const auto shared_acc_i32 = LL_BUILDER.CreatePointerCast(
223  acc_i32, llvm::Type::getInt32PtrTy(LL_CONTEXT, 3));
224  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
225  shared_acc_i32,
226  LL_INT(1),
227  llvm::AtomicOrdering::Monotonic);
228  } else {
229  LL_BUILDER.CreateAtomicRMW(llvm::AtomicRMWInst::Add,
230  acc_i32,
231  LL_INT(1),
232  llvm::AtomicOrdering::Monotonic);
233  }
234  }
235  return;
236  }
237 
238  size_t target_lv_idx = 0;
239  const bool lazy_fetched{executor->plan_state_->isLazyFetchColumn(target_expr)};
240 
241  CodeGenerator code_generator(executor);
242  for (const auto& agg_base_name : agg_fn_names) {
243  if (target_info.is_distinct && arg_expr->get_type_info().is_array()) {
244  CHECK_EQ(static_cast<size_t>(query_mem_desc.getLogicalSlotWidthBytes(slot_index)),
245  sizeof(int64_t));
246  // TODO(miyu): check if buffer may be columnar here
247  CHECK(!query_mem_desc.didOutputColumnar());
248  const auto& elem_ti = arg_expr->get_type_info().get_elem_type();
249  if (is_group_by) {
250  col_off = query_mem_desc.getColOnlyOffInBytes(slot_index);
251  CHECK_EQ(size_t(0), col_off % sizeof(int64_t));
252  col_off /= sizeof(int64_t);
253  }
254  executor->cgen_state_->emitExternalCall(
255  "agg_count_distinct_array_" + numeric_type_name(elem_ti),
256  llvm::Type::getVoidTy(LL_CONTEXT),
257  {is_group_by
258  ? LL_BUILDER.CreateGEP(std::get<0>(agg_out_ptr_w_idx), LL_INT(col_off))
259  : agg_out_vec[slot_index],
260  target_lvs[target_lv_idx],
261  code_generator.posArg(arg_expr),
262  elem_ti.is_fp()
263  ? static_cast<llvm::Value*>(executor->cgen_state_->inlineFpNull(elem_ti))
264  : static_cast<llvm::Value*>(
265  executor->cgen_state_->inlineIntNull(elem_ti))});
266  ++slot_index;
267  ++target_lv_idx;
268  continue;
269  }
270 
271  llvm::Value* agg_col_ptr{nullptr};
272  const auto chosen_bytes =
273  static_cast<size_t>(query_mem_desc.getPaddedSlotWidthBytes(slot_index));
274  const auto& chosen_type = get_compact_type(target_info);
275  const auto& arg_type =
276  ((arg_expr && arg_expr->get_type_info().get_type() != kNULLT) &&
280  const bool is_fp_arg =
281  !lazy_fetched && arg_type.get_type() != kNULLT && arg_type.is_fp();
282  if (is_group_by) {
283  agg_col_ptr = group_by_and_agg->codegenAggColumnPtr(output_buffer_byte_stream,
284  out_row_idx,
285  agg_out_ptr_w_idx,
286  query_mem_desc,
287  chosen_bytes,
288  slot_index,
289  target_idx);
290  CHECK(agg_col_ptr);
291  agg_col_ptr->setName("agg_col_ptr");
292  }
293 
294  const bool float_argument_input = takes_float_argument(target_info);
295  const bool is_count_in_avg = target_info.agg_kind == kAVG && target_lv_idx == 1;
296  // The count component of an average should never be compacted.
297  const auto agg_chosen_bytes =
298  float_argument_input && !is_count_in_avg ? sizeof(float) : chosen_bytes;
299  if (float_argument_input) {
300  CHECK_GE(chosen_bytes, sizeof(float));
301  }
302 
303  auto target_lv = target_lvs[target_lv_idx];
304  const auto needs_unnest_double_patch = group_by_and_agg->needsUnnestDoublePatch(
305  target_lv, agg_base_name, query_mem_desc.threadsShareMemory(), co);
306  const auto need_skip_null = !needs_unnest_double_patch && target_info.skip_null_val;
307  if (!needs_unnest_double_patch) {
308  if (need_skip_null && !is_agg_domain_range_equivalent(target_info.agg_kind)) {
309  target_lv = group_by_and_agg->convertNullIfAny(arg_type, target_info, target_lv);
310  } else if (is_fp_arg) {
311  target_lv = executor->castToFP(target_lv);
312  }
313  if (!dynamic_cast<const Analyzer::AggExpr*>(target_expr) || arg_expr) {
314  target_lv =
315  executor->cgen_state_->castToTypeIn(target_lv, (agg_chosen_bytes << 3));
316  }
317  }
318 
319  std::vector<llvm::Value*> agg_args{
320  executor->castToIntPtrTyIn((is_group_by ? agg_col_ptr : agg_out_vec[slot_index]),
321  (agg_chosen_bytes << 3)),
322  (is_simple_count && !arg_expr)
323  ? (agg_chosen_bytes == sizeof(int32_t) ? LL_INT(int32_t(0))
324  : LL_INT(int64_t(0)))
325  : (is_simple_count && arg_expr && str_target_lv ? str_target_lv : target_lv)};
326  if (query_mem_desc.isLogicalSizedColumnsAllowed()) {
327  if (is_simple_count && arg_expr && str_target_lv) {
328  agg_args[1] =
329  agg_chosen_bytes == sizeof(int32_t) ? LL_INT(int32_t(0)) : LL_INT(int64_t(0));
330  }
331  }
332  std::string agg_fname{agg_base_name};
333  if (is_fp_arg) {
334  if (!lazy_fetched) {
335  if (agg_chosen_bytes == sizeof(float)) {
336  CHECK_EQ(arg_type.get_type(), kFLOAT);
337  agg_fname += "_float";
338  } else {
339  CHECK_EQ(agg_chosen_bytes, sizeof(double));
340  agg_fname += "_double";
341  }
342  }
343  } else if (agg_chosen_bytes == sizeof(int32_t)) {
344  agg_fname += "_int32";
345  } else if (agg_chosen_bytes == sizeof(int16_t) &&
346  query_mem_desc.didOutputColumnar()) {
347  agg_fname += "_int16";
348  } else if (agg_chosen_bytes == sizeof(int8_t) && query_mem_desc.didOutputColumnar()) {
349  agg_fname += "_int8";
350  }
351 
353  CHECK_EQ(agg_chosen_bytes, sizeof(int64_t));
354  CHECK(!chosen_type.is_fp());
355  group_by_and_agg->codegenCountDistinct(
356  target_idx, target_expr, agg_args, query_mem_desc, co.device_type_);
357  } else {
358  const auto& arg_ti = target_info.agg_arg_type;
359  if (need_skip_null && !arg_ti.is_geometry()) {
360  agg_fname += "_skip_val";
361  llvm::Value* null_in_lv{nullptr};
362  if (arg_ti.is_fp()) {
363  null_in_lv =
364  static_cast<llvm::Value*>(executor->cgen_state_->inlineFpNull(arg_ti));
365  } else {
366  null_in_lv = static_cast<llvm::Value*>(executor->cgen_state_->inlineIntNull(
368  ? arg_ti
369  : target_info.sql_type));
370  }
371  CHECK(null_in_lv);
372  auto null_lv =
373  executor->cgen_state_->castToTypeIn(null_in_lv, (agg_chosen_bytes << 3));
374  agg_args.push_back(null_lv);
375  }
376  if (!target_info.is_distinct) {
378  query_mem_desc.threadsShareMemory()) {
379  agg_fname += "_shared";
380  if (needs_unnest_double_patch) {
381  agg_fname = patch_agg_fname(agg_fname);
382  }
383  }
384  group_by_and_agg->emitCall(agg_fname, agg_args);
385  }
386  }
387  if (window_func && window_function_requires_peer_handling(window_func)) {
388  const auto window_func_context =
390  const auto pending_outputs =
391  LL_INT(window_func_context->aggregateStatePendingOutputs());
392  executor->cgen_state_->emitExternalCall("add_window_pending_output",
393  llvm::Type::getVoidTy(LL_CONTEXT),
394  {agg_args.front(), pending_outputs});
395  const auto& window_func_ti = window_func->get_type_info();
396  std::string apply_window_pending_outputs_name = "apply_window_pending_outputs";
397  switch (window_func_ti.get_type()) {
398  case kFLOAT: {
399  apply_window_pending_outputs_name += "_float";
400  if (query_mem_desc.didOutputColumnar()) {
401  apply_window_pending_outputs_name += "_columnar";
402  }
403  break;
404  }
405  case kDOUBLE: {
406  apply_window_pending_outputs_name += "_double";
407  break;
408  }
409  default: {
410  apply_window_pending_outputs_name += "_int";
411  if (query_mem_desc.didOutputColumnar()) {
412  apply_window_pending_outputs_name +=
413  std::to_string(window_func_ti.get_size() * 8);
414  } else {
415  apply_window_pending_outputs_name += "64";
416  }
417  break;
418  }
419  }
420  const auto partition_end =
421  LL_INT(reinterpret_cast<int64_t>(window_func_context->partitionEnd()));
422  executor->cgen_state_->emitExternalCall(apply_window_pending_outputs_name,
423  llvm::Type::getVoidTy(LL_CONTEXT),
424  {pending_outputs,
425  target_lvs.front(),
426  partition_end,
427  code_generator.posArg(nullptr)});
428  }
429 
430  ++slot_index;
431  ++target_lv_idx;
432  }
433 }
#define LL_BUILDER
const Analyzer::Expr * agg_arg(const Analyzer::Expr *expr)
#define CHECK_EQ(x, y)
Definition: Logger.h:198
std::vector< std::string > agg_fn_base_names(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.
bool isLogicalSizedColumnsAllowed() const
SQLTypeInfo sql_type
Definition: TargetInfo.h:42
#define CHECK_GE(x, y)
Definition: Logger.h:203
llvm::Value * emitCall(const std::string &fname, const std::vector< llvm::Value * > &args)
bool needsUnnestDoublePatch(llvm::Value *val_ptr, const std::string &agg_base_name, const bool threads_share_memory, const CompilationOptions &co) const
bool takes_float_argument(const TargetInfo &target_info)
Definition: TargetInfo.h:120
bool skip_null_val
Definition: TargetInfo.h:44
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
std::string to_string(char const *&&v)
SQLTypeInfo agg_arg_type
Definition: TargetInfo.h:43
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_agg
Definition: TargetInfo.h:40
CHECK(cgen_state)
#define LL_INT(v)
HOST DEVICE SQLTypes get_type() const
Definition: sqltypes.h:326
llvm::Value * convertNullIfAny(const SQLTypeInfo &arg_type, const TargetInfo &agg_info, llvm::Value *target)
int get_physical_coord_cols() const
Definition: sqltypes.h:362
bool g_bigint_count
#define LL_CONTEXT
bool is_distinct_target(const TargetInfo &target_info)
Definition: TargetInfo.h:116
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
SQLAgg agg_kind
Definition: TargetInfo.h:41
bool is_geometry() const
Definition: sqltypes.h:489
ExecutorDeviceType device_type_
bool window_function_is_aggregate(const SqlWindowFunctionKind kind)
Definition: WindowContext.h:42
static void resetWindowFunctionContext()
const Analyzer::Expr * target_expr
Definition: sqldefs.h:71
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 window_function_requires_peer_handling(const Analyzer::WindowFunction *window_func)
GroupByMemSharing getGpuMemSharing() const
std::string numeric_type_name(const SQLTypeInfo &ti)
Definition: Execute.h:160
bool is_distinct
Definition: TargetInfo.h:45
const int8_t getLogicalSlotWidthBytes(const size_t slot_idx) const
Definition: sqldefs.h:71
size_t getColOffInBytes(const size_t col_idx) const
static WindowFunctionContext * getActiveWindowFunctionContext()
bool is_agg_domain_range_equivalent(const SQLAgg &agg_kind)
Definition: TargetInfo.h:52

+ Here is the call graph for this function:

Member Data Documentation

int32_t TargetExprCodegen::base_slot_index

Definition at line 59 of file TargetExprBuilder.h.

Referenced by codegen().

const Analyzer::Expr* TargetExprCodegen::target_expr

Definition at line 56 of file TargetExprBuilder.h.

Referenced by codegen().

size_t TargetExprCodegen::target_idx

Definition at line 60 of file TargetExprBuilder.h.

Referenced by codegen().

TargetInfo TargetExprCodegen::target_info

Definition at line 57 of file TargetExprBuilder.h.

Referenced by codegen(), and TargetExprCodegenBuilder::operator()().


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