From 2b1c6633aaf1d59d4affd88e87a507a02836c478 Mon Sep 17 00:00:00 2001 From: Kleshzz Date: Wed, 22 Jul 2026 09:34:43 +0200 Subject: [PATCH] spirv: fix ICEs when indexing slices, emitting large instructions (#36253) Fixes #36229 Reviewed-on: https://codeberg.org/ziglang/zig/pulls/36253 Reviewed-by: Ali Cheraghi --- src/codegen/spirv/Section.zig | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/codegen/spirv/Section.zig b/src/codegen/spirv/Section.zig index 6207cc787cd0c187c8199fc54efd7c9aaf451efc..fcd7739c1dd3a6916a258e79ce36dae6fec3ba2c 100644 --- a/src/codegen/spirv/Section.zig +++ b/src/codegen/spirv/Section.zig @@ -49,8 +49,9 @@ pub fn emitRaw( operand_words: usize, ) !void { const word_count = 1 + operand_words; + if (word_count > std.math.maxInt(u16)) return error.OutOfMemory; try section.instructions.ensureUnusedCapacity(allocator, word_count); - section.writeWord((@as(Word, @intCast(word_count << 16))) | @backingInt(opcode)); + section.writeWord((@as(Word, @intCast(word_count)) << 16) | @backingInt(opcode)); } /// Write an entire instruction, including all operands @@ -70,7 +71,8 @@ pub fn emitAssumeCapacity( operands: opcode.Operands(), ) !void { const word_count = instructionSize(opcode, operands); - section.writeWord(@as(Word, @intCast(word_count << 16)) | @backingInt(opcode)); + if (word_count > std.math.maxInt(u16)) return error.OutOfMemory; + section.writeWord((@as(Word, @intCast(word_count)) << 16) | @backingInt(opcode)); section.writeOperands(opcode.Operands(), operands); } @@ -81,8 +83,9 @@ pub fn emit( operands: opcode.Operands(), ) !void { const word_count = instructionSize(opcode, operands); + if (word_count > std.math.maxInt(u16)) return error.OutOfMemory; try section.instructions.ensureUnusedCapacity(allocator, word_count); - section.writeWord(@as(Word, @intCast(word_count << 16)) | @backingInt(opcode)); + section.writeWord((@as(Word, @intCast(word_count)) << 16) | @backingInt(opcode)); section.writeOperands(opcode.Operands(), operands); } -- 2.54.0