diff --git a/src/dxil/emit.zig b/src/dxil/emit.zig index 8d3588e..6dd997b 100644 --- a/src/dxil/emit.zig +++ b/src/dxil/emit.zig @@ -714,6 +714,23 @@ const Emitter = struct { var mparams: std.ArrayList(mangle.Param) = .empty; var ann: std.ArrayList(ParamAnn) = .empty; const ret_void = f.ret == TypeTable.void_type; + if (!internal) { + // (dxc's check; the validator counts matrix handles as + // resources too.) + const holds = struct { + fn check(em: *Emitter, ty: TypeId) bool { + return isResourceType(em.t, ty) or em.holdsResources(ty) or em.containsLinAlg(ty); + } + }.check; + var bad = holds(e, f.ret); + for (f.params) |p| bad = bad or holds(e, p.ty); + if (bad) { + const mps = try e.arena.alloc(mangle.Param, f.params.len); + for (mps, f.params) |*mp, p| mp.* = .{ .ty = p.ty, .by_ref = p.by_ref }; + _ = e.diags.report(.{ .severity = .@"error", .loc = .none }, "Exported function \\01{s} must not contain a resource in parameter or return type.", .{(try mangle.function(e.arena, e.t, f.name, f.ret, mps))[1..]}); + return error.Unsupported; + } + } try ann.append(e.arena, .{ .qual = 1, .comp = if (ret_void) null else try e.compType(e.compOf(f.ret)), .semantic = f.ret_semantic }); for (f.params) |p| { // Structs go by pointer. diff --git a/src/dxil/mangle.zig b/src/dxil/mangle.zig index e7089e1..ffccf9b 100644 --- a/src/dxil/mangle.zig +++ b/src/dxil/mangle.zig @@ -114,7 +114,7 @@ fn typeCode(a: Allocator, t: *TypeTable, ty: TypeId, out: *std.ArrayList(u8), na if (ty == TypeTable.void_type) return out.append(a, 'X'); switch (t.get(ty)) { .scalar => |s| try out.appendSlice(a, scalarCode(s)), - .@"enum" => |en| try className(a, 'W', try std.fmt.allocPrint(a, "4{s}", .{en.name}), out, names), + .@"enum" => |en| try qualifiedName(a, t, "W4", en.name, en.scopes, out, names), .vector => |v| { var args: std.ArrayList(u8) = .empty; try args.appendSlice(a, scalarCode(v.elem)); @@ -141,7 +141,7 @@ fn typeCode(a: Allocator, t: *TypeTable, ty: TypeId, out: *std.ArrayList(u8), na for (dims.items) |d| try number(a, d, out); try typeCode(a, t, et, out, names); }, - .@"struct" => |st| try className(a, 'U', st.name, out, names), + .@"struct" => |st| try qualifiedName(a, t, if (st.is_class) "V" else "U", try structName(a, t, st), st.scopes, out, names), .resource => |r| { if (r.elem == TypeTable.void_type or r.kind == .raytracing_acceleration_structure) return className(a, 'U', r.name, out, names); // Template arguments have names of their own. @@ -166,6 +166,47 @@ fn typeCode(a: Allocator, t: *TypeTable, ty: TypeId, out: *std.ArrayList(u8), na } } +/// A struct's own name: `name`, or `?$name@@` for a template +/// instance (its arguments with names of their own). +fn structName(a: Allocator, t: *TypeTable, st: *const types.StructInfo) Allocator.Error![]const u8 { + const base = st.base_name orelse return st.name; + var out: std.ArrayList(u8) = .empty; + var inner: Names = .{}; + try inner.add(a, base); + try out.print(a, "?${s}@", .{base}); + for (st.template_args) |arg| switch (arg) { + .type => |ty| try typeCode(a, t, ty, &out, &inner), + .value => |v| { + try out.appendSlice(a, "$0"); + if (v < 0) try out.append(a, '?'); + try number(a, @abs(v), &out); + }, + }; + try out.append(a, '@'); + return out.items; +} + +/// `@...@`: the name and each enclosing scope's, +/// innermost first, each a back-reference when already spelled. +fn qualifiedName(a: Allocator, t: *TypeTable, kind: []const u8, name: []const u8, scopes: []const types.ScopeName, out: *std.ArrayList(u8), names: *Names) Allocator.Error!void { + try out.appendSlice(a, kind); + try namePart(a, name, out, names); + for (scopes) |sc| try namePart(a, switch (sc) { + .name => |n| n, + .strct => |st| try structName(a, t, st), + }, out, names); + try out.append(a, '@'); +} + +/// One part of a qualified name: `name@` (template instances end in `@` +/// themselves), or a back-reference digit. +fn namePart(a: Allocator, name: []const u8, out: *std.ArrayList(u8), names: *Names) Allocator.Error!void { + if (names.find(name)) |k| return out.append(a, '0' + @as(u8, @intCast(k))); + try out.appendSlice(a, name); + if (name[0] != '?') try out.append(a, '@'); + try names.add(a, name); +} + /// `@@`, or `@` for a name already spelled /// (template instances `?$name@args@` end in `@` themselves). fn className(a: Allocator, kind: u8, name: []const u8, out: *std.ArrayList(u8), names: *Names) Allocator.Error!void { diff --git a/src/sema/sema.zig b/src/sema/sema.zig index b3c9539..f9f195e 100644 --- a/src/sema/sema.zig +++ b/src/sema/sema.zig @@ -112,7 +112,7 @@ pub const Function = struct { }; /// A bound template argument. -pub const Bound = union(enum) { type: TypeId, value: i64 }; +pub const Bound = types.TemplateArg; pub const FuncSet = struct { funcs: std.ArrayList(*Function) = .empty, @@ -139,6 +139,8 @@ pub const Scope = struct { syms: std.StringHashMapUnmanaged(Symbol) = .empty, kind: enum { global, namespace, function, block, @"struct" } = .block, name: []const u8 = "", + /// The struct of a struct scope. + strct: ?*const types.StructInfo = null, }; /// Per-struct data kept by Sema (methods, nested scope). @@ -567,7 +569,7 @@ pub const Sema = struct { // Register the type before checking members, so the struct // may name itself. const info = try s.arena.create(types.StructInfo); - info.* = .{ .name = name.items }; + info.* = .{ .name = name.items, .base_name = st.name, .template_args = b.args, .scopes = try s.scopeNames(b.scope), .is_class = st.keyword == .class }; const ty = try s.types.intern(.{ .@"struct" = info }); try b.scope.syms.put(s.arena, name.items, .{ .type = ty }); // The injected class name: `S` alone is this instance. @@ -1702,6 +1704,18 @@ pub const Sema = struct { } } + /// The namespaces and structs enclosing `scope`, innermost first. + fn scopeNames(s: *Sema, scope: *const Scope) Allocator.Error![]const types.ScopeName { + var out: std.ArrayList(types.ScopeName) = .empty; + var sc: ?*const Scope = scope; + while (sc) |x| : (sc = x.parent) switch (x.kind) { + .namespace => try out.append(s.arena, .{ .name = x.name }), + .@"struct" => if (x.strct) |st| try out.append(s.arena, .{ .strct = st }), + else => {}, + }; + return out.items; + } + fn enumDecl(s: *Sema, d: *const ast.Decl, e: ast.EnumDecl) Allocator.Error!void { const info = try s.arena.create(types.EnumInfo); var underlying: Scalar = .int; @@ -1709,7 +1723,7 @@ pub const Sema = struct { const ut = try s.resolveType(u); underlying = s.types.component(ut) orelse .int; } - info.* = .{ .name = e.name orelse "", .underlying = underlying, .scoped = e.scoped }; + info.* = .{ .name = e.name orelse "", .underlying = underlying, .scoped = e.scoped, .scopes = try s.scopeNames(s.scope) }; const ty = try s.types.intern(.{ .@"enum" = info }); if (e.name) |n| try s.declare(n, .{ .type = ty }, d.loc); const sc = try s.arena.create(Scope); @@ -1751,13 +1765,13 @@ pub const Sema = struct { return; } else { info = try s.arena.create(types.StructInfo); - info.* = .{ .name = name }; + info.* = .{ .name = name, .scopes = try s.scopeNames(s.scope), .is_class = st.keyword == .class }; ty = try s.types.intern(.{ .@"struct" = info }); try s.declare(name, .{ .type = ty }, d.loc); } const members = st.members orelse return; const sc = try s.arena.create(Scope); - sc.* = .{ .parent = s.scope, .kind = .@"struct", .name = name }; + sc.* = .{ .parent = s.scope, .kind = .@"struct", .name = name, .strct = info }; const ss = try s.arena.create(StructSema); ss.* = .{ .scope = sc, .decl = d }; info.sema = ss; diff --git a/src/sema/types.zig b/src/sema/types.zig index c2ab1c2..6f826a8 100644 --- a/src/sema/types.zig +++ b/src/sema/types.zig @@ -377,8 +377,22 @@ pub const Interp = packed struct(u8) { }; /// A struct's layout-relevant information. Methods live in Sema. +/// A template argument: a type or an integral value. +pub const TemplateArg = union(enum) { type: TypeId, value: i64 }; + +/// A scope enclosing a declaration (for mangled names): a namespace or a +/// struct. +pub const ScopeName = union(enum) { name: []const u8, strct: *const StructInfo }; + pub const StructInfo = struct { name: []const u8, + /// For mangled names: the name without template arguments, the + /// arguments of a template instance, the enclosing scopes (innermost + /// first) and whether it was declared `class`. + base_name: ?[]const u8 = null, + template_args: []const TemplateArg = &.{}, + scopes: []const ScopeName = &.{}, + is_class: bool = false, fields: std.ArrayList(Field) = .empty, /// Direct base structs (HLSL 2021 single inheritance, in practice). bases: std.ArrayList(*StructInfo) = .empty, @@ -438,6 +452,8 @@ pub const EnumInfo = struct { name: []const u8, underlying: Scalar, scoped: bool, + /// Enclosing scopes, innermost first (mangled names). + scopes: []const ScopeName = &.{}, }; pub const Type = union(enum) { diff --git a/test/dxil/lib_mangling.hlsl b/test/dxil/lib_mangling.hlsl new file mode 100644 index 0000000..6b10462 --- /dev/null +++ b/test/dxil/lib_mangling.hlsl @@ -0,0 +1,20 @@ +// PROFILE: lib_6_6 +// Mangled names of exported functions whose parameters are structs and +// enums in namespaces and structs, class / struct template instances +// (type and integral arguments, negative ones too) and classes, with the +// back-references dxc uses; RDAT carries them byte for byte. +// ZCHECK: define float @"\01?f2@@YAMU?$Box@M$02@inner@ns@@@Z"( +// ZCHECK: define i32 @"\01?f4@@YAHUNested@Outer@inner@ns@@W4E@234@@Z"( +namespace ns { namespace inner { + struct P { float a; }; + template struct Box { T v[N]; }; + class C { float c; }; + struct Outer { struct Nested { int n; }; enum E { E0, E1 }; }; +} } +template struct Neg { int x; }; +export float f1(ns::inner::P p, ns::inner::P q) { return p.a + q.a; } +export float f2(ns::inner::Box b) { return b.v[0]; } +export float f3(ns::inner::C c, Neg<-5> n) { return 1; } +export int f4(ns::inner::Outer::Nested n, ns::inner::Outer::E e) { return n.n + e; } +export float f5(ns::inner::Outer::Nested i, ns::inner::Box b) { return i.n + b.v[1].a; } +export float f6(inout ns::inner::P p, ns::inner::Box b) { return 1; }