authorgravatar for kbz_8.dev@akel-engine.comkbz_8 <kbz_8.dev@akel-engine.com> 2025-11-04 05:40:29+01:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-11-04 04:40:29+00:00
logc603d27f900540b52586ee2638060a4b2ac7c557
treed576609f8f556b37ff19853863add72755115bd6
parentee4df4ad3edad160fb737a1935cd86bc2f9cfbbe
signaturebadge-check Signed by PGP key B5690EEEBB952194

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>

2 files changed, 15 insertions(+), 4 deletions(-)

src/codegen/spirv/Module.zig+13-2
...@@ -425,10 +425,21 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {...@@ -425,10 +425,21 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
425 },425 },
426 };426 };
427427
428 const zig_version = @import("builtin").zig_version;
429 const zig_spirv_compiler_version = comptime (zig_version.major << 12) | (zig_version.minor << 7) | zig_version.patch;
430
431 // A SPIR-V Generator Magic Number is a 32 bit word: The high order 16
432 // bits are a tool ID, which should be unique across all SPIR-V
433 // generators. The low order 16 bits are reserved for use as a tool
434 // version number, or any other purpose the tool supplier chooses.
435 // Only the tool IDs are reserved with Khronos.
436 // See https://github.com/KhronosGroup/SPIRV-Headers/blob/f2e4bd213104fe323a01e935df56557328d37ac8/include/spirv/spir-v.xml#L17C5-L21C54
437 const generator_id: u32 = (spec.zig_generator_id << 16) | zig_spirv_compiler_version;
438
428 const header = [_]Word{439 const header = [_]Word{
429 spec.magic_number,440 spec.magic_number,
430 version.toWord(),441 version.toWord(),
431 spec.zig_generator_id,442 generator_id,
432 module.idBound(),443 module.idBound(),
433 0, // Schema (currently reserved for future use)444 0, // Schema (currently reserved for future use)
434 };445 };
...@@ -437,7 +448,7 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {...@@ -437,7 +448,7 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
437 defer source.deinit(module.gpa);448 defer source.deinit(module.gpa);
438 try module.sections.debug_strings.emit(module.gpa, .OpSource, .{449 try module.sections.debug_strings.emit(module.gpa, .OpSource, .{
439 .source_language = .zig,450 .source_language = .zig,
440 .version = 0,451 .version = zig_spirv_compiler_version,
441 // We cannot emit these because the Khronos translator does not parse this instruction452 // We cannot emit these because the Khronos translator does not parse this instruction
442 // correctly.453 // correctly.
443 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188454 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188
src/link/SpirV/BinaryModule.zig+2-2
...@@ -63,7 +63,7 @@ pub fn finalize(self: BinaryModule, a: Allocator) ![]Word {...@@ -63,7 +63,7 @@ pub fn finalize(self: BinaryModule, a: Allocator) ![]Word {
6363
64 result[0] = spec.magic_number;64 result[0] = spec.magic_number;
65 result[1] = @bitCast(self.version);65 result[1] = @bitCast(self.version);
66 result[2] = spec.zig_generator_id;66 result[2] = @bitCast(self.generator_magic);
67 result[3] = self.id_bound;67 result[3] = self.id_bound;
68 result[4] = 0; // Schema68 result[4] = 0; // Schema
6969
...@@ -196,7 +196,7 @@ pub const Parser = struct {...@@ -196,7 +196,7 @@ pub const Parser = struct {
196196
197 var binary = BinaryModule{197 var binary = BinaryModule{
198 .version = @bitCast(module[1]),198 .version = @bitCast(module[1]),
199 .generator_magic = module[2],199 .generator_magic = @bitCast(module[2]),
200 .id_bound = module[3],200 .id_bound = module[3],
201 .instructions = module[header_words..],201 .instructions = module[header_words..],
202 .ext_inst_map = .{},202 .ext_inst_map = .{},