OmniSciDB  0fdbebe030
 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/MathUtils.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)
 
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 1983 of file NativeCodegen.cpp.

Referenced by TableFunctionCompilationContext::generateEntryPoint().

1986  {
1987  CHECK(byte_stream_arg);
1988  const auto max_col_local_id = num_columns - 1;
1989 
1990  std::vector<llvm::Value*> col_heads;
1991  for (int col_id = 0; col_id <= max_col_local_id; ++col_id) {
1992  col_heads.emplace_back(ir_builder.CreateLoad(ir_builder.CreateGEP(
1993  byte_stream_arg, llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), col_id))));
1994  }
1995  return col_heads;
1996 }
CHECK(cgen_state)

+ Here is the caller graph for this function:

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

Definition at line 607 of file NativeCodegen.cpp.

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

599  {
600  return llvm::StringRef(
601  "e-p:64:64:64-i1:8:8-i8:8:8-"
602  "i16:16:16-i32:32:32-i64:64:64-"
603  "f32:32:32-f64:64:64-v16:16:16-"
604  "v32:32:32-v64:64:64-v128:128:128-n16:32:64");
605 }
llvm::StringRef get_gpu_target_triple_string ( )

Definition at line 595 of file NativeCodegen.cpp.

595  {
596  return llvm::StringRef("nvptx64-nvidia-cuda");
597 }
bool is_rt_udf_module_present ( bool  cpu_only)

Definition at line 1215 of file NativeCodegen.cpp.

1215  {
1216  return (cpu_only || rt_udf_gpu_module != nullptr) && (rt_udf_cpu_module != nullptr);
1217 }
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 1211 of file NativeCodegen.cpp.

1211  {
1212  return (cpu_only || udf_gpu_module != nullptr) && (udf_cpu_module != nullptr);
1213 }
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 1260 of file NativeCodegen.cpp.

Referenced by MapDHandler::register_runtime_extension_functions().

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

Referenced by MapDHandler::register_runtime_extension_functions().

1248  {
1249  llvm::SMDiagnostic parse_error;
1250 
1251  auto buf =
1252  std::make_unique<llvm::MemoryBufferRef>(udf_ir_string, "Runtime UDF for GPU");
1253 
1254  rt_udf_gpu_module = llvm::parseIR(*buf, parse_error, getGlobalLLVMContext());
1255  if (!rt_udf_gpu_module) {
1256  throw_parseIR_error(parse_error);
1257  }
1258 }
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 915 of file NativeCodegen.cpp.

915  {
916  llvm::SMDiagnostic err;
917 
918  auto buffer_or_error = llvm::MemoryBuffer::getFile(mapd_root_abs_path() +
919  "/QueryEngine/RuntimeFunctions.bc");
920  CHECK(!buffer_or_error.getError());
921  llvm::MemoryBuffer* buffer = buffer_or_error.get().get();
922 
923  auto owner = llvm::parseBitcodeFile(buffer->getMemBufferRef(), context);
924  CHECK(!owner.takeError());
925  auto module = owner.get().release();
926  CHECK(module);
927 
928  return module;
929 }
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 1237 of file NativeCodegen.cpp.

Referenced by UdfCompiler::readCpuCompiledModule().

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

Referenced by UdfCompiler::readGpuCompiledModule().

1226  {
1227  llvm::SMDiagnostic parse_error;
1228 
1229  llvm::StringRef file_name_arg(udf_ir_filename);
1230 
1231  udf_gpu_module = llvm::parseIRFile(file_name_arg, parse_error, getGlobalLLVMContext());
1232  if (!udf_gpu_module) {
1233  throw_parseIR_error(parse_error, udf_ir_filename);
1234  }
1235 }
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 1965 of file NativeCodegen.cpp.

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

1965  {
1966  return llvm::CloneModule(
1967 #if LLVM_VERSION_MAJOR >= 7
1968  *g_rt_module.get(),
1969 #else
1970  g_rt_module.get(),
1971 #endif
1972  cgen_state->vmap_,
1973  [](const llvm::GlobalValue* gv) {
1974  auto func = llvm::dyn_cast<llvm::Function>(gv);
1975  if (!func) {
1976  return true;
1977  }
1978  return (func->getLinkage() == llvm::GlobalValue::LinkageTypes::PrivateLinkage ||
1979  func->getLinkage() == llvm::GlobalValue::LinkageTypes::InternalLinkage);
1980  });
1981 }
std::unique_ptr< llvm::Module > g_rt_module
llvm::ValueToValueMapTy vmap_
Definition: CgenState.h:317

+ Here is the caller graph for this function:

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

Definition at line 1219 of file NativeCodegen.cpp.

1219  {
1220  std::string excname = "LLVM IR ParseError: ";
1221  llvm::raw_string_ostream ss(excname);
1222  parse_error.print(src.c_str(), ss, false, false);
1223  throw std::runtime_error(ss.str());
1224 }
int64_t * src
void verify_function_ir ( const llvm::Function *  func)

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

154  {
155  std::stringstream err_ss;
156  llvm::raw_os_ostream err_os(err_ss);
157  if (llvm::verifyFunction(*func, &err_os)) {
158  func->print(llvm::outs());
159  LOG(FATAL) << err_ss.str();
160  }
161 }
#define LOG(tag)
Definition: Logger.h:188

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

Referenced by TableFunctionCompilationContext::finalize().

std::unique_ptr<llvm::Module> rt_udf_gpu_module

Definition at line 69 of file NativeCodegen.cpp.

std::unique_ptr<llvm::Module> udf_cpu_module

Definition at line 68 of file NativeCodegen.cpp.

std::unique_ptr<llvm::Module> udf_gpu_module

Definition at line 28 of file NativeCodegen.cpp.