OmniSciDB  72c90bc290
 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 } // namespace CodegenUtil
std::optional< std::string_view > getCalledFunctionName(llvm::CallInst &call_inst)
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
int64_t bigintval
Definition: Datum.h:74
llvm::Function * findCalledFunction(llvm::CallInst &call_inst)
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:69