Changes since:
* v6: Update the LLVM API call due to the replacement of llvm-spirv to
      the new official SPIRV-LLVM-Translator.
* v5: Update Makefile.am to add to libclllvm its new llvm-spirv
      dependency. (Karol Herbst)
* v3:
  - guard the SPIR-V code;
  - use an istringstream instead of a bidirectional stringstream, and
    initialise it directly (Francisco Jerez).

Signed-off-by: Pierre Moreau <pierre.mor...@free.fr>
---
 src/gallium/state_trackers/clover/Makefile.am |  6 +++-
 .../state_trackers/clover/llvm/invocation.cpp | 29 +++++++++++++++++++
 .../state_trackers/clover/llvm/invocation.hpp |  6 ++++
 src/gallium/state_trackers/clover/meson.build |  2 +-
 4 files changed, 41 insertions(+), 2 deletions(-)

diff --git a/src/gallium/state_trackers/clover/Makefile.am 
b/src/gallium/state_trackers/clover/Makefile.am
index 35ee092f3fd..2f42011104f 100644
--- a/src/gallium/state_trackers/clover/Makefile.am
+++ b/src/gallium/state_trackers/clover/Makefile.am
@@ -39,10 +39,14 @@ libclllvm_la_CXXFLAGS = \
        $(LIBELF_CFLAGS) \
        -DLIBCLC_INCLUDEDIR=\"$(LIBCLC_INCLUDEDIR)/\" \
        -DLIBCLC_LIBEXECDIR=\"$(LIBCLC_LIBEXECDIR)/\" \
-       -DCLANG_RESOURCE_DIR=\"$(CLANG_RESOURCE_DIR)\"
+       -DCLANG_RESOURCE_DIR=\"$(CLANG_RESOURCE_DIR)\" \
+       $(LLVMSPIRVLIB_CFLAGS)
 
 libclllvm_la_SOURCES = $(LLVM_SOURCES)
 
+libclllvm_la_LDFLAGS = \
+       $(LLVMSPIRVLIB_LIBS)
+
 libclover_la_CXXFLAGS = \
        $(CXX11_CXXFLAGS) \
        $(CLOVER_STD_OVERRIDE) \
diff --git a/src/gallium/state_trackers/clover/llvm/invocation.cpp 
b/src/gallium/state_trackers/clover/llvm/invocation.cpp
index 0a677ce2eaa..155dfc6964c 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 <LLVMSPIRVLib/LLVMSPIRVLib.h>
+#endif
 
 #include <clang/CodeGen/CodeGenAction.h>
 #include <clang/Lex/PreprocessorOptions.h>
@@ -395,3 +400,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 ff9caa457ca..85b16f6c902 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 7f0c60d40d9..096c33f52e1 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_llvmspirvlib],
   override_options : clover_cpp_std,
 )
 
-- 
2.20.1

_______________________________________________
mesa-dev mailing list
mesa-dev@lists.freedesktop.org
https://lists.freedesktop.org/mailman/listinfo/mesa-dev

Reply via email to