clover: move find_kernels to functions

This commit is contained in:
Zoltan Gilian
2015-07-27 11:21:07 +02:00
parent cf5667108b
commit d2cd2c69b2

View File

@@ -269,17 +269,19 @@ namespace {
#endif #endif
} }
void std::vector<llvm::Function *>
find_kernels(llvm::Module *mod, std::vector<llvm::Function *> &kernels) { find_kernels(const llvm::Module *mod) {
const llvm::NamedMDNode *kernel_node = const llvm::NamedMDNode *kernel_node =
mod->getNamedMetadata("opencl.kernels"); mod->getNamedMetadata("opencl.kernels");
// This means there are no kernels in the program. The spec does not // This means there are no kernels in the program. The spec does not
// require that we return an error here, but there will be an error if // require that we return an error here, but there will be an error if
// the user tries to pass this program to a clCreateKernel() call. // the user tries to pass this program to a clCreateKernel() call.
if (!kernel_node) { if (!kernel_node) {
return; return std::vector<llvm::Function *>();
} }
std::vector<llvm::Function *> kernels;
kernels.reserve(kernel_node->getNumOperands());
for (unsigned i = 0; i < kernel_node->getNumOperands(); ++i) { for (unsigned i = 0; i < kernel_node->getNumOperands(); ++i) {
#if HAVE_LLVM >= 0x0306 #if HAVE_LLVM >= 0x0306
kernels.push_back(llvm::mdconst::dyn_extract<llvm::Function>( kernels.push_back(llvm::mdconst::dyn_extract<llvm::Function>(
@@ -288,11 +290,11 @@ namespace {
#endif #endif
kernel_node->getOperand(i)->getOperand(0))); kernel_node->getOperand(i)->getOperand(0)));
} }
return kernels;
} }
void void
optimize(llvm::Module *mod, unsigned optimization_level, optimize(llvm::Module *mod, unsigned optimization_level) {
const std::vector<llvm::Function *> &kernels) {
#if HAVE_LLVM >= 0x0307 #if HAVE_LLVM >= 0x0307
llvm::legacy::PassManager PM; llvm::legacy::PassManager PM;
@@ -300,6 +302,8 @@ namespace {
llvm::PassManager PM; llvm::PassManager PM;
#endif #endif
const std::vector<llvm::Function *> kernels = find_kernels(mod);
// Add a function internalizer pass. // Add a function internalizer pass.
// //
// By default, the function internalizer pass will look for a function // By default, the function internalizer pass will look for a function
@@ -435,7 +439,6 @@ namespace {
module module
build_module_llvm(llvm::Module *mod, build_module_llvm(llvm::Module *mod,
const std::vector<llvm::Function *> &kernels,
clang::LangAS::Map& address_spaces) { clang::LangAS::Map& address_spaces) {
module m; module m;
@@ -447,6 +450,7 @@ namespace {
llvm::WriteBitcodeToFile(mod, bitcode_ostream); llvm::WriteBitcodeToFile(mod, bitcode_ostream);
bitcode_ostream.flush(); bitcode_ostream.flush();
const std::vector<llvm::Function *> kernels = find_kernels(mod);
for (unsigned i = 0; i < kernels.size(); ++i) { for (unsigned i = 0; i < kernels.size(); ++i) {
std::string kernel_name = kernels[i]->getName(); std::string kernel_name = kernels[i]->getName();
std::vector<module::argument> args = std::vector<module::argument> args =
@@ -610,10 +614,11 @@ namespace {
module module
build_module_native(std::vector<char> &code, build_module_native(std::vector<char> &code,
const llvm::Module *mod, const llvm::Module *mod,
const std::vector<llvm::Function *> &kernels,
const clang::LangAS::Map &address_spaces, const clang::LangAS::Map &address_spaces,
std::string &r_log) { std::string &r_log) {
const std::vector<llvm::Function *> kernels = find_kernels(mod);
std::map<std::string, unsigned> kernel_offsets = std::map<std::string, unsigned> kernel_offsets =
get_kernel_offsets(code, kernels, r_log); get_kernel_offsets(code, kernels, r_log);
@@ -697,7 +702,6 @@ clover::compile_program_llvm(const std::string &source,
init_targets(); init_targets();
std::vector<llvm::Function *> kernels;
size_t processor_str_len = std::string(target).find_first_of("-"); size_t processor_str_len = std::string(target).find_first_of("-");
std::string processor(target, 0, processor_str_len); std::string processor(target, 0, processor_str_len);
std::string triple(target, processor_str_len + 1, std::string triple(target, processor_str_len + 1,
@@ -717,9 +721,7 @@ clover::compile_program_llvm(const std::string &source,
triple, processor, opts, address_spaces, triple, processor, opts, address_spaces,
optimization_level, r_log); optimization_level, r_log);
find_kernels(mod, kernels); optimize(mod, optimization_level);
optimize(mod, optimization_level, kernels);
if (get_debug_flags() & DBG_LLVM) { if (get_debug_flags() & DBG_LLVM) {
std::string log; std::string log;
@@ -738,13 +740,13 @@ clover::compile_program_llvm(const std::string &source,
m = module(); m = module();
break; break;
case PIPE_SHADER_IR_LLVM: case PIPE_SHADER_IR_LLVM:
m = build_module_llvm(mod, kernels, address_spaces); m = build_module_llvm(mod, address_spaces);
break; break;
case PIPE_SHADER_IR_NATIVE: { case PIPE_SHADER_IR_NATIVE: {
std::vector<char> code = compile_native(mod, triple, processor, std::vector<char> code = compile_native(mod, triple, processor,
get_debug_flags() & DBG_ASM, get_debug_flags() & DBG_ASM,
r_log); r_log);
m = build_module_native(code, mod, kernels, address_spaces, r_log); m = build_module_native(code, mod, address_spaces, r_log);
break; break;
} }
} }