clover/llvm: Clean up compilation into LLVM IR.

Some assorted and mostly trivial clean-ups for the source to bitcode
compilation path.

Reviewed-by: Serge Martin <edb+mesa@sigluy.net>
Tested-by: Jan Vesely <jan.vesely@rutgers.edu>
This commit is contained in:
Francisco Jerez
2016-05-17 16:02:42 +02:00
parent 714b167f57
commit bdc27f13d5

View File

@@ -195,13 +195,11 @@ namespace {
return c; return c;
} }
llvm::Module * std::unique_ptr<Module>
compile_llvm(LLVMContext &ctx, clang::CompilerInstance &c, compile(LLVMContext &ctx, clang::CompilerInstance &c,
const std::string &source, const header_map &headers, const std::string &name, const std::string &source,
const std::string &name, const std::string &target, const header_map &headers, const std::string &target,
const std::string &opts, std::string &r_log) { const std::string &opts, std::string &r_log) {
clang::EmitLLVMOnlyAction act(&ctx);
c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly; c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
c.getHeaderSearchOpts().UseBuiltinIncludes = true; c.getHeaderSearchOpts().UseBuiltinIncludes = true;
c.getHeaderSearchOpts().UseStandardSystemIncludes = true; c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
@@ -210,32 +208,27 @@ namespace {
// Add libclc generic search path // Add libclc generic search path
c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR, c.getHeaderSearchOpts().AddPath(LIBCLC_INCLUDEDIR,
clang::frontend::Angled, clang::frontend::Angled,
false, false false, false);
);
// Add libclc include // Add libclc include
c.getPreprocessorOpts().Includes.push_back("clc/clc.h"); c.getPreprocessorOpts().Includes.push_back("clc/clc.h");
// clc.h requires that this macro be defined: // clc.h requires that this macro be defined:
c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers"); c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers");
c.getPreprocessorOpts().addRemappedFile(
c.getPreprocessorOpts().addRemappedFile(name, name, ::llvm::MemoryBuffer::getMemBuffer(source).release());
llvm::MemoryBuffer::getMemBuffer(source).release());
if (headers.size()) { if (headers.size()) {
const std::string tmp_header_path = "/tmp/clover/"; const std::string tmp_header_path = "/tmp/clover/";
c.getHeaderSearchOpts().AddPath(tmp_header_path, c.getHeaderSearchOpts().AddPath(tmp_header_path,
clang::frontend::Angled, clang::frontend::Angled,
false, false false, false);
);
for (header_map::const_iterator it = headers.begin(); for (const auto &header : headers)
it != headers.end(); ++it) { c.getPreprocessorOpts().addRemappedFile(
const std::string path = tmp_header_path + std::string(it->first); tmp_header_path + header.first,
c.getPreprocessorOpts().addRemappedFile(path, ::llvm::MemoryBuffer::getMemBuffer(header.second).release());
llvm::MemoryBuffer::getMemBuffer(it->second.c_str()).release());
}
} }
// Tell clang to link this file before performing any // Tell clang to link this file before performing any
@@ -249,10 +242,11 @@ namespace {
LIBCLC_LIBEXECDIR + target + ".bc"); LIBCLC_LIBEXECDIR + target + ".bc");
// Compile the code // Compile the code
clang::EmitLLVMOnlyAction act(&ctx);
if (!c.ExecuteAction(act)) if (!c.ExecuteAction(act))
throw compile_error(); throw compile_error();
return act.takeModule().release(); return act.takeModule();
} }
std::vector<llvm::Function *> std::vector<llvm::Function *>
@@ -794,10 +788,9 @@ clover::compile_program_llvm(const std::string &source,
// CompilerInvocation class to recognize it as an OpenCL source file. // CompilerInvocation class to recognize it as an OpenCL source file.
const auto c = create_compiler_instance(target, tokenize(opts + " input.cl"), const auto c = create_compiler_instance(target, tokenize(opts + " input.cl"),
r_log); r_log);
Module *mod = compile_llvm(*ctx, *c, source, headers, "input.cl", auto mod = compile(*ctx, *c, "input.cl", source, headers, target, opts, r_log);
target, opts, r_log);
optimize(mod, c->getCodeGenOpts().OptimizationLevel); optimize(&*mod, c->getCodeGenOpts().OptimizationLevel);
if (get_debug_flags() & DBG_LLVM) { if (get_debug_flags() & DBG_LLVM) {
std::string log; std::string log;
@@ -817,17 +810,16 @@ 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, *c); m = build_module_llvm(&*mod, *c);
break; break;
case PIPE_SHADER_IR_NATIVE: { case PIPE_SHADER_IR_NATIVE: {
std::vector<char> code = compile_native(mod, target, std::vector<char> code = compile_native(&*mod, target,
get_debug_flags() & DBG_ASM, get_debug_flags() & DBG_ASM,
r_log); r_log);
m = build_module_native(code, mod, *c, r_log); m = build_module_native(code, &*mod, *c, r_log);
break; break;
} }
} }
// The user takes ownership of the module.
delete mod;
return m; return m;
} }