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>
This commit is contained in:
Faith Ekstrand 2023-11-16 16:00:12 -06:00 committed by Marge Bot
parent ae17145882
commit faadac57d5
3 changed files with 25 additions and 5 deletions

View file

@ -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);

View file

@ -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
}

View file

@ -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;