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>
This commit is contained in:
kbz_8 2025-11-04 05:40:29 +01:00 committed by GitHub
parent ee4df4ad3e
commit c603d27f90
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 4 deletions

View File

@ -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{ const header = [_]Word{
spec.magic_number, spec.magic_number,
version.toWord(), version.toWord(),
spec.zig_generator_id, generator_id,
module.idBound(), module.idBound(),
0, // Schema (currently reserved for future use) 0, // Schema (currently reserved for future use)
}; };
@ -437,7 +448,7 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
defer source.deinit(module.gpa); defer source.deinit(module.gpa);
try module.sections.debug_strings.emit(module.gpa, .OpSource, .{ try module.sections.debug_strings.emit(module.gpa, .OpSource, .{
.source_language = .zig, .source_language = .zig,
.version = 0, .version = zig_spirv_compiler_version,
// We cannot emit these because the Khronos translator does not parse this instruction // We cannot emit these because the Khronos translator does not parse this instruction
// correctly. // correctly.
// See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188 // See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188

View File

@ -63,7 +63,7 @@ pub fn finalize(self: BinaryModule, a: Allocator) ![]Word {
result[0] = spec.magic_number; result[0] = spec.magic_number;
result[1] = @bitCast(self.version); result[1] = @bitCast(self.version);
result[2] = spec.zig_generator_id; result[2] = @bitCast(self.generator_magic);
result[3] = self.id_bound; result[3] = self.id_bound;
result[4] = 0; // Schema result[4] = 0; // Schema
@ -196,7 +196,7 @@ pub const Parser = struct {
var binary = BinaryModule{ var binary = BinaryModule{
.version = @bitCast(module[1]), .version = @bitCast(module[1]),
.generator_magic = module[2], .generator_magic = @bitCast(module[2]),
.id_bound = module[3], .id_bound = module[3],
.instructions = module[header_words..], .instructions = module[header_words..],
.ext_inst_map = .{}, .ext_inst_map = .{},