diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 180a025..d4f2fc6 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -4033,7 +4033,8 @@ const Emitter = struct { const ptr = try e.add(try e.dm.ptrType(class_ty), .{ .gep = .{ .elem = arr_ty, .ptr = .{ .global = lg }, .indices = try e.arena.dupe(Value, &.{ try e.i32Const(0), index }) } }); if (nonuniform) { const blk = e.blocks.items[e.cur].items; - e.insts.at(blk[blk.len - 1]).md = .{ .kind = try e.dm.mdKind("dx.nonuniform"), .node = try e.dm.mdNode(&.{try e.dm.mdI32(1)}) }; + const kind = try e.dm.mdKind("dx.nonuniform"); + e.insts.at(blk[blk.len - 1]).md = try e.dm.instMd(kind, try e.dm.mdNode(&.{try e.dm.mdI32(1)})); } const loaded = try e.add(class_ty, .{ .load = .{ .ptr = ptr, .@"align" = 4 } }); const struct_name = e.dm.types.items[class_ty].@"struct".name; diff --git a/src/dxil/module.zig b/src/dxil/module.zig index 07a84ee..2533bd9 100644 --- a/src/dxil/module.zig +++ b/src/dxil/module.zig @@ -159,11 +159,15 @@ pub const Inst = struct { extractelement: struct { vec: Value, index: Value }, insertelement: struct { vec: Value, val: Value, index: Value }, }, - /// An attached metadata node (`kind` from Module.mdKind), as - /// `!dx.nonuniform` on a resource array's getelementptr. - md: ?struct { kind: u32, node: MdId } = null, + /// An attached metadata node, as `!dx.nonuniform` on a resource + /// array's getelementptr (see Module.instMd). + md: InstMd = .none, }; +/// An instruction's metadata attachment: an index into Module.inst_mds, +/// kept out of line (few instructions have one). +pub const InstMd = enum(u32) { none = 0, _ }; + pub const Function = struct { name: []const u8, ty: TypeId, @@ -199,6 +203,8 @@ pub const Module = struct { globals: std.ArrayList(GlobalVar) = .empty, metadata: std.ArrayList(Metadata) = .empty, named_metadata: std.ArrayList(struct { name: []const u8, nodes: []const MdId }) = .empty, + /// Instruction metadata attachments (InstMd), from 1. + inst_mds: std.ArrayList(struct { kind: u32, node: MdId }) = .empty, /// Instruction metadata kinds, by id. md_kinds: std.ArrayList([]const u8) = .empty, @@ -285,6 +291,12 @@ pub const Module = struct { return m.constant(.{ .ty = try m.intType(bits), .kind = .{ .int = v } }); } + /// An attachment of node `node` as metadata kind `kind` (from mdKind). + pub fn instMd(m: *Module, kind: u32, node: MdId) !InstMd { + try m.inst_mds.append(m.allocator(), .{ .kind = kind, .node = node }); + return @fromBackingInt(@intCast(m.inst_mds.items.len)); + } + pub fn addFunction(m: *Module, f: Function) !u32 { var owned = f; owned.name = try m.allocator().dupe(u8, f.name); @@ -776,10 +788,11 @@ const Encoder = struct { } // METADATA_ATTACHMENT: [instruction index, (kind, node)...] var any_md = false; - for (f.insts.items) |inst| any_md = any_md or inst.md != null; + for (f.insts.items) |inst| any_md = any_md or inst.md != .none; if (any_md) { try w.enterBlock(block_metadata_attachment, 3); - for (f.insts.items, 0..) |inst, k| if (inst.md) |md| { + for (f.insts.items, 0..) |inst, k| if (inst.md != .none) { + const md = e.m.inst_mds.items[@backingInt(inst.md) - 1]; try e.ops.appendSlice(e.gpa(), &.{ k, md.kind, md.node }); try e.rec(11); };