OmniSciDB  addbbd5075
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ResultSetReductionCodegen.cpp
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 
18 
19 #include "IRCodegenUtils.h"
21 #include "ResultSetReductionJIT.h"
22 #include "ResultSetReductionOps.h"
23 
24 #include <llvm/IR/Instructions.h>
25 
26 llvm::Type* llvm_type(const Type type, llvm::LLVMContext& ctx) {
27  switch (type) {
28  case Type::Int1: {
29  return get_int_type(1, ctx);
30  }
31  case Type::Int8: {
32  return get_int_type(8, ctx);
33  }
34  case Type::Int32: {
35  return get_int_type(32, ctx);
36  }
37  case Type::Int64: {
38  return get_int_type(64, ctx);
39  }
40  case Type::Float: {
41  return get_fp_type(32, ctx);
42  }
43  case Type::Double: {
44  return get_fp_type(64, ctx);
45  }
46  case Type::Void: {
47  return llvm::Type::getVoidTy(ctx);
48  }
49  case Type::Int8Ptr: {
50  return llvm::PointerType::get(get_int_type(8, ctx), 0);
51  }
52  case Type::Int32Ptr: {
53  return llvm::PointerType::get(get_int_type(32, ctx), 0);
54  }
55  case Type::Int64Ptr: {
56  return llvm::PointerType::get(get_int_type(64, ctx), 0);
57  }
58  case Type::FloatPtr: {
59  return llvm::Type::getFloatPtrTy(ctx);
60  }
61  case Type::DoublePtr: {
62  return llvm::Type::getDoublePtrTy(ctx);
63  }
64  case Type::VoidPtr: {
65  return llvm::PointerType::get(get_int_type(8, ctx), 0);
66  }
67  case Type::Int64PtrPtr: {
68  return llvm::PointerType::get(llvm::PointerType::get(get_int_type(64, ctx), 0), 0);
69  }
70  default: {
71  LOG(FATAL) << "Argument type not supported: " << static_cast<int>(type);
72  break;
73  }
74  }
75  UNREACHABLE();
76  return nullptr;
77 }
78 
79 namespace {
80 
81 // Convert an IR predicate to the corresponding LLVM one.
82 llvm::ICmpInst::Predicate llvm_predicate(const ICmp::Predicate predicate) {
83  switch (predicate) {
84  case ICmp::Predicate::EQ: {
85  return llvm::ICmpInst::ICMP_EQ;
86  }
87  case ICmp::Predicate::NE: {
88  return llvm::ICmpInst::ICMP_NE;
89  }
90  default: {
91  LOG(FATAL) << "Invalid predicate: " << static_cast<int>(predicate);
92  }
93  }
94  UNREACHABLE();
95  return llvm::ICmpInst::ICMP_EQ;
96 }
97 
98 // Convert an IR binary operator type to the corresponding LLVM one.
99 llvm::BinaryOperator::BinaryOps llvm_binary_op(const BinaryOperator::BinaryOp op) {
100  switch (op) {
102  return llvm::Instruction::Add;
103  }
105  return llvm::Instruction::Mul;
106  }
107  default: {
108  LOG(FATAL) << "Invalid binary operator: " << static_cast<int>(op);
109  }
110  }
111  UNREACHABLE();
112  return llvm::Instruction::Add;
113 }
114 
115 // Convert an IR cast operator type to the corresponding LLVM one.
116 llvm::Instruction::CastOps llvm_cast_op(const Cast::CastOp op) {
117  switch (op) {
118  case Cast::CastOp::Trunc: {
119  return llvm::Instruction::Trunc;
120  }
121  case Cast::CastOp::SExt: {
122  return llvm::Instruction::SExt;
123  }
124  case Cast::CastOp::BitCast: {
125  return llvm::Instruction::BitCast;
126  }
127  default: {
128  LOG(FATAL) << "Invalid cast operator: " << static_cast<int>(op);
129  }
130  }
131  UNREACHABLE();
132  return llvm::Instruction::SExt;
133 }
134 
135 // Emit an early return from a function when the provided 'cond' is true, which the caller
136 // code can use when entries are empty or the watchdog is triggered. For functions which
137 // return void, the specified error code is ignored. For functions which return an
138 // integer, the error code is returned.
139 void return_early(llvm::Value* cond,
140  const ReductionCode& reduction_code,
141  llvm::Function* func,
142  llvm::Value* error_code) {
143  auto cgen_state = reduction_code.cgen_state.get();
144  auto& ctx = cgen_state->context_;
145  const auto early_return = llvm::BasicBlock::Create(ctx, ".early_return", func, 0);
146  const auto do_reduction = llvm::BasicBlock::Create(ctx, ".do_reduction", func, 0);
147  cgen_state->ir_builder_.CreateCondBr(cond, early_return, do_reduction);
148  cgen_state->ir_builder_.SetInsertPoint(early_return);
149 
150  if (func->getReturnType()->isVoidTy()) {
151  cgen_state->ir_builder_.CreateRetVoid();
152  } else {
153  CHECK(error_code);
154  cgen_state->ir_builder_.CreateRet(error_code);
155  }
156 
157  cgen_state->ir_builder_.SetInsertPoint(do_reduction);
158 }
159 
160 // Returns the corresponding LLVM value for the given IR value.
161 llvm::Value* mapped_value(const Value* val,
162  const std::unordered_map<const Value*, llvm::Value*>& m) {
163  if (val) {
164  const auto it = m.find(val);
165  CHECK(it != m.end());
166  return it->second;
167  } else {
168  return nullptr;
169  }
170 }
171 
172 // Returns the corresponding LLVM function for the given IR function.
173 llvm::Function* mapped_function(
174  const Function* function,
175  const std::unordered_map<const Function*, llvm::Function*>& f) {
176  const auto it = f.find(function);
177  CHECK(it != f.end());
178  return it->second;
179 }
180 
181 // Given a list of IR values and the mapping, return the list of corresponding LLVM IR
182 // values.
183 std::vector<llvm::Value*> llvm_args(
184  const std::vector<const Value*> args,
185  const std::unordered_map<const Value*, llvm::Value*>& m) {
186  std::vector<llvm::Value*> llvm_args;
187  std::transform(
188  args.begin(), args.end(), std::back_inserter(llvm_args), [&m](const Value* value) {
189  return mapped_value(value, m);
190  });
191  return llvm_args;
192 }
193 
194 void translate_for(const For* for_loop,
195  Function* ir_reduce_loop,
196  const ReductionCode& reduction_code,
197  std::unordered_map<const Value*, llvm::Value*>& m,
198  const std::unordered_map<const Function*, llvm::Function*>& f);
199 
200 // Translate a list of instructions to LLVM IR.
201 void translate_body(const std::vector<std::unique_ptr<Instruction>>& body,
202  const Function* function,
203  llvm::Function* llvm_function,
204  const ReductionCode& reduction_code,
205  std::unordered_map<const Value*, llvm::Value*>& m,
206  const std::unordered_map<const Function*, llvm::Function*>& f) {
207  auto cgen_state = reduction_code.cgen_state.get();
208  auto& ctx = cgen_state->context_;
209  for (const auto& instr : body) {
210  const auto instr_ptr = instr.get();
211  llvm::Value* translated{nullptr};
212  if (auto gep = dynamic_cast<const GetElementPtr*>(instr_ptr)) {
213  translated = cgen_state->ir_builder_.CreateGEP(
214  mapped_value(gep->base(), m), mapped_value(gep->index(), m), gep->label());
215  } else if (auto load = dynamic_cast<const Load*>(instr_ptr)) {
216  translated = cgen_state->ir_builder_.CreateLoad(mapped_value(load->source(), m),
217  load->label());
218  } else if (auto icmp = dynamic_cast<const ICmp*>(instr_ptr)) {
219  translated = cgen_state->ir_builder_.CreateICmp(llvm_predicate(icmp->predicate()),
220  mapped_value(icmp->lhs(), m),
221  mapped_value(icmp->rhs(), m),
222  icmp->label());
223  } else if (auto binary_operator = dynamic_cast<const BinaryOperator*>(instr_ptr)) {
224  translated =
225  cgen_state->ir_builder_.CreateBinOp(llvm_binary_op(binary_operator->op()),
226  mapped_value(binary_operator->lhs(), m),
227  mapped_value(binary_operator->rhs(), m),
228  binary_operator->label());
229  } else if (auto cast = dynamic_cast<const Cast*>(instr_ptr)) {
230  translated = cgen_state->ir_builder_.CreateCast(llvm_cast_op(cast->op()),
231  mapped_value(cast->source(), m),
232  llvm_type(cast->type(), ctx),
233  cast->label());
234  } else if (auto ret = dynamic_cast<const Ret*>(instr_ptr)) {
235  if (ret->value()) {
236  cgen_state->ir_builder_.CreateRet(mapped_value(ret->value(), m));
237  } else {
238  cgen_state->ir_builder_.CreateRetVoid();
239  }
240  } else if (auto call = dynamic_cast<const Call*>(instr_ptr)) {
241  std::vector<llvm::Value*> llvm_args;
242  const auto args = call->arguments();
243  std::transform(args.begin(),
244  args.end(),
245  std::back_inserter(llvm_args),
246  [&m](const Value* value) { return mapped_value(value, m); });
247  if (call->callee()) {
248  translated = cgen_state->ir_builder_.CreateCall(
249  mapped_function(call->callee(), f), llvm_args, call->label());
250  } else {
251  translated = cgen_state->emitCall(call->callee_name(), llvm_args);
252  }
253  } else if (auto external_call = dynamic_cast<const ExternalCall*>(instr_ptr)) {
254  translated = cgen_state->emitExternalCall(external_call->callee_name(),
255  llvm_type(external_call->type(), ctx),
256  llvm_args(external_call->arguments(), m));
257  } else if (auto alloca = dynamic_cast<const Alloca*>(instr_ptr)) {
258  translated = cgen_state->ir_builder_.CreateAlloca(
259  llvm_type(pointee_type(alloca->type()), ctx),
260  mapped_value(alloca->array_size(), m),
261  alloca->label());
262  } else if (auto memcpy = dynamic_cast<const MemCpy*>(instr_ptr)) {
263  cgen_state->ir_builder_.CreateMemCpy(mapped_value(memcpy->dest(), m),
264  0,
265  mapped_value(memcpy->source(), m),
266  0,
267  mapped_value(memcpy->size(), m));
268  } else if (auto ret_early = dynamic_cast<const ReturnEarly*>(instr_ptr)) {
269  return_early(mapped_value(ret_early->cond(), m),
270  reduction_code,
271  llvm_function,
272  mapped_value(ret_early->error_code(), m));
273  } else if (auto for_loop = dynamic_cast<const For*>(instr_ptr)) {
274  translate_for(for_loop, reduction_code.ir_reduce_loop.get(), reduction_code, m, f);
275  } else {
276  LOG(FATAL) << "Instruction not supported yet";
277  }
278  if (translated) {
279  const auto it_ok = m.emplace(instr_ptr, translated);
280  CHECK(it_ok.second);
281  }
282  }
283 }
284 
285 // Translate a loop to LLVM IR, using existing loop construction facilities.
286 void translate_for(const For* for_loop,
287  Function* ir_reduce_loop,
288  const ReductionCode& reduction_code,
289  std::unordered_map<const Value*, llvm::Value*>& m,
290  const std::unordered_map<const Function*, llvm::Function*>& f) {
291  auto cgen_state = reduction_code.cgen_state.get();
292  const auto bb_entry = cgen_state->ir_builder_.GetInsertBlock();
293  auto& ctx = cgen_state->context_;
294  const auto i64_type = get_int_type(64, cgen_state->context_);
295  const auto end_index = mapped_value(for_loop->end(), m);
296  const auto start_index = mapped_value(for_loop->start(), m);
297  // The start and end indices are absolute. Subtract the start index from the iterator.
298  const auto iteration_count =
299  cgen_state->ir_builder_.CreateSub(end_index, start_index, "iteration_count");
300  const auto upper_bound = cgen_state->ir_builder_.CreateSExt(iteration_count, i64_type);
301  const auto bb_exit =
302  llvm::BasicBlock::Create(ctx, ".exit", mapped_function(ir_reduce_loop, f));
303  JoinLoop join_loop(
306  [upper_bound](const std::vector<llvm::Value*>& v) {
307  JoinLoopDomain domain{{0}};
308  domain.upper_bound = upper_bound;
309  return domain;
310  },
311  nullptr,
312  nullptr,
313  nullptr,
314  "reduction_loop");
315  const auto bb_loop_body = JoinLoop::codegen(
316  {join_loop},
317  [cgen_state, for_loop, ir_reduce_loop, &f, &m, &reduction_code](
318  const std::vector<llvm::Value*>& iterators) {
319  const auto loop_body_bb = llvm::BasicBlock::Create(
320  cgen_state->context_,
321  ".loop_body",
322  cgen_state->ir_builder_.GetInsertBlock()->getParent());
323  cgen_state->ir_builder_.SetInsertPoint(loop_body_bb);
324  // Make the iterator the same type as start and end indices (32-bit integer).
325  const auto loop_iter =
326  cgen_state->ir_builder_.CreateTrunc(iterators.back(),
327  get_int_type(32, cgen_state->context_),
328  "relative_entry_idx");
329  m.emplace(for_loop->iter(), loop_iter);
330  translate_body(for_loop->body(),
331  ir_reduce_loop,
332  mapped_function(ir_reduce_loop, f),
333  reduction_code,
334  m,
335  f);
336  return loop_body_bb;
337  },
338  nullptr,
339  bb_exit,
340  cgen_state->ir_builder_);
341  cgen_state->ir_builder_.SetInsertPoint(bb_entry);
342  cgen_state->ir_builder_.CreateBr(bb_loop_body);
343  cgen_state->ir_builder_.SetInsertPoint(bb_exit);
344 }
345 
346 // Create the entry basic block into an initially empty function.
347 void create_entry_block(llvm::Function* function, CgenState* cgen_state) {
348  const auto bb_entry =
349  llvm::BasicBlock::Create(cgen_state->context_, ".entry", function, 0);
350  cgen_state->ir_builder_.SetInsertPoint(bb_entry);
351 }
352 
353 } // namespace
354 
355 void translate_function(const Function* function,
356  llvm::Function* llvm_function,
357  const ReductionCode& reduction_code,
358  const std::unordered_map<const Function*, llvm::Function*>& f) {
359  auto cgen_state = reduction_code.cgen_state.get();
360  create_entry_block(llvm_function, cgen_state);
361  // Set the value mapping based on the input arguments.
362  std::unordered_map<const Value*, llvm::Value*> m;
363  auto llvm_arg_it = llvm_function->arg_begin();
364  for (size_t arg_idx = 0; arg_idx < function->arg_types().size(); ++arg_idx) {
365  llvm::Value* llvm_arg = &(*llvm_arg_it);
366  const auto it_ok = m.emplace(function->arg(arg_idx), llvm_arg);
367  CHECK(it_ok.second);
368  ++llvm_arg_it;
369  }
370  // Add mapping for the constants used by the function.
371  for (const auto& constant : function->constants()) {
372  llvm::Value* constant_llvm{nullptr};
373  switch (constant->type()) {
374  case Type::Int8: {
375  constant_llvm =
376  cgen_state->llInt<int8_t>(static_cast<ConstantInt*>(constant.get())->value());
377  break;
378  }
379  case Type::Int32: {
380  constant_llvm = cgen_state->llInt<int32_t>(
381  static_cast<ConstantInt*>(constant.get())->value());
382  break;
383  }
384  case Type::Int64: {
385  constant_llvm = cgen_state->llInt<int64_t>(
386  static_cast<ConstantInt*>(constant.get())->value());
387  break;
388  }
389  case Type::Float: {
390  constant_llvm = cgen_state->llFp(
391  static_cast<float>(static_cast<ConstantFP*>(constant.get())->value()));
392  break;
393  }
394  case Type::Double: {
395  constant_llvm =
396  cgen_state->llFp(static_cast<ConstantFP*>(constant.get())->value());
397  break;
398  }
399  default: {
400  LOG(FATAL) << "Constant type not supported: "
401  << static_cast<int>(constant->type());
402  }
403  }
404  CHECK(constant_llvm);
405  const auto it_ok = m.emplace(constant.get(), constant_llvm);
406  CHECK(it_ok.second);
407  }
408  translate_body(function->body(), function, llvm_function, reduction_code, m, f);
409  verify_function_ir(llvm_function);
410 }
std::unique_ptr< CgenState > cgen_state
void create_entry_block(llvm::Function *function, CgenState *cgen_state)
void translate_for(const For *for_loop, Function *ir_reduce_loop, const ReductionCode &reduction_code, std::unordered_map< const Value *, llvm::Value * > &m, const std::unordered_map< const Function *, llvm::Function * > &f)
std::unique_ptr< Function > ir_reduce_loop
#define LOG(tag)
Definition: Logger.h:188
llvm::IRBuilder ir_builder_
Definition: CgenState.h:271
#define UNREACHABLE()
Definition: Logger.h:237
std::vector< llvm::Value * > llvm_args(const std::vector< const Value * > args, const std::unordered_map< const Value *, llvm::Value * > &m)
llvm::Type * get_fp_type(const int width, llvm::LLVMContext &context)
llvm::ICmpInst::Predicate llvm_predicate(const ICmp::Predicate predicate)
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
void translate_function(const Function *function, llvm::Function *llvm_function, const ReductionCode &reduction_code, const std::unordered_map< const Function *, llvm::Function * > &f)
const Value * end() const
const Value * start() const
false auto cgen_state
void verify_function_ir(const llvm::Function *func)
llvm::LLVMContext & context_
Definition: CgenState.h:269
CHECK(cgen_state)
const int8_t const int64_t const uint64_t const int32_t const int64_t int64_t uint32_t const int64_t int32_t * error_code
Type pointee_type(const Type pointer)
llvm::Instruction::CastOps llvm_cast_op(const Cast::CastOp op)
llvm::Type * llvm_type(const Type type, llvm::LLVMContext &ctx)
void return_early(llvm::Value *cond, const ReductionCode &reduction_code, llvm::Function *func, llvm::Value *error_code)
llvm::Value * upper_bound
Definition: JoinLoop.h:43
void translate_body(const std::vector< std::unique_ptr< Instruction >> &body, const Function *function, llvm::Function *llvm_function, const ReductionCode &reduction_code, std::unordered_map< const Value *, llvm::Value * > &m, const std::unordered_map< const Function *, llvm::Function * > &f)
static llvm::BasicBlock * codegen(const std::vector< JoinLoop > &join_loops, const std::function< llvm::BasicBlock *(const std::vector< llvm::Value * > &)> &body_codegen, llvm::Value *outer_iter, llvm::BasicBlock *exit_bb, llvm::IRBuilder<> &builder)
Definition: JoinLoop.cpp:45
llvm::Function * mapped_function(const Function *function, const std::unordered_map< const Function *, llvm::Function * > &f)
llvm::Value * mapped_value(const Value *val, const std::unordered_map< const Value *, llvm::Value * > &m)
llvm::BinaryOperator::BinaryOps llvm_binary_op(const BinaryOperator::BinaryOp op)