From 00a407bbae241324ea7da4db5ce7afa2ca7c84f5 Mon Sep 17 00:00:00 2001 From: Faith Ekstrand Date: Sun, 31 May 2026 23:46:39 -0400 Subject: [PATCH] nak: Use EnumAsU8 for RegFile Reviewed-by: Mel Henning Part-of: --- src/nouveau/compiler/nak/ir.rs | 46 +++------------------------ src/nouveau/compiler/nak/ir_proc.rs | 5 +++ src/nouveau/compiler/nak/ssa_value.rs | 2 +- 3 files changed, 10 insertions(+), 43 deletions(-) diff --git a/src/nouveau/compiler/nak/ir.rs b/src/nouveau/compiler/nak/ir.rs index 92646029b9d..477bace2149 100644 --- a/src/nouveau/compiler/nak/ir.rs +++ b/src/nouveau/compiler/nak/ir.rs @@ -18,6 +18,7 @@ pub use crate::ssa_value::*; use compiler::as_slice::*; use compiler::cfg::CFG; use compiler::dataflow::ForwardDataflow; +use compiler::enum_as_u8::EnumAsU8; use compiler::smallvec::SmallVec; use compiler::vec_pair::VecPair; use nak_ir_proc::*; @@ -56,7 +57,7 @@ impl LabelAllocator { /// Represents a register file #[repr(u8)] -#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)] +#[derive(Clone, Copy, Debug, EnumAsU8, Eq, Hash, PartialEq)] pub enum RegFile { /// The general-purpose register file /// @@ -185,45 +186,6 @@ impl fmt::Display for RegFile { } } -impl From for u8 { - fn from(value: RegFile) -> u8 { - value as u8 - } -} - -impl TryFrom for RegFile { - type Error = &'static str; - - fn try_from(value: u32) -> Result { - match value { - 0 => Ok(RegFile::GPR), - 1 => Ok(RegFile::UGPR), - 2 => Ok(RegFile::Pred), - 3 => Ok(RegFile::UPred), - 4 => Ok(RegFile::Carry), - 5 => Ok(RegFile::Bar), - 6 => Ok(RegFile::Mem), - _ => Err("Invalid register file number"), - } - } -} - -impl TryFrom for RegFile { - type Error = &'static str; - - fn try_from(value: u16) -> Result { - RegFile::try_from(u32::from(value)) - } -} - -impl TryFrom for RegFile { - type Error = &'static str; - - fn try_from(value: u8) -> Result { - RegFile::try_from(u32::from(value)) - } -} - /// A trait for things which have an associated register file pub trait HasRegFile { fn file(&self) -> RegFile; @@ -311,7 +273,7 @@ impl Iterator for RegFileSet { if self.is_empty() { None } else { - let file = self.bits.trailing_zeros().try_into().unwrap(); + let file = (self.bits.trailing_zeros() as u8).try_into().unwrap(); self.remove(file); Some(file) } @@ -438,7 +400,7 @@ impl RegRef { impl HasRegFile for RegRef { fn file(&self) -> RegFile { - ((self.packed >> 29) & 0x7).try_into().unwrap() + (((self.packed >> 29) & 0x7) as u8).try_into().unwrap() } } diff --git a/src/nouveau/compiler/nak/ir_proc.rs b/src/nouveau/compiler/nak/ir_proc.rs index 3fd9dccaa04..05000f22908 100644 --- a/src/nouveau/compiler/nak/ir_proc.rs +++ b/src/nouveau/compiler/nak/ir_proc.rs @@ -149,3 +149,8 @@ pub fn enum_derive_display_op(input: TokenStream) -> TokenStream { pub fn derive_from_variants(input: TokenStream) -> TokenStream { compiler_proc::from_variants::derive_from_variants(input) } + +#[proc_macro_derive(EnumAsU8)] +pub fn derive_enum_as_u8(input: TokenStream) -> TokenStream { + compiler_proc::enum_as_u8::derive_enum_as_u8(input) +} diff --git a/src/nouveau/compiler/nak/ssa_value.rs b/src/nouveau/compiler/nak/ssa_value.rs index 7a90eda5fa7..edac3afe8e9 100644 --- a/src/nouveau/compiler/nak/ssa_value.rs +++ b/src/nouveau/compiler/nak/ssa_value.rs @@ -51,7 +51,7 @@ impl SSAValue { impl HasRegFile for SSAValue { /// Returns the register file of this SSA value fn file(&self) -> RegFile { - RegFile::try_from(self.packed.get() >> 29).unwrap() + RegFile::try_from((self.packed.get() >> 29) as u8).unwrap() } }