30 #include <llvm/Bitcode/BitcodeReader.h>
31 #include <llvm/IR/Function.h>
32 #include <llvm/IR/IRBuilder.h>
33 #include <llvm/IR/Verifier.h>
34 #include <llvm/Support/SourceMgr.h>
35 #include <llvm/Support/raw_os_ostream.h>
48 return function->add<
Load>(
50 ptr->
label() +
"_loaded");
74 LOG(
FATAL) <<
"Invalid byte width: " << compact_sz;
85 const size_t chosen_bytes,
87 Function* ir_reduce_one_entry) {
89 const auto dest_name = agg_kind +
"_dest";
90 if (sql_type.is_fp()) {
91 if (chosen_bytes ==
sizeof(
float)) {
92 const auto agg = ir_reduce_one_entry->add<
Cast>(
95 ir_reduce_one_entry->add<
Call>(
96 "agg_" + agg_kind +
"_float", std::vector<const Value*>{agg, val},
"");
98 CHECK_EQ(chosen_bytes,
sizeof(
double));
99 const auto agg = ir_reduce_one_entry->add<
Cast>(
102 ir_reduce_one_entry->add<
Call>(
103 "agg_" + agg_kind +
"_double", std::vector<const Value*>{agg, val},
"");
106 if (chosen_bytes ==
sizeof(int32_t)) {
107 const auto agg = ir_reduce_one_entry->add<
Cast>(
109 const auto val =
emit_load(other_ptr, Type::Int32Ptr, ir_reduce_one_entry);
110 ir_reduce_one_entry->add<
Call>(
111 "agg_" + agg_kind +
"_int32", std::vector<const Value*>{agg, val},
"");
113 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
114 const auto agg = ir_reduce_one_entry->add<
Cast>(
116 const auto val =
emit_load(other_ptr, Type::Int64Ptr, ir_reduce_one_entry);
117 ir_reduce_one_entry->add<
Call>(
118 "agg_" + agg_kind, std::vector<const Value*>{agg, val},
"");
127 const int64_t init_val,
128 const size_t chosen_bytes,
130 Function* ir_reduce_one_entry) {
131 const auto dest_name = agg_kind +
"_dest";
134 if (sql_type.is_fp()) {
135 if (chosen_bytes ==
sizeof(
float)) {
136 const auto agg = ir_reduce_one_entry->add<
Cast>(
139 const auto init_val_lv = ir_reduce_one_entry->addConstant<
ConstantFP>(
140 *
reinterpret_cast<const float*
>(may_alias_ptr(&init_val)),
Type::Float);
141 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_float_skip_val",
142 std::vector<const Value*>{agg, val, init_val_lv},
145 CHECK_EQ(chosen_bytes,
sizeof(
double));
146 const auto agg = ir_reduce_one_entry->add<
Cast>(
149 const auto init_val_lv = ir_reduce_one_entry->addConstant<
ConstantFP>(
150 *
reinterpret_cast<const double*
>(may_alias_ptr(&init_val)),
Type::Double);
151 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_double_skip_val",
152 std::vector<const Value*>{agg, val, init_val_lv},
156 if (chosen_bytes ==
sizeof(int32_t)) {
157 const auto agg = ir_reduce_one_entry->add<
Cast>(
159 const auto val =
emit_load(other_ptr, Type::Int32Ptr, ir_reduce_one_entry);
160 const auto init_val_lv =
162 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_int32_skip_val",
163 std::vector<const Value*>{agg, val, init_val_lv},
166 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
167 const auto agg = ir_reduce_one_entry->add<
Cast>(
169 const auto val =
emit_load(other_ptr, Type::Int64Ptr, ir_reduce_one_entry);
170 const auto init_val_lv =
172 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_skip_val",
173 std::vector<const Value*>{agg, val, init_val_lv},
179 agg_kind, val_ptr, other_ptr, chosen_bytes, agg_info, ir_reduce_one_entry);
186 const size_t chosen_bytes,
187 Function* ir_reduce_one_entry) {
188 const auto dest_name =
"count_dest";
189 if (chosen_bytes ==
sizeof(int32_t)) {
190 const auto agg = ir_reduce_one_entry->add<
Cast>(
192 const auto val =
emit_load(other_ptr, Type::Int32Ptr, ir_reduce_one_entry);
193 ir_reduce_one_entry->add<
Call>(
194 "agg_sum_int32", std::vector<const Value*>{agg, val},
"");
196 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
197 const auto agg = ir_reduce_one_entry->add<
Cast>(
199 const auto val =
emit_load(other_ptr, Type::Int64Ptr, ir_reduce_one_entry);
200 ir_reduce_one_entry->add<
Call>(
"agg_sum", std::vector<const Value*>{agg, val},
"");
209 const int64_t init_val,
210 const size_t chosen_bytes,
211 Function* ir_reduce_one_entry) {
212 const auto func_name =
"write_projection_int" +
std::to_string(chosen_bytes * 8);
213 if (chosen_bytes ==
sizeof(int32_t)) {
214 const auto proj_val =
emit_load_i32(other_pi8, ir_reduce_one_entry);
215 ir_reduce_one_entry->add<
Call>(
217 std::vector<const Value*>{
223 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
224 const auto proj_val =
emit_load_i64(other_pi8, ir_reduce_one_entry);
225 ir_reduce_one_entry->add<
Call>(
227 std::vector<const Value*>{
240 const int64_t init_val,
241 const size_t chosen_bytes,
242 Function* ir_reduce_one_entry) {
243 if (chosen_bytes ==
sizeof(int32_t)) {
244 const auto func_name =
"checked_single_agg_id_int32";
245 const auto proj_val =
emit_load_i32(other_pi8, ir_reduce_one_entry);
246 const auto slot_pi32 = ir_reduce_one_entry->add<
Cast>(
248 return ir_reduce_one_entry->add<
Call>(
251 std::vector<const Value*>{
257 const auto func_name =
"checked_single_agg_id";
258 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
259 const auto proj_val =
emit_load_i64(other_pi8, ir_reduce_one_entry);
260 const auto slot_pi64 = ir_reduce_one_entry->add<
Cast>(
263 return ir_reduce_one_entry->add<
Call>(
266 std::vector<const Value*>{
275 const std::string
name,
276 const std::vector<Function::NamedArg>& arg_types,
278 const bool always_inline) {
279 return std::make_unique<Function>(
name, arg_types, ret_type, always_inline);
292 std::string this_ptr_name;
293 std::string that_ptr_name;
296 this_ptr_name =
"this_targets_ptr";
297 that_ptr_name =
"that_targets_ptr";
302 this_ptr_name =
"this_row_ptr";
303 that_ptr_name =
"that_row_ptr";
307 LOG(
FATAL) <<
"Unexpected query description type";
325 {
"that_entry_idx", Type::Int32},
326 {
"that_entry_count", Type::Int32},
340 {
"start_index", Type::Int32},
341 {
"end_index", Type::Int32},
342 {
"that_entry_count", Type::Int32},
353 std::vector<llvm::Type*> parameter_types;
354 const auto& arg_types =
function->arg_types();
355 for (
const auto& named_arg : arg_types) {
357 parameter_types.push_back(
llvm_type(named_arg.type, ctx));
359 const auto func_type = llvm::FunctionType::get(
360 llvm_type(function->ret_type(), ctx), parameter_types,
false);
361 const auto linkage =
function->always_inline() ? llvm::Function::PrivateLinkage
362 : llvm::Function::ExternalLinkage;
364 llvm::Function::Create(func_type, linkage, function->name(), cgen_state->
module_);
365 const auto arg_it = func->arg_begin();
366 for (
size_t i = 0; i < arg_types.size(); ++i) {
367 const auto arg = &*(arg_it + i);
368 arg->setName(arg_types[i].
name);
370 if (function->always_inline()) {
384 return reduction_code;
396 const int8_t* that_ptr1,
397 const int8_t* that_ptr2,
398 const int64_t init_val) {
399 const auto rhs_proj_col = *
reinterpret_cast<const int64_t*
>(that_ptr1);
400 if (rhs_proj_col != init_val) {
401 *
reinterpret_cast<int64_t*
>(this_ptr1) = rhs_proj_col;
403 CHECK(this_ptr2 && that_ptr2);
404 *
reinterpret_cast<int64_t*
>(this_ptr2) = *reinterpret_cast<const int64_t*>(that_ptr2);
410 const void* serialized_varlen_buffer_handle,
413 const int8_t* that_ptr1,
414 const int8_t* that_ptr2,
415 const int64_t init_val,
416 const int64_t length_to_elems) {
417 if (!serialized_varlen_buffer_handle) {
421 const auto& serialized_varlen_buffer =
422 *
reinterpret_cast<const std::vector<std::string>*
>(serialized_varlen_buffer_handle);
423 if (!serialized_varlen_buffer.empty()) {
424 const auto rhs_proj_col = *
reinterpret_cast<const int64_t*
>(that_ptr1);
425 CHECK_LT(static_cast<size_t>(rhs_proj_col), serialized_varlen_buffer.size());
426 const auto& varlen_bytes_str = serialized_varlen_buffer[rhs_proj_col];
427 const auto str_ptr =
reinterpret_cast<const int8_t*
>(varlen_bytes_str.c_str());
428 *
reinterpret_cast<int64_t*
>(this_ptr1) = reinterpret_cast<const int64_t>(str_ptr);
429 *
reinterpret_cast<int64_t*
>(this_ptr2) =
430 static_cast<int64_t>(varlen_bytes_str.size() / length_to_elems);
440 const int64_t new_set_handle,
441 const int64_t old_set_handle,
442 const void* that_qmd_handle,
443 const void* this_qmd_handle,
444 const int64_t target_logical_idx) {
447 const auto& new_count_distinct_desc =
449 const auto& old_count_distinct_desc =
450 this_qmd->getCountDistinctDescriptor(target_logical_idx);
452 CHECK(old_count_distinct_desc.impl_type_ == new_count_distinct_desc.impl_type_);
454 new_set_handle, old_set_handle, new_count_distinct_desc, old_count_distinct_desc);
458 const int64_t old_set_handle,
459 const void* that_qmd_handle,
460 const void* this_qmd_handle,
461 const int64_t target_logical_idx) {
463 if (incoming->centroids().capacity()) {
466 accumulator->mergeTDigest(*incoming);
471 int8_t* groups_buffer,
473 const uint32_t key_count,
474 const void* this_qmd_handle,
475 const int8_t* that_buff,
476 const uint32_t that_entry_idx,
477 const uint32_t that_entry_count,
478 const uint32_t row_size_bytes,
484 this_qmd.getEntryCount(),
485 reinterpret_cast<const int64_t*
>(key),
487 this_qmd.getEffectiveKeyWidth(),
489 reinterpret_cast<const int64_t*
>(that_buff),
492 row_size_bytes >> 3);
493 *buff_out = gvi.first;
514 const std::vector<TargetInfo>& targets,
515 const std::vector<int64_t>& target_init_vals,
516 const size_t executor_id)
517 : executor_id_(executor_id)
518 , query_mem_desc_(query_mem_desc)
520 , target_init_vals_(target_init_vals) {}
573 LOG(
FATAL) <<
"Unexpected query description type";
580 return reduction_code;
584 std::lock_guard<std::mutex> compilation_lock(executor->compilation_mutex_);
585 const auto compilation_context =
587 if (compilation_context) {
588 reduction_code.func_ptr =
590 return reduction_code;
592 auto cgen_state_ = std::unique_ptr<CgenState>(
new CgenState({},
false, executor.get()));
593 auto cgen_state = reduction_code.cgen_state = cgen_state_.get();
594 cgen_state->set_module_shallow_copy(executor->get_rt_module());
595 reduction_code.module = cgen_state->module_;
599 auto ir_reduce_one_entry =
601 auto ir_reduce_one_entry_idx =
603 auto ir_reduce_loop =
605 std::unordered_map<const Function*, llvm::Function*>
f;
606 f.emplace(reduction_code.ir_is_empty.get(), ir_is_empty);
607 f.emplace(reduction_code.ir_reduce_one_entry.get(), ir_reduce_one_entry);
608 f.emplace(reduction_code.ir_reduce_one_entry_idx.get(), ir_reduce_one_entry_idx);
609 f.emplace(reduction_code.ir_reduce_loop.get(), ir_reduce_loop);
612 reduction_code.ir_reduce_one_entry.get(), ir_reduce_one_entry, reduction_code,
f);
614 ir_reduce_one_entry_idx,
618 reduction_code.ir_reduce_loop.get(), ir_reduce_loop, reduction_code,
f);
619 reduction_code.llvm_reduce_loop = ir_reduce_loop;
621 reduction_code.cgen_state =
nullptr;
623 reduction_code, ir_is_empty, ir_reduce_one_entry, ir_reduce_one_entry_idx, key);
624 return reduction_code;
628 auto ir_is_empty = reduction_code.
ir_is_empty.get();
632 Value* empty_key_val{
nullptr};
633 const auto keys_ptr = ir_is_empty->arg(0);
645 "is_empty_slot_ptr");
646 const auto compact_sz =
649 empty_key_val = ir_is_empty->addConstant<
ConstantInt>(
671 ir_is_empty->add<
Ret>(ret);
677 const auto this_row_ptr = ir_reduce_one_entry->arg(0);
678 const auto that_row_ptr = ir_reduce_one_entry->arg(1);
679 const auto that_is_empty =
681 std::vector<const Value*>{that_row_ptr},
688 ir_reduce_one_entry->add<
MemCpy>(
695 const auto key_bytes_lv =
697 const auto this_targets_start_ptr = ir_reduce_one_entry->add<
GetElementPtr>(
698 this_row_ptr, key_bytes_lv,
"this_targets_start");
699 const auto that_targets_start_ptr = ir_reduce_one_entry->add<
GetElementPtr>(
700 that_row_ptr, key_bytes_lv,
"that_targets_start");
703 ir_reduce_one_entry, this_targets_start_ptr, that_targets_start_ptr);
707 Function* ir_reduce_one_entry,
708 Value* this_targets_start_ptr,
709 Value* that_targets_start_ptr)
const {
711 Value* this_targets_ptr = this_targets_start_ptr;
712 Value* that_targets_ptr = that_targets_start_ptr;
713 size_t init_agg_val_idx = 0;
714 for (
size_t target_logical_idx = 0; target_logical_idx <
targets_.size();
715 ++target_logical_idx) {
716 const auto& target_info =
targets_[target_logical_idx];
717 const auto& slots_for_col = col_slot_context.getSlotsForCol(target_logical_idx);
718 Value* this_ptr2{
nullptr};
719 Value* that_ptr2{
nullptr};
721 bool two_slot_target{
false};
722 if (target_info.is_agg &&
723 (target_info.agg_kind ==
kAVG ||
724 (target_info.agg_kind ==
kSAMPLE && target_info.sql_type.is_varlen()))) {
728 two_slot_target =
true;
731 for (
size_t target_slot_idx = slots_for_col.front();
732 target_slot_idx < slots_for_col.back() + 1;
733 target_slot_idx += 2) {
735 const auto slot_off =
738 const auto desc =
"target_" +
std::to_string(target_logical_idx) +
"_second_slot";
740 this_targets_ptr, slot_off,
"this_" + desc);
742 that_targets_ptr, slot_off,
"that_" + desc);
752 slots_for_col.front(),
753 ir_reduce_one_entry);
754 auto increment_agg_val_idx_maybe =
755 [&init_agg_val_idx, &target_logical_idx,
this](
const int slot_count) {
758 init_agg_val_idx += slot_count;
761 if (target_logical_idx + 1 ==
targets_.size() &&
762 target_slot_idx + 1 >= slots_for_col.back()) {
765 const auto next_desc =
766 "target_" +
std::to_string(target_logical_idx + 1) +
"_first_slot";
768 increment_agg_val_idx_maybe(2);
769 const auto two_slot_off = ir_reduce_one_entry->addConstant<
ConstantInt>(
773 this_targets_ptr, two_slot_off,
"this_" + next_desc);
775 that_targets_ptr, two_slot_off,
"that_" + next_desc);
777 increment_agg_val_idx_maybe(1);
779 this_targets_ptr, slot_off,
"this_" + next_desc);
781 that_targets_ptr, slot_off,
"that_" + next_desc);
785 ir_reduce_one_entry->add<
Ret>(
792 const auto this_targets_ptr_arg = ir_reduce_one_entry->arg(0);
793 const auto that_targets_ptr_arg = ir_reduce_one_entry->arg(1);
794 Value* this_ptr1 = this_targets_ptr_arg;
795 Value* that_ptr1 = that_targets_ptr_arg;
797 size_t init_agg_val_idx = 0;
798 for (
size_t target_logical_idx = 0; target_logical_idx <
targets_.size();
799 ++target_logical_idx) {
800 const auto& target_info =
targets_[target_logical_idx];
801 Value* this_ptr2{
nullptr};
802 Value* that_ptr2{
nullptr};
803 if (target_info.is_agg &&
804 (target_info.agg_kind ==
kAVG ||
805 (target_info.agg_kind ==
kSAMPLE && target_info.sql_type.is_varlen()))) {
806 const auto desc =
"target_" +
std::to_string(target_logical_idx) +
"_second_slot";
807 const auto second_slot_rel_off =
810 this_ptr1, second_slot_rel_off,
"this_" + desc);
812 that_ptr1, second_slot_rel_off,
"that_" + desc);
823 ir_reduce_one_entry);
824 if (target_logical_idx + 1 ==
targets_.size()) {
828 init_agg_val_idx =
advance_slot(init_agg_val_idx, target_info,
false);
831 init_agg_val_idx =
advance_slot(init_agg_val_idx, target_info,
false);
835 const auto next_desc =
836 "target_" +
std::to_string(target_logical_idx + 1) +
"_first_slot";
837 auto next_slot_rel_off = ir_reduce_one_entry->addConstant<
ConstantInt>(
840 this_targets_ptr_arg, next_slot_rel_off, next_desc);
842 that_targets_ptr_arg, next_slot_rel_off, next_desc);
844 ir_reduce_one_entry->add<
Ret>(
855 const auto this_buff = ir_reduce_one_entry_idx->arg(0);
856 const auto that_buff = ir_reduce_one_entry_idx->arg(1);
857 const auto entry_idx = ir_reduce_one_entry_idx->arg(2);
858 const auto this_qmd_handle = ir_reduce_one_entry_idx->arg(4);
859 const auto that_qmd_handle = ir_reduce_one_entry_idx->arg(5);
860 const auto serialized_varlen_buffer_arg = ir_reduce_one_entry_idx->arg(6);
861 const auto row_bytes = ir_reduce_one_entry_idx->addConstant<
ConstantInt>(
863 const auto entry_idx_64 = ir_reduce_one_entry_idx->add<
Cast>(
865 const auto row_off_in_bytes = ir_reduce_one_entry_idx->add<
BinaryOperator>(
867 const auto this_row_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
868 this_buff, row_off_in_bytes,
"this_row_ptr");
869 const auto that_row_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
870 that_buff, row_off_in_bytes,
"that_row_ptr");
871 const auto reduce_rc = ir_reduce_one_entry_idx->add<
Call>(
873 std::vector<const Value*>{this_row_ptr,
877 serialized_varlen_buffer_arg},
879 ir_reduce_one_entry_idx->add<
Ret>(reduce_rc);
889 const auto this_buff = ir_reduce_one_entry_idx->arg(0);
890 const auto that_buff = ir_reduce_one_entry_idx->arg(1);
891 const auto that_entry_idx = ir_reduce_one_entry_idx->arg(2);
892 const auto that_entry_count = ir_reduce_one_entry_idx->arg(3);
893 const auto this_qmd_handle = ir_reduce_one_entry_idx->arg(4);
894 const auto that_qmd_handle = ir_reduce_one_entry_idx->arg(5);
895 const auto serialized_varlen_buffer_arg = ir_reduce_one_entry_idx->arg(6);
896 const auto row_bytes = ir_reduce_one_entry_idx->addConstant<
ConstantInt>(
898 const auto that_entry_idx_64 = ir_reduce_one_entry_idx->add<
Cast>(
900 const auto that_row_off_in_bytes =
904 "that_row_off_in_bytes");
905 const auto that_row_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
906 that_buff, that_row_off_in_bytes,
"that_row_ptr");
907 const auto that_is_empty =
909 std::vector<const Value*>{that_row_ptr},
916 const auto one_element =
918 const auto this_targets_ptr_i64_ptr = ir_reduce_one_entry_idx->add<
Alloca>(
920 const auto this_is_empty_ptr =
921 ir_reduce_one_entry_idx->add<
Alloca>(
Type::Int8, one_element,
"this_is_empty_out");
923 "get_group_value_reduction_rt",
925 std::vector<const Value*>{
934 this_targets_ptr_i64_ptr,
937 const auto this_targets_ptr_i64 = ir_reduce_one_entry_idx->add<
Load>(
938 this_targets_ptr_i64_ptr,
"this_targets_ptr_i64");
940 ir_reduce_one_entry_idx->add<
Load>(this_is_empty_ptr,
"this_is_empty");
941 this_is_empty = ir_reduce_one_entry_idx->add<
Cast>(
948 const auto this_targets_ptr = ir_reduce_one_entry_idx->add<
Cast>(
950 const auto key_byte_count = key_qw_count *
sizeof(int64_t);
951 const auto key_byte_count_lv =
953 const auto that_targets_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
954 that_row_ptr, key_byte_count_lv,
"that_targets_ptr");
955 const auto reduce_rc = ir_reduce_one_entry_idx->add<
Call>(
957 std::vector<const Value*>{this_targets_ptr,
961 serialized_varlen_buffer_arg},
963 ir_reduce_one_entry_idx->add<
Ret>(reduce_rc);
969 Function* ir_reduce_loop,
970 Function* ir_reduce_one_entry_idx,
974 Value* that_entry_count,
975 Value* this_qmd_handle,
976 Value* that_qmd_handle,
977 Value* serialized_varlen_buffer) {
980 const auto sample_seed =
983 const auto checker_rt_name =
987 checker_rt_name,
Type::Int8, std::vector<const Value*>{sample_seed},
"");
988 const auto interrupt_triggered_bool =
994 interrupt_triggered_bool,
998 const auto reduce_rc =
999 for_loop->
add<
Call>(ir_reduce_one_entry_idx,
1000 std::vector<const Value*>{this_buff,
1006 serialized_varlen_buffer},
1009 auto reduce_rc_bool =
1021 const auto this_buff_arg = ir_reduce_loop->arg(0);
1022 const auto that_buff_arg = ir_reduce_loop->arg(1);
1023 const auto start_index_arg = ir_reduce_loop->arg(2);
1024 const auto end_index_arg = ir_reduce_loop->arg(3);
1025 const auto that_entry_count_arg = ir_reduce_loop->arg(4);
1026 const auto this_qmd_handle_arg = ir_reduce_loop->arg(5);
1027 const auto that_qmd_handle_arg = ir_reduce_loop->arg(6);
1028 const auto serialized_varlen_buffer_arg = ir_reduce_loop->arg(7);
1030 static_cast<For*
>(ir_reduce_loop->add<
For>(start_index_arg, end_index_arg,
""));
1037 that_entry_count_arg,
1038 this_qmd_handle_arg,
1039 that_qmd_handle_arg,
1040 serialized_varlen_buffer_arg);
1049 const size_t target_logical_idx,
1050 const size_t target_slot_idx,
1051 const size_t init_agg_val_idx,
1052 const size_t first_slot_idx_for_target,
1053 Function* ir_reduce_one_entry)
const {
1064 if (target_info.
is_agg &&
1075 ir_reduce_one_entry);
1078 this_ptr1, that_ptr1, init_val, chosen_bytes, ir_reduce_one_entry);
1080 auto checked_rc_bool = ir_reduce_one_entry->add<
ICmp>(
1086 ir_reduce_one_entry->add<
ReturnEarly>(checked_rc_bool, checked_rc,
"");
1090 this_ptr1, that_ptr1, init_val, chosen_bytes, ir_reduce_one_entry);
1092 CHECK(this_ptr2 && that_ptr2);
1093 size_t length_to_elems{0};
1096 length_to_elems = target_slot_idx == first_slot_idx_for_target ? 1 : 4;
1101 const auto serialized_varlen_buffer_arg = ir_reduce_one_entry->arg(4);
1103 "serialized_varlen_buffer_sample",
1105 std::vector<const Value*>{
1106 serialized_varlen_buffer_arg,
1112 ir_reduce_one_entry->addConstant<
ConstantInt>(length_to_elems,
1124 const size_t target_logical_idx,
1125 const size_t target_slot_idx,
1126 const int64_t init_val,
1127 const int8_t chosen_bytes,
1128 Function* ir_reduce_one_entry)
const {
1133 CHECK_EQ(static_cast<size_t>(chosen_bytes),
sizeof(int64_t));
1135 this_ptr1, that_ptr1, target_logical_idx, ir_reduce_one_entry);
1143 CHECK_EQ(chosen_bytes, static_cast<int8_t>(
sizeof(int64_t)));
1145 this_ptr1, that_ptr1, target_logical_idx, ir_reduce_one_entry);
1152 ir_reduce_one_entry);
1162 ir_reduce_one_entry);
1172 ir_reduce_one_entry);
1182 ir_reduce_one_entry);
1186 LOG(
FATAL) <<
"Invalid aggregate type";
1193 const size_t target_logical_idx,
1194 Function* ir_reduce_one_entry)
const {
1196 const auto old_set_handle =
emit_load_i64(this_ptr1, ir_reduce_one_entry);
1197 const auto new_set_handle =
emit_load_i64(that_ptr1, ir_reduce_one_entry);
1198 const auto this_qmd_arg = ir_reduce_one_entry->arg(2);
1199 const auto that_qmd_arg = ir_reduce_one_entry->arg(3);
1201 "count_distinct_set_union_jit_rt",
1203 std::vector<const Value*>{
1215 const size_t target_logical_idx,
1216 Function* ir_reduce_one_entry)
const {
1218 const auto old_set_handle =
emit_load_i64(this_ptr1, ir_reduce_one_entry);
1219 const auto new_set_handle =
emit_load_i64(that_ptr1, ir_reduce_one_entry);
1220 const auto this_qmd_arg = ir_reduce_one_entry->arg(2);
1221 const auto that_qmd_arg = ir_reduce_one_entry->arg(3);
1223 "approx_quantile_jit_rt",
1225 std::vector<const Value*>{
1236 const llvm::Function* ir_is_empty,
1237 const llvm::Function* ir_reduce_one_entry,
1238 const llvm::Function* ir_reduce_one_entry_idx,
1243 LOG(
IR) <<
"Reduction Loop:\n"
1247 LOG(
IR) <<
"Reduction One Entry Idx Func:\n"
1254 auto cpu_compilation_context = std::make_shared<CpuCompilationContext>(std::move(ee));
1255 cpu_compilation_context->setFunctionPointer(reduction_code.
llvm_reduce_loop);
1261 std::move(cpu_compilation_context));
1280 std::vector<std::string> target_init_vals_strings;
1283 std::back_inserter(target_init_vals_strings),
1285 const auto target_init_vals_key =
1287 std::vector<std::string> targets_strings;
1291 std::back_inserter(targets_strings),
1307 auto cgen_state_ = std::unique_ptr<CgenState>(
new CgenState({},
false, executor.get()));
1308 auto cgen_state = reduction_code.
cgen_state = cgen_state_.get();
1311 reduction_code.
module = cgen_state->module_;
1315 auto ir_reduce_one_entry =
1317 auto ir_reduce_one_entry_idx =
1319 auto ir_reduce_loop =
1321 std::unordered_map<const Function*, llvm::Function*>
f;
1322 f.emplace(reduction_code.
ir_is_empty.get(), ir_is_empty);
1328 reduction_code.ir_reduce_one_entry.get(), ir_reduce_one_entry, reduction_code,
f);
1330 ir_reduce_one_entry_idx,
1334 reduction_code.ir_reduce_loop.get(), ir_reduce_loop, reduction_code,
f);
1335 reduction_code.llvm_reduce_loop = ir_reduce_loop;
1336 reduction_code.cgen_state =
nullptr;
1337 return reduction_code;
GroupValueInfo get_group_value_reduction(int64_t *groups_buffer, const uint32_t groups_buffer_entry_count, const int64_t *key, const uint32_t key_count, const size_t key_width, const QueryMemoryDescriptor &query_mem_desc, const int64_t *that_buff_i64, const size_t that_entry_idx, const size_t that_entry_count, const uint32_t row_size_quad)
void emit_aggregate_one_nullable_value(const std::string &agg_kind, Value *val_ptr, Value *other_ptr, const int64_t init_val, const size_t chosen_bytes, const TargetInfo &agg_info, Function *ir_reduce_one_entry)
void reduceOneSlot(Value *this_ptr1, Value *this_ptr2, Value *that_ptr1, Value *that_ptr2, const TargetInfo &target_info, const size_t target_logical_idx, const size_t target_slot_idx, const size_t init_agg_val_idx, const size_t first_slot_idx_for_target, Function *ir_reduce_one_entry) const
bool is_aggregate_query(const QueryDescriptionType hash_type)
size_t getEntryCount() const
void count_distinct_set_union(const int64_t new_set_handle, const int64_t old_set_handle, const CountDistinctDescriptor &new_count_distinct_desc, const CountDistinctDescriptor &old_count_distinct_desc)
__device__ bool dynamic_watchdog()
const std::string & label() const
RUNTIME_EXPORT uint8_t check_watchdog_rt(const size_t sample_seed)
void reduceOneEntryNoCollisions(const ReductionCode &reduction_code) const
int64_t getTargetGroupbyIndex(const size_t target_idx) const
void varlen_buffer_sample(int8_t *this_ptr1, int8_t *this_ptr2, const int8_t *that_ptr1, const int8_t *that_ptr2, const int64_t init_val)
std::unique_ptr< Function > ir_reduce_loop
Value * emit_read_int_from_buff(Value *ptr, const int8_t compact_sz, Function *function)
void reduceOneEntryBaselineIdx(const ReductionCode &reduction_code) const
void mark_function_always_inline(llvm::Function *func)
Calculate approximate median and general quantiles, based on "Computing Extremely Accurate Quantiles ...
void reduceLoop(const ReductionCode &reduction_code) const
const size_t INTERP_THRESHOLD
llvm::Function * llvm_reduce_loop
void reduceOneEntryNoCollisionsIdx(const ReductionCode &reduction_code) const
bool hasKeylessHash() const
std::vector< std::string > CodeCacheKey
size_t get_slot_off_quad(const QueryMemoryDescriptor &query_mem_desc)
std::string cacheKey() const
size_t getEffectiveKeyWidth() const
std::unique_ptr< Function > ir_reduce_one_entry
bool g_enable_dynamic_watchdog
static ExecutionEngineWrapper generateNativeCPUCode(llvm::Function *func, const std::unordered_set< llvm::Function * > &live_funcs, const CompilationOptions &co)
const std::vector< int64_t > target_init_vals_
void reduceOneAggregateSlot(Value *this_ptr1, Value *this_ptr2, Value *that_ptr1, Value *that_ptr2, const TargetInfo &target_info, const size_t target_logical_idx, const size_t target_slot_idx, const int64_t init_val, const int8_t chosen_bytes, Function *ir_reduce_one_entry) const
bool takes_float_argument(const TargetInfo &target_info)
bool g_enable_non_kernel_time_query_interrupt
Value * add(Args &&...args)
const Value * emit_checked_write_projection(Value *slot_pi8, Value *other_pi8, const int64_t init_val, const size_t chosen_bytes, Function *ir_reduce_one_entry)
std::unique_ptr< Function > setup_reduce_one_entry_idx(ReductionCode *reduction_code)
int32_t(*)(int8_t *this_buff, const int8_t *that_buff, const int32_t start_entry_index, const int32_t end_entry_index, const int32_t that_entry_count, const void *this_qmd, const void *that_qmd, const void *serialized_varlen_buffer) FuncPtr
const QueryMemoryDescriptor query_mem_desc_
std::unique_ptr< Function > ir_is_empty
void translate_function(const Function *function, llvm::Function *llvm_function, const ReductionCode &reduction_code, const std::unordered_map< const Function *, llvm::Function * > &f)
void emit_aggregate_one_value(const std::string &agg_kind, Value *val_ptr, Value *other_ptr, const size_t chosen_bytes, const TargetInfo &agg_info, Function *ir_reduce_one_entry)
RUNTIME_EXPORT void serialized_varlen_buffer_sample(const void *serialized_varlen_buffer_handle, int8_t *this_ptr1, int8_t *this_ptr2, const int8_t *that_ptr1, const int8_t *that_ptr2, const int64_t init_val, const int64_t length_to_elems)
Value * emit_load_i32(Value *ptr, Function *function)
static std::shared_ptr< Executor > getExecutor(const ExecutorId id, const std::string &debug_dir="", const std::string &debug_file="", const SystemParameters &system_parameters=SystemParameters())
const SQLTypeInfo get_compact_type(const TargetInfo &target)
bool blocksShareMemory() const
__device__ bool check_interrupt()
int8_t get_width_for_slot(const size_t target_slot_idx, const bool float_argument_input, const QueryMemoryDescriptor &query_mem_desc)
llvm::LLVMContext & context_
size_t get_byteoff_of_slot(const size_t slot_idx, const QueryMemoryDescriptor &query_mem_desc)
size_t advance_slot(const size_t j, const TargetInfo &target_info, const bool separate_varlen_storage)
void reduceOneCountDistinctSlot(Value *this_ptr1, Value *that_ptr1, const size_t target_logical_idx, Function *ir_reduce_one_entry) const
uint8_t check_interrupt_rt(const size_t sample_seed)
size_t getGroupbyColCount() const
const int32_t INTERRUPT_ERROR
size_t targetGroupbyIndicesSize() const
void generate_loop_body(For *for_loop, Function *ir_reduce_loop, Function *ir_reduce_one_entry_idx, Value *this_buff, Value *that_buff, Value *start_index, Value *that_entry_count, Value *this_qmd_handle, Value *that_qmd_handle, Value *serialized_varlen_buffer)
void emit_write_projection(Value *slot_pi8, Value *other_pi8, const int64_t init_val, const size_t chosen_bytes, Function *ir_reduce_one_entry)
ReductionCode codegen() const override
bool is_distinct_target(const TargetInfo &target_info)
std::string target_info_key(const TargetInfo &target_info)
OUTPUT transform(INPUT const &input, FUNC const &func)
std::unique_ptr< Function > ir_reduce_one_entry_idx
const int8_t getPaddedSlotWidthBytes(const size_t slot_idx) const
ReductionCode setup_functions_ir(const QueryDescriptionType hash_type)
std::unique_ptr< Function > setup_is_empty_entry(ReductionCode *reduction_code)
size_t getCountDistinctDescriptorsSize() const
void reduceOneEntryTargetsNoCollisions(Function *ir_reduce_one_entry, Value *this_targets_start_ptr, Value *that_targets_start_ptr) const
QueryDescriptionType getQueryDescriptionType() const
void set_module_shallow_copy(const std::unique_ptr< llvm::Module > &module, bool always_clone=false)
llvm::Type * llvm_type(const Type type, llvm::LLVMContext &ctx)
virtual ReductionCode codegen() const
const CountDistinctDescriptor & getCountDistinctDescriptor(const size_t idx) const
std::string serialize_llvm_object(const T *llvm_obj)
size_t get_row_bytes(const QueryMemoryDescriptor &query_mem_desc)
RUNTIME_EXPORT void count_distinct_set_union_jit_rt(const int64_t new_set_handle, const int64_t old_set_handle, const void *that_qmd_handle, const void *this_qmd_handle, const int64_t target_logical_idx)
void finalizeReductionCode(ReductionCode &reduction_code, const llvm::Function *ir_is_empty, const llvm::Function *ir_reduce_one_entry, const llvm::Function *ir_reduce_one_entry_idx, const CodeCacheKey &key) const
std::unique_ptr< Function > create_function(const std::string name, const std::vector< Function::NamedArg > &arg_types, const Type ret_type, const bool always_inline)
std::string get_type_name() const
const Value * iter() const
RUNTIME_EXPORT void approx_quantile_jit_rt(const int64_t new_set_handle, const int64_t old_set_handle, const void *that_qmd_handle, const void *this_qmd_handle, const int64_t target_logical_idx)
RUNTIME_EXPORT void get_group_value_reduction_rt(int8_t *groups_buffer, const int8_t *key, const uint32_t key_count, const void *this_qmd_handle, const int8_t *that_buff, const uint32_t that_entry_idx, const uint32_t that_entry_count, const uint32_t row_size_bytes, int64_t **buff_out, uint8_t *empty)
bool didOutputColumnar() const
std::unique_ptr< Function > setup_reduce_one_entry(ReductionCode *reduction_code, const QueryDescriptionType hash_type)
void isEmpty(const ReductionCode &reduction_code) const
Value * emit_load_i64(Value *ptr, Function *function)
const ColSlotContext & getColSlotContext() const
void reduceOneEntryBaseline(const ReductionCode &reduction_code) const
static std::shared_ptr< QueryEngine > getInstance()
const int32_t WATCHDOG_ERROR
Value * emit_load(Value *ptr, Type ptr_type, Function *function)
llvm::Function * create_llvm_function(const Function *function, CgenState *cgen_state)
ResultSetReductionJIT(const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const std::vector< int64_t > &target_init_vals, const size_t executor_id)
HOST DEVICE bool get_notnull() const
void emit_aggregate_one_count(Value *val_ptr, Value *other_ptr, const size_t chosen_bytes, Function *ir_reduce_one_entry)
SQLTypeInfo get_elem_type() const
void reduceOneApproxQuantileSlot(Value *this_ptr1, Value *that_ptr1, const size_t target_logical_idx, Function *ir_reduce_one_entry) const
std::unique_ptr< Function > setup_reduce_loop(ReductionCode *reduction_code)
size_t get_key_bytes_rowwise(const QueryMemoryDescriptor &query_mem_desc)
FORCE_INLINE HOST DEVICE T align_to_int64(T addr)
std::string reductionKey() const
const Executor * getExecutor() const
int32_t getTargetIdxForKey() const
const std::vector< TargetInfo > targets_