clover/llvm: Add options for dumping SPIR-V binaries
authorPierre Moreau <pierre.morrow@free.fr>
Sat, 10 Feb 2018 20:41:19 +0000 (21:41 +0100)
committerKarol Herbst <karolherbst@gmail.com>
Sat, 21 Sep 2019 08:28:32 +0000 (08:28 +0000)
Reviewed-by: Karol Herbst <kherbst@redhat.com>
Acked-by: Francisco Jerez <currojerez@riseup.net>
src/gallium/state_trackers/clover/llvm/util.hpp
src/gallium/state_trackers/clover/spirv/invocation.cpp
src/gallium/state_trackers/clover/spirv/invocation.hpp

index 222becd614e8a7e9803f98112f128f46b50b667b..02e73e650718bee1124e34cab8d622d530fc9093 100644 (file)
@@ -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 =
index fbcbbe8cd197ba07b9cf0eee8dd3f5a964abc59b..fa5b4dba6063871ad83421457a6b5da08a4d46b0 100644 (file)
@@ -680,6 +680,30 @@ clover::spirv::is_valid_spirv(const std::vector<char> &binary,
    return spvTool.Validate(reinterpret_cast<const uint32_t *>(binary.data()),
                            binary.size() / 4u);
 }
+
+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
 bool
 clover::spirv::is_valid_spirv(const std::vector<char> &/*binary*/,
@@ -702,4 +726,10 @@ clover::spirv::link_program(const std::vector<module> &/*modules*/,
    r_log += "SPIR-V support in clover is not enabled.\n";
    throw error(CL_LINKER_NOT_AVAILABLE);
 }
+
+std::string
+clover::spirv::print_module(const std::vector<char> &binary,
+                            const std::string &opencl_version) {
+   return std::string();
+}
 #endif
index 715a125c5ff4cf2d0fd1ca712d257049c005ef6b..472d8c0de71ba9c59ce26fd217edbaa692fd68af 100644 (file)
@@ -46,6 +46,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);
    }
 }