diff --git a/src/codegen/spirv/Module.zig b/src/codegen/spirv/Module.zig index d728360d7e07..4478a620c9e5 100644 --- a/src/codegen/spirv/Module.zig +++ b/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/main/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 diff --git a/src/link/SpirV/BinaryModule.zig b/src/link/SpirV/BinaryModule.zig index 3b8f9bd7b60c..e639994f33b7 100644 --- a/src/link/SpirV/BinaryModule.zig +++ b/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 = .{},