dxil: SM 6.8 Barrier(), and one validator version for PSV and RDAT

- Barrier(memory types, semantics) and Barrier(UAV, semantics) lower to
  barrierByMemoryType (244) and barrierByMemoryHandle (245), as dxc does;
  libraries mark a function as needing a group for groupshared memory
  (not via ALL_MEMORY) or group sync / scope semantics, as dxc's
  BarrierRequiresGroup.
- shader.validator is the one validator version zdxc writes for; the
  PSV runtime info (4 from 1.10, else 3) and RDAT's function records
  (FunctionInfo3 from 1.10, else 2) follow it. Building for validator
  1.8 is now changing that constant. (Validator 1.8 rejected the RDAT of
  12 Glide libraries only because their records were 1.10's: built for
  1.8, all of Glide's compiles validate.)
This commit is contained in:
peterino2 2026-11-03 13:36:18 -08:00
parent 285505e36b
commit 8a77bb723b
4 changed files with 69 additions and 18 deletions

View File

@ -3274,6 +3274,24 @@ const Emitter = struct {
_ = try e.callOp(fn_i, try e.voidTy(), &.{ try e.i32Const(168), (try e.scalars(frame, args[0]))[0], (try e.scalars(frame, args[1]))[0] });
return .none;
}
// SM 6.8's Barrier(memory types or a UAV, semantics): the
// arguments as they are (as dxc).
if (eq(u8, name, "Barrier") and args.len == 2) {
const i32t = try e.dm.intType(32);
const sem = (try e.scalars(frame, args[1]))[0];
switch (frame.vals[args[0].index()]) {
.handle => |h| {
const fn_i = try e.dxOp("dx.op.barrierByMemoryHandle", try e.voidTy(), &.{ i32t, e.handle_ty, i32t }, .nounwind);
_ = try e.callOp(fn_i, try e.voidTy(), &.{ try e.i32Const(245), e.resources.items[h].handle, sem });
},
.scalars => |m| {
const fn_i = try e.dxOp("dx.op.barrierByMemoryType", try e.voidTy(), &.{ i32t, i32t, i32t }, .nounwind);
_ = try e.callOp(fn_i, try e.voidTy(), &.{ try e.i32Const(244), m[0], sem });
},
else => return e.unsupported("Barrier on this object is not supported yet", .{}),
}
return .none;
}
for (args) |x| if (frame.vals[x.index()] != .scalars) return e.unsupported("intrinsic '{s}' is not supported yet", .{name});
var a: [4][]const Value = undefined;
for (args, 0..) |x, k| a[k] = try e.scalars(frame, x);
@ -6178,7 +6196,7 @@ const Emitter = struct {
}.f;
const ident = try dm.mdNode(&.{try dm.mdString("zdxc 0.0.0")});
const version = try dm.mdNode(&.{ try mdI32(dm, 1), try mdI32(dm, e.profile.minor) });
const val_ver = try dm.mdNode(&.{ try mdI32(dm, 1), try mdI32(dm, 11) });
const val_ver = try dm.mdNode(&.{ try mdI32(dm, shader.validator[0]), try mdI32(dm, shader.validator[1]) });
const sm = try dm.mdNode(&.{ try dm.mdString("lib"), try mdI32(dm, e.profile.major), try mdI32(dm, e.profile.minor) });
const resources = try e.resourcesMd();
const analysis = try e.libAnalysis(funcs);
@ -6933,7 +6951,7 @@ const Emitter = struct {
const ident = try dm.mdNode(&.{try dm.mdString("zdxc 0.0.0")});
const version = try dm.mdNode(&.{ try mdI32(dm, 1), try mdI32(dm, e.profile.minor) });
const val_ver = try dm.mdNode(&.{ try mdI32(dm, 1), try mdI32(dm, 11) });
const val_ver = try dm.mdNode(&.{ try mdI32(dm, shader.validator[0]), try mdI32(dm, shader.validator[1]) });
const sm = try dm.mdNode(&.{ try dm.mdString(e.profile.kind.profilePrefix()), try mdI32(dm, e.profile.major), try mdI32(dm, e.profile.minor) });
const resources = try e.resourcesMd();

View File

@ -191,6 +191,22 @@ pub fn analyze(m: *const module.Module, f: *const module.Function, o: Options) I
fl |= bit.requires_group;
info.mask &= stage_group;
},
// Barrier by memory type, UAV or node record: groupshared memory
// (not as part of ALL_MEMORY, which may drop it) or group
// sync / scope semantics need a group (as dxc's
// BarrierRequiresGroup).
244, 245, 246 => if (args.len > 2) {
var needs = false;
if (code == 244) if (constInt(m, args[1])) |mem| {
const kept = if (mem == 0xF) mem & ~@as(i64, 2) else mem;
needs = kept & 2 != 0;
};
if (constInt(m, args[2])) |sem| needs = needs or sem & 3 != 0;
if (needs) {
fl |= bit.requires_group;
info.mask &= stage_group;
}
},
else => {},
}
}

View File

@ -10,6 +10,7 @@
const std = @import("std");
const Allocator = std.mem.Allocator;
const shader = @import("shader.zig");
pub const null_ref: u32 = 0xffff_ffff;
@ -24,7 +25,8 @@ pub const Resource = struct {
flags: u32 = 0,
};
/// RuntimeDataFunctionInfo3 (validator 1.10+).
/// RuntimeDataFunctionInfo3 (validator 1.10+; before, 2: without the
/// extended properties).
pub const Function = struct {
name: u32,
unmangled_name: u32,
@ -115,10 +117,10 @@ pub const Builder = struct {
pub fn addFunction(b: *Builder, f: Function) Allocator.Error!void {
const packed_wave: u32 = @as(u32, f.min_wave_lanes) | (@as(u32, f.max_wave_lanes) << 8) | (@as(u32, f.shader_flags) << 16);
try b.functions.append(b.gpa, .{
f.name, f.unmangled_name, f.resources, f.function_dependencies,
f.shader_kind, f.payload_size, f.attribute_size, f.feature_info1,
f.feature_info2, f.shader_stage_flag, f.min_shader_target, packed_wave,
f.raw_shader_ref, f.extended_properties,
f.name, f.unmangled_name, f.resources, f.function_dependencies,
f.shader_kind, f.payload_size, f.attribute_size, f.feature_info1,
f.feature_info2, f.shader_stage_flag, f.min_shader_target, packed_wave,
f.raw_shader_ref, f.extended_properties,
});
}
@ -132,7 +134,7 @@ pub const Builder = struct {
/// (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];
const tab = &b.ext[@backingInt(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);
@ -165,7 +167,11 @@ pub const Builder = struct {
try parts.append(b.gpa, .{ .ty = 3, .data = t });
}
if (b.functions.items.len > 0) {
const t = try table(b.gpa, 14, std.mem.sliceAsBytes(b.functions.items), b.functions.items.len);
const words: usize = if (shader.validatorAtLeast(1, 10)) 14 else 13;
const rows = try b.gpa.alloc(u32, words * b.functions.items.len);
defer b.gpa.free(rows);
for (b.functions.items, 0..) |f, k| @memcpy(rows[k * words ..][0..words], f[0..words]);
const t = try table(b.gpa, words, std.mem.sliceAsBytes(rows), b.functions.items.len);
try tables.append(b.gpa, t);
try parts.append(b.gpa, .{ .ty = 4, .data = t });
}
@ -177,7 +183,7 @@ pub const Builder = struct {
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 stride = @as(Ext, @fromBackingInt(@intCast(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 });

View File

@ -71,8 +71,16 @@ pub const ComputeInfo = struct {
num_threads: [3]u32,
};
/// Validator version we claim in !dx.valver (matches the oracle dxc).
const valver = .{ 1, 11 };
/// The validator version zdxc writes for (!dx.valver; matches the oracle
/// dxc). What its output looks like follows from it: PSVRuntimeInfo4 and
/// RDAT's RuntimeDataFunctionInfo3 from 1.10. Testing with an older
/// validator means changing only this.
pub const validator: [2]u32 = .{ 1, 11 };
/// Whether the validator is at least `major.minor`.
pub fn validatorAtLeast(major: u32, minor: u32) bool {
return validator[0] > major or (validator[0] == major and validator[1] >= minor);
}
pub fn buildEmptyCompute(gpa: Allocator, info: ComputeInfo) ![]u8 {
var m = Module.init(gpa);
@ -86,7 +94,7 @@ pub fn buildEmptyCompute(gpa: Allocator, info: ComputeInfo) ![]u8 {
// Metadata, in the same shape dxc emits.
const ident = try m.mdNode(&.{try m.mdString("zdxc 0.0.0")});
const version = try m.mdNode(&.{ try m.mdI32(1), try m.mdI32(info.profile.minor) });
const val_ver = try m.mdNode(&.{ try m.mdI32(valver[0]), try m.mdI32(valver[1]) });
const val_ver = try m.mdNode(&.{ try m.mdI32(validator[0]), try m.mdI32(validator[1]) });
const sm = try m.mdNode(&.{ try m.mdString(info.profile.kind.profilePrefix()), try m.mdI32(info.profile.major), try m.mdI32(info.profile.minor) });
const threads = try m.mdNode(&.{ try m.mdI32(info.num_threads[0]), try m.mdI32(info.num_threads[1]), try m.mdI32(info.num_threads[2]) });
const props = try m.mdNode(&.{ try m.mdI32(4), threads }); // kDxilNumThreadsTag
@ -161,7 +169,7 @@ pub fn psvCompute(gpa: Allocator, entry: []const u8, num_threads: [3]u32, resour
return psv(gpa, .{ .kind = .compute, .entry = entry, .num_threads = num_threads, .resources = resources });
}
/// PSV0 with PSVRuntimeInfo4 (60 bytes), as produced for validator 1.11.
/// PSV0 with PSVRuntimeInfo4 (60 bytes; validator 1.10+) or 3 (52).
pub fn psv(gpa: Allocator, info: PsvInfo) ![]u8 {
var buf: std.ArrayList(u8) = .empty;
errdefer buf.deinit(gpa);
@ -171,7 +179,8 @@ pub fn psv(gpa: Allocator, info: PsvInfo) ![]u8 {
for (&out_vectors, 0..) |*v, s| v.* = signature.vectorsUsed(info.outputs, @intCast(s));
const pc_vectors = signature.vectorsUsed(info.patch_constants, 0);
try w(&buf, gpa, 60); // sizeof(PSVRuntimeInfo4)
const info4 = validatorAtLeast(1, 10);
try w(&buf, gpa, if (info4) 60 else 52); // sizeof(PSVRuntimeInfo4 / 3)
// PSVRuntimeInfo0
try buf.appendSlice(gpa, &info.stage_info);
try w(&buf, gpa, info.wave_lanes[0]); // MinimumExpectedWaveLaneCount
@ -232,9 +241,11 @@ pub fn psv(gpa: Allocator, info: PsvInfo) ![]u8 {
// PSVRuntimeInfo3: EntryFunctionName
try w(&buf, gpa, entry_off);
// PSVRuntimeInfo4
try w(&buf, gpa, info.groupshared_bytes); // NumBytesGroupSharedMemory
try w(&buf, gpa, @intFromBool(linalg != null)); // Flags: LinAlgRuntimeInfoPresent
if (info4) {
// PSVRuntimeInfo4
try w(&buf, gpa, info.groupshared_bytes); // NumBytesGroupSharedMemory
try w(&buf, gpa, @intFromBool(linalg != null)); // Flags: LinAlgRuntimeInfoPresent
}
try w(&buf, gpa, @intCast(info.resources.len));
if (info.resources.len > 0) {