[Mesa-dev] [PATCH 3/3] clover: add clLinkProgram
EdB
edb+mesa at sigluy.net
Sat Jun 13 10:39:12 PDT 2015
---
src/gallium/state_trackers/clover/api/dispatch.cpp | 2 +-
src/gallium/state_trackers/clover/api/program.cpp | 31 +++
.../state_trackers/clover/core/compiler.hpp | 6 +
src/gallium/state_trackers/clover/core/error.hpp | 7 +
src/gallium/state_trackers/clover/core/program.cpp | 48 +++++
src/gallium/state_trackers/clover/core/program.hpp | 3 +
.../state_trackers/clover/llvm/invocation.cpp | 219 ++++++++++++++++-----
7 files changed, 270 insertions(+), 46 deletions(-)
diff --git a/src/gallium/state_trackers/clover/api/dispatch.cpp b/src/gallium/state_trackers/clover/api/dispatch.cpp
index b5a4094..44bff4f 100644
--- a/src/gallium/state_trackers/clover/api/dispatch.cpp
+++ b/src/gallium/state_trackers/clover/api/dispatch.cpp
@@ -123,7 +123,7 @@ namespace clover {
clCreateImage,
clCreateProgramWithBuiltInKernels,
clCompileProgram,
- NULL, // clLinkProgram
+ clLinkProgram,
clUnloadPlatformCompiler,
NULL, // clGetKernelArgInfo
NULL, // clEnqueueFillBuffer
diff --git a/src/gallium/state_trackers/clover/api/program.cpp b/src/gallium/state_trackers/clover/api/program.cpp
index 204d047..b8394f8 100644
--- a/src/gallium/state_trackers/clover/api/program.cpp
+++ b/src/gallium/state_trackers/clover/api/program.cpp
@@ -231,6 +231,37 @@ clCompileProgram(cl_program d_prog, cl_uint num_devs,
return e.get();
}
+CLOVER_API cl_program
+clLinkProgram (cl_context d_ctx, cl_uint num_devs, const cl_device_id *d_devs,
+ const char *p_opts, cl_uint num_progs, const cl_program *d_progs,
+ void (*pfn_notify) (cl_program, void *), void *user_data,
+ cl_int *r_errcode) try {
+ auto &ctx = obj(d_ctx);
+ auto devs = (d_devs ? objs(d_devs, num_devs) :
+ ref_vector<device>(ctx.devices()));
+ auto opts = (p_opts ? p_opts : "");
+ auto progs = objs(d_progs, num_progs);
+
+ if ((!pfn_notify && user_data))
+ throw error(CL_INVALID_VALUE);
+
+ if (any_of([&](const device &dev) {
+ return !count(dev, ctx.devices());
+ }, objs<allow_empty_tag>(d_devs, num_devs)))
+ throw error(CL_INVALID_DEVICE);
+
+ auto prog = create<program>(ctx);
+ if (prog().link(devs, opts, progs))
+ *r_errcode = CL_SUCCESS;
+ else
+ *r_errcode = CL_LINK_PROGRAM_FAILURE;
+
+ return ret_object(prog);
+} catch (error &e) {
+ ret_error(r_errcode, e);
+ return NULL;
+}
+
CLOVER_API cl_int
clUnloadCompiler() {
return CL_SUCCESS;
diff --git a/src/gallium/state_trackers/clover/core/compiler.hpp b/src/gallium/state_trackers/clover/core/compiler.hpp
index 9072ae3..faf110b 100644
--- a/src/gallium/state_trackers/clover/core/compiler.hpp
+++ b/src/gallium/state_trackers/clover/core/compiler.hpp
@@ -42,6 +42,12 @@ namespace clover {
const std::string &opts,
std::string &r_log);
+ module link_program_llvm(const std::vector<module> &modules,
+ pipe_shader_ir ir,
+ const std::string &target,
+ const std::string &opts,
+ std::string &r_log);
+
module build_program_tgsi(const std::string &source);
}
diff --git a/src/gallium/state_trackers/clover/core/error.hpp b/src/gallium/state_trackers/clover/core/error.hpp
index 780b973..abeb523 100644
--- a/src/gallium/state_trackers/clover/core/error.hpp
+++ b/src/gallium/state_trackers/clover/core/error.hpp
@@ -72,6 +72,13 @@ namespace clover {
}
};
+ class link_option_error : public error {
+ public:
+ link_option_error(const std::string &what = "") :
+ error(CL_INVALID_LINKER_OPTIONS, what) {
+ }
+ };
+
template<typename O>
class invalid_object_error;
diff --git a/src/gallium/state_trackers/clover/core/program.cpp b/src/gallium/state_trackers/clover/core/program.cpp
index f4bc6a9..45ec1c8 100644
--- a/src/gallium/state_trackers/clover/core/program.cpp
+++ b/src/gallium/state_trackers/clover/core/program.cpp
@@ -24,6 +24,10 @@
using namespace clover;
+program::program(clover::context &ctx) :
+ has_source(false), context(ctx), _kernel_ref_counter(0) {
+}
+
program::program(clover::context &ctx, const std::string &source) :
has_source(true), context(ctx), _source(source), _kernel_ref_counter(0) {
}
@@ -95,6 +99,50 @@ program::compile(const ref_vector<device> &devs, const char *opts,
}
}
+bool
+program::link(const ref_vector<device> &devs, const char *opts,
+ const ref_vector<program> &progs) {
+ assert(!has_source);
+
+ bool r = true;
+
+ _devices = devs;
+
+ for (auto &dev : devs) {
+ clean(&dev);
+
+ _opts.insert({ &dev, opts });
+
+ std::vector<module> mods;
+ mods.reserve(progs.size());
+ for (auto &prog : progs)
+ mods.push_back(prog.binary(dev));
+
+ if (mods.size() == 0) {
+ _logs.insert({ &dev, "Nothing to link."});
+ r = false;
+ continue;
+ }
+
+ std::string log;
+
+ try {
+ auto module = link_program_llvm(mods,
+ dev.ir_format(), dev.ir_target(),
+ opts, log);
+ _binaries.insert({ &dev, module });
+ _logs.insert({ &dev, log });
+ } catch (const link_option_error &) {
+ _logs.insert({ &dev, log });
+ throw;
+ } catch (const error &) {
+ _logs.insert({ &dev, log });
+ r = false;
+ }
+ }
+
+ return r;
+}
void
program::clean(const device *dev) {
diff --git a/src/gallium/state_trackers/clover/core/program.hpp b/src/gallium/state_trackers/clover/core/program.hpp
index b8a06aa..401c8a2 100644
--- a/src/gallium/state_trackers/clover/core/program.hpp
+++ b/src/gallium/state_trackers/clover/core/program.hpp
@@ -37,6 +37,7 @@ namespace clover {
evals, const std::vector<intrusive_ref<device>> &> device_range;
public:
+ program(clover::context &ctx);
program(clover::context &ctx,
const std::string &source);
program(clover::context &ctx,
@@ -50,6 +51,8 @@ namespace clover {
void build(const ref_vector<device> &devs, const char *opts);
void compile(const ref_vector<device> &devs, const char *opts,
const header_map &headers = {});
+ bool link(const ref_vector<device> &devs, const char *opts,
+ const ref_vector<program> &progs);
const bool has_source;
const std::string &source() const;
diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index b1d7c71..109ff76 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -130,20 +130,10 @@ namespace {
}
}
- llvm::Module *
- compile_llvm(llvm::LLVMContext &llvm_ctx, const std::string &source,
- const header_map &headers,
- const std::string &name, const std::string &triple,
- const std::string &processor, const std::string &opts,
- clang::LangAS::Map& address_spaces, unsigned &optimization_level,
- std::string &r_log) {
-
- clang::CompilerInstance c;
- clang::EmitLLVMOnlyAction act(&llvm_ctx);
- std::string log;
- llvm::raw_string_ostream s_log(log);
- std::string libclc_path = LIBCLC_LIBEXECDIR + processor + "-"
- + triple + ".bc";
+ bool
+ create_from_arg_llvm(clang::CompilerInstance &c,
+ const std::string &target, const std::string &opts,
+ llvm::raw_string_ostream &log) {
// Parse the compiler options:
std::vector<std::string> opts_array;
@@ -155,8 +145,6 @@ namespace {
opts_array.push_back(opt);
}
- opts_array.push_back(name);
-
std::vector<const char *> opts_carray;
for (unsigned i = 0; i < opts_array.size(); i++) {
opts_carray.push_back(opts_array.at(i).c_str());
@@ -171,15 +159,59 @@ namespace {
DiagsBuffer = new clang::TextDiagnosticBuffer();
clang::DiagnosticsEngine Diags(DiagID, &*DiagOpts, DiagsBuffer);
- bool Success;
+ bool Success;
Success = clang::CompilerInvocation::CreateFromArgs(c.getInvocation(),
opts_carray.data(),
opts_carray.data() + opts_carray.size(),
Diags);
- if (!Success) {
+
+ if (Success) {
+ const size_t processor_str_len = target.find_first_of("-");
+ const std::string processor(target, 0, processor_str_len);
+ const std::string triple(target.begin() + processor_str_len + 1,
+ target.end());
+
+ c.getLangOpts().NoBuiltin = true;
+ c.getTargetOpts().Triple = triple;
+ c.getTargetOpts().CPU = processor;
+
+ // This is a workaround for a Clang bug which causes the number
+ // of warnings and errors to be printed to stderr.
+ // http://www.llvm.org/bugs/show_bug.cgi?id=19735
+ c.getDiagnosticOpts().ShowCarets = false;
+ c.getInvocation().setLangDefaults(c.getLangOpts(), clang::IK_OpenCL,
+ clang::LangStandard::lang_opencl11);
+ c.createDiagnostics(
+ new clang::TextDiagnosticPrinter(
+ log, &c.getDiagnosticOpts())
+ );
+
+ c.setTarget(clang::TargetInfo::CreateTargetInfo(c.getDiagnostics(),
+ c.getInvocation().TargetOpts));
+ }
+
+ return Success;
+ }
+
+ llvm::Module *
+ compile_llvm(llvm::LLVMContext &llvm_ctx, const std::string &source,
+ const header_map &headers,
+ const std::string &target, const std::string &opts,
+ clang::LangAS::Map& address_spaces, unsigned &optimization_level,
+ std::string &r_log) {
+
+ clang::CompilerInstance c;
+ clang::EmitLLVMOnlyAction act(&llvm_ctx);
+ std::string log;
+ llvm::raw_string_ostream s_log(log);
+ const std::string name = "input.cl";
+
+ if (!create_from_arg_llvm(c, target, opts + " " + name, s_log)) {
+ r_log = log;
throw error(CL_INVALID_COMPILER_OPTIONS);
}
+
c.getFrontendOpts().ProgramAction = clang::frontend::EmitLLVMOnly;
c.getHeaderSearchOpts().UseBuiltinIncludes = true;
c.getHeaderSearchOpts().UseStandardSystemIncludes = true;
@@ -197,21 +229,6 @@ namespace {
// clc.h requires that this macro be defined:
c.getPreprocessorOpts().addMacroDef("cl_clang_storage_class_specifiers");
- c.getLangOpts().NoBuiltin = true;
- c.getTargetOpts().Triple = triple;
- c.getTargetOpts().CPU = processor;
-
- // This is a workaround for a Clang bug which causes the number
- // of warnings and errors to be printed to stderr.
- // http://www.llvm.org/bugs/show_bug.cgi?id=19735
- c.getDiagnosticOpts().ShowCarets = false;
- c.getInvocation().setLangDefaults(c.getLangOpts(), clang::IK_OpenCL,
- clang::LangStandard::lang_opencl11);
- c.createDiagnostics(
- new clang::TextDiagnosticPrinter(
- s_log,
- &c.getDiagnosticOpts()));
-
#if HAVE_LLVM >= 0x0306
c.getPreprocessorOpts().addRemappedFile(name,
llvm::MemoryBuffer::getMemBuffer(source).release());
@@ -247,6 +264,7 @@ namespace {
// attribute. This attribute will prevent Clang from creating
// illegal uses of barrier() (e.g. Moving barrier() inside a conditional
// that is no executed by all threads) during its optimizaton passes.
+ const std::string libclc_path = LIBCLC_LIBEXECDIR + target + ".bc";
c.getCodeGenOpts().LinkBitcodeFile = libclc_path;
optimization_level = c.getCodeGenOpts().OptimizationLevel;
@@ -697,10 +715,6 @@ clover::build_program_llvm(const std::string &source,
init_targets();
std::vector<llvm::Function *> kernels;
- size_t processor_str_len = std::string(target).find_first_of("-");
- std::string processor(target, 0, processor_str_len);
- std::string triple(target, processor_str_len + 1,
- target.size() - processor_str_len - 1);
clang::LangAS::Map address_spaces;
llvm::LLVMContext llvm_ctx;
unsigned optimization_level;
@@ -712,8 +726,8 @@ clover::build_program_llvm(const std::string &source,
// The input file name must have the .cl extension in order for the
// CompilerInvocation class to recognize it as an OpenCL source file.
- llvm::Module *mod = compile_llvm(llvm_ctx, source, {}, "input.cl",
- triple, processor, opts, address_spaces,
+ llvm::Module *mod = compile_llvm(llvm_ctx, source, {}, target,
+ opts, address_spaces,
optimization_level, r_log);
find_kernels(mod, kernels);
@@ -740,6 +754,11 @@ clover::build_program_llvm(const std::string &source,
m = build_module_llvm(mod, kernels, address_spaces);
break;
case PIPE_SHADER_IR_NATIVE: {
+ size_t processor_str_len = std::string(target).find_first_of("-");
+ std::string processor(target, 0, processor_str_len);
+ std::string triple(target, processor_str_len + 1,
+ target.size() - processor_str_len - 1);
+
std::vector<char> code = compile_native(mod, triple, processor,
get_debug_flags() & DBG_ASM,
r_log);
@@ -764,10 +783,6 @@ clover::compile_program_llvm(const std::string &source,
init_targets();
- size_t processor_str_len = std::string(target).find_first_of("-");
- std::string processor(target, 0, processor_str_len);
- std::string triple(target, processor_str_len + 1,
- target.size() - processor_str_len - 1);
clang::LangAS::Map address_spaces;
llvm::LLVMContext llvm_ctx;
unsigned optimization_level;
@@ -779,8 +794,8 @@ clover::compile_program_llvm(const std::string &source,
// The input file name must have the .cl extension in order for the
// CompilerInvocation class to recognize it as an OpenCL source file.
- llvm::Module *mod = compile_llvm(llvm_ctx, source, headers, "input.cl",
- triple, processor, opts, address_spaces,
+ llvm::Module *mod = compile_llvm(llvm_ctx, source, headers, target,
+ opts, address_spaces,
optimization_level, r_log);
if (get_debug_flags() & DBG_LLVM) {
@@ -810,3 +825,117 @@ clover::compile_program_llvm(const std::string &source,
return m;
}
+
+module
+clover::link_program_llvm(const std::vector<module> &modules,
+ enum pipe_shader_ir ir,
+ const std::string &target,
+ const std::string &opts,
+ std::string &r_log) {
+
+ init_targets();
+
+ std::string options = opts;
+ bool create_library = false;
+ size_t pos = options.find("-create-library");
+ if (pos != std::string::npos) {
+ create_library = true;
+ options.erase(pos, 15);
+ }
+ options += " link.cl";
+
+ llvm::LLVMContext llvm_ctx;
+ llvm_ctx.setDiagnosticHandler(diagnostic_handler, &r_log);
+
+ std::string ci_log;
+ llvm::raw_string_ostream rso(ci_log);
+ clang::CompilerInstance c;
+ if (!create_from_arg_llvm(c, target, options, rso)) {
+ r_log = ci_log;
+ throw link_option_error();
+ }
+
+ llvm::Module linked_mod("link", llvm_ctx);
+ llvm::Linker linker(&linked_mod);
+
+ for (const auto &mod : modules) {
+ const auto s = llvm::StringRef(mod.secs[0].data.data(),
+ mod.secs[0].data.size());
+
+ llvm::ErrorOr<llvm::Module*> m = llvm::parseBitcodeFile(
+ llvm::MemoryBufferRef(s, " "), llvm_ctx);
+
+ if (!m) {
+ r_log = m.getError().message();
+ throw error(CL_INVALID_PROGRAM);
+ }
+
+#if HAVE_LLVM < 0x0306
+ if (linker.linkInModule(*m, &r_log))
+#else
+ if (linker.linkInModule(*m))
+#endif
+ throw error(CL_LINK_PROGRAM_FAILURE);
+ }
+
+ module m;
+ std::vector<llvm::Function *> kernels;
+
+ //optimize
+ if (!create_library)
+ find_kernels(&linked_mod, kernels);
+
+ unsigned optimization_level = c.getCodeGenOpts().OptimizationLevel;
+ optimize(&linked_mod, optimization_level, kernels);
+
+
+ unsigned debug_flags = get_debug_flags();
+
+ if (debug_flags & DBG_LLVM) {
+ std::string log;
+ llvm::raw_string_ostream s_log(log);
+ linked_mod.print(s_log, NULL);
+ s_log.flush();
+ debug_log(log, ".ll");
+ }
+
+ if (create_library) {
+ //serialize for later use
+ llvm::SmallVector<char, 1024> llvm_bitcode;
+ llvm::raw_svector_ostream bitcode_ostream(llvm_bitcode);
+ llvm::BitstreamWriter writer(llvm_bitcode);
+ llvm::WriteBitcodeToFile(&linked_mod, bitcode_ostream);
+ bitcode_ostream.flush();
+
+ std::vector<char> data(llvm_bitcode.begin(), llvm_bitcode.end());
+ m.secs.push_back(module::section(0, module::section::text,
+ data.size(), data));
+
+ } else {
+ // Get address spaces map to be able to find kernel argument address space
+ clang::LangAS::Map address_spaces;
+ memcpy(address_spaces, c.getTarget().getAddressSpaceMap(),
+ sizeof(address_spaces));
+
+ // Build the clover::module
+ switch (ir) {
+ case PIPE_SHADER_IR_TGSI:
+ assert(0); // not supported
+ break;
+ case PIPE_SHADER_IR_LLVM:
+ m = build_module_llvm(&linked_mod, kernels, address_spaces);
+ break;
+ case PIPE_SHADER_IR_NATIVE: {
+ auto code = compile_native(&linked_mod, c.getTargetOpts().Triple,
+ c.getTargetOpts().CPU,
+ debug_flags & DBG_ASM, r_log);
+ m = build_module_native(code, &linked_mod, kernels, address_spaces,
+ r_log);
+ break;
+ }
+ }
+
+ }
+
+ return m;
+}
--
2.4.3
More information about the mesa-dev
mailing list