From 237feff917840637a3036ff2f212080f2e5787cb Mon Sep 17 00:00:00 2001 From: Karol Herbst Date: Tue, 21 Feb 2023 16:52:44 +0100 Subject: [PATCH] rusticl/nir: add a nir_pass macro Signed-off-by: Karol Herbst Part-of: --- .../frontends/rusticl/mesa/compiler/nir.rs | 69 +++++++++++++++++++ src/gallium/frontends/rusticl/meson.build | 2 + 2 files changed, 71 insertions(+) diff --git a/src/gallium/frontends/rusticl/mesa/compiler/nir.rs b/src/gallium/frontends/rusticl/mesa/compiler/nir.rs index 98d5dbad02c..ab6941cfe30 100644 --- a/src/gallium/frontends/rusticl/mesa/compiler/nir.rs +++ b/src/gallium/frontends/rusticl/mesa/compiler/nir.rs @@ -40,6 +40,75 @@ impl<'a, T: 'a> Iterator for ExecListIter<'a, T> { } } +#[macro_export] +#[cfg(debug_assertions)] +macro_rules! nir_pass_impl { + ($nir:ident, $pass:ident, $func:ident $(,$arg:expr)* $(,)?) => { + { + let func_str = ::std::stringify!($func); + let func_cstr = ::std::ffi::CString::new(func_str).unwrap(); + let res = if unsafe { should_skip_nir(func_cstr.as_ptr()) } { + println!("skipping {}", func_str); + false + } else { + $nir.metadata_set_validation_flag(); + if $nir.should_print() { + println!("{}", func_str); + } + if $nir.$pass($func $(,$arg)*) { + $nir.validate(&format!("after {} in {}:{}", func_str, file!(), line!())); + if $nir.should_print() { + $nir.print(); + } + $nir.metadata_check_validation_flag(); + true + } else { + false + } + }; + + // SAFETY: mutable static can't be read safely, but this value isn't going to change + let ndebug = unsafe { nir_debug }; + if ndebug & NIR_DEBUG_CLONE != 0 { + $nir.validate_clone(); + } + + if ndebug & NIR_DEBUG_SERIALIZE != 0 { + $nir.validate_serialize_deserialize(); + } + + res + } + }; +} + +#[macro_export] +#[cfg(not(debug_assertions))] +macro_rules! nir_pass_impl { + ($nir:ident, $pass:ident, $func:ident $(,$arg:expr)* $(,)?) => { + $nir.$pass($func $(,$arg)*) + }; +} + +#[macro_export] +macro_rules! nir_pass { + ($nir:ident, $func:ident $(,)?) => { + $crate::nir_pass_impl!($nir, pass0, $func) + }; + + ($nir:ident, $func:ident, $a:expr $(,)?) => { + $crate::nir_pass_impl!($nir, pass1, $func, $a) + }; + + ($nir:ident, $func:ident, $a:expr, $b:expr $(,)?) => { + $crate::nir_pass_impl!($nir, pass2, $func, $a, $b) + }; + + ($nir:ident, $func:ident, $a:expr, $b:expr, $c:expr $(,)?) => { + $crate::nir_pass_impl!($nir, pass3, $func, $a, $b, $c) + }; +} + pub struct NirPrintfInfo { count: usize, printf_info: *mut u_printf_info, diff --git a/src/gallium/frontends/rusticl/meson.build b/src/gallium/frontends/rusticl/meson.build index e1fbf70cb85..84df0a9edb9 100644 --- a/src/gallium/frontends/rusticl/meson.build +++ b/src/gallium/frontends/rusticl/meson.build @@ -267,6 +267,8 @@ rusticl_mesa_bindings_rs = rust.bindgen( # nir and spirv '--allowlist-function', 'nir_.*', + '--allowlist-var', 'nir_debug', + '--allowlist-var', 'NIR_DEBUG_.*', '--bitfield-enum', 'nir_lower_int64_options', '--bitfield-enum', 'nir_opt_if_options', '--bitfield-enum', 'nir_variable_mode',