Intrinsics with out parameters; 64-bit DXIL cbuffer members

Lowering passes `out` arguments of intrinsics through temporaries and
`ref` arguments (atomic destinations) as the storage itself. sincos,
modf, frexp (dxc's bit-level lowering on DXIL; ModfStruct/FrexpStruct
per row on SPIR-V, so matrices work) and asuint(double, out lo, out hi).
DXIL constant buffers now load 64-bit and 16-bit members, and SPIR-V
64-bit fragment inputs are Flat.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Peter Li 2026-10-31 10:40:10 -07:00
parent 0148195c7c
commit 88fd16781a
5 changed files with 194 additions and 13 deletions

View File

@ -1087,6 +1087,7 @@ const Emitter = struct {
fn intrinsic(e: *Emitter, frame: *Frame, name: []const u8, ty: TypeId, args: []const Ref) Error!Lowered {
const eq = std.mem.eql;
if (try e.outIntrinsic(frame, name, ty, args)) |r| return r;
var a: [4][]const Value = undefined;
for (args, 0..) |x, k| a[k] = try e.scalars(frame, x);
const ac = if (args.len > 0) e.compOf(frame.f.inst(args[0]).ty) else e.compOf(ty);
@ -1299,6 +1300,83 @@ const Emitter = struct {
return e.unsupported("intrinsic '{s}' is not supported yet", .{name});
}
/// The pointer an out argument was lowered to.
fn outPtr(e: *Emitter, frame: *Frame, r: Ref) Error!Ptr {
return switch (frame.vals[r.index()]) {
.ptr => |p| p,
else => e.unsupported("internal: out argument is not a pointer", .{}),
};
}
/// Intrinsics with out parameters (null for the others).
fn outIntrinsic(e: *Emitter, frame: *Frame, name: []const u8, ty: TypeId, args: []const Ref) Error!?Lowered {
const eq = std.mem.eql;
if (eq(u8, name, "sincos") or eq(u8, name, "modf") or eq(u8, name, "frexp")) {
const xty = frame.f.inst(args[0]).ty;
const xs = try e.scalars(frame, args[0]);
const ft = try e.scalarTy(e.compOf(xty));
const n = xs.len;
const a1 = try e.arena.alloc(Value, n);
const a2 = try e.arena.alloc(Value, n);
if (eq(u8, name, "sincos")) {
for (xs, a1, a2) |x, *sv, *cv| {
sv.* = try e.unaryOp(13, "unary", x, ft);
cv.* = try e.unaryOp(12, "unary", x, ft);
}
try e.storePtr(try e.outPtr(frame, args[1]), a1, frame.f.inst(args[1]).ty);
try e.storePtr(try e.outPtr(frame, args[2]), a2, frame.f.inst(args[2]).ty);
return .none;
}
if (eq(u8, name, "modf")) {
// ip = trunc(x), result x - ip.
for (xs, a1, a2) |x, *ip, *fr| {
ip.* = try e.unaryOp(29, "unary", x, ft);
fr.* = try e.fbin(.sub, x, ip.*, ft);
}
try e.storePtr(try e.outPtr(frame, args[1]), a1, frame.f.inst(args[1]).ty);
return .{ .scalars = a2 };
}
// frexp, as dxc: exponent and mantissa from the bits.
const i32t = try e.dm.intType(32);
for (xs, a1, a2) |x, *ex, *mn| {
const nz = try e.add(i32t, .{ .cast = .{ .op = .sext, .val = try e.fcmp(14, x, try e.constant(try e.t.scalar(.float), .{ .float = 0 })) } });
const bitsv = try e.add(i32t, .{ .cast = .{ .op = .bitcast, .val = x } });
var t = try e.fbin(.@"and", bitsv, try e.i32Const(0x7f800000), i32t);
t = try e.fbin(.add, t, try e.i32Const(-0x3f000000), i32t);
t = try e.fbin(.@"and", t, nz, i32t);
t = try e.fbin(.ashr, t, try e.i32Const(23), i32t);
ex.* = try e.add(ft, .{ .cast = .{ .op = .sitofp, .val = t } });
var m = try e.fbin(.@"and", bitsv, try e.i32Const(0x007fffff), i32t);
m = try e.fbin(.@"or", m, try e.i32Const(0x3f000000), i32t);
m = try e.fbin(.@"and", m, nz, i32t);
mn.* = try e.add(ft, .{ .cast = .{ .op = .bitcast, .val = m } });
}
try e.storePtr(try e.outPtr(frame, args[1]), a1, frame.f.inst(args[1]).ty);
_ = ty;
return .{ .scalars = a2 };
}
if (eq(u8, name, "asuint") and args.len == 3) {
// SplitDouble.
const i32t = try e.dm.intType(32);
const f64t = try e.dm.getType(.double);
e.uses_double = true;
const st = try e.dm.getType(.{ .@"struct" = .{ .name = "dx.types.splitdouble", .elems = &.{ i32t, i32t } } });
const fn_i = try e.dxOp("dx.op.splitDouble.f64", st, &.{ i32t, f64t }, .nounwind_readnone);
const ds = try e.scalars(frame, args[0]);
const lo = try e.arena.alloc(Value, ds.len);
const hi = try e.arena.alloc(Value, ds.len);
for (ds, lo, hi) |d, *l, *h| {
const r = try e.callOp(fn_i, st, &.{ try e.i32Const(102), d });
l.* = try e.add(i32t, .{ .extractvalue = .{ .agg = r, .index = 0 } });
h.* = try e.add(i32t, .{ .extractvalue = .{ .agg = r, .index = 1 } });
}
try e.storePtr(try e.outPtr(frame, args[1]), lo, frame.f.inst(args[1]).ty);
try e.storePtr(try e.outPtr(frame, args[2]), hi, frame.f.inst(args[2]).ty);
return .none;
}
return null;
}
fn fbin(e: *Emitter, op: module.BinOp, x: Value, y: Value, ty: DType) Error!Value {
return e.add(ty, .{ .binop = .{ .op = op, .lhs = x, .rhs = y } });
}
@ -1523,27 +1601,32 @@ const Emitter = struct {
const i32t = try e.dm.intType(32);
var cache_reg: ?u32 = null;
var cache_val: Value = undefined;
var cache_c: Scalar = .int;
var cache_st: DType = 0;
for (out, 0..) |*o, k| {
const comp_ty = try e.componentType(ty, @intCast(k));
const c = e.compOf(comp_ty);
const mem_c: Scalar = if (c == .bool) .uint else c;
if (mem_c.size() != 4) return e.unsupported("{d}-bit constant buffer members are not supported yet", .{mem_c.size() * 8});
// A register holds 16 bytes: 4 32-bit, 2 64-bit or 8 16-bit values.
const sz = mem_c.size();
const off = offset + (layout.componentOffset(e.arena, e.t, ty, row_major, @intCast(k), .dx_legacy) catch return e.unsupported("constant buffer member types", .{}));
const reg = off / 16;
const st = try e.scalarTy(mem_c);
const same_kind = (mem_c.isFloat() == cache_c.isFloat());
if (cache_reg == null or cache_reg.? != reg or !same_kind) {
const ret_name = try std.fmt.allocPrint(e.arena, "dx.types.CBufRet.{s}", .{e.overloadName(st)});
const ret_ty = try e.dm.getType(.{ .@"struct" = .{ .name = ret_name, .elems = &.{ st, st, st, st } } });
if (cache_reg == null or cache_reg.? != reg or cache_st != st) {
const ret_name = try std.fmt.allocPrint(e.arena, "dx.types.CBufRet.{s}{s}", .{ e.overloadName(st), if (sz == 2) ".8" else "" });
const elems: []const DType = switch (sz) {
8 => &.{ st, st },
2 => &.{ st, st, st, st, st, st, st, st },
else => &.{ st, st, st, st },
};
const ret_ty = try e.dm.getType(.{ .@"struct" = .{ .name = ret_name, .elems = try e.arena.dupe(DType, elems) } });
const fn_i = try e.dxOp(try std.fmt.allocPrint(e.arena, "dx.op.cbufferLoadLegacy.{s}", .{e.overloadName(st)}), ret_ty, &.{ i32t, e.handle_ty, i32t }, .nounwind_readonly);
var reg_v = try e.i32Const(reg);
if (dyn_regs) |d| reg_v = try e.add(i32t, .{ .binop = .{ .op = .add, .lhs = d, .rhs = reg_v } });
cache_val = try e.callOp(fn_i, ret_ty, &.{ try e.i32Const(59), res.handle, reg_v });
cache_reg = reg;
cache_c = mem_c;
cache_st = st;
}
const v = try e.add(st, .{ .extractvalue = .{ .agg = cache_val, .index = (off % 16) / 4 } });
const v = try e.add(st, .{ .extractvalue = .{ .agg = cache_val, .index = (off % 16) / sz } });
o.* = if (c == .bool) try e.convertScalar(v, .uint, .bool) else v;
}
return .{ .scalars = out };

View File

@ -854,14 +854,31 @@ const Lowerer = struct {
};
return result;
}
// `out` parameters go through temporaries copied back
// afterwards; `ref` ones (atomic destinations) pass the
// storage itself.
const vals = try l.arena.alloc(Ref, args.len);
var any_out = false;
for (args, 0..) |a, i| {
if (i < spec.params.len and spec.params[i].qual != .in) {
return l.fail(a, "out arguments of intrinsics");
const q = if (i < spec.params.len) spec.params[i].qual else .in;
const pt = try l.concrete(it.params[i]);
switch (q) {
.in => vals[i] = try l.converted(a, pt),
.ref => vals[i] = try l.lvalue(a),
.out, .inout => {
vals[i] = try l.b.add(.{ .op = .local_var, .ty = pt, .ptr = .function });
if (q == .inout) try l.b.store(vals[i], try l.converted(a, pt));
any_out = true;
},
}
vals[i] = try l.converted(a, try l.concrete(it.params[i]));
}
return l.b.add(.{ .op = .{ .intrinsic = .{ .name = name, .args = vals } }, .ty = ret_ty });
const result = try l.b.add(.{ .op = .{ .intrinsic = .{ .name = name, .args = vals } }, .ty = ret_ty });
if (any_out) for (args, 0..) |a, i| {
const q = if (i < spec.params.len) spec.params[i].qual else .in;
if (q != .out and q != .inout) continue;
try l.storeTo(a, try l.convert(try l.b.load(vals[i]), a.ty));
};
return result;
},
else => return l.fail(e, "this call"),
}

View File

@ -612,7 +612,7 @@ const Emitter = struct {
if ((input and entry.stage == .pixel) or (!input and entry.stage == .vertex)) {
var et = ty;
while (e.t.get(et) == .array) et = e.t.get(et).array.elem;
if (!e.t.component(et).?.isFloat()) {
if (!e.t.component(et).?.isFloat() or e.t.component(et).? == .double) {
try e.b.emit(.annotations, .decorate, &.{ var_id, 14 }); // Flat
} else {
if (el.interp.centroid) try e.b.emit(.annotations, .decorate, &.{ var_id, 16 }); // Centroid
@ -1751,7 +1751,63 @@ const Emitter = struct {
return e.glsl;
}
/// Intrinsics with out parameters (null for the others).
fn outIntrinsic(e: *Emitter, name: []const u8, ty: TypeId, args: []const Ref) Error!?Id {
const eq = std.mem.eql;
const xty = if (args.len > 0) e.f.inst(args[0]).ty else ty;
if (eq(u8, name, "sincos") or eq(u8, name, "modf") or eq(u8, name, "frexp")) {
// Matrices go row by row; each piece gives two results.
const m = e.isTrueMatrix(xty);
const nrows: u32 = if (m) e.t.get(xty).matrix.rows else 1;
const pty = if (m) try e.t.vector(e.t.get(xty).matrix.elem, e.t.get(xty).matrix.cols) else xty;
const first = try e.arena.alloc(Id, nrows);
const second = try e.arena.alloc(Id, nrows);
const n = e.t.componentCount(pty).?;
const ity = if (n == 1) try e.t.scalar(.int) else try e.t.vector(.int, @intCast(n));
for (first, second, 0..) |*a, *b, k| {
const x = if (m) try e.row(e.val(args[0]), pty, @intCast(k)) else e.val(args[0]);
if (eq(u8, name, "sincos")) {
a.* = try e.op1(.ext_inst, pty, &.{ e.glslSet(), 13, x });
b.* = try e.op1(.ext_inst, pty, &.{ e.glslSet(), 14, x });
continue;
}
// ModfStruct {fraction, whole}; FrexpStruct {mantissa, exponent}.
const is_modf = eq(u8, name, "modf");
const st = e.b.newId();
try e.b.emit(.types_globals, .type_struct, &.{ st, try e.typeId(pty), try e.typeId(if (is_modf) pty else ity) });
const r = try e.opRaw(.ext_inst, st, &.{ e.glslSet(), if (is_modf) 36 else 52, x });
a.* = try e.op1(.composite_extract, pty, &.{ r, 0 });
b.* = if (is_modf) try e.op1(.composite_extract, pty, &.{ r, 1 }) else try e.convert(try e.op1(.composite_extract, ity, &.{ r, 1 }), ity, pty);
}
const join = struct {
fn f(em: *Emitter, parts: []const Id, is_m: bool, t: TypeId) Error!Id {
return if (is_m) em.op1(.composite_construct, t, parts) else parts[0];
}
}.f;
const av = try join(e, first, m, xty);
const bv = try join(e, second, m, xty);
if (eq(u8, name, "sincos")) {
try e.b.emit(.functions, .store, &.{ e.val(args[1]), av });
try e.b.emit(.functions, .store, &.{ e.val(args[2]), bv });
return 0;
}
try e.b.emit(.functions, .store, &.{ e.val(args[1]), try e.convert(bv, xty, e.f.inst(args[1]).ty) });
return av;
}
if (eq(u8, name, "asuint") and args.len == 3) {
const n = e.t.componentCount(xty).?;
if (n != 1) return e.unsupported("asuint of double vectors is not supported yet", .{});
const pair = try e.op1(.bitcast, try e.t.vector(.uint, 2), &.{e.val(args[0])});
const u = try e.t.scalar(.uint);
try e.b.emit(.functions, .store, &.{ e.val(args[1]), try e.op1(.composite_extract, u, &.{ pair, 0 }) });
try e.b.emit(.functions, .store, &.{ e.val(args[2]), try e.op1(.composite_extract, u, &.{ pair, 1 }) });
return 0;
}
return null;
}
fn intrinsic(e: *Emitter, name: []const u8, ty: TypeId, args: []const Ref) Error!Id {
if (try e.outIntrinsic(name, ty, args)) |id| return id;
if (std.mem.eql(u8, name, "NonUniformResourceIndex")) {
// A decorated copy marks the index (as dxc).
const id = try e.op1(.copy_object, ty, &.{e.val(args[0])});

View File

@ -0,0 +1,12 @@
// PROFILE: ps_6_0
// Intrinsics with out parameters.
// ZCHECK: @dx.op.splitDouble.f64(i32 102
// ZCHECK: @dx.op.unary.f32(i32 29,
double d;
float4 main(float4 x : X) : SV_Target {
float4 s, c; sincos(x, s, c);
float3 ip; float3 fr = modf(x.xyz, ip);
float e; float m = frexp(x.w, e);
uint lo, hi; asuint(d, lo, hi);
return s + c + float4(fr + ip, m * e) + lo + hi;
}

View File

@ -0,0 +1,13 @@
// PROFILE: ps_6_0
// Intrinsics with out parameters.
// ZCHECK: FrexpStruct
// ZCHECK: ModfStruct
double d;
float4 main(float4 x : X) : SV_Target {
float4 s, c; sincos(x, s, c);
float3 ip; float3 fr = modf(x.xyz, ip);
float e; float m = frexp(x.w, e);
uint lo, hi; asuint(d, lo, hi);
return s + c + float4(fr + ip, m * e) + lo + hi;
}
float2x3 mats(float2x3 m) { float2x3 s, c; sincos(m, s, c); float2x3 ip; return modf(m, ip) + s + c + ip; }