Commit c603d27f90

kbz_8 <kbz_8.dev@akel-engine.com>
2025-11-04 05:40:29
Fixing SPIR-V header generator magic + Adding Zig compiler version to SPIR-V OpSource (#25435)
* fixing Zig generator magic in SPIR-V header; adding zig compiler version to SPIR-V OpSource * Update src/codegen/spirv/Module.zig Co-authored-by: rpkak <67059904+rpkak@users.noreply.github.com> --------- Co-authored-by: rpkak <67059904+rpkak@users.noreply.github.com>
1 parent ee4df4a
Changed files (2)
src
codegen
link
src/codegen/spirv/Module.zig
@@ -425,10 +425,21 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
         },
     };
 
+    const zig_version = @import("builtin").zig_version;
+    const zig_spirv_compiler_version = comptime (zig_version.major << 12) | (zig_version.minor << 7) | zig_version.patch;
+
+    // A SPIR-V Generator Magic Number is a 32 bit word: The high order 16
+    // bits are a tool ID, which should be unique across all SPIR-V
+    // generators. The low order 16 bits are reserved for use as a tool
+    // version number, or any other purpose the tool supplier chooses.
+    // Only the tool IDs are reserved with Khronos.
+    // See https://github.com/KhronosGroup/SPIRV-Headers/blob/f2e4bd213104fe323a01e935df56557328d37ac8/include/spirv/spir-v.xml#L17C5-L21C54
+    const generator_id: u32 = (spec.zig_generator_id << 16) | zig_spirv_compiler_version;
+
     const header = [_]Word{
         spec.magic_number,
         version.toWord(),
-        spec.zig_generator_id,
+        generator_id,
         module.idBound(),
         0, // Schema (currently reserved for future use)
     };
@@ -437,7 +448,7 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
     defer source.deinit(module.gpa);
     try module.sections.debug_strings.emit(module.gpa, .OpSource, .{
         .source_language = .zig,
-        .version = 0,
+        .version = zig_spirv_compiler_version,
         // We cannot emit these because the Khronos translator does not parse this instruction
         // correctly.
         // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188
src/link/SpirV/BinaryModule.zig
@@ -63,7 +63,7 @@ pub fn finalize(self: BinaryModule, a: Allocator) ![]Word {
 
     result[0] = spec.magic_number;
     result[1] = @bitCast(self.version);
-    result[2] = spec.zig_generator_id;
+    result[2] = @bitCast(self.generator_magic);
     result[3] = self.id_bound;
     result[4] = 0; // Schema
 
@@ -196,7 +196,7 @@ pub const Parser = struct {
 
         var binary = BinaryModule{
             .version = @bitCast(module[1]),
-            .generator_magic = module[2],
+            .generator_magic = @bitCast(module[2]),
             .id_bound = module[3],
             .instructions = module[header_words..],
             .ext_inst_map = .{},