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

Pierre Moreau pierre.morrow at free.fr
Wed May 23 21:43:51 UTC 2018


Signed-off-by: Pierre Moreau <pierre.morrow at free.fr>
---
Changes in:
- v7: Update the LLVM API call due to the replacement of llvm-spirv to the new
      official SPIRV-LLVM-Translator

 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 b2c64bc48f8..5397d3b8e9d 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>
@@ -392,3 +397,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 c52f0faa406..ff9ed044f3c 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],
 )
 
 clover_files = files(
-- 
2.17.0



More information about the mesa-dev mailing list