OmniSciDB  a5dc49c757
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
CodegenHelper.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2022 HEAVY.AI, 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 #include "CodegenHelper.h"
18 
19 namespace CodegenUtil {
20 
21 llvm::Function* findCalledFunction(llvm::CallInst& call_inst) {
22  if (llvm::Function* cf = call_inst.getCalledFunction()) {
23  return cf;
24  } else if (llvm::Value* cv = call_inst.getCalledOperand()) {
25  if (llvm::Function* cvf = llvm::dyn_cast<llvm::Function>(cv->stripPointerCasts())) {
26  // this happens when bitcast function is called first before calling the actual
27  // function i.e., %17 = call i64 bitcast (i64 (i8*)* @actual_func to i64 (i32*)*)
28  return cvf;
29  }
30  }
31  return nullptr;
32 }
33 
34 std::optional<std::string_view> getCalledFunctionName(llvm::CallInst& call_inst) {
35  if (llvm::Function* cf = findCalledFunction(call_inst)) {
36  return std::make_optional<std::string_view>(cf->getName().data(),
37  cf->getName().size());
38  }
39  return std::nullopt;
40 }
41 
42 // currently, we assume that when this is called for GPUs,
43 // the argument `num_devices_to_hoist_literal` indicates the number of devices
44 // that the system has equipped with which is reasonable under the current
45 // query execution logic, but when supporting a query execution with a specific
46 // set of devices, we must need to classify the exact devices to hoist the literal
47 // up for correctness
48 // todo (yoonmin) : support this to specific set of devices
49 std::vector<llvm::Value*> createPtrWithHoistedMemoryAddr(
50  CgenState* cgen_state,
51  CodeGenerator* code_generator,
52  CompilationOptions const& co,
53  llvm::ConstantInt* ptr_int_val,
55  size_t num_devices_to_hoist_literal) {
56  if (!co.hoist_literals) {
57  return {cgen_state->ir_builder_.CreateIntToPtr(ptr_int_val, type)};
58  }
59  Datum d;
60  d.bigintval = ptr_int_val->getSExtValue();
61  auto ptr = makeExpr<Analyzer::Constant>(kBIGINT, false, d);
62  std::vector<Analyzer::Constant const*> literals(num_devices_to_hoist_literal,
63  ptr.get());
64  auto hoisted_literal_lvs =
65  code_generator->codegenHoistedConstants(literals, kENCODING_NONE, {});
66  std::vector<llvm::Value*> hoisted_ptrs;
67  hoisted_ptrs.reserve(num_devices_to_hoist_literal);
68  for (size_t device_id = 0; device_id < num_devices_to_hoist_literal; device_id++) {
69  hoisted_ptrs[device_id] =
70  cgen_state->ir_builder_.CreateIntToPtr(hoisted_literal_lvs[device_id], type);
71  }
72  return hoisted_ptrs;
73 }
74 
75 // todo (yoonmin): support String literal
76 std::vector<llvm::Value*> hoistLiteral(CodeGenerator* code_generator,
77  CompilationOptions const& co,
78  Datum d,
80  size_t num_devices_to_hoist_literal) {
82  CHECK(type.is_integer() || type.is_decimal() || type.is_fp() || type.is_boolean());
83  auto literal_expr = makeExpr<Analyzer::Constant>(type, false, d);
84  std::vector<Analyzer::Constant const*> literals(num_devices_to_hoist_literal,
85  literal_expr.get());
86  return code_generator->codegenHoistedConstants(literals, kENCODING_NONE, {});
87 }
88 
89 } // namespace CodegenUtil
std::optional< std::string_view > getCalledFunctionName(llvm::CallInst &call_inst)
std::vector< llvm::Value * > hoistLiteral(CodeGenerator *code_generator, CompilationOptions const &co, Datum d, SQLTypeInfo type, size_t num_devices_to_hoist_literal)
bool is_fp() const
Definition: sqltypes.h:573
llvm::IRBuilder ir_builder_
Definition: CgenState.h:384
std::vector< llvm::Value * > codegenHoistedConstants(const std::vector< const Analyzer::Constant * > &constants, const EncodingType enc_type, const shared::StringDictKey &dict_id)
Definition: ConstantIR.cpp:373
bool is_integer() const
Definition: sqltypes.h:567
int64_t bigintval
Definition: Datum.h:76
bool is_boolean() const
Definition: sqltypes.h:582
llvm::Function * findCalledFunction(llvm::CallInst &call_inst)
#define CHECK(condition)
Definition: Logger.h:291
std::vector< llvm::Value * > createPtrWithHoistedMemoryAddr(CgenState *cgen_state, CodeGenerator *code_generator, CompilationOptions const &co, llvm::ConstantInt *ptr_int_val, llvm::Type *type, size_t num_devices_to_hoist_literal)
Definition: Datum.h:71
bool is_decimal() const
Definition: sqltypes.h:570