OmniSciDB  340b00dbf6
 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  std::shared_ptr<CompilationContext> compilation_context;
51 
52  static std::mutex s_reduction_mutex;
53 };
54 
56  public:
58  const std::vector<TargetInfo>& targets,
59  const std::vector<int64_t>& target_init_vals);
60  virtual ~ResultSetReductionJIT() = default;
61 
62  // Generate the code for the result set reduction loop.
63  virtual ReductionCode codegen() const;
64  static void clearCache();
65 
66  protected:
67  // Generate a function which checks whether a row is empty.
68  void isEmpty(const ReductionCode& reduction_code) const;
69 
70  // Generate a function which reduces two rows given by their start pointer, for the
71  // perfect hash layout.
72  void reduceOneEntryNoCollisions(const ReductionCode& reduction_code) const;
73 
74  // Generate a function which reduces two rows given by the start pointer of the result
75  // buffers they are part of and the indices inside those buffers.
76  void reduceOneEntryNoCollisionsIdx(const ReductionCode& reduction_code) const;
77 
78  // Generate a function for the reduction of an entire result set chunk.
79  void reduceLoop(const ReductionCode& reduction_code) const;
80 
81  private:
82  // Used to implement 'reduceOneEntryNoCollisions'.
83  void reduceOneEntryTargetsNoCollisions(Function* ir_reduce_one_entry,
84  Value* this_targets_start_ptr,
85  Value* that_targets_start_ptr) const;
86 
87  // Same as above, for the baseline layout.
88  void reduceOneEntryBaseline(const ReductionCode& reduction_code) const;
89 
90  // Same as above, for the baseline layout.
91  void reduceOneEntryBaselineIdx(const ReductionCode& reduction_code) const;
92 
93  // Generate reduction code for a single slot.
94  void reduceOneSlot(Value* this_ptr1,
95  Value* this_ptr2,
96  Value* that_ptr1,
97  Value* that_ptr2,
98  const TargetInfo& target_info,
99  const size_t target_logical_idx,
100  const size_t target_slot_idx,
101  const size_t init_agg_val_idx,
102  const size_t first_slot_idx_for_target,
103  Function* ir_reduce_one_entry) const;
104 
105  // Generate reduction code for a single aggregate (with the exception of sample) slot.
106  void reduceOneAggregateSlot(Value* this_ptr1,
107  Value* this_ptr2,
108  Value* that_ptr1,
109  Value* that_ptr2,
110  const TargetInfo& target_info,
111  const size_t target_logical_idx,
112  const size_t target_slot_idx,
113  const int64_t init_val,
114  const int8_t chosen_bytes,
115  Function* ir_reduce_one_entry) const;
116 
117  // Generate reduction code for a count distinct slot.
118  void reduceOneCountDistinctSlot(Value* this_ptr1,
119  Value* that_ptr1,
120  const size_t target_logical_idx,
121  Function* ir_reduce_one_entry) const;
122 
124  const llvm::Function* ir_is_empty,
125  const llvm::Function* ir_reduce_one_entry,
126  const llvm::Function* ir_reduce_one_entry_idx,
127  const CodeCacheKey& key) const;
128 
129  std::string cacheKey() const;
130 
132  const std::vector<TargetInfo> targets_;
133  const std::vector<int64_t> target_init_vals_;
135 };
136 
143  public:
145  const std::vector<TargetInfo>& targets,
146  const std::vector<int64_t>& target_init_vals)
147  : ResultSetReductionJIT(query_mem_desc, targets, target_init_vals)
148  , query_mem_desc_(query_mem_desc) {
153  }
160  virtual ReductionCode codegen() const;
161 
162  private:
164 };
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::shared_ptr< CompilationContext > compilation_context
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:25
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
const QueryMemoryDescriptor & query_mem_desc_
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)
virtual ~ResultSetReductionJIT()=default
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
QueryDescriptionType getQueryDescriptionType() const
virtual ReductionCode codegen() const
virtual ReductionCode codegen() const
Descriptor for the result set buffer layout.
void isEmpty(const ReductionCode &reduction_code) const
#define CHECK(condition)
Definition: Logger.h:197
std::unique_ptr< llvm::Module > module
void reduceOneEntryBaseline(const ReductionCode &reduction_code) const
GpuReductionHelperJIT(const QueryMemoryDescriptor &query_mem_desc, const std::vector< TargetInfo > &targets, const std::vector< int64_t > &target_init_vals)
const std::vector< TargetInfo > targets_