From a3c9a1635153175eeeedaf8dd62775c022b8d044 Mon Sep 17 00:00:00 2001 From: Peter Li Date: Sat, 31 Oct 2026 20:49:41 -0700 Subject: [PATCH] SM 6.9 long vectors (DXIL) - vector with N up to 1024 from shader model 6.9 (vector lengths and shapes widen to u16); intrinsic layouts accept long vectors. - DXIL keeps the scalarized form: buffers, groupshared memory, structs, intrinsics and LinAlg vector operands work for any length. - dxc's diagnostics: no swizzles on long vectors, and none in cbuffers, tbuffers, $Globals, ConstantBuffer/TextureBuffer, patches, streams or entry point parameters / return types. - SPIR-V reports long vectors as unsupported (Vulkan has none; dxc's output is invalid). Co-Authored-By: Claude Opus 5.5 --- src/sema/intrinsics.zig | 15 ++++++------ src/sema/sema.zig | 52 +++++++++++++++++++++++++++++++++++++---- src/sema/types.zig | 8 +++---- src/spirv/emit.zig | 1 + 4 files changed, 60 insertions(+), 16 deletions(-) diff --git a/src/sema/intrinsics.zig b/src/sema/intrinsics.zig index 0628c15..6e37df7 100644 --- a/src/sema/intrinsics.zig +++ b/src/sema/intrinsics.zig @@ -120,20 +120,20 @@ const Layout = struct { rows: u8, cols: u8, kind: TypeTable.Shape }; /// Symbolic dimension bindings (`r`, `c`, `r2`, `c2`, ...). const Bindings = struct { names: [8][]const u8 = undefined, - values: [8]u8 = undefined, + values: [8]u16 = undefined, n: usize = 0, /// The widest shape of the arguments declared `<>`: all such types of /// an intrinsic share their dimensions (dxc's r / c), scalars /// splatting to the widest. any_shape: ?TypeTable.Shape = null, - fn get(b: *const Bindings, name: []const u8) ?u8 { + fn get(b: *const Bindings, name: []const u8) ?u16 { for (b.names[0..b.n], b.values[0..b.n]) |k, v| if (std.mem.eql(u8, k, name)) return v; return null; } /// Bind or check; false on conflict. - fn bind(b: *Bindings, name: []const u8, v: u8) bool { + fn bind(b: *Bindings, name: []const u8, v: u16) bool { if (b.get(name)) |old| return old == v; if (b.n == b.names.len) return false; b.names[b.n] = name; @@ -291,18 +291,19 @@ fn layoutType(sema: anytype, ls: *const table.Spec, comp: Scalar, group: usize, const arg_shape: ?TypeTable.Shape = if (group > 0 and group - 1 < args.len) t.shape(args[group - 1].ty) else null; if (ls.dims.len == 1) { const len = dimValue(ls.dims[0], bindings, if (arg_shape) |sh| (if (sh.kind == .vector) sh.cols else if (sh.kind == .scalar) 1 else sh.cols) else null) orelse return null; - if (len < 1 or len > 4) return null; + // (Long vectors, SM 6.9: only arguments can make them.) + if (len < 1 or len > 1024) return null; return try t.vector(comp, len); } const rows = dimValue(ls.dims[0], bindings, if (arg_shape) |sh| sh.rows else null) orelse return null; const cols = dimValue(ls.dims[1], bindings, if (arg_shape) |sh| sh.cols else null) orelse return null; if (rows < 1 or rows > 4 or cols < 1 or cols > 4) return null; - return try t.matrix(comp, rows, cols); + return try t.matrix(comp, @intCast(rows), @intCast(cols)); } /// A layout dimension: a number, or a symbol bound from the argument. -fn dimValue(dim: []const u8, b: *Bindings, from_arg: ?u8) ?u8 { - if (std.fmt.parseInt(u8, dim, 10)) |v| return v else |_| {} +fn dimValue(dim: []const u8, b: *Bindings, from_arg: ?u16) ?u16 { + if (std.fmt.parseInt(u16, dim, 10)) |v| return v else |_| {} if (b.get(dim)) |v| return v; const v = from_arg orelse return null; if (!b.bind(dim, v)) return null; diff --git a/src/sema/sema.zig b/src/sema/sema.zig index 8918ae1..b3c9539 100644 --- a/src/sema/sema.zig +++ b/src/sema/sema.zig @@ -1109,7 +1109,6 @@ pub const Sema = struct { s.err(templateArgLoc(args[1]), "invalid value, valid range is between 1 and {d} inclusive", .{max}); return TypeTable.error_type; } - if (len > 4) return TypeTable.error_type; // long vectors: not modelled return s.types.vector(comp, @intCast(len)); } if (std.mem.eql(u8, n, "matrix")) { @@ -1169,16 +1168,19 @@ pub const Sema = struct { }, } } + if ((kind == .constant_buffer or kind == .texture_buffer) and args.len > 0) s.checkLongVectors(elem, templateArgLoc(args[0]), "ConstantBuffers or TextureBuffers"); return s.types.intern(.{ .resource = .{ .kind = kind, .elem = elem, .name = n } }); } const streams = [_]struct { []const u8, @FieldType(@FieldType(types.Type, "stream"), "kind") }{ .{ "PointStream", .point }, .{ "LineStream", .line }, .{ "TriangleStream", .triangle } }; for (streams) |st| if (std.mem.eql(u8, n, st[0])) { const elem = if (args.len > 0) try s.typeArg(args[0]) orelse TypeTable.error_type else TypeTable.error_type; + s.checkLongVectors(elem, t.loc, "geometry streams"); return s.types.intern(.{ .stream = .{ .kind = st[1], .elem = elem } }); }; if (std.mem.eql(u8, n, "InputPatch") or std.mem.eql(u8, n, "OutputPatch")) { const elem = if (args.len > 0) try s.typeArg(args[0]) orelse TypeTable.error_type else TypeTable.error_type; const count: u32 = if (args.len > 1) @intCast(@max(0, s.constArg(args[1]) orelse 0)) else 0; + s.checkLongVectors(elem, t.loc, "tessellation patches"); return s.types.intern(.{ .patch = .{ .output = n[0] == 'O', .elem = elem, .count = count } }); } } @@ -1343,6 +1345,23 @@ pub const Sema = struct { return .{ .type = ty }; } + /// SM 6.9 long vectors (over 4 elements) are not allowed in `context`. + fn checkLongVectors(s: *Sema, ty: TypeId, loc: Loc, comptime context: []const u8) void { + if (s.hasLongVector(ty, 0)) s.err(loc, "vectors of over 4 elements in " ++ context ++ " are not supported", .{}); + } + + fn hasLongVector(s: *Sema, ty: TypeId, depth: u32) bool { + if (depth > 32) return false; + return switch (s.types.get(ty)) { + .vector => |v| v.len > 4, + .array => |a| s.hasLongVector(a.elem, depth + 1), + .@"struct" => |st| for (st.fields.items) |f| { + if (s.hasLongVector(f.type, depth + 1)) break true; + } else false, + else => false, + }; + } + /// The string argument of attribute `name` (`[[vk::builtin("X")]]`). pub fn vkAttrString(attrs: []const ast.Attribute, name: []const u8) ?[]const u8 { for (attrs) |a| if (std.mem.eql(u8, a.name, name) and a.args.len == 1 and a.args[0].kind == .string_lit) { @@ -1525,6 +1544,19 @@ pub const Sema = struct { pub fn checkUnit(s: *Sema, tu: ast.TranslationUnit) Allocator.Error!void { for (tu.decls) |d| try s.decl(d, .global); + // Entry points take and return no long vectors (SM 6.9). + for (s.all_functions.items) |f| { + if (f.owner != null) continue; + const entry = if (s.opts.library) for (f.decl.attrs) |a| { + if (eql(a.name, "shader")) break true; + } else false else if (s.opts.entry) |en| eql(f.name, en) else false; + if (!entry) continue; + for (f.params, f.decl.params) |p, pd| switch (s.types.get(p.type)) { + .patch, .stream => {}, + else => s.checkLongVectors(p.type, pd.name_loc, "entry function parameters"), + }; + s.checkLongVectors(f.ret, f.decl.name.loc, "entry function return type"); + } if (s.opts.entry) |entry| { const found = if (s.global.syms.get(entry)) |sym| sym == .functions else false; if (!found) _ = s.diags.report(.{ .severity = .@"error", .loc = .none }, "missing entry point definition", .{}); @@ -2203,8 +2235,14 @@ pub const Sema = struct { }; try s.declare(v.name, .{ .variable = variable }, v.name_loc); try s.decl_vars.put(s.arena, v, variable); - if (ctx == .cbuffer) variable.block = s.current_block; - if (storage == .global_uniform) try s.uniform_globals.append(s.arena, variable); + if (ctx == .cbuffer) { + variable.block = s.current_block; + s.checkLongVectors(ty, v.name_loc, "cbuffers or tbuffers"); + } + if (storage == .global_uniform) { + try s.uniform_globals.append(s.arena, variable); + s.checkLongVectors(ty, v.name_loc, "cbuffers or tbuffers"); + } if (storage == .global_uniform or (storage == .global_static and !q.static and ctx == .global)) try s.global_order.append(s.arena, .{ .variable = variable }); if (storage == .global_static or storage == .constant or storage == .local_static) try s.static_globals.append(s.arena, variable); // Unsized arrays take their size from the initializer. @@ -3191,8 +3229,12 @@ pub const Sema = struct { switch (s.types.get(b.ty)) { .scalar, .vector => { const comp = s.types.component(b.ty).?; - const len: u8 = if (s.types.get(b.ty) == .vector) s.types.get(b.ty).vector.len else 1; - const sw = parseSwizzle(name, len) orelse { + const len: u16 = if (s.types.get(b.ty) == .vector) s.types.get(b.ty).vector.len else 1; + if (len > 4) { + s.err(name_loc, "invalid swizzle '{s}' on vector of over 4 elements.", .{name}); + return err_typed; + } + const sw = parseSwizzle(name, @intCast(len)) orelse { s.err(name_loc, "invalid format for vector swizzle '{s}'", .{name}); return err_typed; }; diff --git a/src/sema/types.zig b/src/sema/types.zig index 969cfb1..c2ab1c2 100644 --- a/src/sema/types.zig +++ b/src/sema/types.zig @@ -445,7 +445,7 @@ pub const Type = union(enum) { /// The type of an ill-formed expression: suppresses follow-on errors. @"error", scalar: Scalar, - vector: struct { elem: Scalar, len: u8 }, + vector: struct { elem: Scalar, len: u16 }, matrix: struct { elem: Scalar, rows: u8, cols: u8 }, /// `len == 0` means unsized (`float a[]`). array: struct { elem: TypeId, len: u32 }, @@ -538,7 +538,7 @@ pub const TypeTable = struct { } /// `len == 1` vectors are kept distinct from scalars (float1 != float). - pub fn vector(t: *TypeTable, s: Scalar, len: u8) !TypeId { + pub fn vector(t: *TypeTable, s: Scalar, len: u16) !TypeId { return t.intern(.{ .vector = .{ .elem = s, .len = len } }); } @@ -563,7 +563,7 @@ pub const TypeTable = struct { pub const Shape = struct { rows: u8, - cols: u8, + cols: u16, kind: enum { scalar, vector, matrix }, pub fn count(sh: Shape) u32 { @@ -650,7 +650,7 @@ pub const TypeTable = struct { return switch (sh.kind) { .scalar => t.scalar(s), .vector => t.vector(s, sh.cols), - .matrix => t.matrix(s, sh.rows, sh.cols), + .matrix => t.matrix(s, sh.rows, @intCast(sh.cols)), }; } diff --git a/src/spirv/emit.zig b/src/spirv/emit.zig index 1fecc2b..6cd6107 100644 --- a/src/spirv/emit.zig +++ b/src/spirv/emit.zig @@ -350,6 +350,7 @@ const Emitter = struct { .vector => |v| blk: { // vector is a plain T (SPIR-V has no 1-vectors). if (v.len == 1) break :blk try e.scalarType(v.elem); + if (v.len > 4) return e.unsupported("vectors of over 4 elements are not supported in SPIR-V (Vulkan has no long vectors)", .{}); if (storageScalar(v.elem) != v.elem) break :blk try e.typeId(try e.t.vector(storageScalar(v.elem), v.len)); const elem = try e.scalarType(v.elem); const id = e.b.newId();