Module: Mesa
Branch: main
Commit: faadac57d5e2c9684b05d2121924fbe2e049eefb
URL:    
http://cgit.freedesktop.org/mesa/mesa/commit/?id=faadac57d5e2c9684b05d2121924fbe2e049eefb

Author: Faith Ekstrand <[email protected]>
Date:   Thu Nov 16 16:00:12 2023 -0600

nak: Support dumping shader assembly as part of compile

This dumps it to a string that gets attached to the nak_shader_bin.

Part-of: <https://gitlab.freedesktop.org/mesa/mesa/-/merge_requests/26241>

---

 src/nouveau/compiler/nak.h      |  5 ++++-
 src/nouveau/compiler/nak.rs     | 23 ++++++++++++++++++++---
 src/nouveau/vulkan/nvk_shader.c |  2 +-
 3 files changed, 25 insertions(+), 5 deletions(-)

diff --git a/src/nouveau/compiler/nak.h b/src/nouveau/compiler/nak.h
index fa07cdac380..de38a395f1e 100644
--- a/src/nouveau/compiler/nak.h
+++ b/src/nouveau/compiler/nak.h
@@ -131,14 +131,17 @@ struct nak_shader_info {
 
 struct nak_shader_bin {
    struct nak_shader_info info;
+
    uint32_t code_size;
    const void *code;
+
+   const char *asm_str;
 };
 
 void nak_shader_bin_destroy(struct nak_shader_bin *bin);
 
 struct nak_shader_bin *
-nak_compile_shader(nir_shader *nir,
+nak_compile_shader(nir_shader *nir, bool dump_asm,
                    const struct nak_compiler *nak,
                    const struct nak_fs_key *fs_key);
 
diff --git a/src/nouveau/compiler/nak.rs b/src/nouveau/compiler/nak.rs
index 47cbe18594a..9cb75648590 100644
--- a/src/nouveau/compiler/nak.rs
+++ b/src/nouveau/compiler/nak.rs
@@ -33,7 +33,8 @@ use nak_from_nir::*;
 use nak_ir::ShaderIoInfo;
 use std::cmp::max;
 use std::env;
-use std::ffi::CStr;
+use std::ffi::{CStr, CString};
+use std::fmt::Write;
 use std::os::raw::c_void;
 use std::sync::OnceLock;
 
@@ -181,18 +182,27 @@ pub extern "C" fn nak_nir_options(
 struct ShaderBin {
     bin: nak_shader_bin,
     code: Vec<u32>,
+    asm: CString,
 }
 
 impl ShaderBin {
-    pub fn new(info: nak_shader_info, code: Vec<u32>) -> ShaderBin {
+    pub fn new(info: nak_shader_info, code: Vec<u32>, asm: &str) -> ShaderBin {
+        let asm = CString::new(asm)
+            .expect("NAK assembly has unexpected null characters");
         let bin = nak_shader_bin {
             info: info,
             code_size: (code.len() * 4).try_into().unwrap(),
             code: code.as_ptr() as *const c_void,
+            asm_str: if asm.is_empty() {
+                std::ptr::null()
+            } else {
+                asm.as_ptr()
+            },
         };
         ShaderBin {
             bin: bin,
             code: code,
+            asm: asm,
         }
     }
 }
@@ -219,6 +229,7 @@ fn eprint_hex(label: &str, data: &[u32]) {
 #[no_mangle]
 pub extern "C" fn nak_compile_shader(
     nir: *mut nir_shader,
+    dump_asm: bool,
     nak: *const nak_compiler,
     fs_key: *const nak_fs_key,
 ) -> *mut nak_shader_bin {
@@ -378,6 +389,11 @@ pub extern "C" fn nak_compile_shader(
         hdr: nak_sph::encode_header(&s.info, fs_key),
     };
 
+    let mut asm = String::new();
+    if dump_asm {
+        write!(asm, "{}", s).expect("Failed to dump assembly");
+    }
+
     let code = if nak.sm >= 70 {
         s.encode_sm70()
     } else {
@@ -400,5 +416,6 @@ pub extern "C" fn nak_compile_shader(
         eprint_hex("Encoded shader", &code);
     }
 
-    Box::into_raw(Box::new(ShaderBin::new(info, code))) as *mut nak_shader_bin
+    let bin = Box::new(ShaderBin::new(info, code, &asm));
+    Box::into_raw(bin) as *mut nak_shader_bin
 }
diff --git a/src/nouveau/vulkan/nvk_shader.c b/src/nouveau/vulkan/nvk_shader.c
index 3ad019c61f8..b82a5572102 100644
--- a/src/nouveau/vulkan/nvk_shader.c
+++ b/src/nouveau/vulkan/nvk_shader.c
@@ -398,7 +398,7 @@ nvk_compile_nir_with_nak(struct nvk_physical_device *pdev,
                          const struct nak_fs_key *fs_key,
                          struct nvk_shader *shader)
 {
-   shader->nak = nak_compile_shader(nir, pdev->nak, fs_key);
+   shader->nak = nak_compile_shader(nir, false, pdev->nak, fs_key);
    shader->info = shader->nak->info;
    shader->code_ptr = shader->nak->code;
    shader->code_size = shader->nak->code_size;

Reply via email to