29 #include <llvm/Bitcode/BitcodeReader.h>
30 #include <llvm/IR/Function.h>
31 #include <llvm/IR/IRBuilder.h>
32 #include <llvm/IR/Verifier.h>
33 #include <llvm/Support/SourceMgr.h>
34 #include <llvm/Support/raw_os_ostream.h>
53 return function->add<
Load>(
55 ptr->
label() +
"_loaded");
79 LOG(
FATAL) <<
"Invalid byte width: " << compact_sz;
90 const size_t chosen_bytes,
92 Function* ir_reduce_one_entry) {
94 const auto dest_name = agg_kind +
"_dest";
95 if (sql_type.is_fp()) {
96 if (chosen_bytes ==
sizeof(
float)) {
97 const auto agg = ir_reduce_one_entry->add<
Cast>(
100 ir_reduce_one_entry->add<
Call>(
101 "agg_" + agg_kind +
"_float", std::vector<const Value*>{agg, val},
"");
103 CHECK_EQ(chosen_bytes,
sizeof(
double));
104 const auto agg = ir_reduce_one_entry->add<
Cast>(
107 ir_reduce_one_entry->add<
Call>(
108 "agg_" + agg_kind +
"_double", std::vector<const Value*>{agg, val},
"");
111 if (chosen_bytes ==
sizeof(int32_t)) {
112 const auto agg = ir_reduce_one_entry->add<
Cast>(
114 const auto val =
emit_load(other_ptr, Type::Int32Ptr, ir_reduce_one_entry);
115 ir_reduce_one_entry->add<
Call>(
116 "agg_" + agg_kind +
"_int32", std::vector<const Value*>{agg, val},
"");
118 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
119 const auto agg = ir_reduce_one_entry->add<
Cast>(
121 const auto val =
emit_load(other_ptr, Type::Int64Ptr, ir_reduce_one_entry);
122 ir_reduce_one_entry->add<
Call>(
123 "agg_" + agg_kind, std::vector<const Value*>{agg, val},
"");
132 const int64_t init_val,
133 const size_t chosen_bytes,
135 Function* ir_reduce_one_entry) {
136 const auto dest_name = agg_kind +
"_dest";
139 if (sql_type.is_fp()) {
140 if (chosen_bytes ==
sizeof(
float)) {
141 const auto agg = ir_reduce_one_entry->add<
Cast>(
144 const auto init_val_lv = ir_reduce_one_entry->addConstant<
ConstantFP>(
145 *
reinterpret_cast<const float*
>(may_alias_ptr(&init_val)),
Type::Float);
146 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_float_skip_val",
147 std::vector<const Value*>{agg, val, init_val_lv},
150 CHECK_EQ(chosen_bytes,
sizeof(
double));
151 const auto agg = ir_reduce_one_entry->add<
Cast>(
154 const auto init_val_lv = ir_reduce_one_entry->addConstant<
ConstantFP>(
155 *
reinterpret_cast<const double*
>(may_alias_ptr(&init_val)),
Type::Double);
156 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_double_skip_val",
157 std::vector<const Value*>{agg, val, init_val_lv},
161 if (chosen_bytes ==
sizeof(int32_t)) {
162 const auto agg = ir_reduce_one_entry->add<
Cast>(
164 const auto val =
emit_load(other_ptr, Type::Int32Ptr, ir_reduce_one_entry);
165 const auto init_val_lv =
167 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_int32_skip_val",
168 std::vector<const Value*>{agg, val, init_val_lv},
171 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
172 const auto agg = ir_reduce_one_entry->add<
Cast>(
174 const auto val =
emit_load(other_ptr, Type::Int64Ptr, ir_reduce_one_entry);
175 const auto init_val_lv =
177 ir_reduce_one_entry->add<
Call>(
"agg_" + agg_kind +
"_skip_val",
178 std::vector<const Value*>{agg, val, init_val_lv},
184 agg_kind, val_ptr, other_ptr, chosen_bytes, agg_info, ir_reduce_one_entry);
191 const size_t chosen_bytes,
192 Function* ir_reduce_one_entry) {
193 const auto dest_name =
"count_dest";
194 if (chosen_bytes ==
sizeof(int32_t)) {
195 const auto agg = ir_reduce_one_entry->add<
Cast>(
197 const auto val =
emit_load(other_ptr, Type::Int32Ptr, ir_reduce_one_entry);
198 ir_reduce_one_entry->add<
Call>(
199 "agg_sum_int32", std::vector<const Value*>{agg, val},
"");
201 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
202 const auto agg = ir_reduce_one_entry->add<
Cast>(
204 const auto val =
emit_load(other_ptr, Type::Int64Ptr, ir_reduce_one_entry);
205 ir_reduce_one_entry->add<
Call>(
"agg_sum", std::vector<const Value*>{agg, val},
"");
214 const int64_t init_val,
215 const size_t chosen_bytes,
216 Function* ir_reduce_one_entry) {
217 const auto func_name =
"write_projection_int" +
std::to_string(chosen_bytes * 8);
218 if (chosen_bytes ==
sizeof(int32_t)) {
219 const auto proj_val =
emit_load_i32(other_pi8, ir_reduce_one_entry);
220 ir_reduce_one_entry->add<
Call>(
222 std::vector<const Value*>{
228 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
229 const auto proj_val =
emit_load_i64(other_pi8, ir_reduce_one_entry);
230 ir_reduce_one_entry->add<
Call>(
232 std::vector<const Value*>{
245 const int64_t init_val,
246 const size_t chosen_bytes,
247 Function* ir_reduce_one_entry) {
248 if (chosen_bytes ==
sizeof(int32_t)) {
249 const auto func_name =
"checked_single_agg_id_int32";
250 const auto proj_val =
emit_load_i32(other_pi8, ir_reduce_one_entry);
251 const auto slot_pi32 = ir_reduce_one_entry->add<
Cast>(
253 return ir_reduce_one_entry->add<
Call>(
256 std::vector<const Value*>{
262 const auto func_name =
"checked_single_agg_id";
263 CHECK_EQ(chosen_bytes,
sizeof(int64_t));
264 const auto proj_val =
emit_load_i64(other_pi8, ir_reduce_one_entry);
265 const auto slot_pi64 = ir_reduce_one_entry->add<
Cast>(
268 return ir_reduce_one_entry->add<
Call>(
271 std::vector<const Value*>{
280 const std::string
name,
281 const std::vector<Function::NamedArg>& arg_types,
283 const bool always_inline) {
284 return std::make_unique<Function>(
name, arg_types, ret_type, always_inline);
297 std::string this_ptr_name;
298 std::string that_ptr_name;
301 this_ptr_name =
"this_targets_ptr";
302 that_ptr_name =
"that_targets_ptr";
307 this_ptr_name =
"this_row_ptr";
308 that_ptr_name =
"that_row_ptr";
312 LOG(
FATAL) <<
"Unexpected query description type";
330 {
"that_entry_idx", Type::Int32},
331 {
"that_entry_count", Type::Int32},
345 {
"start_index", Type::Int32},
346 {
"end_index", Type::Int32},
347 {
"that_entry_count", Type::Int32},
358 std::vector<llvm::Type*> parameter_types;
359 const auto& arg_types =
function->arg_types();
360 for (
const auto& named_arg : arg_types) {
362 parameter_types.push_back(
llvm_type(named_arg.type, ctx));
364 const auto func_type = llvm::FunctionType::get(
365 llvm_type(function->ret_type(), ctx), parameter_types,
false);
366 const auto linkage =
function->always_inline() ? llvm::Function::PrivateLinkage
367 : llvm::Function::ExternalLinkage;
369 llvm::Function::Create(func_type, linkage, function->name(), cgen_state->
module_);
370 const auto arg_it = func->arg_begin();
371 for (
size_t i = 0;
i < arg_types.size(); ++
i) {
372 const auto arg = &*(arg_it +
i);
373 arg->setName(arg_types[
i].
name);
375 if (function->always_inline()) {
389 return reduction_code;
401 const int8_t* that_ptr1,
402 const int8_t* that_ptr2,
403 const int64_t init_val) {
404 const auto rhs_proj_col = *
reinterpret_cast<const int64_t*
>(that_ptr1);
405 if (rhs_proj_col != init_val) {
406 *
reinterpret_cast<int64_t*
>(this_ptr1) = rhs_proj_col;
408 CHECK(this_ptr2 && that_ptr2);
409 *
reinterpret_cast<int64_t*
>(this_ptr2) = *reinterpret_cast<const int64_t*>(that_ptr2);
415 const void* serialized_varlen_buffer_handle,
418 const int8_t* that_ptr1,
419 const int8_t* that_ptr2,
420 const int64_t init_val,
421 const int64_t length_to_elems) {
422 if (!serialized_varlen_buffer_handle) {
426 const auto& serialized_varlen_buffer =
427 *
reinterpret_cast<const std::vector<std::string>*
>(serialized_varlen_buffer_handle);
428 if (!serialized_varlen_buffer.empty()) {
429 const auto rhs_proj_col = *
reinterpret_cast<const int64_t*
>(that_ptr1);
430 CHECK_LT(static_cast<size_t>(rhs_proj_col), serialized_varlen_buffer.size());
431 const auto& varlen_bytes_str = serialized_varlen_buffer[rhs_proj_col];
432 const auto str_ptr =
reinterpret_cast<const int8_t*
>(varlen_bytes_str.c_str());
433 *
reinterpret_cast<int64_t*
>(this_ptr1) = reinterpret_cast<const int64_t>(str_ptr);
434 *
reinterpret_cast<int64_t*
>(this_ptr2) =
435 static_cast<int64_t>(varlen_bytes_str.size() / length_to_elems);
445 const int64_t new_set_handle,
446 const int64_t old_set_handle,
447 const void* that_qmd_handle,
448 const void* this_qmd_handle,
449 const int64_t target_logical_idx) {
452 const auto& new_count_distinct_desc =
454 const auto& old_count_distinct_desc =
455 this_qmd->getCountDistinctDescriptor(target_logical_idx);
457 CHECK(old_count_distinct_desc.impl_type_ == new_count_distinct_desc.impl_type_);
459 new_set_handle, old_set_handle, new_count_distinct_desc, old_count_distinct_desc);
463 const int64_t old_set_handle,
464 const void* that_qmd_handle,
465 const void* this_qmd_handle,
466 const int64_t target_logical_idx) {
468 if (incoming->centroids().capacity()) {
471 accumulator->mergeTDigest(*incoming);
476 int8_t* groups_buffer,
478 const uint32_t key_count,
479 const void* this_qmd_handle,
480 const int8_t* that_buff,
481 const uint32_t that_entry_idx,
482 const uint32_t that_entry_count,
483 const uint32_t row_size_bytes,
489 this_qmd.getEntryCount(),
490 reinterpret_cast<const int64_t*
>(key),
492 this_qmd.getEffectiveKeyWidth(),
494 reinterpret_cast<const int64_t*
>(that_buff),
497 row_size_bytes >> 3);
498 *buff_out = gvi.first;
519 const std::vector<TargetInfo>& targets,
520 const std::vector<int64_t>& target_init_vals)
521 : query_mem_desc_(query_mem_desc)
523 , target_init_vals_(target_init_vals) {}
576 LOG(
FATAL) <<
"Unexpected query description type";
583 return reduction_code;
588 if (compilation_context) {
596 std::move(reduction_code.ir_is_empty),
597 std::move(reduction_code.ir_reduce_one_entry),
598 std::move(reduction_code.ir_reduce_one_entry_idx),
599 std::move(reduction_code.ir_reduce_loop)};
601 reduction_code.cgen_state.reset(
new CgenState({},
false));
602 auto cgen_state = reduction_code.cgen_state.get();
604 cgen_state->module_ = module.get();
607 auto ir_reduce_one_entry =
609 auto ir_reduce_one_entry_idx =
611 auto ir_reduce_loop =
613 std::unordered_map<const Function*, llvm::Function*>
f;
614 f.emplace(reduction_code.ir_is_empty.get(), ir_is_empty);
615 f.emplace(reduction_code.ir_reduce_one_entry.get(), ir_reduce_one_entry);
616 f.emplace(reduction_code.ir_reduce_one_entry_idx.get(), ir_reduce_one_entry_idx);
617 f.emplace(reduction_code.ir_reduce_loop.get(), ir_reduce_loop);
620 reduction_code.ir_reduce_one_entry.get(), ir_reduce_one_entry, reduction_code,
f);
622 ir_reduce_one_entry_idx,
626 reduction_code.ir_reduce_loop.get(), ir_reduce_loop, reduction_code,
f);
627 reduction_code.llvm_reduce_loop = ir_reduce_loop;
628 reduction_code.module = std::move(module);
633 ir_reduce_one_entry_idx,
646 auto ir_is_empty = reduction_code.
ir_is_empty.get();
650 Value* empty_key_val{
nullptr};
651 const auto keys_ptr = ir_is_empty->arg(0);
663 "is_empty_slot_ptr");
664 const auto compact_sz =
667 empty_key_val = ir_is_empty->addConstant<
ConstantInt>(
689 ir_is_empty->add<
Ret>(ret);
695 const auto this_row_ptr = ir_reduce_one_entry->arg(0);
696 const auto that_row_ptr = ir_reduce_one_entry->arg(1);
697 const auto that_is_empty =
699 std::vector<const Value*>{that_row_ptr},
706 ir_reduce_one_entry->add<
MemCpy>(
713 const auto key_bytes_lv =
715 const auto this_targets_start_ptr = ir_reduce_one_entry->add<
GetElementPtr>(
716 this_row_ptr, key_bytes_lv,
"this_targets_start");
717 const auto that_targets_start_ptr = ir_reduce_one_entry->add<
GetElementPtr>(
718 that_row_ptr, key_bytes_lv,
"that_targets_start");
721 ir_reduce_one_entry, this_targets_start_ptr, that_targets_start_ptr);
725 Function* ir_reduce_one_entry,
726 Value* this_targets_start_ptr,
727 Value* that_targets_start_ptr)
const {
729 Value* this_targets_ptr = this_targets_start_ptr;
730 Value* that_targets_ptr = that_targets_start_ptr;
731 size_t init_agg_val_idx = 0;
732 for (
size_t target_logical_idx = 0; target_logical_idx <
targets_.size();
733 ++target_logical_idx) {
734 const auto& target_info =
targets_[target_logical_idx];
735 const auto& slots_for_col = col_slot_context.getSlotsForCol(target_logical_idx);
736 Value* this_ptr2{
nullptr};
737 Value* that_ptr2{
nullptr};
739 bool two_slot_target{
false};
740 if (target_info.is_agg &&
741 (target_info.agg_kind ==
kAVG ||
742 (target_info.agg_kind ==
kSAMPLE && target_info.sql_type.is_varlen()))) {
746 two_slot_target =
true;
749 for (
size_t target_slot_idx = slots_for_col.front();
750 target_slot_idx < slots_for_col.back() + 1;
751 target_slot_idx += 2) {
753 const auto slot_off =
756 const auto desc =
"target_" +
std::to_string(target_logical_idx) +
"_second_slot";
758 this_targets_ptr, slot_off,
"this_" + desc);
760 that_targets_ptr, slot_off,
"that_" + desc);
770 slots_for_col.front(),
771 ir_reduce_one_entry);
772 auto increment_agg_val_idx_maybe =
773 [&init_agg_val_idx, &target_logical_idx,
this](
const int slot_count) {
776 init_agg_val_idx += slot_count;
779 if (target_logical_idx + 1 ==
targets_.size() &&
780 target_slot_idx + 1 >= slots_for_col.back()) {
783 const auto next_desc =
784 "target_" +
std::to_string(target_logical_idx + 1) +
"_first_slot";
786 increment_agg_val_idx_maybe(2);
787 const auto two_slot_off = ir_reduce_one_entry->addConstant<
ConstantInt>(
791 this_targets_ptr, two_slot_off,
"this_" + next_desc);
793 that_targets_ptr, two_slot_off,
"that_" + next_desc);
795 increment_agg_val_idx_maybe(1);
797 this_targets_ptr, slot_off,
"this_" + next_desc);
799 that_targets_ptr, slot_off,
"that_" + next_desc);
803 ir_reduce_one_entry->add<
Ret>(
810 const auto this_targets_ptr_arg = ir_reduce_one_entry->arg(0);
811 const auto that_targets_ptr_arg = ir_reduce_one_entry->arg(1);
812 Value* this_ptr1 = this_targets_ptr_arg;
813 Value* that_ptr1 = that_targets_ptr_arg;
815 size_t init_agg_val_idx = 0;
816 for (
size_t target_logical_idx = 0; target_logical_idx <
targets_.size();
817 ++target_logical_idx) {
818 const auto& target_info =
targets_[target_logical_idx];
819 Value* this_ptr2{
nullptr};
820 Value* that_ptr2{
nullptr};
821 if (target_info.is_agg &&
822 (target_info.agg_kind ==
kAVG ||
823 (target_info.agg_kind ==
kSAMPLE && target_info.sql_type.is_varlen()))) {
824 const auto desc =
"target_" +
std::to_string(target_logical_idx) +
"_second_slot";
825 const auto second_slot_rel_off =
828 this_ptr1, second_slot_rel_off,
"this_" + desc);
830 that_ptr1, second_slot_rel_off,
"that_" + desc);
841 ir_reduce_one_entry);
842 if (target_logical_idx + 1 ==
targets_.size()) {
846 init_agg_val_idx =
advance_slot(init_agg_val_idx, target_info,
false);
849 init_agg_val_idx =
advance_slot(init_agg_val_idx, target_info,
false);
853 const auto next_desc =
854 "target_" +
std::to_string(target_logical_idx + 1) +
"_first_slot";
855 auto next_slot_rel_off = ir_reduce_one_entry->addConstant<
ConstantInt>(
858 this_targets_ptr_arg, next_slot_rel_off, next_desc);
860 that_targets_ptr_arg, next_slot_rel_off, next_desc);
862 ir_reduce_one_entry->add<
Ret>(
873 const auto this_buff = ir_reduce_one_entry_idx->arg(0);
874 const auto that_buff = ir_reduce_one_entry_idx->arg(1);
875 const auto entry_idx = ir_reduce_one_entry_idx->arg(2);
876 const auto this_qmd_handle = ir_reduce_one_entry_idx->arg(4);
877 const auto that_qmd_handle = ir_reduce_one_entry_idx->arg(5);
878 const auto serialized_varlen_buffer_arg = ir_reduce_one_entry_idx->arg(6);
879 const auto row_bytes = ir_reduce_one_entry_idx->addConstant<
ConstantInt>(
881 const auto entry_idx_64 = ir_reduce_one_entry_idx->add<
Cast>(
883 const auto row_off_in_bytes = ir_reduce_one_entry_idx->add<
BinaryOperator>(
885 const auto this_row_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
886 this_buff, row_off_in_bytes,
"this_row_ptr");
887 const auto that_row_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
888 that_buff, row_off_in_bytes,
"that_row_ptr");
889 const auto reduce_rc = ir_reduce_one_entry_idx->add<
Call>(
891 std::vector<const Value*>{this_row_ptr,
895 serialized_varlen_buffer_arg},
897 ir_reduce_one_entry_idx->add<
Ret>(reduce_rc);
907 const auto this_buff = ir_reduce_one_entry_idx->arg(0);
908 const auto that_buff = ir_reduce_one_entry_idx->arg(1);
909 const auto that_entry_idx = ir_reduce_one_entry_idx->arg(2);
910 const auto that_entry_count = ir_reduce_one_entry_idx->arg(3);
911 const auto this_qmd_handle = ir_reduce_one_entry_idx->arg(4);
912 const auto that_qmd_handle = ir_reduce_one_entry_idx->arg(5);
913 const auto serialized_varlen_buffer_arg = ir_reduce_one_entry_idx->arg(6);
914 const auto row_bytes = ir_reduce_one_entry_idx->addConstant<
ConstantInt>(
916 const auto that_entry_idx_64 = ir_reduce_one_entry_idx->add<
Cast>(
918 const auto that_row_off_in_bytes =
922 "that_row_off_in_bytes");
923 const auto that_row_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
924 that_buff, that_row_off_in_bytes,
"that_row_ptr");
925 const auto that_is_empty =
927 std::vector<const Value*>{that_row_ptr},
934 const auto one_element =
936 const auto this_targets_ptr_i64_ptr = ir_reduce_one_entry_idx->add<
Alloca>(
938 const auto this_is_empty_ptr =
939 ir_reduce_one_entry_idx->add<
Alloca>(
Type::Int8, one_element,
"this_is_empty_out");
941 "get_group_value_reduction_rt",
943 std::vector<const Value*>{
952 this_targets_ptr_i64_ptr,
955 const auto this_targets_ptr_i64 = ir_reduce_one_entry_idx->add<
Load>(
956 this_targets_ptr_i64_ptr,
"this_targets_ptr_i64");
958 ir_reduce_one_entry_idx->add<
Load>(this_is_empty_ptr,
"this_is_empty");
959 this_is_empty = ir_reduce_one_entry_idx->add<
Cast>(
966 const auto this_targets_ptr = ir_reduce_one_entry_idx->add<
Cast>(
968 const auto key_byte_count = key_qw_count *
sizeof(int64_t);
969 const auto key_byte_count_lv =
971 const auto that_targets_ptr = ir_reduce_one_entry_idx->add<
GetElementPtr>(
972 that_row_ptr, key_byte_count_lv,
"that_targets_ptr");
973 const auto reduce_rc = ir_reduce_one_entry_idx->add<
Call>(
975 std::vector<const Value*>{this_targets_ptr,
979 serialized_varlen_buffer_arg},
981 ir_reduce_one_entry_idx->add<
Ret>(reduce_rc);
987 Function* ir_reduce_loop,
988 Function* ir_reduce_one_entry_idx,
992 Value* that_entry_count,
993 Value* this_qmd_handle,
994 Value* that_qmd_handle,
995 Value* serialized_varlen_buffer) {
998 const auto sample_seed =
1001 const auto checker_rt_name =
1005 checker_rt_name,
Type::Int8, std::vector<const Value*>{sample_seed},
"");
1006 const auto interrupt_triggered_bool =
1012 interrupt_triggered_bool,
1016 const auto reduce_rc =
1017 for_loop->
add<
Call>(ir_reduce_one_entry_idx,
1018 std::vector<const Value*>{this_buff,
1024 serialized_varlen_buffer},
1027 auto reduce_rc_bool =
1039 const auto this_buff_arg = ir_reduce_loop->arg(0);
1040 const auto that_buff_arg = ir_reduce_loop->arg(1);
1041 const auto start_index_arg = ir_reduce_loop->arg(2);
1042 const auto end_index_arg = ir_reduce_loop->arg(3);
1043 const auto that_entry_count_arg = ir_reduce_loop->arg(4);
1044 const auto this_qmd_handle_arg = ir_reduce_loop->arg(5);
1045 const auto that_qmd_handle_arg = ir_reduce_loop->arg(6);
1046 const auto serialized_varlen_buffer_arg = ir_reduce_loop->arg(7);
1048 static_cast<For*
>(ir_reduce_loop->add<
For>(start_index_arg, end_index_arg,
""));
1055 that_entry_count_arg,
1056 this_qmd_handle_arg,
1057 that_qmd_handle_arg,
1058 serialized_varlen_buffer_arg);
1067 const size_t target_logical_idx,
1068 const size_t target_slot_idx,
1069 const size_t init_agg_val_idx,
1070 const size_t first_slot_idx_for_target,
1071 Function* ir_reduce_one_entry)
const {
1082 if (target_info.
is_agg &&
1093 ir_reduce_one_entry);
1096 this_ptr1, that_ptr1, init_val, chosen_bytes, ir_reduce_one_entry);
1098 auto checked_rc_bool = ir_reduce_one_entry->add<
ICmp>(
1104 ir_reduce_one_entry->add<
ReturnEarly>(checked_rc_bool, checked_rc,
"");
1108 this_ptr1, that_ptr1, init_val, chosen_bytes, ir_reduce_one_entry);
1110 CHECK(this_ptr2 && that_ptr2);
1111 size_t length_to_elems{0};
1114 length_to_elems = target_slot_idx == first_slot_idx_for_target ? 1 : 4;
1119 const auto serialized_varlen_buffer_arg = ir_reduce_one_entry->arg(4);
1121 "serialized_varlen_buffer_sample",
1123 std::vector<const Value*>{
1124 serialized_varlen_buffer_arg,
1130 ir_reduce_one_entry->addConstant<
ConstantInt>(length_to_elems,
1142 const size_t target_logical_idx,
1143 const size_t target_slot_idx,
1144 const int64_t init_val,
1145 const int8_t chosen_bytes,
1146 Function* ir_reduce_one_entry)
const {
1151 CHECK_EQ(static_cast<size_t>(chosen_bytes),
sizeof(int64_t));
1153 this_ptr1, that_ptr1, target_logical_idx, ir_reduce_one_entry);
1161 CHECK_EQ(chosen_bytes, static_cast<int8_t>(
sizeof(int64_t)));
1163 this_ptr1, that_ptr1, target_logical_idx, ir_reduce_one_entry);
1170 ir_reduce_one_entry);
1180 ir_reduce_one_entry);
1190 ir_reduce_one_entry);
1200 ir_reduce_one_entry);
1204 LOG(
FATAL) <<
"Invalid aggregate type";
1211 const size_t target_logical_idx,
1212 Function* ir_reduce_one_entry)
const {
1214 const auto old_set_handle =
emit_load_i64(this_ptr1, ir_reduce_one_entry);
1215 const auto new_set_handle =
emit_load_i64(that_ptr1, ir_reduce_one_entry);
1216 const auto this_qmd_arg = ir_reduce_one_entry->arg(2);
1217 const auto that_qmd_arg = ir_reduce_one_entry->arg(3);
1219 "count_distinct_set_union_jit_rt",
1221 std::vector<const Value*>{
1233 const size_t target_logical_idx,
1234 Function* ir_reduce_one_entry)
const {
1236 const auto old_set_handle =
emit_load_i64(this_ptr1, ir_reduce_one_entry);
1237 const auto new_set_handle =
emit_load_i64(that_ptr1, ir_reduce_one_entry);
1238 const auto this_qmd_arg = ir_reduce_one_entry->arg(2);
1239 const auto that_qmd_arg = ir_reduce_one_entry->arg(3);
1241 "approx_median_jit_rt",
1243 std::vector<const Value*>{
1254 const llvm::Function* ir_is_empty,
1255 const llvm::Function* ir_reduce_one_entry,
1256 const llvm::Function* ir_reduce_one_entry_idx,
1262 LOG(
IR) <<
"Reduction Loop:\n"
1266 LOG(
IR) <<
"Reduction One Entry Idx Func:\n"
1272 reduction_code.
module.release();
1278 auto cpu_compilation_context = std::make_shared<CpuCompilationContext>(std::move(ee));
1279 cpu_compilation_context->setFunctionPointer(reduction_code.
llvm_reduce_loop);
1285 return reduction_code;
1304 std::vector<std::string> target_init_vals_strings;
1307 std::back_inserter(target_init_vals_strings),
1309 const auto target_init_vals_key =
1311 std::vector<std::string> targets_strings;
1315 std::back_inserter(targets_strings),
1331 auto cgen_state = reduction_code.
cgen_state.get();
1334 cgen_state->module_ = module.get();
1337 auto ir_reduce_one_entry =
1339 auto ir_reduce_one_entry_idx =
1341 auto ir_reduce_loop =
1343 std::unordered_map<const Function*, llvm::Function*>
f;
1344 f.emplace(reduction_code.
ir_is_empty.get(), ir_is_empty);
1350 reduction_code.ir_reduce_one_entry.get(), ir_reduce_one_entry, reduction_code,
f);
1352 ir_reduce_one_entry_idx,
1356 reduction_code.ir_reduce_loop.get(), ir_reduce_loop, reduction_code,
f);
1357 reduction_code.llvm_reduce_loop = ir_reduce_loop;
1358 reduction_code.module = std::move(module);
1359 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)
std::unique_ptr< CgenState > cgen_state
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
std::unique_ptr< llvm::Module > runtime_module_shallow_copy(CgenState *cgen_state)
RUNTIME_EXPORT uint8_t check_watchdog_rt(const size_t sample_seed)
void reduceOneEntryNoCollisions(const ReductionCode &reduction_code) const
std::shared_ptr< CompilationContext > compilation_context
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)
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
ReductionCode 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
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)
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)
ResultSetReductionJIT(const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const std::vector< int64_t > &target_init_vals)
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)
std::unique_ptr< llvm::Module > g_rt_module
static std::mutex s_reduction_mutex
bool is_distinct_target(const TargetInfo &target_info)
std::string target_info_key(const TargetInfo &target_info)
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
static void addCodeToCache(const CodeCacheKey &, std::shared_ptr< CompilationContext >, llvm::Module *, CodeCache &)
llvm::Type * llvm_type(const Type type, llvm::LLVMContext &ctx)
virtual ReductionCode codegen() const
const CountDistinctDescriptor & getCountDistinctDescriptor(const size_t idx) const
static CodeCache s_code_cache
std::string serialize_llvm_object(const T *llvm_obj)
void reduceOneApproxMedianSlot(Value *this_ptr1, Value *that_ptr1, const size_t target_logical_idx, Function *ir_reduce_one_entry) const
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)
virtual ReductionCode codegen() 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 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
std::unique_ptr< llvm::Module > module
void reduceOneEntryBaseline(const ReductionCode &reduction_code) const
RUNTIME_EXPORT void approx_median_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)
const int32_t WATCHDOG_ERROR
Value * emit_load(Value *ptr, Type ptr_type, Function *function)
value_t * get(const key_t &key)
llvm::Function * create_llvm_function(const Function *function, CgenState *cgen_state)
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
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_