From 5f4d2dc8e42c241cc014755856a7d3ec76f48996 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sun, 1 Nov 2026 00:07:09 -0700 Subject: [PATCH] zdxc: SM 6.10 LinAlg in libraries Write each library function's RDAT ExtendedProperties the way dxc's AddLinAlgFunctionProperties does: matrix constructions with their operation shapes, thread mat-vec multiplies, wave and thread-group matrix multiplies, outer products and accumulating stores, merged from callees, in the extended property record tables (part types 13-20) with record deduplication. Removes the library LinAlg gate. --- src/dxil/emit.zig | 15 +- src/dxil/linalg_info.zig | 297 +++++++++++++++++++++++++-------------- src/dxil/rdat.zig | 46 +++++- 3 files changed, 249 insertions(+), 109 deletions(-) diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 7a4cef3..e922985 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -4495,12 +4495,17 @@ const Emitter = struct { /// What the module does with matrices (PSV0), when it has any. fn linAlgTables(e: *Emitter) Error!?linalg_info.Tables { if (!e.uses_linalg) return null; + return try linalg_info.collect(e.arena, &e.dm, try e.linAlgMatrices()); + } + + /// Each matrix type's attributes. + fn linAlgMatrices(e: *Emitter) Error!linalg_info.Matrices { const mats = try e.arena.alloc(struct { module.TypeId, linalg_info.Matrix }, e.linalg_types.items.len); for (mats, e.linalg_types.items) |*m, x| { const la = e.t.get(x.ty).linalg; m.* = .{ x.dty, .{ .comp = la.comp, .m = la.m, .n = la.n, .use = la.use, .scope = la.scope } }; } - return try linalg_info.collect(e.arena, &e.dm, mats); + return mats; } /// `!dx.targetTypes`: each matrix type with its attributes. @@ -4925,8 +4930,6 @@ const Emitter = struct { } fn writeLibraryModule(e: *Emitter, funcs: []const LibFunc) Error![]u8 { - // (RDAT's per-function linear algebra properties are not written.) - if (e.uses_linalg) return e.unsupported("linear algebra in libraries is not supported yet", .{}); const dm = &e.dm; const mdI32 = struct { fn f(m: *module.Module, v: i64) !module.MdId { @@ -5028,6 +5031,7 @@ const Emitter = struct { if (r.lib_global) |lg| try res_index.put(e.arena, lg, count); count += 1; }; + const mats = try e.linAlgMatrices(); for (funcs, analysis.infos) |lf, info| { const f = &e.dm.functions.items[lf.func]; const name = try b.string(lf.name); @@ -5040,11 +5044,13 @@ const Emitter = struct { } }; std.mem.sort(u32, used.items, {}, std.sort.asc(u32)); + const resources = if (used.items.len > 0) try b.array(used.items) else rdat.null_ref; + const props = if (e.uses_linalg) try linalg_info.rdatProperties(e.arena, &b, &try linalg_info.functionInfo(e.arena, &e.dm, mats, lf.func)) else rdat.null_ref; const kind = shaderKind(lf.stage); try b.addFunction(.{ .name = name, .unmangled_name = unmangled, - .resources = if (used.items.len > 0) try b.array(used.items) else rdat.null_ref, + .resources = resources, .shader_kind = kind, .payload_size = lf.payload_size, .attribute_size = lf.attr_size, @@ -5052,6 +5058,7 @@ const Emitter = struct { .feature_info2 = @truncate(libinfo.featureInfo(info.flags) >> 32), .shader_stage_flag = if (lf.stage == .library) info.mask & 0xffff else info.mask & (@as(u32, 1) << @intCast(kind)), .min_shader_target = (kind << 16) | (6 << 4) | libinfo.minMinor(info, if (lf.stage == .library) null else kind), + .extended_properties = props, }); } for (e.m.subobjects.items) |so| { diff --git a/src/dxil/linalg_info.zig b/src/dxil/linalg_info.zig index 42b75ac..3fc02ab 100644 --- a/src/dxil/linalg_info.zig +++ b/src/dxil/linalg_info.zig @@ -6,6 +6,7 @@ const std = @import("std"); const Allocator = std.mem.Allocator; const module = @import("module.zig"); +const rdat = @import("rdat.zig"); /// A matrix type's attributes (`!dx.targetTypes`). pub const Matrix = struct { comp: u32, m: u32, n: u32, use: u32, scope: u32 }; @@ -35,7 +36,7 @@ const Key2 = [2]u8; const Key3 = [3]u8; const Key4 = [4]u8; -const Info = struct { +pub const Info = struct { constructions: std.AutoArrayHashMapUnmanaged(Key2, std.AutoArrayHashMapUnmanaged(Shape, void)) = .empty, matvec: std.AutoArrayHashMapUnmanaged(Key3, u8) = .empty, multiplies: std.AutoArrayHashMapUnmanaged(Key4, std.AutoArrayHashMapUnmanaged(Shape, void)) = .empty, @@ -46,117 +47,142 @@ const Info = struct { const transposed: u8 = 1; const non_mul_optimal: u8 = 2; +pub const Matrices = []const struct { module.TypeId, Matrix }; + /// Collects the usage in every function of `m`; `matrices` maps matrix /// types to their attributes. -pub fn collect(a: Allocator, m: *const module.Module, matrices: []const struct { module.TypeId, Matrix }) Allocator.Error!Tables { +pub fn collect(a: Allocator, m: *const module.Module, matrices: Matrices) Allocator.Error!Tables { var info: Info = .{}; - const C = struct { - m: *const module.Module, - mats: []const struct { module.TypeId, Matrix }, - - fn matrix(c: @This(), ty: ?module.TypeId) ?Matrix { - const t = ty orelse return null; - for (c.mats) |x| if (x[0] == t) return x[1]; - return null; - } - - fn constInt(c: @This(), v: module.Value) ?u32 { - if (v != .constant) return null; - return switch (c.m.constants.items[v.constant].kind) { - .int => |x| @truncate(@as(u64, @bitCast(x))), - else => null, - }; - } - - /// DXIL::ComponentType of a scalar (or a vector's element). - fn component(c: @This(), ty: module.TypeId, signed: bool) u8 { - const t = switch (c.m.types.items[ty]) { - .vector => |v| c.m.types.items[v.elem], - else => |x| x, - }; - return switch (t) { - .half => 8, - .float => 9, - .double => 10, - .int => |w| switch (w) { - 8 => if (signed) 19 else 20, - 16 => if (signed) 2 else 3, - 32 => if (signed) 4 else 5, - 64 => if (signed) 6 else 7, - else => 0, - }, - else => 0, - }; - } - }; const c: C = .{ .m = m, .mats = matrices }; - for (m.functions.items) |*f| { - const fty = m.types.items[f.ty].function; - const typeOf = struct { - fn t(mm: *const module.Module, ff: *const module.Function, ft: @FieldType(module.Type, "function"), v: module.Value) ?module.TypeId { - return switch (v) { - .global => |g| mm.globals.items[g].ptr_ty, - .function => null, - .constant => |k| mm.constants.items[k].ty, - .arg => |x| ft.params[x], - .inst => |i| ff.insts.items[i].ty, - }; + for (m.functions.items) |*f| try collectFunction(a, c, &info, f); + return serialize(a, &info); +} + +/// The usage of function `func` and of the functions it calls (dxc merges +/// callee usage into callers). +pub fn functionInfo(a: Allocator, m: *const module.Module, matrices: Matrices, func: u32) Allocator.Error!Info { + var info: Info = .{}; + const c: C = .{ .m = m, .mats = matrices }; + var seen: std.AutoArrayHashMapUnmanaged(u32, void) = .empty; + try seen.put(a, func, {}); + var k: usize = 0; + while (k < seen.count()) : (k += 1) { + const f = &m.functions.items[seen.keys()[k]]; + try collectFunction(a, c, &info, f); + for (f.insts.items) |inst| if (inst.op == .call) { + const callee = inst.op.call.callee; + if (m.functions.items[callee].num_blocks > 0) try seen.put(a, callee, {}); + }; + } + return info; +} + +const C = struct { + m: *const module.Module, + mats: Matrices, + + fn matrix(c: @This(), ty: ?module.TypeId) ?Matrix { + const t = ty orelse return null; + for (c.mats) |x| if (x[0] == t) return x[1]; + return null; + } + + fn constInt(c: @This(), v: module.Value) ?u32 { + if (v != .constant) return null; + return switch (c.m.constants.items[v.constant].kind) { + .int => |x| @truncate(@as(u64, @bitCast(x))), + else => null, + }; + } + + /// DXIL::ComponentType of a scalar (or a vector's element). + fn component(c: @This(), ty: module.TypeId, signed: bool) u8 { + const t = switch (c.m.types.items[ty]) { + .vector => |v| c.m.types.items[v.elem], + else => |x| x, + }; + return switch (t) { + .half => 8, + .float => 9, + .double => 10, + .int => |w| switch (w) { + 8 => if (signed) 19 else 20, + 16 => if (signed) 2 else 3, + 32 => if (signed) 4 else 5, + 64 => if (signed) 6 else 7, + else => 0, + }, + else => 0, + }; + } +}; + +fn collectFunction(a: Allocator, c: C, info: *Info, f: *const module.Function) Allocator.Error!void { + const m = c.m; + const fty = m.types.items[f.ty].function; + const typeOf = struct { + fn t(mm: *const module.Module, ff: *const module.Function, ft: @FieldType(module.Type, "function"), v: module.Value) ?module.TypeId { + return switch (v) { + .global => |g| mm.globals.items[g].ptr_ty, + .function => null, + .constant => |k| mm.constants.items[k].ty, + .arg => |x| ft.params[x], + .inst => |i| ff.insts.items[i].ty, + }; + } + }.t; + for (f.insts.items) |inst| { + if (inst.op != .call) continue; + const call = inst.op.call; + if (!std.mem.startsWith(u8, m.functions.items[call.callee].name, "dx.op.linAlg") or call.args.len == 0) continue; + const op = c.constInt(call.args[0]) orelse continue; + const arg = struct { + fn t(cc: C, ff: *const module.Function, ft: @FieldType(module.Type, "function"), args: []const module.Value, k: usize) ?module.TypeId { + return if (k < args.len) typeOf(cc.m, ff, ft, args[k]) else null; } }.t; - for (f.insts.items) |inst| { - if (inst.op != .call) continue; - const call = inst.op.call; - if (!std.mem.startsWith(u8, m.functions.items[call.callee].name, "dx.op.linAlg") or call.args.len == 0) continue; - const op = c.constInt(call.args[0]) orelse continue; - const arg = struct { - fn t(cc: C, ff: *const module.Function, ft: @FieldType(module.Type, "function"), args: []const module.Value, k: usize) ?module.TypeId { - return if (k < args.len) typeOf(cc.m, ff, ft, args[k]) else null; - } - }.t; - switch (op) { - 336, 337 => { - const mat = c.matrix(arg(c, f, fty, call.args, 1)) orelse continue; - const signed = (c.constInt(call.args[2]) orelse 0) != 0; - const key: Key3 = .{ c.component(inst.ty, signed), @intCast(mat.comp), @intCast(c.constInt(call.args[4]) orelse 0) }; - const gop = try info.matvec.getOrPut(a, key); - if (!gop.found_existing) gop.value_ptr.* = 0; - gop.value_ptr.* |= layoutFlags(c, f, call.args[1]); - }, - 334, 322 => { - const r = c.matrix(inst.ty) orelse continue; - const ma = c.matrix(arg(c, f, fty, call.args, 1)) orelse continue; - const mb = c.matrix(arg(c, f, fty, call.args, 2)) orelse continue; - const key: Key4 = .{ @intCast(r.scope), @intCast(r.comp), @intCast(ma.comp), @intCast(mb.comp) }; - const gop = try info.multiplies.getOrPut(a, key); - if (!gop.found_existing) gop.value_ptr.* = .empty; - try gop.value_ptr.put(a, .{ .m = ma.m, .n = mb.n, .k = ma.n }, {}); - }, - 340 => { - const r = c.matrix(inst.ty) orelse continue; - const signed = (c.constInt(call.args[1]) orelse 0) != 0; - const vt = arg(c, f, fty, call.args, 2) orelse continue; - try info.outer.put(a, .{ @intCast(r.comp), c.component(vt, signed) }, {}); - }, - 338, 339 => { - const mat = c.matrix(arg(c, f, fty, call.args, 1)) orelse continue; - const gop = try info.accumulate.getOrPut(a, @intCast(mat.comp)); - if (!gop.found_existing) gop.value_ptr.* = 0; - gop.value_ptr.* |= if (op == 338) 1 else 2; - }, - 342 => { - const vt = arg(c, f, fty, call.args, 4) orelse continue; - const gop = try info.accumulate.getOrPut(a, c.component(vt, true)); - if (!gop.found_existing) gop.value_ptr.* = 0; - gop.value_ptr.* |= 1; - }, - else => { - try construction(a, &info, c.matrix(inst.ty)); - for (call.args, 0..) |_, k| try construction(a, &info, c.matrix(arg(c, f, fty, call.args, k))); - }, - } + switch (op) { + 336, 337 => { + const mat = c.matrix(arg(c, f, fty, call.args, 1)) orelse continue; + const signed = (c.constInt(call.args[2]) orelse 0) != 0; + const key: Key3 = .{ c.component(inst.ty, signed), @intCast(mat.comp), @intCast(c.constInt(call.args[4]) orelse 0) }; + const gop = try info.matvec.getOrPut(a, key); + if (!gop.found_existing) gop.value_ptr.* = 0; + gop.value_ptr.* |= layoutFlags(c, f, call.args[1]); + }, + 334, 322 => { + const r = c.matrix(inst.ty) orelse continue; + const ma = c.matrix(arg(c, f, fty, call.args, 1)) orelse continue; + const mb = c.matrix(arg(c, f, fty, call.args, 2)) orelse continue; + const key: Key4 = .{ @intCast(r.scope), @intCast(r.comp), @intCast(ma.comp), @intCast(mb.comp) }; + const gop = try info.multiplies.getOrPut(a, key); + if (!gop.found_existing) gop.value_ptr.* = .empty; + try gop.value_ptr.put(a, .{ .m = ma.m, .n = mb.n, .k = ma.n }, {}); + }, + 340 => { + const r = c.matrix(inst.ty) orelse continue; + const signed = (c.constInt(call.args[1]) orelse 0) != 0; + const vt = arg(c, f, fty, call.args, 2) orelse continue; + try info.outer.put(a, .{ @intCast(r.comp), c.component(vt, signed) }, {}); + }, + 338, 339 => { + const mat = c.matrix(arg(c, f, fty, call.args, 1)) orelse continue; + const gop = try info.accumulate.getOrPut(a, @intCast(mat.comp)); + if (!gop.found_existing) gop.value_ptr.* = 0; + gop.value_ptr.* |= if (op == 338) 1 else 2; + }, + 342 => { + const vt = arg(c, f, fty, call.args, 4) orelse continue; + const gop = try info.accumulate.getOrPut(a, c.component(vt, true)); + if (!gop.found_existing) gop.value_ptr.* = 0; + gop.value_ptr.* |= 1; + }, + else => { + try construction(a, info, c.matrix(inst.ty)); + for (call.args, 0..) |_, k| try construction(a, info, c.matrix(arg(c, f, fty, call.args, k))); + }, } } - return serialize(a, &info); } fn construction(a: Allocator, info: *Info, mat: ?Matrix) Allocator.Error!void { @@ -309,3 +335,66 @@ pub fn psvBlock(a: Allocator, t: Tables, out: *std.ArrayList(u8)) Allocator.Erro try out.appendSlice(a, r); }; } + +/// AddLinAlgShapes: the shape records' index array. +fn rdatShapes(a: Allocator, b: *rdat.Builder, set: std.AutoArrayHashMapUnmanaged(Shape, void)) Allocator.Error!u32 { + var idx: std.ArrayList(u32) = .empty; + for (try sortedShapes(a, set)) |s| { + var rec: [12]u8 = undefined; + for ([_]u32{ s.m, s.n, s.k }, 0..) |v, k| std.mem.writeInt(u32, rec[4 * k ..][0..4], v, .little); + try idx.append(a, try b.record(.shapes, &rec)); + } + return b.array(idx.items); +} + +/// A record with an index array reference, then four bytes. +fn refRecord(ref: u32, tail: [4]u8) [8]u8 { + var rec: [8]u8 = undefined; + std.mem.writeInt(u32, rec[0..4], ref, .little); + rec[4..8].* = tail; + return rec; +} + +/// The function's RDAT ExtendedProperties (dxc's +/// AddLinAlgFunctionProperties): one property per non-empty kind, in +/// FunctionPropertyType order. +pub fn rdatProperties(a: Allocator, b: *rdat.Builder, info: *const Info) Allocator.Error!u32 { + var props: std.ArrayList(u32) = .empty; + var recs: std.ArrayList(u32) = .empty; + const flush = struct { + fn f(aa: Allocator, bb: *rdat.Builder, ps: *std.ArrayList(u32), rs: *std.ArrayList(u32), ty: u32) Allocator.Error!void { + if (rs.items.len == 0) return; + var arr: [4]u8 = undefined; + std.mem.writeInt(u32, &arr, try bb.array(rs.items), .little); + try ps.append(aa, try bb.record(.properties, &refRecord(ty, arr))); + rs.clearRetainingCapacity(); + } + }.f; + for (try sortedKeys(Key2, a, info.constructions.keys())) |k| { + const shapes = try rdatShapes(a, b, info.constructions.get(k).?); + try recs.append(a, try b.record(.constructions, &refRecord(shapes, .{ k[0], 0, 0, 0 }))); + } + try flush(a, b, &props, &recs, 1); + for (try sortedKeys(Key3, a, info.matvec.keys())) |k| { + try recs.append(a, try b.record(.thread_matvec, &.{ k[0], k[1], k[2], info.matvec.get(k).? })); + } + try flush(a, b, &props, &recs, 2); + const mul_keys = try sortedKeys(Key4, a, info.multiplies.keys()); + for ([_]u8{ 1, 2 }, [_]rdat.Ext{ .wave_multiplies, .group_multiplies }, [_]u32{ 3, 4 }) |scope, t, ty| { + for (mul_keys) |k| if (k[0] == scope) { + const shapes = try rdatShapes(a, b, info.multiplies.get(k).?); + try recs.append(a, try b.record(t, &refRecord(shapes, .{ k[1], k[2], k[3], 0 }))); + }; + try flush(a, b, &props, &recs, ty); + } + for (try sortedKeys(Key2, a, info.outer.keys())) |k| { + try recs.append(a, try b.record(.outer_products, &.{ k[0], k[1], 0, 0 })); + } + try flush(a, b, &props, &recs, 5); + for (try sortedKeys(u8, a, info.accumulate.keys())) |k| { + try recs.append(a, try b.record(.accumulate_stores, &.{ k, info.accumulate.get(k).?, 0, 0 })); + } + try flush(a, b, &props, &recs, 6); + if (props.items.len == 0) return rdat.null_ref; + return b.array(props.items); +} diff --git a/src/dxil/rdat.zig b/src/dxil/rdat.zig index 7d759d6..98d98fc 100644 --- a/src/dxil/rdat.zig +++ b/src/dxil/rdat.zig @@ -5,7 +5,8 @@ //! Layout: a header (version, part count), part offsets, then each //! non-empty part as (type, size) and its 4-byte aligned contents. Parts //! come in the order dxc adds them: strings, resources, functions, index -//! arrays, raw bytes, subobjects (only the first four are produced here). +//! arrays, raw bytes, subobjects, then the record tables of the extended +//! function properties (SM 6.10 linear algebra). const std = @import("std"); const Allocator = std.mem.Allocator; @@ -43,6 +44,28 @@ pub const Function = struct { extended_properties: u32 = null_ref, }; +/// The extended function property tables, in part order (part types +/// 13..20). +pub const Ext = enum(u32) { + properties = 13, + shapes, + constructions, + thread_matvec, + wave_multiplies, + group_multiplies, + outer_products, + accumulate_stores, + + /// Record size in bytes. + pub fn stride(t: Ext) usize { + return switch (t) { + .shapes => 12, + .properties, .constructions, .wave_multiplies, .group_multiplies => 8, + .thread_matvec, .outer_products, .accumulate_stores => 4, + }; + } +}; + pub const Builder = struct { gpa: Allocator, strings: std.ArrayList(u8) = .empty, @@ -52,6 +75,7 @@ pub const Builder = struct { functions: std.ArrayList([14]u32) = .empty, raw: std.ArrayList(u8) = .empty, subobjects: std.ArrayList([6]u32) = .empty, + ext: [8]std.ArrayList(u8) = @splat(.empty), pub fn init(gpa: Allocator) Allocator.Error!Builder { var b: Builder = .{ .gpa = gpa }; @@ -104,6 +128,19 @@ pub const Builder = struct { try b.subobjects.append(b.gpa, rec); } + /// Index of a record in table `t`; an identical record is shared + /// (record deduplication, validator 1.7+). + pub fn record(b: *Builder, t: Ext, rec: []const u8) Allocator.Error!u32 { + std.debug.assert(rec.len == t.stride()); + const tab = &b.ext[@intFromEnum(t) - 13]; + var at: usize = 0; + while (at < tab.items.len) : (at += rec.len) { + if (std.mem.eql(u8, tab.items[at..][0..rec.len], rec)) return @intCast(at / rec.len); + } + try tab.appendSlice(b.gpa, rec); + return @intCast(at / rec.len); + } + /// Offset of `bytes` in the raw bytes part. pub fn bytes(b: *Builder, data: []const u8) Allocator.Error!u32 { const off: u32 = @intCast(b.raw.items.len); @@ -139,6 +176,12 @@ pub const Builder = struct { try tables.append(b.gpa, t); try parts.append(b.gpa, .{ .ty = 6, .data = t }); } + for (b.ext, 13..) |rows, ty| if (rows.items.len > 0) { + const stride = @as(Ext, @enumFromInt(ty)).stride(); + const t = try table(b.gpa, stride / 4, rows.items, rows.items.len / stride); + try tables.append(b.gpa, t); + try parts.append(b.gpa, .{ .ty = @intCast(ty), .data = t }); + }; var size: usize = 8 + 4 * parts.items.len; for (parts.items) |p| size += 8 + std.mem.alignForward(usize, p.data.len, 4); @@ -174,6 +217,7 @@ pub const Builder = struct { b.functions.deinit(b.gpa); b.raw.deinit(b.gpa); b.subobjects.deinit(b.gpa); + for (&b.ext) |*t| t.deinit(b.gpa); } };