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).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 20:49:41 -07:00
parent 92a5690d9a
commit dc90b606a3
5 changed files with 97 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`, ...).
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;

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});
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;
};

View File

@ -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)),
};
}

View File

@ -350,6 +350,7 @@ const Emitter = struct {
.vector => |v| blk: {
// 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 > 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();

View File

@ -0,0 +1,37 @@
// PROFILE: cs_6_9
// FLAGS: -enable-16bit-types
// SM 6.9 long vectors (scalarized): structured and byte-address buffer
// loads and stores, groupshared arrays, construction from shorter
// vectors, comparisons and select, element-wise and reducing
// intrinsics, dynamic indexing and functions taking long vectors.
// ZCHECK: @dx.op.rawBufferLoad.f32(i32 139, %dx.types.Handle {{.*}}, i32 16, i8 15, i32 4)
// ZCHECK: call float @dx.op.unary.f32(i32 13,
// ZCHECK: @dx.op.rawBufferStore.f32(i32 140,
struct S { float a; vector<half, 6> h; };
RWStructuredBuffer<vector<float, 8> > sb : register(u0);
RWStructuredBuffer<S> ss : register(u1);
RWByteAddressBuffer bab : register(u2);
groupshared vector<int, 5> gs[64];
vector<float, 8> scaleBy(vector<float, 8> v, float k) { return v * k; }
[numthreads(64, 1, 1)] void main(uint i : SV_GroupIndex) {
vector<float, 8> a = sb[i];
vector<float, 8> b = bab.Load<vector<float, 8> >(32 * i);
vector<float, 8> c = sin(a) * b + 1.0;
c[i % 8] = dot(a, b);
vector<bool, 8> big = c > 2.0;
c = select(big, scaleBy(c, 0.5), clamp(c, -1.0, 1.0));
c += float4(1, 2, 3, 4).xxyy.xyzw.x;
vector<float, 8> d = vector<float, 8>(float4(1, 2, 3, 4), float2(a[0], a[1]), 7, 8);
sb[i] = max(c, d) + (any(big) ? 1.0 : 0.0) + (all(big) ? 1.0 : 0.0);
S s = ss[i];
s.h = s.h * (half)2 + abs(s.h);
ss[i] = s;
gs[i] = vector<int, 5>(i, i + 1, i + 2, i + 3, i + 4);
GroupMemoryBarrierWithGroupSync();
vector<int, 5> g = gs[63 - i];
bab.Store<vector<int, 5> >(4096 + 20 * i, countbits(g) + firstbithigh(g) + (g & 3));
}