Skip to content

Commit dc3466a

Browse files
committed
fixing Zig generator magic in SPIR-V header; adding zig compiler version to SPIR-V OpSource
1 parent 14019a9 commit dc3466a

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

src/codegen/spirv/Module.zig

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -425,10 +425,21 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
425425
},
426426
};
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/main/include/spirv/spir-v.xml#L17C5-L21C54
437+
const generator_id: u32 = (spec.zig_generator_id << 16) | zig_spirv_compiler_version;
438+
428439
const header = [_]Word{
429440
spec.magic_number,
430441
version.toWord(),
431-
spec.zig_generator_id,
442+
generator_id,
432443
module.idBound(),
433444
0, // Schema (currently reserved for future use)
434445
};
@@ -437,7 +448,7 @@ pub fn finalize(module: *Module, gpa: Allocator) ![]Word {
437448
defer source.deinit(module.gpa);
438449
try module.sections.debug_strings.emit(module.gpa, .OpSource, .{
439450
.source_language = .zig,
440-
.version = 0,
451+
.version = zig_spirv_compiler_version,
441452
// We cannot emit these because the Khronos translator does not parse this instruction
442453
// correctly.
443454
// See https://github.com/KhronosGroup/SPIRV-LLVM-Translator/issues/2188

src/link/SpirV/BinaryModule.zig

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ pub fn finalize(self: BinaryModule, a: Allocator) ![]Word {
6363

6464
result[0] = spec.magic_number;
6565
result[1] = @bitCast(self.version);
66-
result[2] = spec.zig_generator_id;
66+
result[2] = @bitCast(self.generator_magic);
6767
result[3] = self.id_bound;
6868
result[4] = 0; // Schema
6969

@@ -196,7 +196,7 @@ pub const Parser = struct {
196196

197197
var binary = BinaryModule{
198198
.version = @bitCast(module[1]),
199-
.generator_magic = module[2],
199+
.generator_magic = @bitCast(module[2]),
200200
.id_bound = module[3],
201201
.instructions = module[header_words..],
202202
.ext_inst_map = .{},

0 commit comments

Comments
 (0)