OmniSciDB  a47db9e897
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResultSetReductionJIT.h
Go to the documentation of this file.
1 /*
2  * Copyright 2019 OmniSci, Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #pragma once
18 
19 #include "CgenState.h"
20 #include "CodeCache.h"
21 #include "ResultSetReductionOps.h"
22 
24 #include "Shared/TargetInfo.h"
25 
26 #include <llvm/ExecutionEngine/ExecutionEngine.h>
27 #include <llvm/IR/Module.h>
28 #include <llvm/IR/Value.h>
29 
30 struct ReductionCode {
31  // Function which reduces 'that_buff' into 'this_buff', for rows between
32  // [start_entry_index, end_entry_index).
33  using FuncPtr = int32_t (*)(int8_t* this_buff,
34  const int8_t* that_buff,
35  const int32_t start_entry_index,
36  const int32_t end_entry_index,
37  const int32_t that_entry_count,
38  const void* this_qmd,
39  const void* that_qmd,
40  const void* serialized_varlen_buffer);
41 
43  llvm::Function* llvm_reduce_loop;
44  std::unique_ptr<CgenState> cgen_state;
45  std::unique_ptr<llvm::Module> module;
46  std::unique_ptr<Function> ir_is_empty;
47  std::unique_ptr<Function> ir_reduce_one_entry;
48  std::unique_ptr<Function> ir_reduce_one_entry_idx;
49  std::unique_ptr<Function> ir_reduce_loop;
50 
51  static std::mutex s_reduction_mutex;
52 };
53 
55  public:
57  const std::vector<TargetInfo>& targets,
58  const std::vector<int64_t>& target_init_vals);
59 
60  // Generate the code for the result set reduction loop.
61  ReductionCode codegen() const;
62 
63  static void clearCache();
64 
65  private:
66  // Generate a function which checks whether a row is empty.
67  void isEmpty(const ReductionCode& reduction_code) const;
68 
69  // Generate a function which reduces two rows given by their start pointer, for the
70  // perfect hash layout.
71  void reduceOneEntryNoCollisions(const ReductionCode& reduction_code) const;
72 
73  // Used to implement 'reduceOneEntryNoCollisions'.
74  void reduceOneEntryTargetsNoCollisions(Function* ir_reduce_one_entry,
75  Value* this_targets_start_ptr,
76  Value* that_targets_start_ptr) const;
77 
78  // Same as above, for the baseline layout.
79  void reduceOneEntryBaseline(const ReductionCode& reduction_code) const;
80 
81  // Generate a function which reduces two rows given by the start pointer of the result
82  // buffers they are part of and the indices inside those buffers.
83  void reduceOneEntryNoCollisionsIdx(const ReductionCode& reduction_code) const;
84 
85  // Same as above, for the baseline layout.
86  void reduceOneEntryBaselineIdx(const ReductionCode& reduction_code) const;
87 
88  // Generate a function for the reduction of an entire result set chunk.
89  void reduceLoop(const ReductionCode& reduction_code) const;
90 
91  // Generate reduction code for a single slot.
92  void reduceOneSlot(Value* this_ptr1,
93  Value* this_ptr2,
94  Value* that_ptr1,
95  Value* that_ptr2,
96  const TargetInfo& target_info,
97  const size_t target_logical_idx,
98  const size_t target_slot_idx,
99  const size_t init_agg_val_idx,
100  const size_t first_slot_idx_for_target,
101  Function* ir_reduce_one_entry) const;
102 
103  // Generate reduction code for a single aggregate (with the exception of sample) slot.
104  void reduceOneAggregateSlot(Value* this_ptr1,
105  Value* this_ptr2,
106  Value* that_ptr1,
107  Value* that_ptr2,
108  const TargetInfo& target_info,
109  const size_t target_logical_idx,
110  const size_t target_slot_idx,
111  const int64_t init_val,
112  const int8_t chosen_bytes,
113  Function* ir_reduce_one_entry) const;
114 
115  // Generate reduction code for a count distinct slot.
116  void reduceOneCountDistinctSlot(Value* this_ptr1,
117  Value* that_ptr1,
118  const size_t target_logical_idx,
119  Function* ir_reduce_one_entry) const;
120 
122  const llvm::Function* ir_is_empty,
123  const llvm::Function* ir_reduce_one_entry,
124  const llvm::Function* ir_reduce_one_entry_idx,
125  const CodeCacheKey& key) const;
126 
127  std::string cacheKey() const;
128 
130  const std::vector<TargetInfo> targets_;
131  const std::vector<int64_t> target_init_vals_;
133 };
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
void reduceOneEntryNoCollisions(const ReductionCode &reduction_code) const
std::unique_ptr< Function > ir_reduce_loop
void reduceOneEntryBaselineIdx(const ReductionCode &reduction_code) const
void reduceLoop(const ReductionCode &reduction_code) const
llvm::Function * llvm_reduce_loop
void reduceOneEntryNoCollisionsIdx(const ReductionCode &reduction_code) const
std::vector< std::string > CodeCacheKey
Definition: CodeCache.h:61
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
std::string cacheKey() const
std::unique_ptr< Function > ir_reduce_one_entry
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
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
ResultSetReductionJIT(const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const std::vector< int64_t > &target_init_vals)
void reduceOneCountDistinctSlot(Value *this_ptr1, Value *that_ptr1, const size_t target_logical_idx, Function *ir_reduce_one_entry) const
static std::mutex s_reduction_mutex
std::unique_ptr< Function > ir_reduce_one_entry_idx
void reduceOneEntryTargetsNoCollisions(Function *ir_reduce_one_entry, Value *this_targets_start_ptr, Value *that_targets_start_ptr) const
ReductionCode codegen() const
Descriptor for the result set buffer layout.
void isEmpty(const ReductionCode &reduction_code) const
std::unique_ptr< llvm::Module > module
void reduceOneEntryBaseline(const ReductionCode &reduction_code) const
const std::vector< TargetInfo > targets_