zdxc: partial stores to shared memory; one SPIR-V type per strided array
Writing some components of a vector or matrix in an RW structured buffer or groupshared memory read the whole value and wrote it all back, racing with other threads writing the other components. Those stores now write only the named components, as dxc does. Matrix rows and elements of buffer matrices (W[i][1][2] = x, B[i].m[r] = v, at constant or dynamic rows) are stored element by element instead of being rejected. SPIR-V arrays with the same element, length and stride are now one type, so buffers of the same element share their runtime array. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
bf073b8907
commit
b5a47b7c8e
|
|
@ -1717,7 +1717,10 @@ const Emitter = struct {
|
||||||
}
|
}
|
||||||
if (frame.vals[s.ptr.index()] == .buf) {
|
if (frame.vals[s.ptr.index()] == .buf) {
|
||||||
const b = frame.vals[s.ptr.index()].buf;
|
const b = frame.vals[s.ptr.index()].buf;
|
||||||
if (b.mat != null) return e.unsupported("writing part of a matrix in a buffer is not supported yet", .{});
|
if (b.mat) |mat| {
|
||||||
|
try e.matrixPartStore(b, mat, try e.scalars(frame, s.value));
|
||||||
|
return .none;
|
||||||
|
}
|
||||||
try e.structuredStore(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, f.inst(s.value).ty, try e.scalars(frame, s.value));
|
try e.structuredStore(e.resources.items[b.res], b.index, b.offset, b.dyn, b.row_major, f.inst(s.value).ty, try e.scalars(frame, s.value));
|
||||||
return .none;
|
return .none;
|
||||||
}
|
}
|
||||||
|
|
@ -3736,6 +3739,30 @@ const Emitter = struct {
|
||||||
return .{ .buf = b };
|
return .{ .buf = b };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Stores a row (or one component) of a matrix in a structured buffer
|
||||||
|
/// element by element: the row need not be contiguous.
|
||||||
|
fn matrixPartStore(e: *Emitter, b: @FieldType(Lowered, "buf"), mat: @typeInfo(@FieldType(@FieldType(Lowered, "buf"), "mat")).optional.child, vals: []const Value) Error!void {
|
||||||
|
const m = e.t.get(mat.ty).matrix;
|
||||||
|
const ec = try e.t.scalar(m.elem);
|
||||||
|
const i32t = try e.dm.intType(32);
|
||||||
|
const res = e.resources.items[b.res];
|
||||||
|
for (vals, 0..) |v, j| {
|
||||||
|
const c: u32 = mat.col orelse @intCast(j);
|
||||||
|
const off0 = layout.componentOffset(e.arena, e.t, mat.ty, b.row_major, c, .dx_packed) catch return e.unsupported("buffer element types", .{});
|
||||||
|
const const_row: ?i64 = if (m.rows == 1) 0 else e.constIntOf(mat.row);
|
||||||
|
if (const_row) |row| {
|
||||||
|
const off = layout.componentOffset(e.arena, e.t, mat.ty, b.row_major, @as(u32, @intCast(row)) * m.cols + c, .dx_packed) catch return e.unsupported("buffer element types", .{});
|
||||||
|
try e.structuredStore(res, b.index, b.offset + off, b.dyn, b.row_major, ec, &.{v});
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
// A dynamic row: rows are a constant byte step apart.
|
||||||
|
const step = (layout.componentOffset(e.arena, e.t, mat.ty, b.row_major, m.cols + c, .dx_packed) catch return e.unsupported("buffer element types", .{})) - off0;
|
||||||
|
var dyn = try e.add(i32t, .{ .binop = .{ .op = .mul, .lhs = mat.row, .rhs = try e.i32Const(step) } });
|
||||||
|
if (b.dyn) |d| dyn = try e.add(i32t, .{ .binop = .{ .op = .add, .lhs = d, .rhs = dyn } });
|
||||||
|
try e.structuredStore(res, b.index, b.offset + off0, dyn, b.row_major, ec, &.{v});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Loads a value of `ty` at a byte offset of a structured buffer
|
/// Loads a value of `ty` at a byte offset of a structured buffer
|
||||||
/// element: contiguous components of one type are read together.
|
/// element: contiguous components of one type are read together.
|
||||||
fn structuredLoad(e: *Emitter, res: Resource, idx: Value, base: u32, dyn: ?Value, row_major: bool, ty: TypeId) Error![]const Value {
|
fn structuredLoad(e: *Emitter, res: Resource, idx: Value, base: u32, dyn: ?Value, row_major: bool, ty: TypeId) Error![]const Value {
|
||||||
|
|
|
||||||
|
|
@ -1787,6 +1787,25 @@ const Lowerer = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Whether `e` designates part of an RW structured buffer element or a
|
||||||
|
/// groupshared variable: memory other threads may write.
|
||||||
|
fn isSharedMemory(l: *Lowerer, e: *const ast.Expr) bool {
|
||||||
|
const t = l.types();
|
||||||
|
var x = e;
|
||||||
|
while (true) switch (x.kind) {
|
||||||
|
.member => |m| x = m.base,
|
||||||
|
.index => |ix| {
|
||||||
|
if (t.get(ix.base.ty) == .resource) return t.get(ix.base.ty).resource.kind == .rw_structured_buffer;
|
||||||
|
x = ix.base;
|
||||||
|
},
|
||||||
|
.name => {
|
||||||
|
const r = l.s.resolution(x) orelse return false;
|
||||||
|
return r == .variable and r.variable.storage == .groupshared;
|
||||||
|
},
|
||||||
|
else => return false,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
/// Pointer to the storage field holding bitfield `bf` of the struct
|
/// Pointer to the storage field holding bitfield `bf` of the struct
|
||||||
/// `ptr` points to.
|
/// `ptr` points to.
|
||||||
fn bitStorage(l: *Lowerer, ptr: Ref, bf: Bitfield) Error!Ref {
|
fn bitStorage(l: *Lowerer, ptr: Ref, bf: Bitfield) Error!Ref {
|
||||||
|
|
@ -1863,6 +1882,22 @@ const Lowerer = struct {
|
||||||
.member => |m| if (t.get(m.base.ty) == .matrix) {
|
.member => |m| if (t.get(m.base.ty) == .matrix) {
|
||||||
const mt = t.get(m.base.ty).matrix;
|
const mt = t.get(m.base.ty).matrix;
|
||||||
const ks = matrixSwizzle(l.arena, m.name, mt.cols) orelse return error.Unsupported;
|
const ks = matrixSwizzle(l.arena, m.name, mt.cols) orelse return error.Unsupported;
|
||||||
|
if (l.isSharedMemory(m.base)) {
|
||||||
|
// Other threads may write the other elements: store
|
||||||
|
// only the named ones (as dxc).
|
||||||
|
const base_ptr = try l.lvalue(m.base);
|
||||||
|
const space = l.b.f.inst(base_ptr).ptr;
|
||||||
|
const row_ty = try t.vector(mt.elem, mt.cols);
|
||||||
|
const ec = try t.scalar(mt.elem);
|
||||||
|
for (ks, 0..) |k, i| {
|
||||||
|
const part = if (ks.len == 1) value else try l.component(value, @intCast(i));
|
||||||
|
const r = try l.b.constInt(try t.scalar(.uint), k / mt.cols);
|
||||||
|
const c = try l.b.constInt(try t.scalar(.uint), k % mt.cols);
|
||||||
|
const row = try l.b.add(.{ .op = .{ .access = .{ .base = base_ptr, .indices = try l.arena.dupe(Ref, &.{r}) } }, .ty = row_ty, .ptr = space });
|
||||||
|
try l.b.store(try l.b.add(.{ .op = .{ .access = .{ .base = row, .indices = try l.arena.dupe(Ref, &.{c}) } }, .ty = ec, .ptr = space }), part);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
var whole = try l.rvalue(m.base);
|
var whole = try l.rvalue(m.base);
|
||||||
const row_ty = try t.vector(mt.elem, mt.cols);
|
const row_ty = try t.vector(mt.elem, mt.cols);
|
||||||
for (ks, 0..) |k, i| {
|
for (ks, 0..) |k, i| {
|
||||||
|
|
@ -1888,6 +1923,17 @@ const Lowerer = struct {
|
||||||
}
|
}
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (l.isSharedMemory(m.base)) {
|
||||||
|
// Store only the named components (as dxc).
|
||||||
|
const base_ptr = try l.lvalue(m.base);
|
||||||
|
const ec = try t.scalar(t.component(m.base.ty).?);
|
||||||
|
for (comps, 0..) |c, i| {
|
||||||
|
const part = if (comps.len == 1) value else try l.b.add(.{ .op = .{ .extract = .{ .agg = value, .index = @intCast(i) } }, .ty = ec });
|
||||||
|
const idx = try l.b.constInt(try t.scalar(.uint), c);
|
||||||
|
try l.b.store(try l.b.add(.{ .op = .{ .access = .{ .base = base_ptr, .indices = try l.arena.dupe(Ref, &.{idx}) } }, .ty = ec, .ptr = l.b.f.inst(base_ptr).ptr }), part);
|
||||||
|
}
|
||||||
|
return;
|
||||||
|
}
|
||||||
var whole = try l.rvalue(m.base);
|
var whole = try l.rvalue(m.base);
|
||||||
const ec = try t.scalar(t.component(m.base.ty).?);
|
const ec = try t.scalar(t.component(m.base.ty).?);
|
||||||
for (comps, 0..) |c, i| {
|
for (comps, 0..) |c, i| {
|
||||||
|
|
|
||||||
|
|
@ -140,6 +140,9 @@ const Emitter = struct {
|
||||||
/// spellings are one type).
|
/// spellings are one type).
|
||||||
spirv_types: std.StringHashMapUnmanaged(Id) = .empty,
|
spirv_types: std.StringHashMapUnmanaged(Id) = .empty,
|
||||||
/// Explicitly laid out variants of arrays and structs (buffers).
|
/// Explicitly laid out variants of arrays and structs (buffers).
|
||||||
|
/// Arrays with an ArrayStride by element type, length (0: runtime
|
||||||
|
/// array) and stride: one type per layout, as in dxc.
|
||||||
|
strided_arrays: std.AutoHashMapUnmanaged([3]u32, Id) = .empty,
|
||||||
layout_types: std.AutoHashMapUnmanaged(LayoutKey, Id) = .empty,
|
layout_types: std.AutoHashMapUnmanaged(LayoutKey, Id) = .empty,
|
||||||
/// Per instruction (current function): the buffer layout its pointer
|
/// Per instruction (current function): the buffer layout its pointer
|
||||||
/// result points into, if any.
|
/// result points into, if any.
|
||||||
|
|
@ -479,27 +482,35 @@ const Emitter = struct {
|
||||||
}
|
}
|
||||||
const key: LayoutKey = .{ ty, rules, row_major };
|
const key: LayoutKey = .{ ty, rules, row_major };
|
||||||
if (e.layout_types.get(key)) |id| return id;
|
if (e.layout_types.get(key)) |id| return id;
|
||||||
const id = e.b.newId();
|
const id = switch (e.t.get(ty)) {
|
||||||
switch (e.t.get(ty)) {
|
// Row-major integer matrix: an array of its rows.
|
||||||
.matrix => |m| {
|
.matrix => |m| try e.stridedArray(try e.layoutTypeId(try e.t.vector(m.elem, m.cols), rules, false), m.rows, layout.matrixStride(e.t, ty, true, rules)),
|
||||||
// Row-major integer matrix: an array of its rows.
|
.array => |a| try e.stridedArray(try e.layoutTypeId(a.elem, rules, row_major), a.len, layout.arrayStride(e.t, a.elem, row_major, rules) catch return e.unsupported("buffer member type", .{})),
|
||||||
const row_ty = try e.layoutTypeId(try e.t.vector(m.elem, m.cols), rules, false);
|
.@"struct" => |st| blk: {
|
||||||
try e.b.emit(.types_globals, .type_array, &.{ id, row_ty, try e.constUint(m.rows) });
|
const id = e.b.newId();
|
||||||
try e.b.emit(.annotations, .decorate, &.{ id, 6, layout.matrixStride(e.t, ty, true, rules) }); // ArrayStride
|
try e.layoutStruct(id, st, rules, st.name);
|
||||||
|
break :blk id;
|
||||||
},
|
},
|
||||||
.array => |a| {
|
|
||||||
const elem = try e.layoutTypeId(a.elem, rules, row_major);
|
|
||||||
try e.b.emit(.types_globals, .type_array, &.{ id, elem, try e.constUint(a.len) });
|
|
||||||
const stride = layout.arrayStride(e.t, a.elem, row_major, rules) catch return e.unsupported("buffer member type", .{});
|
|
||||||
try e.b.emit(.annotations, .decorate, &.{ id, 6, stride }); // ArrayStride
|
|
||||||
},
|
|
||||||
.@"struct" => |st| try e.layoutStruct(id, st, rules, st.name),
|
|
||||||
else => return e.unsupported("buffer members of type '{s}' are not supported yet", .{try e.t.format(e.arena, ty)}),
|
else => return e.unsupported("buffer members of type '{s}' are not supported yet", .{try e.t.format(e.arena, ty)}),
|
||||||
}
|
};
|
||||||
try e.layout_types.put(e.arena, key, id);
|
try e.layout_types.put(e.arena, key, id);
|
||||||
return id;
|
return id;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// `elem[len]` (a runtime array for `len` 0) with an ArrayStride.
|
||||||
|
fn stridedArray(e: *Emitter, elem: Id, len: u32, stride: u32) Error!Id {
|
||||||
|
const len_id = if (len == 0) 0 else try e.constUint(len);
|
||||||
|
const gop = try e.strided_arrays.getOrPut(e.arena, .{ elem, len_id, stride });
|
||||||
|
if (gop.found_existing) return gop.value_ptr.*;
|
||||||
|
const id = e.b.newId();
|
||||||
|
if (len == 0) {
|
||||||
|
try e.b.emit(.types_globals, .type_runtime_array, &.{ id, elem });
|
||||||
|
} else try e.b.emit(.types_globals, .type_array, &.{ id, elem, len_id });
|
||||||
|
try e.b.emit(.annotations, .decorate, &.{ id, 6, stride }); // ArrayStride
|
||||||
|
gop.value_ptr.* = id;
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
/// Emits struct `id` with `st`'s fields laid out by `rules`.
|
/// Emits struct `id` with `st`'s fields laid out by `rules`.
|
||||||
fn layoutStruct(e: *Emitter, id: Id, st: *const types.StructInfo, rules: layout.Rules, name: []const u8) Error!void {
|
fn layoutStruct(e: *Emitter, id: Id, st: *const types.StructInfo, rules: layout.Rules, name: []const u8) Error!void {
|
||||||
{
|
{
|
||||||
|
|
@ -2982,9 +2993,7 @@ const Emitter = struct {
|
||||||
const elem_ty = if (r.kind == .byte_address_buffer or r.kind == .rw_byte_address_buffer) try e.t.scalar(.uint) else r.elem;
|
const elem_ty = if (r.kind == .byte_address_buffer or r.kind == .rw_byte_address_buffer) try e.t.scalar(.uint) else r.elem;
|
||||||
const elem = (try e.bufferElemType(ty)).?;
|
const elem = (try e.bufferElemType(ty)).?;
|
||||||
const stride = layout.arrayStride(e.t, elem_ty, r.row_major, e.sbRules()) catch return e.unsupported("buffer element type '{s}'", .{try e.t.format(e.arena, elem_ty)});
|
const stride = layout.arrayStride(e.t, elem_ty, r.row_major, e.sbRules()) catch return e.unsupported("buffer element type '{s}'", .{try e.t.format(e.arena, elem_ty)});
|
||||||
const rt = e.b.newId();
|
const rt = try e.stridedArray(elem, 0, stride);
|
||||||
try e.b.emit(.types_globals, .type_runtime_array, &.{ rt, elem });
|
|
||||||
try e.b.emit(.annotations, .decorate, &.{ rt, 6, stride }); // ArrayStride
|
|
||||||
const st = e.b.newId();
|
const st = e.b.newId();
|
||||||
try e.b.emit(.types_globals, .type_struct, &.{ st, rt });
|
try e.b.emit(.types_globals, .type_struct, &.{ st, rt });
|
||||||
try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 35, 0 }); // Offset 0
|
try e.b.emit(.annotations, .member_decorate, &.{ st, 0, 35, 0 }); // Offset 0
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,32 @@
|
||||||
|
// PROFILE: cs_6_0
|
||||||
|
// Writing some components of a vector or matrix in an RW structured
|
||||||
|
// buffer or groupshared memory stores just those components (as dxc), not
|
||||||
|
// the whole value read back: other threads may write the rest. Matrix
|
||||||
|
// rows and elements of buffers, at constant or dynamic rows, are stored
|
||||||
|
// element by element.
|
||||||
|
// ZCHECK: bufferStore.f32(i32 69, %dx.types.Handle {{.*}}, i32 8, float 2.000000e+00, float undef, float undef, float undef, i8 1)
|
||||||
|
// ZCHECK: bufferStore.f32(i32 69, %dx.types.Handle {{.*}}, i32 32, float 3.000000e+00, float undef, float undef, float undef, i8 1)
|
||||||
|
// ZCHECK: bufferStore.f32(i32 69, %dx.types.Handle {{.*}}, i32 28, float 4.000000e+00, float undef, float undef, float undef, i8 1)
|
||||||
|
// ZCHECK: bufferStore.f32(i32 69, %dx.types.Handle {{.*}}, i32 20, float 6.000000e+00, float undef, float undef, float undef, i8 1)
|
||||||
|
// ZCHECK: bufferStore.f32(i32 69, %dx.types.Handle {{.*}}, i32 24, float 8.000000e+00, float undef, float undef, float undef, i8 1)
|
||||||
|
// ZCHECK: mul i32 {{.*}}, 4
|
||||||
|
// ZCHECK: store float 1.000000e+01, float addrspace(3)*
|
||||||
|
// ZCHECK: store float 1.100000e+01, float addrspace(3)*
|
||||||
|
struct S { float4 v; float2x3 m; };
|
||||||
|
RWStructuredBuffer<S> B;
|
||||||
|
RWStructuredBuffer<float2x3> W;
|
||||||
|
RWByteAddressBuffer R;
|
||||||
|
groupshared float4 G[4];
|
||||||
|
groupshared float2x3 GM[4];
|
||||||
|
[numthreads(4, 1, 1)] void main(uint i : SV_GroupIndex) {
|
||||||
|
B[i].v.xz = float2(1, 2);
|
||||||
|
B[i].m._m02_m11 = float2(3, 4);
|
||||||
|
W[i]._m10 = 5;
|
||||||
|
W[i][1][2] = 6;
|
||||||
|
B[i].m[0] = float3(7, 8, 9);
|
||||||
|
B[i + 1].m[i % 2] = float3(1, 2, 3);
|
||||||
|
G[i].y = 10;
|
||||||
|
GM[i]._m12 = 11;
|
||||||
|
GroupMemoryBarrierWithGroupSync();
|
||||||
|
R.Store(i * 4, asuint(G[(i + 1) % 4].x + GM[(i + 1) % 4]._m00));
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,23 @@
|
||||||
|
// PROFILE: cs_6_0
|
||||||
|
// Writing some components of a vector or matrix in an RW structured
|
||||||
|
// buffer or groupshared memory stores just those components. Arrays with
|
||||||
|
// the same element and stride are one type (StructuredBuffer and
|
||||||
|
// RWStructuredBuffer of float2x3 share their runtime array, as in dxc).
|
||||||
|
// ZCHECK: OpAccessChain %_ptr_Workgroup_float {{.*}} %uint_3
|
||||||
|
// ZCHECK: OpStore {{.*}} %float_10
|
||||||
|
// ZCHECK: OpAccessChain %_ptr_Uniform_float {{.*}} %uint_2
|
||||||
|
// ZCHECK: OpStore {{.*}} %float_3
|
||||||
|
// ZCHECK: %type_StructuredBuffer_mat2v3float = OpTypeStruct %_runtimearr_mat2v3float
|
||||||
|
// ZCHECK: %type_RWStructuredBuffer_mat2v3float = OpTypeStruct %_runtimearr_mat2v3float
|
||||||
|
struct S { float4 v; float2x3 m; };
|
||||||
|
RWStructuredBuffer<S> B;
|
||||||
|
StructuredBuffer<float2x3> M;
|
||||||
|
RWStructuredBuffer<float2x3> W;
|
||||||
|
groupshared float4 G[4];
|
||||||
|
[numthreads(4, 1, 1)] void main(uint i : SV_GroupIndex) {
|
||||||
|
G[i].wy = 10;
|
||||||
|
B[i].m._m02 = 3;
|
||||||
|
W[i] = M[i];
|
||||||
|
GroupMemoryBarrierWithGroupSync();
|
||||||
|
B[i].v.x = G[(i + 1) % 4].y;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue