OmniSciDB  cde582ebc3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
NativeCodegen.cpp File Reference
#include "QueryEngine/Execute.h"
#include <llvm/Analysis/ScopedNoAliasAA.h>
#include <llvm/Analysis/TypeBasedAliasAnalysis.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/IntrinsicInst.h>
#include <llvm/IR/Intrinsics.h>
#include <llvm/IR/LegacyPassManager.h>
#include <llvm/IR/Verifier.h>
#include <llvm/IRReader/IRReader.h>
#include <llvm/Linker/Linker.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/Support/raw_ostream.h>
#include <llvm/Transforms/IPO.h>
#include <llvm/Transforms/IPO/AlwaysInliner.h>
#include <llvm/Transforms/IPO/InferFunctionAttrs.h>
#include <llvm/Transforms/IPO/PassManagerBuilder.h>
#include <llvm/Transforms/InstCombine/InstCombine.h>
#include <llvm/Transforms/Instrumentation.h>
#include <llvm/Transforms/Scalar.h>
#include <llvm/Transforms/Scalar/GVN.h>
#include <llvm/Transforms/Scalar/InstSimplifyPass.h>
#include <llvm/Transforms/Utils.h>
#include <llvm/Transforms/Utils/BasicBlockUtils.h>
#include <llvm/Transforms/Utils/Cloning.h>
#include "CudaMgr/CudaMgr.h"
#include "QueryEngine/CodeGenerator.h"
#include "QueryEngine/ExtensionFunctionsWhitelist.h"
#include "QueryEngine/GpuSharedMemoryUtils.h"
#include "QueryEngine/LLVMFunctionAttributesUtil.h"
#include "QueryEngine/Optimization/AnnotateInternalFunctionsPass.h"
#include "QueryEngine/OutputBufferInitialization.h"
#include "QueryEngine/QueryEngine.h"
#include "QueryEngine/QueryTemplateGenerator.h"
#include "Shared/InlineNullValues.h"
#include "Shared/MathUtils.h"
#include "StreamingTopN.h"
+ Include dependency graph for NativeCodegen.cpp:

Go to the source code of this file.

Namespaces

 anonymous_namespace{NativeCodegen.cpp}
 

Macros

#define SHOW_DEFINED(MODULE)
 
#define SHOW_FUNCTIONS(MODULE)
 

Functions

void anonymous_namespace{NativeCodegen.cpp}::throw_parseIR_error (const llvm::SMDiagnostic &parse_error, std::string src="", const bool is_gpu=false)
 
template<typename T = void>
void anonymous_namespace{NativeCodegen.cpp}::show_defined (llvm::Module &llvm_module)
 
template<typename T = void>
void anonymous_namespace{NativeCodegen.cpp}::show_defined (llvm::Module *llvm_module)
 
template<typename T = void>
void anonymous_namespace{NativeCodegen.cpp}::show_defined (std::unique_ptr< llvm::Module > &llvm_module)
 
template<typename T = void>
void anonymous_namespace{NativeCodegen.cpp}::scan_function_calls (llvm::Function &F, std::unordered_set< std::string > &defined, std::unordered_set< std::string > &undefined, const std::unordered_set< std::string > &ignored)
 
template<typename T = void>
void anonymous_namespace{NativeCodegen.cpp}::scan_function_calls (llvm::Module &llvm_module, std::unordered_set< std::string > &defined, std::unordered_set< std::string > &undefined, const std::unordered_set< std::string > &ignored)
 
template<typename T = void>
std::tuple< std::unordered_set
< std::string >
, std::unordered_set
< std::string > > 
anonymous_namespace{NativeCodegen.cpp}::scan_function_calls (llvm::Module &llvm_module, const std::unordered_set< std::string > &ignored={})
 
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 *llvm_module, llvm::legacy::PassManager &pass_manager, const std::unordered_set< llvm::Function * > &live_funcs, const bool is_gpu_smem_used, const CompilationOptions &co)
 
void verify_function_ir (const llvm::Function *func)
 
std::string anonymous_namespace{NativeCodegen.cpp}::assemblyForCPU (ExecutionEngineWrapper &execution_engine, llvm::Module *llvm_module)
 
ExecutionEngineWrapper anonymous_namespace{NativeCodegen.cpp}::create_execution_engine (llvm::Module *llvm_module, llvm::EngineBuilder &eb, const CompilationOptions &co)
 
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 (bool cpu_only)
 
std::unique_ptr< llvm::Module > read_llvm_module_from_bc_file (const std::string &bc_filename, llvm::LLVMContext &context)
 
std::unique_ptr< llvm::Module > read_llvm_module_from_ir_file (const std::string &udf_ir_filename, llvm::LLVMContext &ctx, bool is_gpu=false)
 
std::unique_ptr< llvm::Module > read_llvm_module_from_ir_string (const std::string &udf_ir_string, llvm::LLVMContext &ctx, bool is_gpu=false)
 
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 *llvm_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)
 
llvm::Function * anonymous_namespace{NativeCodegen.cpp}::create_row_function (const size_t in_col_count, const size_t agg_col_count, const bool hoist_literals, llvm::Module *llvm_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 *llvm_module)
 
std::vector< std::string > anonymous_namespace{NativeCodegen.cpp}::get_agg_fnames (const std::vector< Analyzer::Expr * > &target_exprs, const bool is_group_by)
 
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)
 
size_t anonymous_namespace{NativeCodegen.cpp}::get_shared_memory_size (const bool shared_mem_used, const QueryMemoryDescriptor *query_mem_desc_ptr)
 
bool anonymous_namespace{NativeCodegen.cpp}::is_gpu_shared_mem_supported (const QueryMemoryDescriptor *query_mem_desc_ptr, const RelAlgExecutionUnit &ra_exe_unit, const CudaMgr_Namespace::CudaMgr *cuda_mgr, const ExecutorDeviceType device_type, const unsigned gpu_blocksize, const unsigned num_blocks_per_mp)
 
std::string anonymous_namespace{NativeCodegen.cpp}::serialize_llvm_metadata_footnotes (llvm::Function *query_func, 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

float g_fraction_code_cache_to_evict = 0.2
 
static llvm::sys::Mutex g_ee_create_mutex
 
const std::string anonymous_namespace{NativeCodegen.cpp}::cuda_rt_decls
 

Macro Definition Documentation

#define SHOW_DEFINED (   MODULE)
Value:
{ \
std::cout << __func__ << "#" << __LINE__ << ": " #MODULE << " "; \
::show_defined(MODULE); \
}
void show_defined(llvm::Module &llvm_module)

Definition at line 140 of file NativeCodegen.cpp.

#define SHOW_FUNCTIONS (   MODULE)
Value:
{ \
std::cout << __func__ << "#" << __LINE__ << ": " #MODULE << " "; \
::show_functions(MODULE); \
}

Definition at line 146 of file NativeCodegen.cpp.

Function Documentation

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

Referenced by TableFunctionCompilationContext::generateEntryPoint().

3315  {
3316  CHECK(byte_stream_arg);
3317  const auto max_col_local_id = num_columns - 1;
3318 
3319  std::vector<llvm::Value*> col_heads;
3320  for (int col_id = 0; col_id <= max_col_local_id; ++col_id) {
3321  auto* gep = ir_builder.CreateGEP(
3322  byte_stream_arg->getType()->getScalarType()->getPointerElementType(),
3323  byte_stream_arg,
3324  llvm::ConstantInt::get(llvm::Type::getInt32Ty(ctx), col_id));
3325  col_heads.emplace_back(
3326  ir_builder.CreateLoad(gep->getType()->getPointerElementType(), gep));
3327  }
3328  return col_heads;
3329 }
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the caller graph for this function:

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

Definition at line 910 of file NativeCodegen.cpp.

References CHECK, clear_function_attributes(), DEBUG_TIMER, anonymous_namespace{Utm.h}::f, CodeGenerator::linkModuleWithLibdevice(), and anonymous_namespace{NativeCodegen.cpp}::optimize_ir().

910  {
911  std::map<std::string, std::string> result;
912 
913  result.insert(std::make_pair("cpu_name", llvm::sys::getHostCPUName()));
914  result.insert(std::make_pair("cpu_triple", llvm::sys::getProcessTriple()));
915  result.insert(
916  std::make_pair("cpu_cores", std::to_string(llvm::sys::getHostNumPhysicalCores())));
917  result.insert(std::make_pair("cpu_threads", std::to_string(cpu_threads())));
918 
919  // https://en.cppreference.com/w/cpp/language/types
920  std::string sizeof_types;
921  sizeof_types += "bool:" + std::to_string(sizeof(bool)) + ";";
922  sizeof_types += "size_t:" + std::to_string(sizeof(size_t)) + ";";
923  sizeof_types += "ssize_t:" + std::to_string(sizeof(ssize_t)) + ";";
924  sizeof_types += "char:" + std::to_string(sizeof(char)) + ";";
925  sizeof_types += "uchar:" + std::to_string(sizeof(unsigned char)) + ";";
926  sizeof_types += "short:" + std::to_string(sizeof(short)) + ";";
927  sizeof_types += "ushort:" + std::to_string(sizeof(unsigned short int)) + ";";
928  sizeof_types += "int:" + std::to_string(sizeof(int)) + ";";
929  sizeof_types += "uint:" + std::to_string(sizeof(unsigned int)) + ";";
930  sizeof_types += "long:" + std::to_string(sizeof(long int)) + ";";
931  sizeof_types += "ulong:" + std::to_string(sizeof(unsigned long int)) + ";";
932  sizeof_types += "longlong:" + std::to_string(sizeof(long long int)) + ";";
933  sizeof_types += "ulonglong:" + std::to_string(sizeof(unsigned long long int)) + ";";
934  sizeof_types += "float:" + std::to_string(sizeof(float)) + ";";
935  sizeof_types += "double:" + std::to_string(sizeof(double)) + ";";
936  sizeof_types += "longdouble:" + std::to_string(sizeof(long double)) + ";";
937  sizeof_types += "voidptr:" + std::to_string(sizeof(void*)) + ";";
938 
939  result.insert(std::make_pair("type_sizeof", sizeof_types));
940 
941  std::string null_values;
942  null_values += "boolean1:" + std::to_string(serialized_null_value<bool>()) + ";";
943  null_values += "boolean8:" + std::to_string(serialized_null_value<int8_t>()) + ";";
944  null_values += "int8:" + std::to_string(serialized_null_value<int8_t>()) + ";";
945  null_values += "int16:" + std::to_string(serialized_null_value<int16_t>()) + ";";
946  null_values += "int32:" + std::to_string(serialized_null_value<int32_t>()) + ";";
947  null_values += "int64:" + std::to_string(serialized_null_value<int64_t>()) + ";";
948  null_values += "uint8:" + std::to_string(serialized_null_value<uint8_t>()) + ";";
949  null_values += "uint16:" + std::to_string(serialized_null_value<uint16_t>()) + ";";
950  null_values += "uint32:" + std::to_string(serialized_null_value<uint32_t>()) + ";";
951  null_values += "uint64:" + std::to_string(serialized_null_value<uint64_t>()) + ";";
952  null_values += "float32:" + std::to_string(serialized_null_value<float>()) + ";";
953  null_values += "float64:" + std::to_string(serialized_null_value<double>()) + ";";
954  null_values +=
955  "Array<boolean8>:" + std::to_string(serialized_null_value<int8_t, true>()) + ";";
956  null_values +=
957  "Array<int8>:" + std::to_string(serialized_null_value<int8_t, true>()) + ";";
958  null_values +=
959  "Array<int16>:" + std::to_string(serialized_null_value<int16_t, true>()) + ";";
960  null_values +=
961  "Array<int32>:" + std::to_string(serialized_null_value<int32_t, true>()) + ";";
962  null_values +=
963  "Array<int64>:" + std::to_string(serialized_null_value<int64_t, true>()) + ";";
964  null_values +=
965  "Array<float32>:" + std::to_string(serialized_null_value<float, true>()) + ";";
966  null_values +=
967  "Array<float64>:" + std::to_string(serialized_null_value<double, true>()) + ";";
968 
969  result.insert(std::make_pair("null_values", null_values));
970 
971  llvm::StringMap<bool> cpu_features;
972  if (llvm::sys::getHostCPUFeatures(cpu_features)) {
973  std::string features_str = "";
974  for (auto it = cpu_features.begin(); it != cpu_features.end(); ++it) {
975  features_str += (it->getValue() ? " +" : " -");
976  features_str += it->getKey().str();
977  }
978  result.insert(std::make_pair("cpu_features", features_str));
979  }
980 
981  result.insert(std::make_pair("llvm_version",
982  std::to_string(LLVM_VERSION_MAJOR) + "." +
983  std::to_string(LLVM_VERSION_MINOR) + "." +
984  std::to_string(LLVM_VERSION_PATCH)));
985 
986 #ifdef HAVE_CUDA
987  if (!cpu_only) {
988  int device_count = 0;
989  checkCudaErrors(cuDeviceGetCount(&device_count));
990  if (device_count) {
991  CUdevice device{};
992  char device_name[256];
993  int major = 0, minor = 0;
994  int driver_version;
995  checkCudaErrors(cuDeviceGet(&device, 0)); // assuming homogeneous multi-GPU system
996  checkCudaErrors(cuDeviceGetName(device_name, 256, device));
997  checkCudaErrors(cuDeviceGetAttribute(
998  &major, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MAJOR, device));
999  checkCudaErrors(cuDeviceGetAttribute(
1000  &minor, CU_DEVICE_ATTRIBUTE_COMPUTE_CAPABILITY_MINOR, device));
1001  checkCudaErrors(cuDriverGetVersion(&driver_version));
1002 
1003  result.insert(std::make_pair("gpu_name", device_name));
1004  result.insert(std::make_pair("gpu_count", std::to_string(device_count)));
1005  result.insert(std::make_pair("gpu_compute_capability",
1006  std::to_string(major) + "." + std::to_string(minor)));
1007  result.insert(std::make_pair("gpu_triple", get_gpu_target_triple_string()));
1008  result.insert(std::make_pair("gpu_datalayout", get_gpu_data_layout()));
1009  result.insert(std::make_pair("gpu_driver",
1010  "CUDA " + std::to_string(driver_version / 1000) + "." +
1011  std::to_string((driver_version % 1000) / 10)));
1012  }
1013  }
1014 #endif
1015 
1016  return result;
1017 }
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:24

+ Here is the call graph for this function:

llvm::StringRef get_gpu_data_layout ( )

Definition at line 902 of file NativeCodegen.cpp.

902  {
903  return llvm::StringRef(
904  "e-p:64:64:64-i1:8:8-i8:8:8-"
905  "i16:16:16-i32:32:32-i64:64:64-"
906  "f32:32:32-f64:64:64-v16:16:16-"
907  "v32:32:32-v64:64:64-v128:128:128-n16:32:64");
908 }
llvm::StringRef get_gpu_target_triple_string ( )

Definition at line 898 of file NativeCodegen.cpp.

898  {
899  return llvm::StringRef("nvptx64-nvidia-cuda");
900 }
std::unique_ptr<llvm::Module> read_llvm_module_from_bc_file ( const std::string &  bc_filename,
llvm::LLVMContext &  context 
)

Definition at line 1502 of file NativeCodegen.cpp.

Referenced by Executor::update_extension_modules().

1504  {
1505  llvm::SMDiagnostic err;
1506 
1507  auto buffer_or_error = llvm::MemoryBuffer::getFile(bc_filename);
1508  CHECK(!buffer_or_error.getError()) << "bc_filename=" << bc_filename;
1509  llvm::MemoryBuffer* buffer = buffer_or_error.get().get();
1510 
1511  auto owner = llvm::parseBitcodeFile(buffer->getMemBufferRef(), context);
1512  CHECK(!owner.takeError());
1513  CHECK(owner->get());
1514  return std::move(owner.get());
1515 }
#define CHECK(condition)
Definition: Logger.h:222

+ Here is the caller graph for this function:

std::unique_ptr<llvm::Module> read_llvm_module_from_ir_file ( const std::string &  udf_ir_filename,
llvm::LLVMContext &  ctx,
bool  is_gpu = false 
)

Definition at line 1517 of file NativeCodegen.cpp.

Referenced by Executor::update_extension_modules().

1520  {
1521  llvm::SMDiagnostic parse_error;
1522 
1523  llvm::StringRef file_name_arg(udf_ir_filename);
1524 
1525  auto owner = llvm::parseIRFile(file_name_arg, parse_error, ctx);
1526  if (!owner) {
1527  throw_parseIR_error(parse_error, udf_ir_filename, is_gpu);
1528  }
1529 
1530  if (is_gpu) {
1531  llvm::Triple gpu_triple(owner->getTargetTriple());
1532  if (!gpu_triple.isNVPTX()) {
1533  LOG(WARNING)
1534  << "Expected triple nvptx64-nvidia-cuda for NVVM IR of loadtime UDFs but got "
1535  << gpu_triple.str() << ". Disabling the NVVM IR module.";
1536  return std::unique_ptr<llvm::Module>();
1537  }
1538  }
1539  return owner;
1540 }
#define LOG(tag)
Definition: Logger.h:216
void throw_parseIR_error(const llvm::SMDiagnostic &parse_error, std::string src="", const bool is_gpu=false)

+ Here is the caller graph for this function:

std::unique_ptr<llvm::Module> read_llvm_module_from_ir_string ( const std::string &  udf_ir_string,
llvm::LLVMContext &  ctx,
bool  is_gpu = false 
)

Definition at line 1542 of file NativeCodegen.cpp.

Referenced by Executor::update_extension_modules().

1545  {
1546  llvm::SMDiagnostic parse_error;
1547 
1548  auto buf = std::make_unique<llvm::MemoryBufferRef>(udf_ir_string,
1549  "Runtime UDF/UDTF LLVM/NVVM IR");
1550 
1551  auto owner = llvm::parseIR(*buf, parse_error, ctx);
1552  if (!owner) {
1553  LOG(IR) << "read_llvm_module_from_ir_string:\n"
1554  << udf_ir_string << "\nEnd of LLVM/NVVM IR";
1555  throw_parseIR_error(parse_error, "", /* is_gpu= */ is_gpu);
1556  }
1557 
1558  if (is_gpu) {
1559  llvm::Triple gpu_triple(owner->getTargetTriple());
1560  if (!gpu_triple.isNVPTX()) {
1561  LOG(IR) << "read_llvm_module_from_ir_string:\n"
1562  << udf_ir_string << "\nEnd of NNVM IR";
1563  LOG(WARNING) << "Expected triple nvptx64-nvidia-cuda for NVVM IR but got "
1564  << gpu_triple.str()
1565  << ". Executing runtime UDF/UDTFs on GPU will be disabled.";
1566  return std::unique_ptr<llvm::Module>();
1567  ;
1568  }
1569  }
1570  return owner;
1571 }
#define LOG(tag)
Definition: Logger.h:216
void throw_parseIR_error(const llvm::SMDiagnostic &parse_error, std::string src="", const bool is_gpu=false)

+ Here is the caller graph for this function:

void verify_function_ir ( const llvm::Function *  func)

Definition at line 385 of file NativeCodegen.cpp.

References logger::FATAL, and LOG.

Referenced by GpuSharedMemCodeBuilder::codegen(), spatial_type::Transform::codegen(), anonymous_namespace{JoinLoopTest.cpp}::create_loop_test_function(), TableFunctionCompilationContext::generateEntryPoint(), StubGenerator::generateStub(), and translate_function().

385  {
386  std::stringstream err_ss;
387  llvm::raw_os_ostream err_os(err_ss);
388  err_os << "\n-----\n";
389  if (llvm::verifyFunction(*func, &err_os)) {
390  err_os << "\n-----\n";
391  func->print(err_os, nullptr);
392  err_os << "\n-----\n";
393  LOG(FATAL) << err_ss.str();
394  }
395 }
#define LOG(tag)
Definition: Logger.h:216

+ Here is the caller graph for this function:

Variable Documentation

llvm::sys::Mutex g_ee_create_mutex
static
float g_fraction_code_cache_to_evict = 0.2

Definition at line 76 of file NativeCodegen.cpp.

Referenced by CommandLineOptions::fillAdvancedOptions().