zdxc: number DXIL instructions once per function

The bitcode writer found an instruction's value ID by counting the
non-void instructions before it, for every operand: quadratic in the
function's size. Large functions (unrolled loops, -O0 hull shaders) took
up to 190 ms; the IDs are now computed once per function. Output is
byte-identical; SubD11_SubDToBezierHS4444 goes from 192 ms to 10 ms
(dxc: 64 ms).
This commit is contained in:
Peter Li 2026-11-01 09:27:47 -08:00
parent 3fbbd5c4b5
commit 9aa5f83508
1 changed files with 14 additions and 5 deletions

View File

@ -276,6 +276,10 @@ const Encoder = struct {
m: *Module, m: *Module,
w: bitstream.Writer, w: bitstream.Writer,
ops: std.ArrayList(u64) = .empty, ops: std.ArrayList(u64) = .empty,
/// The function being written, and each of its instructions' value
/// ID offset (non-void instructions before it).
numbered: ?*const Function = null,
inst_ids: std.ArrayList(u32) = .empty,
fn gpa(e: *Encoder) Allocator { fn gpa(e: *Encoder) Allocator {
return e.w.gpa; return e.w.gpa;
@ -305,12 +309,9 @@ const Encoder = struct {
.arg => |a| e.moduleValueCount() + a, .arg => |a| e.moduleValueCount() + a,
.inst => |i| blk: { .inst => |i| blk: {
const func = f.?; const func = f.?;
std.debug.assert(e.numbered == func);
const nargs: u32 = @intCast(e.m.types.items[func.ty].function.params.len); const nargs: u32 = @intCast(e.m.types.items[func.ty].function.params.len);
var n: u32 = 0; break :blk e.moduleValueCount() + nargs + e.inst_ids.items[i];
for (func.insts.items[0..i]) |prev| {
if (e.m.types.items[prev.ty] != .void) n += 1;
}
break :blk e.moduleValueCount() + nargs + n;
}, },
}; };
} }
@ -337,6 +338,7 @@ const Encoder = struct {
fn write(e: *Encoder) ![]u8 { fn write(e: *Encoder) ![]u8 {
defer e.ops.deinit(e.gpa()); defer e.ops.deinit(e.gpa());
defer e.inst_ids.deinit(e.gpa());
const w = &e.w; const w = &e.w;
try w.emit('B', 8); try w.emit('B', 8);
try w.emit('C', 8); try w.emit('C', 8);
@ -534,6 +536,13 @@ const Encoder = struct {
fn writeFunction(e: *Encoder, f: *const Function) !void { fn writeFunction(e: *Encoder, f: *const Function) !void {
const w = &e.w; const w = &e.w;
e.inst_ids.clearRetainingCapacity();
var n: u32 = 0;
for (f.insts.items) |inst| {
try e.inst_ids.append(e.gpa(), n);
if (e.m.types.items[inst.ty] != .void) n += 1;
}
e.numbered = f;
try w.enterBlock(block_function, 4); try w.enterBlock(block_function, 4);
try e.push(f.num_blocks); try e.push(f.num_blocks);
try e.rec(1); // DECLAREBLOCKS try e.rec(1); // DECLAREBLOCKS