[08/15] clover/llvm: Add options for dumping SPIR-V binaries

Submitted by Karol Herbst on May 11, 2019, 2:07 p.m.

Details

Message ID 20190511140712.27095-9-kherbst@redhat.com
State New
Headers show
Series "Clover: support CL through SPIR-V" ( rev: 1 ) in Mesa

Not browsing as part of any series.

Commit Message

Karol Herbst May 11, 2019, 2:07 p.m.
From: Pierre Moreau <pierre.morrow@free.fr>

Reviewed-by: Karol Herbst <kherbst@redhat.com>
---
 .../state_trackers/clover/llvm/util.hpp       |  4 ++-
 .../clover/spirv/invocation.cpp               | 30 +++++++++++++++++++
 .../clover/spirv/invocation.hpp               |  4 +++
 3 files changed, 37 insertions(+), 1 deletion(-)

Patch hide | download patch | download mbox

diff --git a/src/gallium/state_trackers/clover/llvm/util.hpp b/src/gallium/state_trackers/clover/llvm/util.hpp
index 222becd614e..02e73e65071 100644
--- a/src/gallium/state_trackers/clover/llvm/util.hpp
+++ b/src/gallium/state_trackers/clover/llvm/util.hpp
@@ -101,7 +101,8 @@  namespace clover {
          enum flag {
             clc = 1 << 0,
             llvm = 1 << 1,
-            native = 1 << 2
+            native = 1 << 2,
+            spirv = 1 << 3,
          };
 
          inline bool
@@ -111,6 +112,7 @@  namespace clover {
                { "llvm", llvm, "Dump the generated LLVM IR for all kernels." },
                { "native", native, "Dump kernel assembly code for targets "
                  "specifying PIPE_SHADER_IR_NATIVE" },
+               { "spirv", spirv, "Dump the generated SPIR-V for all kernels." },
                DEBUG_NAMED_VALUE_END
             };
             static const unsigned flags =
diff --git a/src/gallium/state_trackers/clover/spirv/invocation.cpp b/src/gallium/state_trackers/clover/spirv/invocation.cpp
index 62886e77495..6100fca0065 100644
--- a/src/gallium/state_trackers/clover/spirv/invocation.cpp
+++ b/src/gallium/state_trackers/clover/spirv/invocation.cpp
@@ -709,6 +709,30 @@  clover::spirv::is_valid_spirv(const uint32_t *binary, size_t length,
 
    return spvTool.Validate(binary, length);
 }
+
+std::string
+clover::spirv::print_module(const std::vector<char> &binary,
+                            const std::string &opencl_version) {
+   const spv_target_env target_env =
+      convert_opencl_str_to_target_env(opencl_version);
+   spvtools::SpirvTools spvTool(target_env);
+   spv_context spvContext = spvContextCreate(target_env);
+   if (!spvContext)
+      return "Failed to create an spv_context for disassembling the module.";
+
+   spv_text disassembly;
+   spvBinaryToText(spvContext,
+                   reinterpret_cast<const uint32_t *>(binary.data()),
+                   binary.size() / 4u, SPV_BINARY_TO_TEXT_OPTION_NONE,
+                   &disassembly, nullptr);
+   spvContextDestroy(spvContext);
+
+   const std::string disassemblyStr = disassembly->str;
+   spvTextDestroy(disassembly);
+
+   return disassemblyStr;
+}
+
 #else
 module
 clover::spirv::link_program(const std::vector<module> &/*modules*/,
@@ -724,4 +748,10 @@  clover::spirv::is_valid_spirv(const uint32_t * /*binary*/, size_t /*length*/,
                               const context::notify_action &/*notify*/) {
    return false;
 }
+
+std::string
+clover::spirv::print_module(const std::vector<char> &binary,
+                            const std::string &opencl_version) {
+   return std::string();
+}
 #endif
diff --git a/src/gallium/state_trackers/clover/spirv/invocation.hpp b/src/gallium/state_trackers/clover/spirv/invocation.hpp
index 37cd1377cb2..4818ab5daf4 100644
--- a/src/gallium/state_trackers/clover/spirv/invocation.hpp
+++ b/src/gallium/state_trackers/clover/spirv/invocation.hpp
@@ -53,6 +53,10 @@  namespace clover {
       // link dependencies between them.
       module link_program(const std::vector<module> &modules, const device &dev,
                           const std::string &opts, std::string &r_log);
+
+      // Returns a textual representation of the given binary.
+      std::string print_module(const std::vector<char> &binary,
+                               const std::string &opencl_version);
    }
 }