From 940aab3ef0076a33729455240c83ce103f81cae7 Mon Sep 17 00:00:00 2001 From: firestar99 Date: Fri, 29 May 2026 15:09:24 +0200 Subject: [PATCH 1/2] cleanup: increase LiteralConst SmallVec since it's free --- crates/rspirv2-types/src/operand/literal_const.rs | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/crates/rspirv2-types/src/operand/literal_const.rs b/crates/rspirv2-types/src/operand/literal_const.rs index 2bab0aba..a3494aaa 100644 --- a/crates/rspirv2-types/src/operand/literal_const.rs +++ b/crates/rspirv2-types/src/operand/literal_const.rs @@ -37,7 +37,12 @@ pub const OPERAND_KIND_LITERAL_CONTEXT_DEPENDENT_NUMBER: OperandKind = OperandKi /// [`Instruction`]: crate::meta::InstMeta /// [`Quantifier::ZeroOrMore`]: `crate::meta::Quantifier` #[derive(Clone, Debug, Eq, PartialEq, Hash)] -pub struct LiteralConst(SmallVec<[Word; 2]>); +pub struct LiteralConst(SmallVec<[Word; 4]>); + +const _: () = { + // usually 2 Words should be sufficient to represent 64bit values, but 4 words is free: + assert!(size_of::() == size_of::>()); +}; pub type LiteralContextDependentNumber = LiteralConst; From 4363d461477e379b05e956ec871a39368cc05a62 Mon Sep 17 00:00:00 2001 From: firestar99 Date: Fri, 29 May 2026 15:36:59 +0200 Subject: [PATCH 2/2] cleanup: split codegen `mod instructions` into `inst` and `inst_set` --- .../src/codegen/{instructions.rs => inst.rs} | 82 +------------------ crates/grammar-parser/src/codegen/inst_set.rs | 82 +++++++++++++++++++ crates/grammar-parser/src/codegen/mod.rs | 3 +- crates/grammar-parser/src/codegen/modules.rs | 3 +- 4 files changed, 87 insertions(+), 83 deletions(-) rename crates/grammar-parser/src/codegen/{instructions.rs => inst.rs} (67%) create mode 100644 crates/grammar-parser/src/codegen/inst_set.rs diff --git a/crates/grammar-parser/src/codegen/instructions.rs b/crates/grammar-parser/src/codegen/inst.rs similarity index 67% rename from crates/grammar-parser/src/codegen/instructions.rs rename to crates/grammar-parser/src/codegen/inst.rs index 41d3f23c..717ce51a 100644 --- a/crates/grammar-parser/src/codegen/instructions.rs +++ b/crates/grammar-parser/src/codegen/inst.rs @@ -1,7 +1,6 @@ -use crate::codegen::options::CodegenOptions; use crate::codegen::{GrammarWriter, OPERAND_ID_RESULT, OPERAND_ID_RESULT_TYPE}; use crate::parse::{Grammar, InstMeta, Operand, Quantifier}; -use quote::{format_ident, quote}; +use quote::quote; pub fn write_inst(writer: &mut GrammarWriter, grammar: &Grammar<'_>) -> anyhow::Result<()> { let insts = grammar.insts.iter().map(|inst| { @@ -156,82 +155,3 @@ pub fn write_inst(writer: &mut GrammarWriter, grammar: &Grammar<'_>) -> anyhow:: }, ) } - -pub fn write_inst_enum( - writer: &mut GrammarWriter, - grammar: &Grammar<'_>, - opt: &CodegenOptions<'_>, -) -> anyhow::Result<()> { - let name = format_ident!("{}InstSet", opt.name_suffix_type); - let insts = grammar - .insts - .iter() - .map(|inst| { - let type_ident = InstMeta::type_ident(&inst.opname); - let enum_ident = InstMeta::enum_ident(&inst.opname); - (inst, type_ident, enum_ident) - }) - .collect::>(); - let enum_variants = insts - .iter() - .map(|(_, type_ident, enum_ident)| quote!(#enum_ident(#type_ident),)); - let encode_match = insts.iter().map( - |(_, _, enum_ident)| quote!(Self::#enum_ident(inst) => InstEncoding::encode(inst, writer),), - ); - let decode_match = insts.iter().map(|(inst, type_ident, enum_ident)| { - let opcode = inst.opcode; - quote!(#opcode => Self::#enum_ident(<#type_ident as InstEncoding>::decode(reader)?),) - }); - let dis_match = insts.iter().map( - |(_, _, enum_ident)| quote!(Self::#enum_ident(inst) => InstEncoding::dis_fmt(inst, f, ctx),), - ); - let from_impls = insts.iter().map(|(_, type_ident, enum_ident)| { - quote! { - impl From<#type_ident> for #name { - fn from(inst: #type_ident) -> Self { - Self::#enum_ident(inst) - } - } - } - }); - writer.write_module( - "inst_set", - quote! { - #[derive(Clone, Debug, Eq, PartialEq, Hash)] - pub enum #name { - #(#enum_variants)* - } - - impl InstEncoding for #name { - fn name() -> &'static str { - stringify!(#name) - } - - fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> { - profiling::function_scope!(); - match self { - #(#encode_match)* - } - } - - fn decode(reader: InstReader<'_>) -> Result { - profiling::function_scope!(); - let opcode = reader.opcode(); - Ok(match opcode { - #(#decode_match)* - _ => return Err(DecodeErrorKind::UnknownOpCode { opcode }.into()), - }) - } - - fn dis_fmt(&self, f: &mut Formatter<'_>, ctx: &DisContext) -> std::fmt::Result { - profiling::function_scope!(); - match self { - #(#dis_match)* - } - } - } - - #(#from_impls)* - }, - ) -} diff --git a/crates/grammar-parser/src/codegen/inst_set.rs b/crates/grammar-parser/src/codegen/inst_set.rs new file mode 100644 index 00000000..8e00e100 --- /dev/null +++ b/crates/grammar-parser/src/codegen/inst_set.rs @@ -0,0 +1,82 @@ +use crate::codegen::{CodegenOptions, GrammarWriter}; +use crate::parse::{Grammar, InstMeta}; +use quote::{format_ident, quote}; + +pub fn write_inst_enum( + writer: &mut GrammarWriter, + grammar: &Grammar<'_>, + opt: &CodegenOptions<'_>, +) -> anyhow::Result<()> { + let name = format_ident!("{}InstSet", opt.name_suffix_type); + let insts = grammar + .insts + .iter() + .map(|inst| { + let type_ident = InstMeta::type_ident(&inst.opname); + let enum_ident = InstMeta::enum_ident(&inst.opname); + (inst, type_ident, enum_ident) + }) + .collect::>(); + let enum_variants = insts + .iter() + .map(|(_, type_ident, enum_ident)| quote!(#enum_ident(#type_ident),)); + let encode_match = insts.iter().map( + |(_, _, enum_ident)| quote!(Self::#enum_ident(inst) => InstEncoding::encode(inst, writer),), + ); + let decode_match = insts.iter().map(|(inst, type_ident, enum_ident)| { + let opcode = inst.opcode; + quote!(#opcode => Self::#enum_ident(<#type_ident as InstEncoding>::decode(reader)?),) + }); + let dis_match = insts.iter().map( + |(_, _, enum_ident)| quote!(Self::#enum_ident(inst) => InstEncoding::dis_fmt(inst, f, ctx),), + ); + let from_impls = insts.iter().map(|(_, type_ident, enum_ident)| { + quote! { + impl From<#type_ident> for #name { + fn from(inst: #type_ident) -> Self { + Self::#enum_ident(inst) + } + } + } + }); + writer.write_module( + "inst_set", + quote! { + #[derive(Clone, Debug, Eq, PartialEq, Hash)] + pub enum #name { + #(#enum_variants)* + } + + impl InstEncoding for #name { + fn name() -> &'static str { + stringify!(#name) + } + + fn encode(&self, writer: &mut impl WordWriter) -> Result<(), EncodeError> { + profiling::function_scope!(); + match self { + #(#encode_match)* + } + } + + fn decode(reader: InstReader<'_>) -> Result { + profiling::function_scope!(); + let opcode = reader.opcode(); + Ok(match opcode { + #(#decode_match)* + _ => return Err(DecodeErrorKind::UnknownOpCode { opcode }.into()), + }) + } + + fn dis_fmt(&self, f: &mut Formatter<'_>, ctx: &DisContext) -> std::fmt::Result { + profiling::function_scope!(); + match self { + #(#dis_match)* + } + } + } + + #(#from_impls)* + }, + ) +} diff --git a/crates/grammar-parser/src/codegen/mod.rs b/crates/grammar-parser/src/codegen/mod.rs index 7eddcaa4..87781df4 100644 --- a/crates/grammar-parser/src/codegen/mod.rs +++ b/crates/grammar-parser/src/codegen/mod.rs @@ -1,5 +1,6 @@ mod emit; -mod instructions; +mod inst; +mod inst_set; mod modules; mod operands; mod options; diff --git a/crates/grammar-parser/src/codegen/modules.rs b/crates/grammar-parser/src/codegen/modules.rs index 69b3ba3d..1daf378c 100644 --- a/crates/grammar-parser/src/codegen/modules.rs +++ b/crates/grammar-parser/src/codegen/modules.rs @@ -1,4 +1,5 @@ -use crate::codegen::instructions::{write_inst, write_inst_enum}; +use crate::codegen::inst::write_inst; +use crate::codegen::inst_set::write_inst_enum; use crate::codegen::operands::write_operands; use crate::codegen::options::CodegenOptions; use crate::codegen::{EmitRef, GrammarWriter};