OmniSciDB  c07336695a
NativeCodegen.cpp File Reference
#include "CodeGenerator.h"
#include "Execute.h"
#include "ExtensionFunctionsWhitelist.h"
#include "LLVMFunctionAttributesUtil.h"
#include "OutputBufferInitialization.h"
#include "QueryTemplateGenerator.h"
#include "Shared/mapdpath.h"
#include <llvm/Bitcode/BitcodeReader.h>
#include <llvm/Bitcode/BitcodeWriter.h>
#include <llvm/ExecutionEngine/MCJIT.h>
#include <llvm/IR/Attributes.h>
#include <llvm/IR/GlobalValue.h>
#include <llvm/IR/InstIterator.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Support/Casting.h>
#include <llvm/Support/FileSystem.h>
#include <llvm/Support/FormattedStream.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Support/TargetRegistry.h>
#include <llvm/Support/TargetSelect.h>
#include <llvm/Support/raw_os_ostream.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/IPO/AlwaysInliner.h>
#include <llvm/Transforms/InstCombine/InstCombine.h>
#include <llvm/Transforms/Instrumentation.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
#include <llvm/Linker/Linker.h>
#include <llvm/Support/raw_ostream.h>
+ Include dependency graph for NativeCodegen.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{NativeCodegen.cpp}
 

Functions

void anonymous_namespace{NativeCodegen.cpp}::eliminate_dead_self_recursive_funcs (llvm::Module &M, const std::unordered_set< llvm::Function *> &live_funcs)
 
void anonymous_namespace{NativeCodegen.cpp}::optimize_ir (llvm::Function *query_func, llvm::Module *module, const std::unordered_set< llvm::Function *> &live_funcs, const CompilationOptions &co)
 
template<class T >
std::string serialize_llvm_object (const T *llvm_obj)
 
void verify_function_ir (const llvm::Function *func)
 
std::string anonymous_namespace{NativeCodegen.cpp}::cpp_to_llvm_name (const std::string &s)
 
std::string anonymous_namespace{NativeCodegen.cpp}::gen_array_any_all_sigs ()
 
std::string anonymous_namespace{NativeCodegen.cpp}::gen_translate_null_key_sigs ()
 
void anonymous_namespace{NativeCodegen.cpp}::link_udf_module (const std::unique_ptr< llvm::Module > &udf_module, llvm::Module &module, CgenState *cgen_state, llvm::Linker::Flags flags=llvm::Linker::Flags::None)
 
llvm::StringRef get_gpu_target_triple_string ()
 
llvm::StringRef get_gpu_data_layout ()
 
std::map< std::string, std::string > get_device_parameters ()
 
llvm::Module * read_template_module (llvm::LLVMContext &context)
 
void anonymous_namespace{NativeCodegen.cpp}::bind_pos_placeholders (const std::string &pos_fn_name, const bool use_resume_param, llvm::Function *query_func, llvm::Module *module)
 
std::vector< llvm::Value * > anonymous_namespace{NativeCodegen.cpp}::generate_column_heads_load (const int num_columns, llvm::Function *query_func, llvm::LLVMContext &context)
 
void anonymous_namespace{NativeCodegen.cpp}::set_row_func_argnames (llvm::Function *row_func, const size_t in_col_count, const size_t agg_col_count, const bool hoist_literals)
 
std::pair< llvm::Function *, std::vector< llvm::Value * > > anonymous_namespace{NativeCodegen.cpp}::create_row_function (const size_t in_col_count, const size_t agg_col_count, const bool hoist_literals, llvm::Function *query_func, llvm::Module *module, llvm::LLVMContext &context)
 
void anonymous_namespace{NativeCodegen.cpp}::bind_query (llvm::Function *query_func, const std::string &query_fname, llvm::Function *multifrag_query_func, llvm::Module *module)
 
std::vector< std::string > anonymous_namespace{NativeCodegen.cpp}::get_agg_fnames (const std::vector< Analyzer::Expr *> &target_exprs, const bool is_group_by)
 
std::unique_ptr< llvm::Module > g_rt_module (read_template_module(getGlobalLLVMContext()))
 
bool is_udf_module_present (bool cpu_only)
 
bool is_rt_udf_module_present (bool cpu_only)
 
void throw_parseIR_error (const llvm::SMDiagnostic &parse_error, std::string src="")
 
void read_udf_gpu_module (const std::string &udf_ir_filename)
 
void read_udf_cpu_module (const std::string &udf_ir_filename)
 
void read_rt_udf_gpu_module (const std::string &udf_ir_string)
 
void read_rt_udf_cpu_module (const std::string &udf_ir_string)
 
template<typename InstType >
llvm::Value * anonymous_namespace{NativeCodegen.cpp}::find_variable_in_basic_block (llvm::Function *func, std::string bb_name, std::string variable_name)
 
bool anonymous_namespace{NativeCodegen.cpp}::always_clone_runtime_function (const llvm::Function *func)
 

Variables

std::unique_ptr< llvm::Module > udf_gpu_module
 
std::unique_ptr< llvm::Module > udf_cpu_module
 
std::unique_ptr< llvm::Module > rt_udf_gpu_module
 
std::unique_ptr< llvm::Module > rt_udf_cpu_module
 
const std::string anonymous_namespace{NativeCodegen.cpp}::cuda_rt_decls
 

Function Documentation

◆ g_rt_module()

std::unique_ptr<llvm::Module> g_rt_module ( read_template_module(getGlobalLLVMContext())  )

Referenced by anonymous_namespace{NativeCodegen.cpp}::get_agg_fnames().

+ Here is the caller graph for this function:

◆ get_device_parameters()

std::map<std::string, std::string> get_device_parameters ( )

Definition at line 604 of file NativeCodegen.cpp.

References checkCudaErrors(), cpu_threads(), get_gpu_data_layout(), get_gpu_target_triple_string(), run-benchmark-import::result, and to_string().

Referenced by MapDHandler::max_bytes_for_thrift().

604  {
605  std::map<std::string, std::string> result;
606 
607  result.insert(std::make_pair("cpu_name", llvm::sys::getHostCPUName()));
608  result.insert(std::make_pair("cpu_triple", llvm::sys::getProcessTriple()));
609  result.insert(
610  std::make_pair("cpu_cores", std::to_string(llvm::sys::getHostNumPhysicalCores())));
611  result.insert(std::make_pair("cpu_threads", std::to_string(cpu_threads())));
612 
613  llvm::StringMap<bool> cpu_features;
614  if (llvm::sys::getHostCPUFeatures(cpu_features)) {
615  std::string features_str = "";
616  for (auto it = cpu_features.begin(); it != cpu_features.end(); ++it) {
617  features_str += (it->getValue() ? " +" : " -");
618  features_str += it->getKey().str();
619  }
620  result.insert(std::make_pair("cpu_features", features_str));
621  }
622 
623 #ifdef HAVE_CUDA
624  int device_count = 0;
625  checkCudaErrors(cuDeviceGetCount(&device_count));
626  if (device_count) {
627  CUdevice device{};
628  char device_name[256];
629  int major = 0, minor = 0;
630  checkCudaErrors(cuDeviceGet(&device, 0)); // assuming homogeneous multi-GPU system
631  checkCudaErrors(cuDeviceGetName(device_name, 256, device));
632  checkCudaErrors(cuDeviceGetAttribute(
633  &major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device));
634  checkCudaErrors(cuDeviceGetAttribute(
635  &minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device));
636 
637  result.insert(std::make_pair("gpu_name", device_name));
638  result.insert(std::make_pair("gpu_count", std::to_string(device_count)));
639  result.insert(std::make_pair("gpu_compute_capability",
640  std::to_string(major) + "." + std::to_string(minor)));
641  result.insert(std::make_pair("gpu_triple", get_gpu_target_triple_string()));
642  result.insert(std::make_pair("gpu_datalayout", get_gpu_data_layout()));
643  }
644 #endif
645 
646  return result;
647 }
void checkCudaErrors(CUresult err)
Definition: sample.cpp:38
llvm::StringRef get_gpu_data_layout()
std::string to_string(char const *&&v)
llvm::StringRef get_gpu_target_triple_string()
int CUdevice
Definition: nocuda.h:20
int cpu_threads()
Definition: thread_count.h:23
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ get_gpu_data_layout()

llvm::StringRef get_gpu_data_layout ( )

Definition at line 596 of file NativeCodegen.cpp.

Referenced by Executor::compileWorkUnit(), and get_device_parameters().

596  {
597  return llvm::StringRef(
598  "e-p:64:64:64-i1:8:8-i8:8:8-"
599  "i16:16:16-i32:32:32-i64:64:64-"
600  "f32:32:32-f64:64:64-v16:16:16-"
601  "v32:32:32-v64:64:64-v128:128:128-n16:32:64");
602 }
+ Here is the caller graph for this function:

◆ get_gpu_target_triple_string()

llvm::StringRef get_gpu_target_triple_string ( )

Definition at line 592 of file NativeCodegen.cpp.

Referenced by Executor::compileWorkUnit(), and get_device_parameters().

592  {
593  return llvm::StringRef("nvptx64-nvidia-cuda");
594 }
+ Here is the caller graph for this function:

◆ is_rt_udf_module_present()

bool is_rt_udf_module_present ( bool  cpu_only)

Definition at line 1197 of file NativeCodegen.cpp.

References rt_udf_cpu_module, and rt_udf_gpu_module.

Referenced by Executor::compileWorkUnit(), and CodeGenerator::generateNativeGPUCode().

1197  {
1198  return (cpu_only || rt_udf_gpu_module != nullptr) && (rt_udf_cpu_module != nullptr);
1199 }
std::unique_ptr< llvm::Module > rt_udf_cpu_module
std::unique_ptr< llvm::Module > rt_udf_gpu_module
+ Here is the caller graph for this function:

◆ is_udf_module_present()

bool is_udf_module_present ( bool  cpu_only)

Definition at line 1193 of file NativeCodegen.cpp.

References udf_cpu_module, and udf_gpu_module.

Referenced by Executor::compileWorkUnit(), and CodeGenerator::generateNativeGPUCode().

1193  {
1194  return (cpu_only || udf_gpu_module != nullptr) && (udf_cpu_module != nullptr);
1195 }
std::unique_ptr< llvm::Module > udf_cpu_module
std::unique_ptr< llvm::Module > udf_gpu_module
+ Here is the caller graph for this function:

◆ read_rt_udf_cpu_module()

void read_rt_udf_cpu_module ( const std::string &  udf_ir_string)

Definition at line 1242 of file NativeCodegen.cpp.

References getGlobalLLVMContext(), rt_udf_cpu_module, and throw_parseIR_error().

Referenced by MapDHandler::register_runtime_udf().

1242  {
1243  llvm::SMDiagnostic parse_error;
1244 
1245  auto buf =
1246  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for CPU");
1247 
1248  rt_udf_cpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1249  if (!rt_udf_cpu_module) {
1250  throw_parseIR_error(parse_error);
1251  }
1252 }
std::unique_ptr< llvm::Module > rt_udf_cpu_module
llvm::LLVMContext & getGlobalLLVMContext()
void throw_parseIR_error(const llvm::SMDiagnostic &parse_error, std::string src="")
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_rt_udf_gpu_module()

void read_rt_udf_gpu_module ( const std::string &  udf_ir_string)

Definition at line 1230 of file NativeCodegen.cpp.

References getGlobalLLVMContext(), rt_udf_gpu_module, and throw_parseIR_error().

Referenced by MapDHandler::register_runtime_udf().

1230  {
1231  llvm::SMDiagnostic parse_error;
1232 
1233  auto buf =
1234  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for GPU");
1235 
1236  rt_udf_gpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1237  if (!rt_udf_gpu_module) {
1238  throw_parseIR_error(parse_error);
1239  }
1240 }
std::unique_ptr< llvm::Module > rt_udf_gpu_module
llvm::LLVMContext & getGlobalLLVMContext()
void throw_parseIR_error(const llvm::SMDiagnostic &parse_error, std::string src="")
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_template_module()

llvm::Module* read_template_module ( llvm::LLVMContext &  context)

Definition at line 888 of file NativeCodegen.cpp.

References CHECK, and mapd_root_abs_path().

Referenced by anonymous_namespace{NativeCodegen.cpp}::get_agg_fnames(), ll_bool(), and TEST().

888  {
889  llvm::SMDiagnostic err;
890 
891  auto buffer_or_error = llvm::MemoryBuffer::getFile(mapd_root_abs_path() +
892  "/QueryEngine/RuntimeFunctions.bc");
893  CHECK(!buffer_or_error.getError());
894  llvm::MemoryBuffer* buffer = buffer_or_error.get().get();
895 
896  auto owner = llvm::parseBitcodeFile(buffer->getMemBufferRef(), context);
897  CHECK(!owner.takeError());
898  auto module = owner.get().release();
899  CHECK(module);
900 
901  return module;
902 }
std::string mapd_root_abs_path()
Definition: mapdpath.h:30
#define CHECK(condition)
Definition: Logger.h:187
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_udf_cpu_module()

void read_udf_cpu_module ( const std::string &  udf_ir_filename)

Definition at line 1219 of file NativeCodegen.cpp.

References getGlobalLLVMContext(), throw_parseIR_error(), and udf_cpu_module.

Referenced by UdfCompiler::readCompiledModules().

1219  {
1220  llvm::SMDiagnostic parse_error;
1221 
1222  llvm::StringRef file_name_arg(udf_ir_filename);
1223 
1224  udf_cpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1225  if (!udf_cpu_module) {
1226  throw_parseIR_error(parse_error, udf_ir_filename);
1227  }
1228 }
std::unique_ptr< llvm::Module > udf_cpu_module
llvm::LLVMContext & getGlobalLLVMContext()
void throw_parseIR_error(const llvm::SMDiagnostic &parse_error, std::string src="")
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ read_udf_gpu_module()

void read_udf_gpu_module ( const std::string &  udf_ir_filename)

Definition at line 1208 of file NativeCodegen.cpp.

References getGlobalLLVMContext(), throw_parseIR_error(), and udf_gpu_module.

Referenced by UdfCompiler::readCompiledModules().

1208  {
1209  llvm::SMDiagnostic parse_error;
1210 
1211  llvm::StringRef file_name_arg(udf_ir_filename);
1212 
1213  udf_gpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1214  if (!udf_gpu_module) {
1215  throw_parseIR_error(parse_error, udf_ir_filename);
1216  }
1217 }
std::unique_ptr< llvm::Module > udf_gpu_module
llvm::LLVMContext & getGlobalLLVMContext()
void throw_parseIR_error(const llvm::SMDiagnostic &parse_error, std::string src="")
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ serialize_llvm_object()

template<class T >
std::string serialize_llvm_object ( const T *  llvm_obj)

Definition at line 128 of file NativeCodegen.cpp.

Referenced by Executor::compileWorkUnit(), ResultSetReductionJIT::finalizeReductionCode(), ll_bool(), Executor::optimizeAndCodegenCPU(), and Executor::optimizeAndCodegenGPU().

128  {
129  std::stringstream ss;
130  llvm::raw_os_ostream os(ss);
131  llvm_obj->print(os);
132  os.flush();
133  return ss.str();
134 }
+ Here is the caller graph for this function:

◆ throw_parseIR_error()

void throw_parseIR_error ( const llvm::SMDiagnostic &  parse_error,
std::string  src = "" 
)

Definition at line 1201 of file NativeCodegen.cpp.

References src.

Referenced by read_rt_udf_cpu_module(), read_rt_udf_gpu_module(), read_udf_cpu_module(), and read_udf_gpu_module().

1201  {
1202  std::string excname = "LLVM IR ParseError: ";
1203  llvm::raw_string_ostream ss(excname);
1204  parse_error.print(src.c_str(), ss, false, false);
1205  throw std::runtime_error(ss.str());
1206 }
int64_t * src
+ Here is the caller graph for this function:

◆ verify_function_ir()

void verify_function_ir ( const llvm::Function *  func)

Definition at line 161 of file NativeCodegen.cpp.

References logger::FATAL, and LOG.

Referenced by Executor::compileWorkUnit(), ResultSetReductionJIT::isEmpty(), ll_bool(), ResultSetReductionJIT::reduceLoop(), ResultSetReductionJIT::reduceOneEntryBaseline(), ResultSetReductionJIT::reduceOneEntryBaselineIdx(), ResultSetReductionJIT::reduceOneEntryNoCollisions(), ResultSetReductionJIT::reduceOneEntryNoCollisionsIdx(), ResultSetReductionJIT::reduceOneEntryTargetsNoCollisions(), and TEST().

161  {
162  std::stringstream err_ss;
163  llvm::raw_os_ostream err_os(err_ss);
164  if (llvm::verifyFunction(*func, &err_os)) {
165  func->print(llvm::outs());
166  LOG(FATAL) << err_ss.str();
167  }
168 }
#define LOG(tag)
Definition: Logger.h:182
+ Here is the caller graph for this function:

Variable Documentation

◆ rt_udf_cpu_module

std::unique_ptr<llvm::Module> rt_udf_cpu_module

◆ rt_udf_gpu_module

std::unique_ptr<llvm::Module> rt_udf_gpu_module

◆ udf_cpu_module

std::unique_ptr<llvm::Module> udf_cpu_module

◆ udf_gpu_module

std::unique_ptr<llvm::Module> udf_gpu_module