From 3419fb03d94505e06f2cdd5a7d733fba5df806ff Mon Sep 17 00:00:00 2001 From: Lauro Oyen Date: Sat, 13 Jul 2024 22:32:03 +0200 Subject: [PATCH] Fix crash when enabling optimizations Due to generating rust style enums for slang enums that can contain more variants than explicitly defined, the generated optimized code probably trunkated the integer values of these additional variants. Fix by using constified enums for these cases. --- slang-sys/build.rs | 13 ++++++++++--- src/lib.rs | 42 +++++++++++++++++++++++++++++++----------- 2 files changed, 41 insertions(+), 14 deletions(-) diff --git a/slang-sys/build.rs b/slang-sys/build.rs index d94fc57..aa876ce 100644 --- a/slang-sys/build.rs +++ b/slang-sys/build.rs @@ -24,10 +24,16 @@ fn main() { .allowlist_type("slang.*") .allowlist_var("SLANG_.*") .with_codegen_config( - bindgen::CodegenConfig::FUNCTIONS | bindgen::CodegenConfig::TYPES | bindgen::CodegenConfig::VARS, + bindgen::CodegenConfig::FUNCTIONS + | bindgen::CodegenConfig::TYPES + | bindgen::CodegenConfig::VARS, ) .parse_callbacks(Box::new(ParseCallback {})) - .default_enum_style(bindgen::EnumVariation::Rust { non_exhaustive: true }) + .default_enum_style(bindgen::EnumVariation::Rust { + non_exhaustive: false, + }) + .constified_enum("SlangProfileID") + .constified_enum("SlangCapabilityID") .vtable_generation(true) .layout_tests(false) .derive_copy(true) @@ -40,7 +46,8 @@ fn main() { fn link_libraries(slang_dir: &Path) { let target_os = env::var("CARGO_CFG_TARGET_OS").expect("Couldn't determine target OS."); - let target_arch = env::var("CARGO_CFG_TARGET_ARCH").expect("Couldn't determine target architecture."); + let target_arch = + env::var("CARGO_CFG_TARGET_ARCH").expect("Couldn't determine target architecture."); let target = match (&*target_os, &*target_arch) { ("windows", "x86") => "windows-x86", diff --git a/src/lib.rs b/src/lib.rs index aadb9cc..6a09689 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -5,12 +5,11 @@ use slang_sys as sys; pub use sys::{ slang_CompilerOptionName as CompilerOptionName, slang_SessionDesc as SessionDesc, - slang_TargetDesc as TargetDesc, SlangCapabilityID as CapabilityID, - SlangCompileTarget as CompileTarget, SlangDebugInfoLevel as DebugInfoLevel, - SlangFloatingPointMode as FloatingPointMode, SlangLineDirectiveMode as LineDirectiveMode, - SlangMatrixLayoutMode as MatrixLayoutMode, SlangOptimizationLevel as OptimizationLevel, - SlangProfileID as ProfileID, SlangSourceLanguage as SourceLanguage, SlangStage as Stage, - SlangUUID as UUID, + slang_TargetDesc as TargetDesc, SlangCompileTarget as CompileTarget, + SlangDebugInfoLevel as DebugInfoLevel, SlangFloatingPointMode as FloatingPointMode, + SlangLineDirectiveMode as LineDirectiveMode, SlangMatrixLayoutMode as MatrixLayoutMode, + SlangOptimizationLevel as OptimizationLevel, SlangSourceLanguage as SourceLanguage, + SlangStage as Stage, SlangUUID as UUID, }; macro_rules! vcall { @@ -28,6 +27,18 @@ const fn uuid(data1: u32, data2: u16, data3: u16, data4: [u8; 8]) -> UUID { } } +pub struct ProfileID(sys::SlangProfileID); + +impl ProfileID { + pub const UNKNOWN: ProfileID = ProfileID(sys::SlangProfileID_SlangProfileUnknown); +} + +pub struct CapabilityID(sys::SlangCapabilityID); + +impl CapabilityID { + pub const UNKNOWN: CapabilityID = CapabilityID(sys::SlangCapabilityID_SlangCapabilityUnknown); +} + unsafe trait Interface: Sized { type Vtable; const IID: UUID; @@ -146,12 +157,12 @@ impl GlobalSession { pub fn find_profile(&self, name: &str) -> ProfileID { let name = CString::new(name).unwrap(); - vcall!(self, findProfile(name.as_ptr())) + ProfileID(vcall!(self, findProfile(name.as_ptr()))) } pub fn find_capability(&self, name: &str) -> CapabilityID { let name = CString::new(name).unwrap(); - vcall!(self, findCapability(name.as_ptr())) + CapabilityID(vcall!(self, findCapability(name.as_ptr()))) } } @@ -369,7 +380,7 @@ impl TargetDescBuilder { } pub fn profile(mut self, profile: ProfileID) -> Self { - self.inner.profile = profile; + self.inner.profile = profile.0; self } @@ -522,7 +533,12 @@ impl OptionsBuilder { option!(Language, language(language: SourceLanguage)); option!(MatrixLayoutColumn, matrix_layout_column(enable: bool)); option!(MatrixLayoutRow, matrix_layout_row(enable: bool)); - option!(Profile, profile(profile: ProfileID)); + + #[inline(always)] + pub fn profile(self, profile: ProfileID) -> Self { + self.push_ints(CompilerOptionName::Profile, profile.0 as _, 0) + } + option!(Stage, stage(stage: Stage)); option!(Target, target(target: CompileTarget)); option!(WarningsAsErrors, warnings_as_errors(warning_codes: &str)); @@ -534,7 +550,11 @@ impl OptionsBuilder { option!(SkipSPIRVValidation, skip_spirv_validation(enable: bool)); // Target - option!(Capability, capability(capability: CapabilityID)); + #[inline(always)] + pub fn capability(self, capability: CapabilityID) -> Self { + self.push_ints(CompilerOptionName::Capability, capability.0 as _, 0) + } + option!(DefaultImageFormatUnknown, default_image_format_unknown(enable: bool)); option!(DisableDynamicDispatch, disable_dynamic_dispatch(enable: bool)); option!(DisableSpecialization, disable_specialization(enable: bool));