From 97bfcddde0a0e827cb868b38c7da8d8692da6cce Mon Sep 17 00:00:00 2001 From: Tom Stellard Date: Thu, 7 Mar 2013 16:51:08 -0500 Subject: r600g/llvm: Move llvm wrapper functions into the radeon directory --- src/gallium/drivers/radeon/Makefile.sources | 3 +- src/gallium/drivers/radeon/radeon_llvm_util.cpp | 62 +++++++++++++++++++++++++ src/gallium/drivers/radeon/radeon_llvm_util.h | 20 ++++++++ 3 files changed, 84 insertions(+), 1 deletion(-) create mode 100644 src/gallium/drivers/radeon/radeon_llvm_util.cpp create mode 100644 src/gallium/drivers/radeon/radeon_llvm_util.h (limited to 'src/gallium/drivers/radeon') diff --git a/src/gallium/drivers/radeon/Makefile.sources b/src/gallium/drivers/radeon/Makefile.sources index 39f9532fe40..aa327490f44 100644 --- a/src/gallium/drivers/radeon/Makefile.sources +++ b/src/gallium/drivers/radeon/Makefile.sources @@ -1,5 +1,6 @@ CPP_FILES := \ - radeon_llvm_emit.cpp + radeon_llvm_emit.cpp \ + radeon_llvm_util.cpp C_FILES := \ radeon_setup_tgsi_llvm.c diff --git a/src/gallium/drivers/radeon/radeon_llvm_util.cpp b/src/gallium/drivers/radeon/radeon_llvm_util.cpp new file mode 100644 index 00000000000..22c6dc6f65c --- /dev/null +++ b/src/gallium/drivers/radeon/radeon_llvm_util.cpp @@ -0,0 +1,62 @@ +#include +#include +#if HAVE_LLVM < 0x0303 +#include +#else +#include +#endif +#include +#include +#include +#include +#include + +#include "radeon_llvm_util.h" + + +extern "C" LLVMModuleRef radeon_llvm_parse_bitcode(const unsigned char * bitcode, unsigned bitcode_len) +{ + llvm::OwningPtr M; + llvm::StringRef str((const char*)bitcode, bitcode_len); + llvm::MemoryBuffer* buffer = llvm::MemoryBuffer::getMemBufferCopy(str); + llvm::SMDiagnostic Err; + M.reset(llvm::ParseIR(buffer, Err, llvm::getGlobalContext())); + return wrap(M.take()); +} + +extern "C" void radeon_llvm_strip_unused_kernels(LLVMModuleRef mod, const char *kernel_name) +{ + llvm::Module *M = llvm::unwrap(mod); + std::vector export_list; + export_list.push_back(kernel_name); + llvm::PassManager PM; + PM.add(llvm::createInternalizePass(export_list)); + PM.add(llvm::createGlobalDCEPass()); + PM.run(*M); +} + +extern "C" unsigned radeon_llvm_get_num_kernels(const unsigned char *bitcode, + unsigned bitcode_len) +{ + LLVMModuleRef mod = radeon_llvm_parse_bitcode(bitcode, bitcode_len); + llvm::Module *M = llvm::unwrap(mod); + const llvm::NamedMDNode *kernel_node + = M->getNamedMetadata("opencl.kernels"); + unsigned kernel_count = kernel_node->getNumOperands(); + delete M; + return kernel_count; +} + +extern "C" LLVMModuleRef radeon_llvm_get_kernel_module(unsigned index, + const unsigned char *bitcode, unsigned bitcode_len) +{ + LLVMModuleRef mod = radeon_llvm_parse_bitcode(bitcode, bitcode_len); + llvm::Module *M = llvm::unwrap(mod); + const llvm::NamedMDNode *kernel_node = + M->getNamedMetadata("opencl.kernels"); + const char* kernel_name = kernel_node->getOperand(index)-> + getOperand(0)->getName().data(); + radeon_llvm_strip_unused_kernels(mod, kernel_name); + return mod; + +} diff --git a/src/gallium/drivers/radeon/radeon_llvm_util.h b/src/gallium/drivers/radeon/radeon_llvm_util.h new file mode 100644 index 00000000000..a7dfa9baf56 --- /dev/null +++ b/src/gallium/drivers/radeon/radeon_llvm_util.h @@ -0,0 +1,20 @@ +#ifndef RADEON_LLVM_UTIL_H +#define RADEON_LLVM_UTIL_H + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +LLVMModuleRef radeon_llvm_parse_bitcode(const unsigned char * bitcode, unsigned bitcode_len); +void radeon_llvm_strip_unused_kernels(LLVMModuleRef mod, const char *kernel_name); +unsigned radeon_llvm_get_num_kernels(const unsigned char *bitcode, unsigned bitcode_len); +LLVMModuleRef radeon_llvm_get_kernel_module(unsigned index, + const unsigned char *bitcode, unsigned bitcode_len); + +#ifdef __cplusplus +} +#endif + +#endif -- cgit v1.2.3