1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
#include <llvm/ADT/OwningPtr.h>
#include <llvm/ADT/StringRef.h>
#if HAVE_LLVM < 0x0303
#include <llvm/LLVMContext.h>
#else
#include <llvm/IR/LLVMContext.h>
#endif
#include <llvm/PassManager.h>
#include <llvm/Support/IRReader.h>
#include <llvm/Support/MemoryBuffer.h>
#include <llvm/Support/SourceMgr.h>
#include <llvm/Transforms/IPO.h>
#include <llvm-c/BitReader.h>
#include <llvm-c/Core.h>
#include "radeon_llvm_util.h"
static LLVMModuleRef radeon_llvm_parse_bitcode(const unsigned char * bitcode,
unsigned bitcode_len)
{
LLVMMemoryBufferRef buf;
LLVMModuleRef module = LLVMModuleCreateWithName("radeon");
buf = LLVMCreateMemoryBufferWithMemoryRangeCopy((const char*)bitcode,
bitcode_len, "radeon");
LLVMParseBitcode(buf, &module, NULL);
return module;
}
extern "C" void radeon_llvm_strip_unused_kernels(LLVMModuleRef mod, const char *kernel_name)
{
llvm::Module *M = llvm::unwrap(mod);
std::vector<const char *> 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);
return LLVMGetNamedMetadataNumOperands(mod, "opencl.kernels");
}
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;
}
|