[Mesa-dev] [PATCH v7 4/7] clover: separate compilation and link stages

Serge Martin edb+mesa at sigluy.net
Sat Feb 13 22:08:38 UTC 2016


---
 src/gallium/state_trackers/clover/api/program.cpp  | 19 ++++--
 .../state_trackers/clover/core/compiler.hpp        |  7 +-
 src/gallium/state_trackers/clover/core/error.hpp   |  7 ++
 src/gallium/state_trackers/clover/core/program.cpp | 38 ++++++++++-
 src/gallium/state_trackers/clover/core/program.hpp |  6 +-
 .../state_trackers/clover/llvm/invocation.cpp      | 74 ++++++++++++++++++----
 .../state_trackers/clover/llvm/ir_compiler.cpp     | 39 +++++++++++-
 .../state_trackers/clover/llvm/ir_compiler.hpp     |  6 +-
 8 files changed, 168 insertions(+), 28 deletions(-)

diff --git a/src/gallium/state_trackers/clover/api/program.cpp b/src/gallium/state_trackers/clover/api/program.cpp
index 27ca2ef..f8d946e 100644
--- a/src/gallium/state_trackers/clover/api/program.cpp
+++ b/src/gallium/state_trackers/clover/api/program.cpp
@@ -181,13 +181,20 @@ clBuildProgram(cl_program d_prog, cl_uint num_devs,
 
    validate_build_program_common(prog, num_devs, d_devs, pfn_notify, user_data);
 
-   prog.build(devs, opts);
+   if (prog.has_source) {
+      prog.compile(devs, opts);
+      prog.link(devs, opts, { prog });
+   }
    return CL_SUCCESS;
 } catch (error &e) {
-   if (e.get() == CL_INVALID_COMPILER_OPTIONS)
-      return CL_INVALID_BUILD_OPTIONS;
-   if (e.get() == CL_COMPILE_PROGRAM_FAILURE)
-      return CL_BUILD_PROGRAM_FAILURE;
+   switch (e.get()) {
+      case CL_INVALID_COMPILER_OPTIONS:
+      case CL_INVALID_LINKER_OPTIONS:
+         return CL_INVALID_BUILD_OPTIONS;
+      case CL_COMPILE_PROGRAM_FAILURE:
+      case CL_LINK_PROGRAM_FAILURE:
+         return CL_BUILD_PROGRAM_FAILURE;
+   }
    return e.get();
 }
 
@@ -224,7 +231,7 @@ clCompileProgram(cl_program d_prog, cl_uint num_devs,
       range(header_names, num_headers),
       objs<allow_empty_tag>(d_header_progs, num_headers));
 
-   prog.build(devs, opts, headers);
+   prog.compile(devs, opts, headers);
    return CL_SUCCESS;
 
 } catch (error &e) {
diff --git a/src/gallium/state_trackers/clover/core/compiler.hpp b/src/gallium/state_trackers/clover/core/compiler.hpp
index 2076417..0d6766a 100644
--- a/src/gallium/state_trackers/clover/core/compiler.hpp
+++ b/src/gallium/state_trackers/clover/core/compiler.hpp
@@ -32,11 +32,16 @@ namespace clover {
 
    module compile_program_llvm(const std::string &source,
                                const header_map &headers,
-                               pipe_shader_ir ir,
                                const std::string &target,
                                const std::string &opts,
                                std::string &r_log);
 
+   module 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);
+
    module compile_program_tgsi(const std::string &source,
                                std::string &r_log);
 }
diff --git a/src/gallium/state_trackers/clover/core/error.hpp b/src/gallium/state_trackers/clover/core/error.hpp
index 59a5af4..f3039fa 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_error : public error {
+   public:
+      link_error(const std::string &what = "") :
+         error(CL_LINK_PROGRAM_FAILURE, 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 1549be7..1056cb9 100644
--- a/src/gallium/state_trackers/clover/core/program.cpp
+++ b/src/gallium/state_trackers/clover/core/program.cpp
@@ -40,8 +40,8 @@ program::program(clover::context &ctx,
 }
 
 void
-program::build(const ref_vector<device> &devs, const std::string &opts,
-               const header_map &headers) {
+program::compile(const ref_vector<device> &devs, const std::string &opts,
+                 const header_map &headers) {
    if (has_source) {
       _devices = devs;
 
@@ -58,7 +58,6 @@ program::build(const ref_vector<device> &devs, const std::string &opts,
             auto module = (dev.ir_format() == PIPE_SHADER_IR_TGSI ?
                            compile_program_tgsi(_source, log) :
                            compile_program_llvm(_source, headers,
-                                                dev.ir_format(),
                                                 dev.ir_target(), opts,
                                                 log));
             _binaries.insert({ &dev, module });
@@ -71,6 +70,39 @@ program::build(const ref_vector<device> &devs, const std::string &opts,
    }
 }
 
+void
+program::link(const ref_vector<device> &devs, const std::string &opts,
+              const ref_vector<program> &progs) {
+   _devices = devs;
+
+   for (auto &dev : devs) {
+      if (dev.ir_format() == PIPE_SHADER_IR_TGSI)
+         continue;
+
+       const std::vector<module> mods = map([&](const program &prog) {
+          return prog.binary(dev);
+       }, progs);
+
+      _binaries.erase(&dev);
+      _opts.erase(&dev);
+
+      _opts.insert({ &dev, opts });
+
+      std::string log;
+
+      try {
+         auto module = link_program_llvm(mods,
+                                         dev.ir_format(), dev.ir_target(),
+                                         opts, log);
+         _binaries.insert({ &dev, module });
+         _logs[&dev] += log;
+      } catch (const error &) {
+         _logs[&dev] += log;
+         throw;
+      }
+   }
+}
+
 const std::string &
 program::source() const {
    return _source;
diff --git a/src/gallium/state_trackers/clover/core/program.hpp b/src/gallium/state_trackers/clover/core/program.hpp
index 8a7be7a..0512b4e 100644
--- a/src/gallium/state_trackers/clover/core/program.hpp
+++ b/src/gallium/state_trackers/clover/core/program.hpp
@@ -47,8 +47,10 @@ namespace clover {
       program &
       operator=(const program &prog) = delete;
 
-      void build(const ref_vector<device> &devs, const std::string &opts,
-                 const header_map &headers = {});
+      void compile(const ref_vector<device> &devs, const std::string &opts,
+                   const header_map &headers = {});
+      void link(const ref_vector<device> &devs, const std::string &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 d3347de..be00b65 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -434,7 +434,7 @@ namespace {
       LLVMDisposeMessage(err_message);
 
       if (err) {
-         throw compile_error();
+         throw link_error();
       }
    }
 
@@ -454,7 +454,7 @@ namespace {
       if (LLVMGetTargetFromTriple(triple.c_str(), &target, &error_message)) {
          r_log = std::string(error_message);
          LLVMDisposeMessage(error_message);
-         throw compile_error();
+         throw link_error();
       }
 
       LLVMTargetMachineRef tm = LLVMCreateTargetMachine(
@@ -463,7 +463,7 @@ namespace {
 
       if (!tm) {
          r_log = "Could not create TargetMachine: " + triple;
-         throw compile_error();
+         throw link_error();
       }
 
       if (dump_asm) {
@@ -520,7 +520,7 @@ namespace {
             const char *name;
             if (gelf_getshdr(section, &symtab_header) != &symtab_header) {
                r_log = "Failed to read ELF section header.";
-               throw compile_error();
+               throw link_error();
             }
             name = elf_strptr(elf, section_str_index, symtab_header.sh_name);
            if (!strcmp(name, ".symtab")) {
@@ -530,9 +530,9 @@ namespace {
          }
          if (!symtab) {
             r_log = "Unable to find symbol table.";
-            throw compile_error();
+            throw link_error();
          }
-      } catch (compile_error &e) {
+      } catch (error &e) {
          elf_end(elf);
          throw e;
       }
@@ -594,6 +594,7 @@ namespace {
       return m;
    }
 
+   template <typename T>
    void
    diagnostic_handler(const llvm::DiagnosticInfo &di, void *data) {
       if (di.getSeverity() == llvm::DS_Error) {
@@ -604,7 +605,7 @@ namespace {
          stream.flush();
          *(std::string*)data = message;
 
-         throw compile_error();
+         throw T();
       }
    }
 
@@ -644,16 +645,13 @@ namespace {
 module
 clover::compile_program_llvm(const std::string &source,
                              const header_map &headers,
-                             enum pipe_shader_ir ir,
                              const std::string &target,
                              const std::string &opts,
                              std::string &r_log) {
 
-   init_targets();
-
    llvm::LLVMContext llvm_ctx;
 
-   llvm_ctx.setDiagnosticHandler(diagnostic_handler, &r_log);
+   llvm_ctx.setDiagnosticHandler(diagnostic_handler<compile_error>, &r_log);
 
    if (get_debug_flags() & DBG_CLC)
       debug_log("// Build options: " + opts + '\n' + source, ".cl");
@@ -661,7 +659,7 @@ clover::compile_program_llvm(const std::string &source,
    llvm_ir_compiler c(&llvm_ctx);
 
    try {
-      c.parse_args(target, opts);
+      c.parse_args(target, opts, CL_INVALID_COMPILER_OPTIONS);
       c.compile(source, headers);
    } catch (const error &) {
       r_log = c.get_log();
@@ -682,6 +680,58 @@ clover::compile_program_llvm(const std::string &source,
       debug_log(log, ".ll");
     }
 
+   //serialize for later use
+   module m;
+   llvm::SmallVector<char, 1024> llvm_bitcode;
+   llvm::raw_svector_ostream bitcode_ostream(llvm_bitcode);
+   llvm::BitstreamWriter writer(llvm_bitcode);
+   llvm::WriteBitcodeToFile(mod, bitcode_ostream);
+#if HAVE_LLVM < 0x0308
+   bitcode_ostream.flush();
+#endif
+
+   std::vector<char> data(llvm_bitcode.begin(), llvm_bitcode.end());
+   m.secs.push_back(module::section(0, module::section::text,
+                                       data.size(), data));
+
+   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();
+
+   llvm::LLVMContext llvm_ctx;
+
+   llvm_ctx.setDiagnosticHandler(diagnostic_handler<link_error>, &r_log);
+
+   llvm_ir_compiler c(&llvm_ctx);
+
+   try {
+      c.parse_args(target, opts, CL_INVALID_LINKER_OPTIONS);
+      c.link(modules);
+   } catch (const error &) {
+      r_log = c.get_log();
+      throw;
+   }
+
+   c.optimize();
+
+   r_log = c.get_log();
+
+   llvm::Module *mod = c.get_module();
+
+   if (get_debug_flags() & DBG_LLVM) {
+      std::string log;
+      llvm::raw_string_ostream s_log(log);
+      mod->print(s_log, NULL);
+      s_log.flush();
+      debug_log(log, ".ll");
+    }
+
    const clang::TargetInfo &info = c.get_info();
    // Get address spaces map to be able to find kernel argument address space
    clang::LangAS::Map address_spaces;
diff --git a/src/gallium/state_trackers/clover/llvm/ir_compiler.cpp b/src/gallium/state_trackers/clover/llvm/ir_compiler.cpp
index 8042a3e..5aa77db 100644
--- a/src/gallium/state_trackers/clover/llvm/ir_compiler.cpp
+++ b/src/gallium/state_trackers/clover/llvm/ir_compiler.cpp
@@ -21,7 +21,6 @@
 
 #include "llvm/ir_compiler.hpp"
 
-#include "core/error.hpp"
 #include "core/module.hpp"
 
 #include <clang/Frontend/TextDiagnosticBuffer.h>
@@ -61,7 +60,7 @@ llvm_ir_compiler::~llvm_ir_compiler() {
 
 void
 llvm_ir_compiler::parse_args(const std::string &target,
-                             const std::string &opts) {
+                             const std::string &opts, cl_int error_kind) {
    assert(!_module);
 
    // Parse the compiler options.
@@ -99,7 +98,7 @@ llvm_ir_compiler::parse_args(const std::string &target,
                                      opts_carray.data() + opts_carray.size(),
                                      Diags);
    if (!success)
-      throw clover::error(CL_INVALID_COMPILER_OPTIONS);
+      throw clover::error(error_kind);
 
    size_t len = target.find_first_of("-");
    std::string processor(target, 0, len);
@@ -206,6 +205,40 @@ llvm_ir_compiler::compile(const std::string &source,
 }
 
 void
+llvm_ir_compiler::link(const std::vector<clover::module> &modules) {
+   assert(!_module);
+
+   _module = new llvm::Module("link", *_llvm_ctx);
+#if HAVE_LLVM >= 0x0308
+   llvm::Linker linker(*_module);
+#else
+   llvm::Linker linker(_module);
+#endif
+
+   for (const auto &mod : modules) {
+      const auto data = llvm::StringRef(mod.secs[0].data.data(),
+                                                     mod.secs[0].data.size());
+
+      auto bitcode =
+         llvm::parseBitcodeFile(llvm::MemoryBufferRef(data, " "), *_llvm_ctx);
+
+      if (!bitcode)
+         throw clover::error(CL_INVALID_OPERATION);
+
+#if HAVE_LLVM == 0x0305
+      if (linker.linkInModule(*bitcode, &_log))
+#elif HAVE_LLVM == 0x0306
+      if (linker.linkInModule(*bitcode))
+#elif HAVE_LLVM == 0x0307
+      if (linker.linkInModule((*bitcode).get()))
+#else
+      if (linker.linkInModule(std::move(bitcode.get())))
+#endif
+         throw clover::link_error();
+   }
+}
+
+void
 llvm_ir_compiler::optimize() {
    assert(_module);
 
diff --git a/src/gallium/state_trackers/clover/llvm/ir_compiler.hpp b/src/gallium/state_trackers/clover/llvm/ir_compiler.hpp
index c825c6e..b8b9ce3 100644
--- a/src/gallium/state_trackers/clover/llvm/ir_compiler.hpp
+++ b/src/gallium/state_trackers/clover/llvm/ir_compiler.hpp
@@ -23,6 +23,8 @@
 #ifndef CLOVER_LLVM_COMPILER_HPP
 #define CLOVER_LLVM_COMPILER_HPP
 
+#include "core/error.hpp"
+
 #include <llvm/IR/LLVMContext.h>
 #include <llvm/IR/Module.h>
 #include <clang/Frontend/CompilerInstance.h>
@@ -38,8 +40,10 @@ public:
    llvm_ir_compiler(llvm::LLVMContext *llvm_ctx);
    ~llvm_ir_compiler();
 
-   void parse_args(const std::string &target, const std::string &opts);
+   void parse_args(const std::string &target, const std::string &opts,
+                   cl_int error_kind);
    void compile(const std::string &source, const header_map &headers);
+   void link(const std::vector<clover::module> &modules);
    void optimize();
 
    const clang::TargetInfo &get_info() const;
-- 
2.5.0



More information about the mesa-dev mailing list