clover/nir: Register callback for translation messages (v2)

This allows us to add SPIR-V to NIR translation failure messages to the
program’s compilation log, which can then be queried by the user.

v2: Replace the if-statement in `debug_function()` with an assert.

Signed-off-by: Pierre Moreau <dev@pmoreau.org>
Reviewed-by: Serge Martin <edb@sigluy.net>
Reviewed-by: Francisco Jerez <currojerez@riseup.net>
Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/5038>
diff --git a/src/gallium/frontends/clover/nir/invocation.cpp b/src/gallium/frontends/clover/nir/invocation.cpp
index 3d7fda8..032fb03 100644
--- a/src/gallium/frontends/clover/nir/invocation.cpp
+++ b/src/gallium/frontends/clover/nir/invocation.cpp
@@ -58,6 +58,15 @@
    return static_cast<const nir_shader_compiler_options*>(co);
 }
 
+static void debug_function(void *private_data,
+                   enum nir_spirv_debug_level level, size_t spirv_offset,
+                   const char *message)
+{
+   assert(private_data);
+   auto r_log = reinterpret_cast<std::string *>(private_data);
+   *r_log += message;
+}
+
 module clover::nir::spirv_to_nir(const module &mod, const device &dev,
                                  std::string &r_log)
 {
@@ -80,6 +89,8 @@
    spirv_options.caps.kernel = true;
    spirv_options.caps.int64_atomics = dev.has_int64_atomics();
    spirv_options.constant_as_global = true;
+   spirv_options.debug.func = &debug_function;
+   spirv_options.debug.private_data = &r_log;
 
    module m;
    // We only insert one section.