OmniSciDB  addbbd5075
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
anonymous_namespace{ResultSetReductionCodegen.cpp} Namespace Reference

Functions

llvm::ICmpInst::Predicate llvm_predicate (const ICmp::Predicate predicate)
 
llvm::BinaryOperator::BinaryOps llvm_binary_op (const BinaryOperator::BinaryOp op)
 
llvm::Instruction::CastOps llvm_cast_op (const Cast::CastOp op)
 
void return_early (llvm::Value *cond, const ReductionCode &reduction_code, llvm::Function *func, llvm::Value *error_code)
 
llvm::Value * mapped_value (const Value *val, const std::unordered_map< const Value *, llvm::Value * > &m)
 
llvm::Function * mapped_function (const Function *function, const std::unordered_map< const Function *, llvm::Function * > &f)
 
std::vector< llvm::Value * > llvm_args (const std::vector< const Value * > args, const std::unordered_map< const Value *, llvm::Value * > &m)
 
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)
 
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)
 
void create_entry_block (llvm::Function *function, CgenState *cgen_state)
 

Function Documentation

void anonymous_namespace{ResultSetReductionCodegen.cpp}::create_entry_block ( llvm::Function *  function,
CgenState cgen_state 
)

Definition at line 347 of file ResultSetReductionCodegen.cpp.

References CgenState::context_, and CgenState::ir_builder_.

Referenced by translate_function().

347  {
348  const auto bb_entry =
349  llvm::BasicBlock::Create(cgen_state->context_, ".entry", function, 0);
350  cgen_state->ir_builder_.SetInsertPoint(bb_entry);
351 }
llvm::IRBuilder ir_builder_
Definition: CgenState.h:271
llvm::LLVMContext & context_
Definition: CgenState.h:269

+ Here is the caller graph for this function:

std::vector<llvm::Value*> anonymous_namespace{ResultSetReductionCodegen.cpp}::llvm_args ( const std::vector< const Value * >  args,
const std::unordered_map< const Value *, llvm::Value * > &  m 
)

Definition at line 183 of file ResultSetReductionCodegen.cpp.

References mapped_value().

Referenced by translate_body().

185  {
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 }
std::vector< llvm::Value * > llvm_args(const std::vector< const Value * > args, const std::unordered_map< const Value *, llvm::Value * > &m)
llvm::Value * mapped_value(const Value *val, const std::unordered_map< const Value *, llvm::Value * > &m)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::BinaryOperator::BinaryOps anonymous_namespace{ResultSetReductionCodegen.cpp}::llvm_binary_op ( const BinaryOperator::BinaryOp  op)

Definition at line 99 of file ResultSetReductionCodegen.cpp.

References BinaryOperator::Add, logger::FATAL, LOG, BinaryOperator::Mul, and UNREACHABLE.

Referenced by translate_body().

99  {
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 }
#define LOG(tag)
Definition: Logger.h:188
#define UNREACHABLE()
Definition: Logger.h:237

+ Here is the caller graph for this function:

llvm::Instruction::CastOps anonymous_namespace{ResultSetReductionCodegen.cpp}::llvm_cast_op ( const Cast::CastOp  op)

Definition at line 116 of file ResultSetReductionCodegen.cpp.

References Cast::BitCast, logger::FATAL, LOG, Cast::SExt, Cast::Trunc, and UNREACHABLE.

Referenced by translate_body().

116  {
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 }
#define LOG(tag)
Definition: Logger.h:188
#define UNREACHABLE()
Definition: Logger.h:237

+ Here is the caller graph for this function:

llvm::ICmpInst::Predicate anonymous_namespace{ResultSetReductionCodegen.cpp}::llvm_predicate ( const ICmp::Predicate  predicate)

Definition at line 82 of file ResultSetReductionCodegen.cpp.

References ICmp::EQ, logger::FATAL, LOG, ICmp::NE, and UNREACHABLE.

Referenced by translate_body().

82  {
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 }
#define LOG(tag)
Definition: Logger.h:188
#define UNREACHABLE()
Definition: Logger.h:237

+ Here is the caller graph for this function:

llvm::Function* anonymous_namespace{ResultSetReductionCodegen.cpp}::mapped_function ( const Function function,
const std::unordered_map< const Function *, llvm::Function * > &  f 
)

Definition at line 173 of file ResultSetReductionCodegen.cpp.

References CHECK().

Referenced by translate_body(), and translate_for().

175  {
176  const auto it = f.find(function);
177  CHECK(it != f.end());
178  return it->second;
179 }
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

llvm::Value* anonymous_namespace{ResultSetReductionCodegen.cpp}::mapped_value ( const Value val,
const std::unordered_map< const Value *, llvm::Value * > &  m 
)

Definition at line 161 of file ResultSetReductionCodegen.cpp.

References CHECK().

Referenced by llvm_args(), translate_body(), and translate_for().

162  {
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 }
CHECK(cgen_state)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{ResultSetReductionCodegen.cpp}::return_early ( llvm::Value *  cond,
const ReductionCode reduction_code,
llvm::Function *  func,
llvm::Value *  error_code 
)

Definition at line 139 of file ResultSetReductionCodegen.cpp.

References ReductionCode::cgen_state, cgen_state, and CHECK().

Referenced by translate_body().

142  {
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 }
std::unique_ptr< CgenState > cgen_state
false auto cgen_state
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

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{ResultSetReductionCodegen.cpp}::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 
)

Definition at line 201 of file ResultSetReductionCodegen.cpp.

References run_benchmark_import::args, ReductionCode::cgen_state, cgen_state, CHECK(), logger::FATAL, ReductionCode::ir_reduce_loop, llvm_args(), llvm_binary_op(), llvm_cast_op(), llvm_predicate(), llvm_type(), LOG, mapped_function(), mapped_value(), pointee_type(), return_early(), and translate_for().

Referenced by translate_for(), and translate_function().

206  {
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 }
std::unique_ptr< 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
std::vector< llvm::Value * > llvm_args(const std::vector< const Value * > args, const std::unordered_map< const Value *, llvm::Value * > &m)
llvm::ICmpInst::Predicate llvm_predicate(const ICmp::Predicate predicate)
false auto cgen_state
CHECK(cgen_state)
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::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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function:

void anonymous_namespace{ResultSetReductionCodegen.cpp}::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 
)

Definition at line 286 of file ResultSetReductionCodegen.cpp.

References ReductionCode::cgen_state, cgen_state, JoinLoop::codegen(), For::end(), get_int_type(), INNER, mapped_function(), mapped_value(), For::start(), translate_body(), JoinLoopDomain::upper_bound, and UpperBound.

Referenced by translate_body().

290  {
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 }
std::unique_ptr< CgenState > cgen_state
llvm::Type * get_int_type(const int width, llvm::LLVMContext &context)
const Value * end() const
const Value * start() const
false auto cgen_state
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)

+ Here is the call graph for this function:

+ Here is the caller graph for this function: