OmniSciDB  1dac507f6e
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
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

std::unique_ptr<llvm::Module> g_rt_module ( read_template_module(getGlobalLLVMContext())  )
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 1911 of file NativeCodegen.cpp.

Referenced by TableFunctionCompilationContext::generateEntryPoint().

1914  {
1915  CHECK(byte_stream_arg);
1916  const auto max_col_local_id = num_columns - 1;
1917 
1918  std::vector<llvm::Value*> col_heads;
1919  for (int col_id = 0; col_id <= max_col_local_id; ++col_id) {
1920  col_heads.emplace_back(ir_builder.CreateLoad(ir_builder.CreateGEP(
1921  byte_stream_arg, llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), col_id))));
1922  }
1923  return col_heads;
1924 }
CHECK(cgen_state)

+ Here is the caller graph for this function:

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

Definition at line 613 of file NativeCodegen.cpp.

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
llvm::StringRef get_gpu_data_layout ( )

Definition at line 605 of file NativeCodegen.cpp.

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 }
llvm::StringRef get_gpu_target_triple_string ( )

Definition at line 601 of file NativeCodegen.cpp.

601  {
602  return llvm::StringRef("nvptx64-nvidia-cuda");
603 }
bool is_rt_udf_module_present ( bool  cpu_only)

Definition at line 1222 of file NativeCodegen.cpp.

1222  {
1223  return (cpu_only || rt_udf_gpu_module != nullptr) && (rt_udf_cpu_module != nullptr);
1224 }
std::unique_ptr< llvm::Module > rt_udf_cpu_module
std::unique_ptr< llvm::Module > rt_udf_gpu_module
bool is_udf_module_present ( bool  cpu_only)

Definition at line 1218 of file NativeCodegen.cpp.

1218  {
1219  return (cpu_only || udf_gpu_module != nullptr) && (udf_cpu_module != nullptr);
1220 }
std::unique_ptr< llvm::Module > udf_gpu_module
std::unique_ptr< llvm::Module > udf_cpu_module
void read_rt_udf_cpu_module ( const std::string &  udf_ir_string)

Definition at line 1267 of file NativeCodegen.cpp.

Referenced by MapDHandler::register_runtime_extension_functions().

1267  {
1268  llvm::SMDiagnostic parse_error;
1269 
1270  auto buf =
1271  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for CPU");
1272 
1273  rt_udf_cpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1274  if (!rt_udf_cpu_module) {
1275  throw_parseIR_error(parse_error);
1276  }
1277 }
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 caller graph for this function:

void read_rt_udf_gpu_module ( const std::string &  udf_ir_string)

Definition at line 1255 of file NativeCodegen.cpp.

Referenced by MapDHandler::register_runtime_extension_functions().

1255  {
1256  llvm::SMDiagnostic parse_error;
1257 
1258  auto buf =
1259  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for GPU");
1260 
1261  rt_udf_gpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1262  if (!rt_udf_gpu_module) {
1263  throw_parseIR_error(parse_error);
1264  }
1265 }
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 caller graph for this function:

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

Definition at line 926 of file NativeCodegen.cpp.

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::unique_ptr< llvm::Module > module(runtime_module_shallow_copy(cgen_state))
std::string mapd_root_abs_path()
Definition: mapdpath.h:30
CHECK(cgen_state)
void read_udf_cpu_module ( const std::string &  udf_ir_filename)

Definition at line 1244 of file NativeCodegen.cpp.

Referenced by UdfCompiler::readCpuCompiledModule().

1244  {
1245  llvm::SMDiagnostic parse_error;
1246 
1247  llvm::StringRef file_name_arg(udf_ir_filename);
1248 
1249  udf_cpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1250  if (!udf_cpu_module) {
1251  throw_parseIR_error(parse_error, udf_ir_filename);
1252  }
1253 }
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 caller graph for this function:

void read_udf_gpu_module ( const std::string &  udf_ir_filename)

Definition at line 1233 of file NativeCodegen.cpp.

Referenced by UdfCompiler::readGpuCompiledModule().

1233  {
1234  llvm::SMDiagnostic parse_error;
1235 
1236  llvm::StringRef file_name_arg(udf_ir_filename);
1237 
1238  udf_gpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1239  if (!udf_gpu_module) {
1240  throw_parseIR_error(parse_error, udf_ir_filename);
1241  }
1242 }
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 caller graph for this function:

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 1893 of file NativeCodegen.cpp.

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

1893  {
1894  return llvm::CloneModule(
1895 #if LLVM_VERSION_MAJOR >= 7
1896  *g_rt_module.get(),
1897 #else
1898  g_rt_module.get(),
1899 #endif
1900  cgen_state->vmap_,
1901  [](const llvm::GlobalValue* gv) {
1902  auto func = llvm::dyn_cast<llvm::Function>(gv);
1903  if (!func) {
1904  return true;
1905  }
1906  return (func->getLinkage() == llvm::GlobalValue::LinkageTypes::PrivateLinkage ||
1907  func->getLinkage() == llvm::GlobalValue::LinkageTypes::InternalLinkage);
1908  });
1909 }
std::unique_ptr< llvm::Module > g_rt_module
llvm::ValueToValueMapTy vmap_
Definition: CgenState.h:268

+ Here is the caller graph for this function:

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

Definition at line 129 of file NativeCodegen.cpp.

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

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:

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

Definition at line 1226 of file NativeCodegen.cpp.

1226  {
1227  std::string excname = "LLVM IR ParseError: ";
1228  llvm::raw_string_ostream ss(excname);
1229  parse_error.print(src.c_str(), ss, false, false);
1230  throw std::runtime_error(ss.str());
1231 }
int64_t * src
void verify_function_ir ( const llvm::Function *  func)

Definition at line 162 of file NativeCodegen.cpp.

References logger::FATAL, and LOG.

Referenced by anonymous_namespace{JoinLoopTest.cpp}::create_loop_test_function(), TableFunctionCompilationContext::generateEntryPoint(), StubGenerator::generateStub(), 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:185

+ Here is the caller graph for this function:

Variable Documentation

std::unique_ptr<llvm::Module> g_rt_module
std::unique_ptr<llvm::Module> rt_udf_cpu_module

Definition at line 69 of file NativeCodegen.cpp.

Referenced by TableFunctionCompilationContext::finalize().

std::unique_ptr<llvm::Module> rt_udf_gpu_module

Definition at line 68 of file NativeCodegen.cpp.

std::unique_ptr<llvm::Module> udf_cpu_module

Definition at line 67 of file NativeCodegen.cpp.

Referenced by Executor::inlineHoistedLiterals().

std::unique_ptr<llvm::Module> udf_gpu_module

Definition at line 27 of file NativeCodegen.cpp.