OmniSciDB  29e35f4d58
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 ()
 
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)
 
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)
 
std::unique_ptr< llvm::Module > runtime_module_shallow_copy (CgenState *cgen_state)
 
std::vector< llvm::Value * > generate_column_heads_load (const int num_columns, llvm::Value *byte_stream_arg, llvm::IRBuilder<> &ir_builder, llvm::LLVMContext &ctx)
 

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
 
std::unique_ptr< llvm::Module > g_rt_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())  )

◆ generate_column_heads_load()

std::vector<llvm::Value*> generate_column_heads_load ( const int  num_columns,
llvm::Value *  byte_stream_arg,
llvm::IRBuilder<> &  ir_builder,
llvm::LLVMContext &  ctx 
)

Loads individual columns from a single, packed pointers buffer (the byte stream arg)

Definition at line 1915 of file NativeCodegen.cpp.

References CHECK.

Referenced by anonymous_namespace{NativeCodegen.cpp}::create_row_function(), and TableFunctionCompilationContext::generateEntryPoint().

1918  {
1919  CHECK(byte_stream_arg);
1920  const auto max_col_local_id = num_columns - 1;
1921 
1922  std::vector<llvm::Value*> col_heads;
1923  for (int col_id = 0; col_id <= max_col_local_id; ++col_id) {
1924  col_heads.emplace_back(ir_builder.CreateLoad(ir_builder.CreateGEP(
1925  byte_stream_arg, llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), col_id))));
1926  }
1927  return col_heads;
1928 }
#define CHECK(condition)
Definition: Logger.h:193
+ Here is the caller graph for this function:

◆ get_device_parameters()

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

Definition at line 613 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().

613  {
614  std::map<std::string, std::string> result;
615 
616  result.insert(std::make_pair("cpu_name", llvm::sys::getHostCPUName()));
617  result.insert(std::make_pair("cpu_triple", llvm::sys::getProcessTriple()));
618  result.insert(
619  std::make_pair("cpu_cores", std::to_string(llvm::sys::getHostNumPhysicalCores())));
620  result.insert(std::make_pair("cpu_threads", std::to_string(cpu_threads())));
621 
622  llvm::StringMap<bool> cpu_features;
623  if (llvm::sys::getHostCPUFeatures(cpu_features)) {
624  std::string features_str = "";
625  for (auto it = cpu_features.begin(); it != cpu_features.end(); ++it) {
626  features_str += (it->getValue() ? " +" : " -");
627  features_str += it->getKey().str();
628  }
629  result.insert(std::make_pair("cpu_features", features_str));
630  }
631 
632 #ifdef HAVE_CUDA
633  int device_count = 0;
634  checkCudaErrors(cuDeviceGetCount(&device_count));
635  if (device_count) {
636  CUdevice device{};
637  char device_name[256];
638  int major = 0, minor = 0;
639  checkCudaErrors(cuDeviceGet(&device, 0)); // assuming homogeneous multi-GPU system
640  checkCudaErrors(cuDeviceGetName(device_name, 256, device));
641  checkCudaErrors(cuDeviceGetAttribute(
642  &major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device));
643  checkCudaErrors(cuDeviceGetAttribute(
644  &minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device));
645 
646  result.insert(std::make_pair("gpu_name", device_name));
647  result.insert(std::make_pair("gpu_count", std::to_string(device_count)));
648  result.insert(std::make_pair("gpu_compute_capability",
649  std::to_string(major) + "." + std::to_string(minor)));
650  result.insert(std::make_pair("gpu_triple", get_gpu_target_triple_string()));
651  result.insert(std::make_pair("gpu_datalayout", get_gpu_data_layout()));
652  }
653 #endif
654 
655  return result;
656 }
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:25
+ 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 605 of file NativeCodegen.cpp.

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

605  {
606  return llvm::StringRef(
607  "e-p:64:64:64-i1:8:8-i8:8:8-"
608  "i16:16:16-i32:32:32-i64:64:64-"
609  "f32:32:32-f64:64:64-v16:16:16-"
610  "v32:32:32-v64:64:64-v128:128:128-n16:32:64");
611 }
+ Here is the caller graph for this function:

◆ get_gpu_target_triple_string()

llvm::StringRef get_gpu_target_triple_string ( )

Definition at line 601 of file NativeCodegen.cpp.

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

601  {
602  return llvm::StringRef("nvptx64-nvidia-cuda");
603 }
+ 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 1226 of file NativeCodegen.cpp.

References rt_udf_cpu_module, and rt_udf_gpu_module.

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

1226  {
1227  return (cpu_only || rt_udf_gpu_module != nullptr) && (rt_udf_cpu_module != nullptr);
1228 }
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 1222 of file NativeCodegen.cpp.

References udf_cpu_module, and udf_gpu_module.

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

1222  {
1223  return (cpu_only || udf_gpu_module != nullptr) && (udf_cpu_module != nullptr);
1224 }
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 1271 of file NativeCodegen.cpp.

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

Referenced by MapDHandler::register_runtime_extension_functions().

1271  {
1272  llvm::SMDiagnostic parse_error;
1273 
1274  auto buf =
1275  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for CPU");
1276 
1277  rt_udf_cpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1278  if (!rt_udf_cpu_module) {
1279  throw_parseIR_error(parse_error);
1280  }
1281 }
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 1259 of file NativeCodegen.cpp.

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

Referenced by MapDHandler::register_runtime_extension_functions().

1259  {
1260  llvm::SMDiagnostic parse_error;
1261 
1262  auto buf =
1263  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for GPU");
1264 
1265  rt_udf_gpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1266  if (!rt_udf_gpu_module) {
1267  throw_parseIR_error(parse_error);
1268  }
1269 }
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 926 of file NativeCodegen.cpp.

References CHECK, and mapd_root_abs_path().

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

926  {
927  llvm::SMDiagnostic err;
928 
929  auto buffer_or_error = llvm::MemoryBuffer::getFile(mapd_root_abs_path() +
930  "/QueryEngine/RuntimeFunctions.bc");
931  CHECK(!buffer_or_error.getError());
932  llvm::MemoryBuffer* buffer = buffer_or_error.get().get();
933 
934  auto owner = llvm::parseBitcodeFile(buffer->getMemBufferRef(), context);
935  CHECK(!owner.takeError());
936  auto module = owner.get().release();
937  CHECK(module);
938 
939  return module;
940 }
std::string mapd_root_abs_path()
Definition: mapdpath.h:30
#define CHECK(condition)
Definition: Logger.h:193
+ 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 1248 of file NativeCodegen.cpp.

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

Referenced by UdfCompiler::readCpuCompiledModule().

1248  {
1249  llvm::SMDiagnostic parse_error;
1250 
1251  llvm::StringRef file_name_arg(udf_ir_filename);
1252 
1253  udf_cpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1254  if (!udf_cpu_module) {
1255  throw_parseIR_error(parse_error, udf_ir_filename);
1256  }
1257 }
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 1237 of file NativeCodegen.cpp.

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

Referenced by UdfCompiler::readGpuCompiledModule().

1237  {
1238  llvm::SMDiagnostic parse_error;
1239 
1240  llvm::StringRef file_name_arg(udf_ir_filename);
1241 
1242  udf_gpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1243  if (!udf_gpu_module) {
1244  throw_parseIR_error(parse_error, udf_ir_filename);
1245  }
1246 }
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:

◆ runtime_module_shallow_copy()

std::unique_ptr<llvm::Module> runtime_module_shallow_copy ( CgenState cgen_state)

Makes a shallow copy (just declarations) of the runtime module. Function definitions are cloned only if they're used from the generated code.

Definition at line 1897 of file NativeCodegen.cpp.

References CgenState::vmap_.

Referenced by ResultSetReductionJIT::codegen(), StubGenerator::generateStub(), and TableFunctionCompilationContext::TableFunctionCompilationContext().

1897  {
1898  return llvm::CloneModule(
1899 #if LLVM_VERSION_MAJOR >= 7
1900  *g_rt_module.get(),
1901 #else
1902  g_rt_module.get(),
1903 #endif
1904  cgen_state->vmap_,
1905  [](const llvm::GlobalValue* gv) {
1906  auto func = llvm::dyn_cast<llvm::Function>(gv);
1907  if (!func) {
1908  return true;
1909  }
1910  return (func->getLinkage() == llvm::GlobalValue::LinkageTypes::PrivateLinkage ||
1911  func->getLinkage() == llvm::GlobalValue::LinkageTypes::InternalLinkage);
1912  });
1913 }
std::unique_ptr< llvm::Module > g_rt_module
llvm::ValueToValueMapTy vmap_
Definition: CgenState.h:270
+ 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 129 of file NativeCodegen.cpp.

Referenced by Executor::compileWorkUnit(), TableFunctionCompilationContext::finalize(), ll_bool(), Executor::optimizeAndCodegenCPU(), and Executor::optimizeAndCodegenGPU().

129  {
130  std::stringstream ss;
131  llvm::raw_os_ostream os(ss);
132  llvm_obj->print(os);
133  os.flush();
134  return ss.str();
135 }
+ 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 1230 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().

1230  {
1231  std::string excname = "LLVM IR ParseError: ";
1232  llvm::raw_string_ostream ss(excname);
1233  parse_error.print(src.c_str(), ss, false, false);
1234  throw std::runtime_error(ss.str());
1235 }
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 162 of file NativeCodegen.cpp.

References logger::FATAL, and LOG.

Referenced by Executor::compileWorkUnit(), TableFunctionCompilationContext::generateEntryPoint(), StubGenerator::generateStub(), ll_bool(), and translate_function().

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

Variable Documentation

◆ g_rt_module

std::unique_ptr<llvm::Module> g_rt_module

◆ 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