[Mesa-dev] [PATCH v4 14/20] clover/llvm: Allow translating from SPIR-V to LLVM IR

Pierre Moreau pierre.morrow at free.fr
Thu Mar 8 00:21:23 UTC 2018


Signed-off-by: Pierre Moreau <pierre.morrow at free.fr>
---

Notes:
    Changes in v4:
    * guard the SPIR-V code;
    * use an istringstream instead of a bidirectional stringstream, and initialise
      it directly (Francisco Jerez).

 .../state_trackers/clover/llvm/invocation.cpp      | 29 ++++++++++++++++++++++
 .../state_trackers/clover/llvm/invocation.hpp      |  6 +++++
 src/gallium/state_trackers/clover/meson.build      |  2 +-
 3 files changed, 36 insertions(+), 1 deletion(-)

diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index 0bc0cccc6e..84043f3405 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.cpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.cpp
@@ -24,12 +24,17 @@
 // OTHER DEALINGS IN THE SOFTWARE.
 //
 
+#include <sstream>
+
 #include <llvm/IR/DiagnosticPrinter.h>
 #include <llvm/IR/DiagnosticInfo.h>
 #include <llvm/IR/LLVMContext.h>
 #include <llvm/Support/raw_ostream.h>
 #include <llvm/Transforms/IPO/PassManagerBuilder.h>
 #include <llvm-c/Target.h>
+#ifdef CLOVER_ALLOW_SPIRV
+#include <llvm-spirv/SPIRV.h>
+#endif
 
 #include <clang/CodeGen/CodeGenAction.h>
 #include <clang/Lex/PreprocessorOptions.h>
@@ -301,3 +306,27 @@ clover::llvm::link_program(const std::vector<module> &modules,
       unreachable("Unsupported IR.");
    }
 }
+
+#ifdef CLOVER_ALLOW_SPIRV
+module
+clover::llvm::compile_from_spirv(const std::vector<char> &binary,
+                                 const device &dev,
+                                 std::string &r_log) {
+   auto ctx = create_context(r_log);
+
+   ::llvm::Module *unsafe_mod;
+   std::string error_msg;
+   std::istringstream input({ binary.begin(), binary.end() }, std::ios_base::binary);
+   if (!::llvm::readSPIRV(*ctx, input, unsafe_mod, error_msg)) {
+      r_log += "Failed to convert SPIR-V to LLVM IR: " + error_msg + ".\n";
+      throw error(CL_INVALID_VALUE);
+   }
+
+   std::unique_ptr<::llvm::Module> mod(unsafe_mod);
+
+   if (has_flag(debug::llvm))
+      debug::log(".ll", print_module_bitcode(*mod));
+
+   return build_module_library(*mod, module::section::text_intermediate);
+}
+#endif
diff --git a/src/gallium/state_trackers/clover/llvm/invocation.hpp b/src/gallium/state_trackers/clover/llvm/invocation.hpp
index ff9caa457c..85b16f6c90 100644
--- a/src/gallium/state_trackers/clover/llvm/invocation.hpp
+++ b/src/gallium/state_trackers/clover/llvm/invocation.hpp
@@ -40,6 +40,12 @@ namespace clover {
                           const device &device,
                           const std::string &opts,
                           std::string &r_log);
+
+#ifdef CLOVER_ALLOW_SPIRV
+      module compile_from_spirv(const std::vector<char> &binary,
+                                const device &dev,
+                                std::string &r_log);
+#endif
    }
 }
 
diff --git a/src/gallium/state_trackers/clover/meson.build b/src/gallium/state_trackers/clover/meson.build
index c52f0faa40..bffd0df11d 100644
--- a/src/gallium/state_trackers/clover/meson.build
+++ b/src/gallium/state_trackers/clover/meson.build
@@ -48,7 +48,7 @@ libclllvm = static_library(
       dep_llvm.get_configtool_variable('version'), 'include',
     )),
   ],
-  dependencies : [dep_llvm, dep_elf],
+  dependencies : [dep_llvm, dep_elf, dep_llvm_spirv],
 )
 
 clover_files = files(
-- 
2.16.2



More information about the mesa-dev mailing list