OmniSciDB  b24e664e58
 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  int 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  if (func->getReturnType()->isVoidTy()) {
150  cgen_state->ir_builder_.CreateRetVoid();
151  } else {
152  cgen_state->ir_builder_.CreateRet(cgen_state->llInt<int32_t>(error_code));
153  }
154  cgen_state->ir_builder_.SetInsertPoint(do_reduction);
155 }
156 
157 // Returns the corresponding LLVM value for the given IR value.
158 llvm::Value* mapped_value(const Value* val,
159  const std::unordered_map<const Value*, llvm::Value*>& m) {
160  const auto it = m.find(val);
161  CHECK(it != m.end());
162  return it->second;
163 }
164 
165 // Returns the corresponding LLVM function for the given IR function.
166 llvm::Function* mapped_function(
167  const Function* function,
168  const std::unordered_map<const Function*, llvm::Function*>& f) {
169  const auto it = f.find(function);
170  CHECK(it != f.end());
171  return it->second;
172 }
173 
174 // Given a list of IR values and the mapping, return the list of corresponding LLVM IR
175 // values.
176 std::vector<llvm::Value*> llvm_args(
177  const std::vector<const Value*> args,
178  const std::unordered_map<const Value*, llvm::Value*>& m) {
179  std::vector<llvm::Value*> llvm_args;
180  std::transform(
181  args.begin(), args.end(), std::back_inserter(llvm_args), [&m](const Value* value) {
182  return mapped_value(value, m);
183  });
184  return llvm_args;
185 }
186 
187 void translate_for(const For* for_loop,
188  Function* ir_reduce_loop,
189  const ReductionCode& reduction_code,
190  std::unordered_map<const Value*, llvm::Value*>& m,
191  const std::unordered_map<const Function*, llvm::Function*>& f);
192 
193 // Translate a list of instructions to LLVM IR.
194 void translate_body(const std::vector<std::unique_ptr<Instruction>>& body,
195  const Function* function,
196  llvm::Function* llvm_function,
197  const ReductionCode& reduction_code,
198  std::unordered_map<const Value*, llvm::Value*>& m,
199  const std::unordered_map<const Function*, llvm::Function*>& f) {
200  auto cgen_state = reduction_code.cgen_state.get();
201  auto& ctx = cgen_state->context_;
202  for (const auto& instr : body) {
203  const auto instr_ptr = instr.get();
204  llvm::Value* translated{nullptr};
205  if (auto gep = dynamic_cast<const GetElementPtr*>(instr_ptr)) {
206  translated = cgen_state->ir_builder_.CreateGEP(
207  mapped_value(gep->base(), m), mapped_value(gep->index(), m), gep->label());
208  } else if (auto load = dynamic_cast<const Load*>(instr_ptr)) {
209  translated = cgen_state->ir_builder_.CreateLoad(mapped_value(load->source(), m),
210  load->label());
211  } else if (auto icmp = dynamic_cast<const ICmp*>(instr_ptr)) {
212  translated = cgen_state->ir_builder_.CreateICmp(llvm_predicate(icmp->predicate()),
213  mapped_value(icmp->lhs(), m),
214  mapped_value(icmp->rhs(), m),
215  icmp->label());
216  } else if (auto binary_operator = dynamic_cast<const BinaryOperator*>(instr_ptr)) {
217  translated =
218  cgen_state->ir_builder_.CreateBinOp(llvm_binary_op(binary_operator->op()),
219  mapped_value(binary_operator->lhs(), m),
220  mapped_value(binary_operator->rhs(), m),
221  binary_operator->label());
222  } else if (auto cast = dynamic_cast<const Cast*>(instr_ptr)) {
223  translated = cgen_state->ir_builder_.CreateCast(llvm_cast_op(cast->op()),
224  mapped_value(cast->source(), m),
225  llvm_type(cast->type(), ctx),
226  cast->label());
227  } else if (auto ret = dynamic_cast<const Ret*>(instr_ptr)) {
228  if (ret->value()) {
229  cgen_state->ir_builder_.CreateRet(mapped_value(ret->value(), m));
230  } else {
231  cgen_state->ir_builder_.CreateRetVoid();
232  }
233  } else if (auto call = dynamic_cast<const Call*>(instr_ptr)) {
234  std::vector<llvm::Value*> llvm_args;
235  const auto args = call->arguments();
236  std::transform(args.begin(),
237  args.end(),
238  std::back_inserter(llvm_args),
239  [&m](const Value* value) { return mapped_value(value, m); });
240  if (call->callee()) {
241  translated = cgen_state->ir_builder_.CreateCall(
242  mapped_function(call->callee(), f), llvm_args, call->label());
243  } else {
244  cgen_state->emitCall(call->callee_name(), llvm_args);
245  }
246  } else if (auto external_call = dynamic_cast<const ExternalCall*>(instr_ptr)) {
247  translated = cgen_state->emitExternalCall(external_call->callee_name(),
248  llvm_type(external_call->type(), ctx),
249  llvm_args(external_call->arguments(), m));
250  } else if (auto alloca = dynamic_cast<const Alloca*>(instr_ptr)) {
251  translated = cgen_state->ir_builder_.CreateAlloca(
252  llvm_type(pointee_type(alloca->type()), ctx),
253  mapped_value(alloca->array_size(), m),
254  alloca->label());
255  } else if (auto memcpy = dynamic_cast<const MemCpy*>(instr_ptr)) {
256  cgen_state->ir_builder_.CreateMemCpy(mapped_value(memcpy->dest(), m),
257  0,
258  mapped_value(memcpy->source(), m),
259  0,
260  mapped_value(memcpy->size(), m));
261  } else if (auto ret_early = dynamic_cast<const ReturnEarly*>(instr_ptr)) {
262  return_early(mapped_value(ret_early->cond(), m),
263  reduction_code,
264  llvm_function,
265  ret_early->error_code());
266  } else if (auto for_loop = dynamic_cast<const For*>(instr_ptr)) {
267  translate_for(for_loop, reduction_code.ir_reduce_loop.get(), reduction_code, m, f);
268  } else {
269  LOG(FATAL) << "Instruction not supported yet";
270  }
271  if (translated) {
272  const auto it_ok = m.emplace(instr_ptr, translated);
273  CHECK(it_ok.second);
274  }
275  }
276 }
277 
278 // Translate a loop to LLVM IR, using existing loop construction facilities.
279 void translate_for(const For* for_loop,
280  Function* ir_reduce_loop,
281  const ReductionCode& reduction_code,
282  std::unordered_map<const Value*, llvm::Value*>& m,
283  const std::unordered_map<const Function*, llvm::Function*>& f) {
284  auto cgen_state = reduction_code.cgen_state.get();
285  const auto bb_entry = cgen_state->ir_builder_.GetInsertBlock();
286  auto& ctx = cgen_state->context_;
287  const auto i64_type = get_int_type(64, cgen_state->context_);
288  const auto end_index = mapped_value(for_loop->end(), m);
289  const auto start_index = mapped_value(for_loop->start(), m);
290  // The start and end indices are absolute. Subtract the start index from the iterator.
291  const auto iteration_count =
292  cgen_state->ir_builder_.CreateSub(end_index, start_index, "iteration_count");
293  const auto upper_bound = cgen_state->ir_builder_.CreateSExt(iteration_count, i64_type);
294  const auto bb_exit =
295  llvm::BasicBlock::Create(ctx, ".exit", mapped_function(ir_reduce_loop, f));
296  JoinLoop join_loop(
299  [upper_bound](const std::vector<llvm::Value*>& v) {
300  JoinLoopDomain domain{{0}};
301  domain.upper_bound = upper_bound;
302  return domain;
303  },
304  nullptr,
305  nullptr,
306  nullptr,
307  "reduction_loop");
308  const auto bb_loop_body = JoinLoop::codegen(
309  {join_loop},
310  [cgen_state, for_loop, ir_reduce_loop, &f, &m, &reduction_code](
311  const std::vector<llvm::Value*>& iterators) {
312  const auto loop_body_bb = llvm::BasicBlock::Create(
313  cgen_state->context_,
314  ".loop_body",
315  cgen_state->ir_builder_.GetInsertBlock()->getParent());
316  cgen_state->ir_builder_.SetInsertPoint(loop_body_bb);
317  // Make the iterator the same type as start and end indices (32-bit integer).
318  const auto loop_iter =
319  cgen_state->ir_builder_.CreateTrunc(iterators.back(),
320  get_int_type(32, cgen_state->context_),
321  "relative_entry_idx");
322  m.emplace(for_loop->iter(), loop_iter);
323  translate_body(for_loop->body(),
324  ir_reduce_loop,
325  mapped_function(ir_reduce_loop, f),
326  reduction_code,
327  m,
328  f);
329  return loop_body_bb;
330  },
331  nullptr,
332  bb_exit,
333  cgen_state->ir_builder_);
334  cgen_state->ir_builder_.SetInsertPoint(bb_entry);
335  cgen_state->ir_builder_.CreateBr(bb_loop_body);
336  cgen_state->ir_builder_.SetInsertPoint(bb_exit);
337 }
338 
339 // Create the entry basic block into an initially empty function.
340 void create_entry_block(llvm::Function* function, CgenState* cgen_state) {
341  const auto bb_entry =
342  llvm::BasicBlock::Create(cgen_state->context_, ".entry", function, 0);
343  cgen_state->ir_builder_.SetInsertPoint(bb_entry);
344 }
345 
346 } // namespace
347 
348 void translate_function(const Function* function,
349  llvm::Function* llvm_function,
350  const ReductionCode& reduction_code,
351  const std::unordered_map<const Function*, llvm::Function*>& f) {
352  auto cgen_state = reduction_code.cgen_state.get();
353  create_entry_block(llvm_function, cgen_state);
354  // Set the value mapping based on the input arguments.
355  std::unordered_map<const Value*, llvm::Value*> m;
356  auto llvm_arg_it = llvm_function->arg_begin();
357  for (size_t arg_idx = 0; arg_idx < function->arg_types().size(); ++arg_idx) {
358  llvm::Value* llvm_arg = &(*llvm_arg_it);
359  const auto it_ok = m.emplace(function->arg(arg_idx), llvm_arg);
360  CHECK(it_ok.second);
361  ++llvm_arg_it;
362  }
363  // Add mapping for the constants used by the function.
364  for (const auto& constant : function->constants()) {
365  llvm::Value* constant_llvm{nullptr};
366  switch (constant->type()) {
367  case Type::Int8: {
368  constant_llvm =
369  cgen_state->llInt<int8_t>(static_cast<ConstantInt*>(constant.get())->value());
370  break;
371  }
372  case Type::Int32: {
373  constant_llvm = cgen_state->llInt<int32_t>(
374  static_cast<ConstantInt*>(constant.get())->value());
375  break;
376  }
377  case Type::Int64: {
378  constant_llvm = cgen_state->llInt<int64_t>(
379  static_cast<ConstantInt*>(constant.get())->value());
380  break;
381  }
382  case Type::Float: {
383  constant_llvm = cgen_state->llFp(
384  static_cast<float>(static_cast<ConstantFP*>(constant.get())->value()));
385  break;
386  }
387  case Type::Double: {
388  constant_llvm =
389  cgen_state->llFp(static_cast<ConstantFP*>(constant.get())->value());
390  break;
391  }
392  default: {
393  LOG(FATAL) << "Constant type not supported: "
394  << static_cast<int>(constant->type());
395  }
396  }
397  CHECK(constant_llvm);
398  const auto it_ok = m.emplace(constant.get(), constant_llvm);
399  CHECK(it_ok.second);
400  }
401  translate_body(function->body(), function, llvm_function, reduction_code, m, f);
402  verify_function_ir(llvm_function);
403 }
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:185
llvm::IRBuilder ir_builder_
Definition: CgenState.h:269
#define UNREACHABLE()
Definition: Logger.h:234
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:267
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)
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)
void return_early(llvm::Value *cond, const ReductionCode &reduction_code, llvm::Function *func, int error_code)
llvm::BinaryOperator::BinaryOps llvm_binary_op(const BinaryOperator::BinaryOp op)