16 #ifndef IMPALA_CODEGEN_LLVM_CODEGEN_H
17 #define IMPALA_CODEGEN_LLVM_CODEGEN_H
24 #include <boost/scoped_ptr.hpp>
25 #include <boost/thread/mutex.hpp>
26 #include <boost/unordered_set.hpp>
28 #include <llvm/Analysis/Verifier.h>
29 #include <llvm/IR/DerivedTypes.h>
30 #include <llvm/IR/IRBuilder.h>
31 #include <llvm/IR/Intrinsics.h>
32 #include <llvm/IR/LLVMContext.h>
33 #include <llvm/IR/Module.h>
34 #include <llvm/Support/raw_ostream.h>
37 #include "impala-ir/impala-ir-functions.h"
46 class ExecutionEngine;
48 class FunctionPassManager;
59 template<
bool B,
typename T,
typename I>
62 template<
bool preserveName>
69 class SubExprElimination;
123 ObjectPool*,
const std::string&
id, boost::scoped_ptr<LlvmCodeGen>* codegen);
129 boost::scoped_ptr<LlvmCodeGen>* codegen);
143 std::string
GetIR(
bool full_module)
const;
172 args_.push_back(var);
182 llvm::Value** params = NULL);
194 llvm::PointerType*
GetPtrType(llvm::Type* type);
261 llvm::Function*
ReplaceCallSites(llvm::Function* caller,
bool update_in_place,
262 llvm::Function* new_fn,
const std::string& target_name,
int* num_replaced);
272 llvm::Argument*
GetArgument(llvm::Function* fn,
int i);
326 template <
typename T>
static std::string
Print(T* value_or_type) {
328 llvm::raw_string_ostream stream(str);
329 value_or_type->print(stream);
360 const char*
name =
"");
368 const std::string& else_name,
369 llvm::BasicBlock** if_block, llvm::BasicBlock** else_block,
370 llvm::BasicBlock* insert_before = NULL);
399 void GetFunctions(std::vector<llvm::Function*>* functions);
402 void GetSymbols(boost::unordered_set<std::string>* symbols);
std::string error_string_
Error string that llvm will write to.
boost::scoped_ptr< llvm::ExecutionEngine > execution_engine_
Execution/Jitting engine.
std::map< int64_t, llvm::Function * > registered_exprs_map_
A mapping of unique id to registered expr functions.
static Status LoadImpalaIR(ObjectPool *, const std::string &id, boost::scoped_ptr< LlvmCodeGen > *codegen)
~LlvmCodeGen()
Removes all jit compiled dynamically linked functions from the process.
RuntimeProfile::Counter * codegen_timer()
llvm::PointerType * GetPtrType(llvm::Type *type)
Return a pointer type to 'type'.
void OptimizeModule()
Optimizes the module. This includes pruning the module of any unused functions.
const std::string & name() const
Returns name of function.
Utility struct that wraps a variable name and llvm type.
llvm::Type * bigint_type()
std::set< std::string > linked_modules_
llvm::Function * CodegenMinMax(const ColumnType &type, bool min)
Generates function to return min/max(v1, v2)
RuntimeProfile * runtime_profile()
std::map< llvm::Intrinsic::ID, llvm::Function * > llvm_intrinsics_
A cache of loaded llvm intrinsics.
Status Init()
Initializes the jitter and execution engine.
int InlineCallSites(llvm::Function *fn, bool skip_registered_fns)
std::vector< llvm::Function * > loaded_functions_
Functions parsed from pre-compiled module. Indexed by ImpalaIR::Function enum.
std::string GetIR(bool full_module) const
RuntimeProfile profile_
Codegen counters.
llvm::Type * boolean_type()
Simple wrappers to reduce code verbosity.
std::vector< std::string > debug_strings_
llvm::Function * GetLibCFunction(FnPrototype *prototype)
Returns the libc function, adding it to the module if it has not already been.
void ReplaceInstWithValue(llvm::Instruction *from, llvm::Value *to)
LlvmCodeGen(ObjectPool *pool, const std::string &module_id)
Top level codegen object. 'module_id' is used for debugging when outputting the IR.
llvm::Argument * GetArgument(llvm::Function *fn, int i)
Returns the i-th argument of fn.
RuntimeProfile::Counter * codegen_timer_
Time spent doing codegen (adding IR to the module)
llvm::Value * null_ptr_value()
llvm::Value * false_value_
std::vector< NamedVariable > args_
llvm::PointerType * ptr_type_
llvm representation of a few common types. Owned by context.
static std::string Print(T *value_or_type)
Returns the string representation of a llvm::Value* or llvm::Type*.
NamedVariable(const std::string &name="", llvm::Type *type=NULL)
LLVM code generator. This is the top level object to generate jitted code.
RuntimeProfile::Counter * module_file_size_
void RegisterExprFn(int64_t id, llvm::Function *function)
llvm::Function * debug_trace_fn_
llvm::Type * double_type()
llvm::Function * GeneratePrototype(LlvmBuilder *builder=NULL, llvm::Value **params=NULL)
llvm::Type * string_val_type()
llvm::Value * CastPtrToLlvmPtr(llvm::Type *type, const void *ptr)
void AddArgument(const NamedVariable &var)
Add argument.
std::set< llvm::Function * > registered_exprs_
A set of all the functions in 'registered_exprs_map_' for quick lookup.
llvm::Function * GetHashFunction(int num_bytes=-1)
void CodegenMemcpy(LlvmBuilder *, llvm::Value *dst, llvm::Value *src, int size)
llvm::ExecutionEngine * execution_engine()
Returns execution engine interface.
Status LinkModule(const std::string &file)
std::map< llvm::Function *, bool > jitted_functions_
llvm::Type * string_val_type_
boost::scoped_ptr< llvm::LLVMContext > context_
void ClearHashFns()
Clears generated hash fns. This is only used for testing.
llvm::Function * GetFunction(IRFunction::Type)
llvm::Value * true_value_
llvm constants to help with code gen verbosity
bool VerifyFunction(llvm::Function *function)
void CreateIfElseBlocks(llvm::Function *fn, const std::string &if_name, const std::string &else_name, llvm::BasicBlock **if_block, llvm::BasicBlock **else_block, llvm::BasicBlock *insert_before=NULL)
void AddFunctionToJit(llvm::Function *fn, void **fn_ptr)
llvm::Function * CloneFunction(llvm::Function *fn)
Returns a copy of fn. The copy is added to the module.
llvm::Value * true_value()
Returns true/false constants (bool type)
FnPrototype(LlvmCodeGen *, const std::string &name, llvm::Type *ret_type)
std::vector< std::pair< llvm::Function *, void ** > > fns_to_jit_compile_
The vector of functions to automatically JIT compile after FinalizeModule().
llvm::Type * tinyint_type()
llvm::IRBuilder LlvmBuilder
Typedef builder in case we want to change the template arguments later.
std::string id_
ID used for debugging (can be e.g. the fragment instance ID)
static void InitializeLlvm(bool load_backend=false)
llvm::Type * float_type()
llvm::Value * false_value()
llvm::Function * GetFnvHashFunction(int num_bytes=-1)
std::map< std::string, llvm::Function * > external_functions_
std::vector< llvm::Function * > codegend_functions_
RuntimeProfile::Counter * prepare_module_timer_
Time spent constructing the in-memory module from the .ir file.
llvm::Type * GetType(const ColumnType &type)
Returns llvm type for the column type.
boost::mutex jitted_functions_lock_
Lock protecting jitted_functions_.
llvm::Value * GetIntConstant(PrimitiveType type, int64_t val)
Returns the constant 'val' of 'type'.
std::map< int, llvm::Function * > hash_fns_
llvm::Function * GetRegisteredExprFn(int64_t id)
Returns a registered expr function for id or NULL if it does not exist.
llvm::Function * FinalizeFunction(llvm::Function *function)
RuntimeProfile::Counter * load_module_timer_
Time spent reading the .ir file from the file system.
llvm::Function * GetMurmurHashFunction(int num_bytes=-1)
void CodegenDebugTrace(LlvmBuilder *builder, const char *message)
llvm::Type * smallint_type()
llvm::Function * ReplaceCallSites(llvm::Function *caller, bool update_in_place, llvm::Function *new_fn, const std::string &target_name, int *num_replaced)
void EnableOptimizations(bool enable)
Turns on/off optimization passes.
RuntimeProfile::Counter * optimization_timer_
Time spent optimizing the module.
void GetSymbols(boost::unordered_set< std::string > *symbols)
Fils in 'symbols' with all the symbols in the module.
llvm::LLVMContext & context()
void * JitFunction(llvm::Function *function)
bool optimizations_enabled_
whether or not optimizations are enabled
llvm::AllocaInst * CreateEntryBlockAlloca(llvm::Function *f, const NamedVariable &var)
llvm::Function * OptimizeFunctionWithExprs(llvm::Function *fn)
static Status LoadFromFile(ObjectPool *, const std::string &file, const std::string &id, boost::scoped_ptr< LlvmCodeGen > *codegen)
llvm::PointerType * ptr_type()
void GetFunctions(std::vector< llvm::Function * > *functions)
static Status LoadModule(LlvmCodeGen *codegen, const std::string &file, llvm::Module **module)
llvm::Type * timestamp_val_type_
RuntimeProfile::Counter * compile_timer_
Time spent compiling the module.
llvm::Module * module()
Returns the underlying llvm module.