DXIL backend for compute shaders
src/dxil/emit.zig lowers IR to DXIL: calls are inlined while emitting (returns merge through phis), values are scalarized, locals become flattened [N x scalar] allocas, and resources become handles created on entry (createHandle before SM 6.6, createHandleFromBinding plus annotateHandle from 6.6). Structured, byte-address and typed buffer reads and writes use bufferLoad/Store or rawBufferLoad/Store by shader model; math intrinsics map to dx.op unary / binary / tertiary / dot operations; compute system values to threadId and friends. The module carries dx.resources, entry properties (shader flags, numthreads) and a PSV0 part with resource bindings; dxv accepts the output. The bitcode writer gains alloca, load, store, getelementptr, unreachable and aggregate constants. Float-to-int constant folding saturates instead of panicking on out-of-range values. zig build oracle-codegen: SPIR-V 281 valid, DXIL 2 valid (compute only so far), 0 invalid on either backend. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
parent
d42fc71c5d
commit
22df96e507
11
build.zig
11
build.zig
|
|
@ -89,6 +89,17 @@ pub fn build(b: *std.Build) void {
|
|||
oracle_sema.step.dependOn(&oracle_parse.step); // builds the corpus list
|
||||
oracle_sema_step.dependOn(&oracle_sema.step);
|
||||
|
||||
const oracle_codegen_step = b.step("oracle-codegen", "Compile the dxc-valid corpus to SPIR-V and DXIL and validate the output");
|
||||
for ([_][]const u8{ "spirv", "dxil" }) |backend| {
|
||||
const run = b.addSystemCommand(&.{ "sh", "test/oracle/codegen.sh" });
|
||||
run.addArtifactArg(exe);
|
||||
run.addArg(backend);
|
||||
run.setCwd(b.path("."));
|
||||
run.has_side_effects = true;
|
||||
run.step.dependOn(&oracle_parse.step); // builds the corpus list
|
||||
oracle_codegen_step.dependOn(&run.step);
|
||||
}
|
||||
|
||||
const oracle_verify_step = b.step("oracle-verify", "Compare Sema errors with dxc's SemaHLSL -verify expectations");
|
||||
const oracle_verify = b.addSystemCommand(&.{ "sh", "test/oracle/verify.sh" });
|
||||
oracle_verify.addArtifactArg(exe);
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load Diff
|
|
@ -47,6 +47,8 @@ pub const Constant = struct {
|
|||
float_bits: u64,
|
||||
undef,
|
||||
null,
|
||||
/// Struct / array / vector of earlier constants.
|
||||
aggregate: []const ConstId,
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -80,6 +82,13 @@ pub const Inst = struct {
|
|||
select: struct { cond: Value, t: Value, f: Value },
|
||||
extractvalue: struct { agg: Value, index: u32 },
|
||||
phi: struct { incoming: []const struct { val: Value, block: u32 } },
|
||||
/// `ty` is the pointer type; `elem` the allocated type.
|
||||
alloca: struct { elem: TypeId, size: Value },
|
||||
load: struct { ptr: Value, @"align": u32 },
|
||||
store: struct { ptr: Value, val: Value, @"align": u32 },
|
||||
/// `ty` is the result pointer type; `elem` the source element type.
|
||||
gep: struct { elem: TypeId, ptr: Value, indices: []const Value },
|
||||
@"unreachable",
|
||||
},
|
||||
};
|
||||
|
||||
|
|
@ -435,6 +444,10 @@ const Encoder = struct {
|
|||
},
|
||||
.undef => try e.rec(3),
|
||||
.null => try e.rec(2),
|
||||
.aggregate => |elems| {
|
||||
for (elems) |el| try e.push(e.valueId(null, .{ .constant = el }));
|
||||
try e.rec(7); // CST_CODE_AGGREGATE
|
||||
},
|
||||
}
|
||||
}
|
||||
try w.endBlock();
|
||||
|
|
@ -545,6 +558,35 @@ const Encoder = struct {
|
|||
try e.push(x.index);
|
||||
try e.rec(26);
|
||||
},
|
||||
.alloca => |a| {
|
||||
try e.push(a.elem);
|
||||
try e.push(e.typeOf(f, a.size));
|
||||
try e.push(e.valueId(f, a.size)); // absolute
|
||||
try e.push(@as(u64, std.math.log2_int(u32, 4)) + 1 | (1 << 6)); // align 4, explicit type
|
||||
try e.rec(19);
|
||||
},
|
||||
.load => |l| {
|
||||
try e.pushValueAndType(f, inst_id, l.ptr);
|
||||
try e.push(inst.ty);
|
||||
try e.push(@as(u64, std.math.log2_int(u32, l.@"align")) + 1);
|
||||
try e.push(0);
|
||||
try e.rec(20);
|
||||
},
|
||||
.store => |s| {
|
||||
try e.pushValueAndType(f, inst_id, s.ptr);
|
||||
try e.pushValueAndType(f, inst_id, s.val);
|
||||
try e.push(@as(u64, std.math.log2_int(u32, s.@"align")) + 1);
|
||||
try e.push(0);
|
||||
try e.rec(44);
|
||||
},
|
||||
.gep => |g| {
|
||||
try e.push(1); // inbounds
|
||||
try e.push(g.elem);
|
||||
try e.pushValueAndType(f, inst_id, g.ptr);
|
||||
for (g.indices) |x| try e.pushValueAndType(f, inst_id, x);
|
||||
try e.rec(43);
|
||||
},
|
||||
.@"unreachable" => try e.rec(15),
|
||||
.phi => |p| {
|
||||
try e.push(inst.ty);
|
||||
for (p.incoming) |in| {
|
||||
|
|
|
|||
|
|
@ -102,7 +102,7 @@ pub fn buildEmptyCompute(gpa: Allocator, info: ComputeInfo) ![]u8 {
|
|||
|
||||
const dxil_part = try programPart(gpa, info.profile, bitcode);
|
||||
defer gpa.free(dxil_part);
|
||||
const psv = try psvCompute(gpa, info.entry, info.num_threads);
|
||||
const psv = try psvCompute(gpa, info.entry, info.num_threads, &.{});
|
||||
defer gpa.free(psv);
|
||||
const empty_sig = [_]u8{ 0, 0, 0, 0, 8, 0, 0, 0 }; // ParamCount 0, ParamOffset 8
|
||||
|
||||
|
|
@ -117,7 +117,7 @@ pub fn buildEmptyCompute(gpa: Allocator, info: ComputeInfo) ![]u8 {
|
|||
}
|
||||
|
||||
/// DxilProgramHeader + DxilBitcodeHeader + bitcode.
|
||||
fn programPart(gpa: Allocator, profile: Profile, bitcode: []const u8) ![]u8 {
|
||||
pub fn programPart(gpa: Allocator, profile: Profile, bitcode: []const u8) ![]u8 {
|
||||
const padded = std.mem.alignForward(usize, bitcode.len, 4);
|
||||
const out = try gpa.alloc(u8, 24 + padded);
|
||||
@memset(out, 0);
|
||||
|
|
@ -132,8 +132,11 @@ fn programPart(gpa: Allocator, profile: Profile, bitcode: []const u8) ![]u8 {
|
|||
return out;
|
||||
}
|
||||
|
||||
/// One PSVResourceBindInfo1 record.
|
||||
pub const ResourceBind = struct { res_type: u32, space: u32, lower: u32, upper: u32, kind: u32, flags: u32 = 0 };
|
||||
|
||||
/// PSV0 with PSVRuntimeInfo4 (60 bytes), as produced for validator 1.11.
|
||||
fn psvCompute(gpa: Allocator, entry: []const u8, num_threads: [3]u32) ![]u8 {
|
||||
pub fn psvCompute(gpa: Allocator, entry: []const u8, num_threads: [3]u32, resources: []const ResourceBind) ![]u8 {
|
||||
var buf: std.ArrayList(u8) = .empty;
|
||||
errdefer buf.deinit(gpa);
|
||||
const w = struct {
|
||||
|
|
@ -161,7 +164,13 @@ fn psvCompute(gpa: Allocator, entry: []const u8, num_threads: [3]u32) ![]u8 {
|
|||
try w(&buf, gpa, 0); // NumBytesGroupSharedMemory
|
||||
try w(&buf, gpa, 0); // Flags
|
||||
|
||||
try w(&buf, gpa, 0); // resource count
|
||||
try w(&buf, gpa, @intCast(resources.len)); // resource count
|
||||
if (resources.len > 0) {
|
||||
try w(&buf, gpa, 24); // sizeof(PSVResourceBindInfo1)
|
||||
for (resources) |r| {
|
||||
for ([_]u32{ r.res_type, r.space, r.lower, r.upper, r.kind, r.flags }) |v| try w(&buf, gpa, v);
|
||||
}
|
||||
}
|
||||
|
||||
// String table: "\0" ++ entry ++ "\0", padded to 4 bytes.
|
||||
const strtab_len = std.mem.alignForward(usize, entry.len + 2, 4);
|
||||
|
|
|
|||
|
|
@ -307,7 +307,7 @@ const Lowerer = struct {
|
|||
else
|
||||
try l.b.constInt(scalar_ty, switch (c) {
|
||||
.int => |i| i,
|
||||
.float => |f| if (std.math.isFinite(f)) @intFromFloat(@trunc(f)) else 0,
|
||||
.float => |f| saturatingInt(f),
|
||||
});
|
||||
if (ts.count() == 1 and ts.kind == .scalar) return k;
|
||||
return l.b.add(.{ .op = .{ .splat = k }, .ty = target });
|
||||
|
|
@ -760,6 +760,15 @@ const Lowerer = struct {
|
|||
}
|
||||
};
|
||||
|
||||
/// Float to integer for constants: truncates toward zero, saturating out of
|
||||
/// range (NaN gives 0).
|
||||
pub fn saturatingInt(f: f64) i64 {
|
||||
if (std.math.isNan(f)) return 0;
|
||||
if (f >= 9.2233720368547758e18) return std.math.maxInt(i64);
|
||||
if (f <= -9.2233720368547758e18) return std.math.minInt(i64);
|
||||
return @intFromFloat(@trunc(f));
|
||||
}
|
||||
|
||||
fn semanticOf(sems: []const ast.Semantic) ?[]const u8 {
|
||||
for (sems) |sem| if (sem.kind == .semantic) return sem.values[0];
|
||||
return null;
|
||||
|
|
|
|||
11
src/main.zig
11
src/main.zig
|
|
@ -303,6 +303,17 @@ fn run(cli: Cli, arena: std.mem.Allocator, args: []const [:0]const u8, stdout: *
|
|||
try Io.Dir.cwd().writeFile(cli.io, .{ .sub_path = path, .data = bytes });
|
||||
return;
|
||||
}
|
||||
const bytes = zdxc.dxil.emit.emit(arena, m, profile.?, &diags) catch |err| switch (err) {
|
||||
error.Unsupported => {
|
||||
try diags.render(&sm, cli.stderr, .{ .format = opts.diag_format, .color = false });
|
||||
try cli.stderr.flush();
|
||||
return error.CompileFailed;
|
||||
},
|
||||
else => |x| return x,
|
||||
};
|
||||
const path = opts.output orelse return cli.fail("missing -Fo <file>", .{});
|
||||
try Io.Dir.cwd().writeFile(cli.io, .{ .sub_path = path, .data = bytes });
|
||||
return;
|
||||
}
|
||||
return cli.fail("code generation is not implemented yet: the input was checked but no output was written (use -zdxc-empty for a placeholder shader)", .{});
|
||||
}
|
||||
|
|
|
|||
|
|
@ -21,6 +21,7 @@ pub const dxil = struct {
|
|||
pub const hash = @import("dxil/hash.zig");
|
||||
pub const module = @import("dxil/module.zig");
|
||||
pub const shader = @import("dxil/shader.zig");
|
||||
pub const emit = @import("dxil/emit.zig");
|
||||
};
|
||||
|
||||
pub const ir = struct {
|
||||
|
|
|
|||
|
|
@ -225,7 +225,7 @@ const Emitter = struct {
|
|||
fn intOf(c: ir.Constant) i64 {
|
||||
return switch (c) {
|
||||
.int => |i| i,
|
||||
.float => |f| @intFromFloat(@trunc(f)),
|
||||
.float => |f| @import("../ir/lower.zig").saturatingInt(f),
|
||||
};
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in New Issue