SM 6.9 long vectors (DXIL)

- vector<T, N> 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).
This commit is contained in:
Peter Li 2026-10-31 20:49:41 -07:00
parent 77f4fb539c
commit 3c7fdb2ad2
4 changed files with 60 additions and 16 deletions

View File

@ -120,20 +120,20 @@ const Layout = struct { rows: u8, cols: u8, kind: TypeTable.Shape };
/// Symbolic dimension bindings (`r`, `c`, `r2`, `c2`, ...). /// Symbolic dimension bindings (`r`, `c`, `r2`, `c2`, ...).
const Bindings = struct { const Bindings = struct {
names: [8][]const u8 = undefined, names: [8][]const u8 = undefined,
values: [8]u8 = undefined, values: [8]u16 = undefined,
n: usize = 0, n: usize = 0,
/// The widest shape of the arguments declared `<>`: all such types of /// The widest shape of the arguments declared `<>`: all such types of
/// an intrinsic share their dimensions (dxc's r / c), scalars /// an intrinsic share their dimensions (dxc's r / c), scalars
/// splatting to the widest. /// splatting to the widest.
any_shape: ?TypeTable.Shape = null, 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; for (b.names[0..b.n], b.values[0..b.n]) |k, v| if (std.mem.eql(u8, k, name)) return v;
return null; return null;
} }
/// Bind or check; false on conflict. /// 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.get(name)) |old| return old == v;
if (b.n == b.names.len) return false; if (b.n == b.names.len) return false;
b.names[b.n] = name; 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; 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) { 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; 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); 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 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; 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; 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. /// A layout dimension: a number, or a symbol bound from the argument.
fn dimValue(dim: []const u8, b: *Bindings, from_arg: ?u8) ?u8 { fn dimValue(dim: []const u8, b: *Bindings, from_arg: ?u16) ?u16 {
if (std.fmt.parseInt(u8, dim, 10)) |v| return v else |_| {} if (std.fmt.parseInt(u16, dim, 10)) |v| return v else |_| {}
if (b.get(dim)) |v| return v; if (b.get(dim)) |v| return v;
const v = from_arg orelse return null; const v = from_arg orelse return null;
if (!b.bind(dim, v)) return null; if (!b.bind(dim, v)) return null;

View File

@ -1109,7 +1109,6 @@ pub const Sema = struct {
s.err(templateArgLoc(args[1]), "invalid value, valid range is between 1 and {d} inclusive", .{max}); s.err(templateArgLoc(args[1]), "invalid value, valid range is between 1 and {d} inclusive", .{max});
return TypeTable.error_type; return TypeTable.error_type;
} }
if (len > 4) return TypeTable.error_type; // long vectors: not modelled
return s.types.vector(comp, @intCast(len)); return s.types.vector(comp, @intCast(len));
} }
if (std.mem.eql(u8, n, "matrix")) { 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 } }); 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 } }; 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])) { 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; 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 } }); return s.types.intern(.{ .stream = .{ .kind = st[1], .elem = elem } });
}; };
if (std.mem.eql(u8, n, "InputPatch") or std.mem.eql(u8, n, "OutputPatch")) { 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 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; 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 } }); return s.types.intern(.{ .patch = .{ .output = n[0] == 'O', .elem = elem, .count = count } });
} }
} }
@ -1343,6 +1345,23 @@ pub const Sema = struct {
return .{ .type = ty }; 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")]]`). /// The string argument of attribute `name` (`[[vk::builtin("X")]]`).
pub fn vkAttrString(attrs: []const ast.Attribute, name: []const u8) ?[]const u8 { 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) { 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 { pub fn checkUnit(s: *Sema, tu: ast.TranslationUnit) Allocator.Error!void {
for (tu.decls) |d| try s.decl(d, .global); 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| { if (s.opts.entry) |entry| {
const found = if (s.global.syms.get(entry)) |sym| sym == .functions else false; 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", .{}); 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.declare(v.name, .{ .variable = variable }, v.name_loc);
try s.decl_vars.put(s.arena, v, variable); try s.decl_vars.put(s.arena, v, variable);
if (ctx == .cbuffer) variable.block = s.current_block; if (ctx == .cbuffer) {
if (storage == .global_uniform) try s.uniform_globals.append(s.arena, variable); 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_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); 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. // Unsized arrays take their size from the initializer.
@ -3191,8 +3229,12 @@ pub const Sema = struct {
switch (s.types.get(b.ty)) { switch (s.types.get(b.ty)) {
.scalar, .vector => { .scalar, .vector => {
const comp = s.types.component(b.ty).?; 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 len: u16 = if (s.types.get(b.ty) == .vector) s.types.get(b.ty).vector.len else 1;
const sw = parseSwizzle(name, len) orelse { 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}); s.err(name_loc, "invalid format for vector swizzle '{s}'", .{name});
return err_typed; return err_typed;
}; };

View File

@ -445,7 +445,7 @@ pub const Type = union(enum) {
/// The type of an ill-formed expression: suppresses follow-on errors. /// The type of an ill-formed expression: suppresses follow-on errors.
@"error", @"error",
scalar: Scalar, scalar: Scalar,
vector: struct { elem: Scalar, len: u8 }, vector: struct { elem: Scalar, len: u16 },
matrix: struct { elem: Scalar, rows: u8, cols: u8 }, matrix: struct { elem: Scalar, rows: u8, cols: u8 },
/// `len == 0` means unsized (`float a[]`). /// `len == 0` means unsized (`float a[]`).
array: struct { elem: TypeId, len: u32 }, array: struct { elem: TypeId, len: u32 },
@ -538,7 +538,7 @@ pub const TypeTable = struct {
} }
/// `len == 1` vectors are kept distinct from scalars (float1 != float). /// `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 } }); return t.intern(.{ .vector = .{ .elem = s, .len = len } });
} }
@ -563,7 +563,7 @@ pub const TypeTable = struct {
pub const Shape = struct { pub const Shape = struct {
rows: u8, rows: u8,
cols: u8, cols: u16,
kind: enum { scalar, vector, matrix }, kind: enum { scalar, vector, matrix },
pub fn count(sh: Shape) u32 { pub fn count(sh: Shape) u32 {
@ -650,7 +650,7 @@ pub const TypeTable = struct {
return switch (sh.kind) { return switch (sh.kind) {
.scalar => t.scalar(s), .scalar => t.scalar(s),
.vector => t.vector(s, sh.cols), .vector => t.vector(s, sh.cols),
.matrix => t.matrix(s, sh.rows, sh.cols), .matrix => t.matrix(s, sh.rows, @intCast(sh.cols)),
}; };
} }

View File

@ -350,6 +350,7 @@ const Emitter = struct {
.vector => |v| blk: { .vector => |v| blk: {
// vector<T, 1> is a plain T (SPIR-V has no 1-vectors). // vector<T, 1> is a plain T (SPIR-V has no 1-vectors).
if (v.len == 1) break :blk try e.scalarType(v.elem); 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)); 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 elem = try e.scalarType(v.elem);
const id = e.b.newId(); const id = e.b.newId();