authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2026-06-08 14:55:38+03:30
committergravatar for alichraghi@noreply.codeberg.orgAli Cheraghi <alichraghi@noreply.codeberg.org> 2026-06-14 09:11:59+02:00
loge2d11ff76bad62b094f50a5d36332c70a5c87ea1
tree005eaea6adf63132e42e92149e71ad26bbdd83af
parent3deb86bafdb2f622b9099c405e0e861f70c36a45

spirv: set execution mode via cc info

Execution modes (e.g. `LocalSize`, `OriginUpperLeft`) were previously set via `gpu.executionMode()`, which used inline assembly to emit `OpExecutionMode`. The SPIR-V assembler now rejects this instruction and retrieves execution mode information from function cc, deleting `gpu.executionMode()` entirely. Two new `spirv_task` and `spirv_mesh` calling conventions are also added and `PackedCallingConvention.unpack()` now takes a trailing data slice.

14 files changed, 300 insertions(+), 148 deletions(-)

lib/std/Target.zig+2
......@@ -1984,6 +1984,8 @@ pub const Cpu = struct {
19841984 .spirv_kernel,
19851985 .spirv_fragment,
19861986 .spirv_vertex,
1987 .spirv_task,
1988 .spirv_mesh,
19871989 => &.{ .spirv32, .spirv64 },
19881990
19891991 .ez80_cet,
lib/std/gpu.zig-83
......@@ -19,86 +19,3 @@ pub extern const local_invocation_id: @Vector(3, u32) addrspace(.input);
1919pub extern const global_invocation_id: @Vector(3, u32) addrspace(.input);
2020pub extern const vertex_index: u32 addrspace(.input);
2121pub extern const instance_index: u32 addrspace(.input);
22
23pub const ExecutionMode = union(Tag) {
24 /// Sets origin of the framebuffer to the upper-left corner
25 origin_upper_left,
26 /// Sets origin of the framebuffer to the lower-left corner
27 origin_lower_left,
28 /// Indicates that the fragment shader writes to `frag_depth`,
29 /// replacing the fixed-function depth value.
30 depth_replacing,
31 /// Indicates that per-fragment tests may assume that
32 /// any `frag_depth` built in-decorated value written by the shader is
33 /// greater-than-or-equal to the fragment’s interpolated depth value
34 depth_greater,
35 /// Indicates that per-fragment tests may assume that
36 /// any `frag_depth` built in-decorated value written by the shader is
37 /// less-than-or-equal to the fragment’s interpolated depth value
38 depth_less,
39 /// Indicates that per-fragment tests may assume that
40 /// any `frag_depth` built in-decorated value written by the shader is
41 /// the same as the fragment’s interpolated depth value
42 depth_unchanged,
43 /// Indicates the workgroup size in the x, y, and z dimensions.
44 local_size: LocalSize,
45
46 pub const Tag = enum(u32) {
47 origin_upper_left = 7,
48 origin_lower_left = 8,
49 depth_replacing = 12,
50 depth_greater = 14,
51 depth_less = 15,
52 depth_unchanged = 16,
53 local_size = 17,
54 };
55
56 pub const LocalSize = struct { x: u32, y: u32, z: u32 };
57};
58
59/// Declare the mode entry point executes in.
60pub fn executionMode(comptime entry_point: anytype, comptime mode: ExecutionMode) void {
61 const cc = @typeInfo(@TypeOf(entry_point)).@"fn".attrs.@"callconv";
62 switch (mode) {
63 .origin_upper_left,
64 .origin_lower_left,
65 .depth_replacing,
66 .depth_greater,
67 .depth_less,
68 .depth_unchanged,
69 => {
70 if (cc != .spirv_fragment) {
71 @compileError(
72 \\invalid execution mode '
73 ++ @tagName(mode) ++
74 \\' for function with '
75 ++ @tagName(cc) ++
76 \\' calling convention
77 );
78 }
79 asm volatile (
80 \\OpExecutionMode %entry_point $mode
81 :
82 : [entry_point] "" (entry_point),
83 [mode] "c" (@intFromEnum(mode)),
84 );
85 },
86 .local_size => |size| {
87 if (cc != .spirv_kernel) {
88 @compileError(
89 \\invalid execution mode 'local_size' for function with '
90 ++ @tagName(cc) ++
91 \\' calling convention
92 );
93 }
94 asm volatile (
95 \\OpExecutionMode %entry_point LocalSize $x $y $z
96 :
97 : [entry_point] "" (entry_point),
98 [x] "c" (size.x),
99 [y] "c" (size.y),
100 [z] "c" (size.z),
101 );
102 },
103 }
104}
lib/std/lang.zig+36-4
......@@ -140,7 +140,7 @@ pub const CallingConvention = union(enum(u8)) {
140140 pub const kernel: CallingConvention = switch (builtin.target.cpu.arch) {
141141 .amdgcn => .amdgcn_kernel,
142142 .nvptx, .nvptx64 => .nvptx_kernel,
143 .spirv32, .spirv64 => .spirv_kernel,
143 .spirv32, .spirv64 => .{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } },
144144 else => unreachable,
145145 };
146146
......@@ -337,11 +337,13 @@ pub const CallingConvention = union(enum(u8)) {
337337 nvptx_device,
338338 nvptx_kernel,
339339
340 // Calling conventions for kernels and shaders on the `spirv`, `spirv32`, and `spirv64` architectures.
340 // Calling conventions for kernels and shaders on the `spirv32` and `spirv64` architectures.
341341 spirv_device,
342 spirv_kernel,
343 spirv_fragment,
344342 spirv_vertex,
343 spirv_kernel: SpirvKernelOptions,
344 spirv_fragment: SpirvFragmentOptions,
345 spirv_task: SpirvKernelOptions,
346 spirv_mesh: SpirvMeshOptions,
345347
346348 // Calling conventions for the `ez80` architecture.
347349 ez80_cet,
......@@ -473,6 +475,36 @@ pub const CallingConvention = union(enum(u8)) {
473475 };
474476 };
475477
478 pub const SpirvKernelOptions = struct {
479 x: u32,
480 y: u32,
481 z: u32,
482 };
483
484 pub const SpirvFragmentOptions = struct {
485 pub const DepthAssumption = enum(u2) {
486 none = 0,
487 greater = 1,
488 less = 2,
489 unchanged = 3,
490 };
491
492 pixel_centered_integer: bool = false,
493 depth_assumption: DepthAssumption = .none,
494 };
495
496 pub const SpirvMeshOptions = struct {
497 pub const StageOutput = enum(u2) {
498 output_points = 0,
499 output_lines = 1,
500 output_triangles = 2,
501 };
502
503 stage_output: StageOutput = .output_triangles,
504 max_primitives: u32 = 1,
505 max_vertices: u32 = 3,
506 };
507
476508 /// Returns the array of `std.Target.Cpu.Arch` to which this `CallingConvention` applies.
477509 /// Asserts that `cc` is not `.auto`, `.@"async"`, `.naked`, or `.@"inline"`.
478510 pub fn archs(cc: CallingConvention) []const std.Target.Cpu.Arch {
src/InternPool.zig+65-4
......@@ -4202,12 +4202,14 @@ pub const Index = enum(u32) {
42024202 type_function: struct {
42034203 const @"data.flags.has_comptime_bits" = opaque {};
42044204 const @"data.flags.has_noalias_bits" = opaque {};
4205 const @"data.flags.cc.extraLen()" = opaque {};
42054206 const @"data.params_len" = opaque {};
42064207 data: *Tag.TypeFunction,
42074208 @"trailing.comptime_bits.len": *@"data.flags.has_comptime_bits",
42084209 @"trailing.noalias_bits.len": *@"data.flags.has_noalias_bits",
4210 @"trailing.cc_bits.len": *@"data.flags.cc.extraLen()",
42094211 @"trailing.param_types.len": *@"data.params_len",
4210 trailing: struct { comptime_bits: []u32, noalias_bits: []u32, param_types: []Index },
4212 trailing: struct { comptime_bits: []u32, noalias_bits: []u32, cc_bits: []u32, param_types: []Index },
42114213 },
42124214 type_tuple: struct {
42134215 const @"data.fields_len" = opaque {};
......@@ -5165,6 +5167,7 @@ pub const Tag = enum(u8) {
51655167 .trailing = struct {
51665168 param_comptime_bits: ?[]u32,
51675169 param_noalias_bits: ?[]u32,
5170 param_cc_bits: ?[]u32,
51685171 param_type: []Index,
51695172 },
51705173 .config = .{
......@@ -5172,6 +5175,8 @@ pub const Tag = enum(u8) {
51725175 .@"trailing.param_comptime_bits.?.len" = .@"(payload.params_len + 31) / 32",
51735176 .@"trailing.param_noalias_bits.?" = .@"payload.flags.has_noalias_bits",
51745177 .@"trailing.param_noalias_bits.?.len" = .@"(payload.params_len + 31) / 32",
5178 .@"trailing.param_cc_bits.?" = .@"payload.flags.cc.extraLen() != 0",
5179 .@"trailing.param_cc_bits.?.len" = .@"payload.flags.cc.extraLen()",
51755180 .@"trailing.param_type.len" = .@"payload.params_len",
51765181 },
51775182 },
......@@ -6983,6 +6988,9 @@ fn extraFuncType(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Ke
69836988 trail_index += 1;
69846989 break :b x;
69856990 };
6991 const cc_extra_len = type_function.data.flags.cc.extraLen();
6992 const cc = type_function.data.flags.cc.unpack(extra.view().items(.@"0")[trail_index..][0..cc_extra_len]);
6993 trail_index += cc_extra_len;
69866994 return .{
69876995 .param_types = .{
69886996 .tid = tid,
......@@ -6992,7 +7000,7 @@ fn extraFuncType(tid: Zcu.PerThread.Id, extra: Local.Extra, extra_index: u32) Ke
69927000 .return_type = type_function.data.return_type,
69937001 .comptime_bits = comptime_bits,
69947002 .noalias_bits = noalias_bits,
6995 .cc = type_function.data.flags.cc.unpack(),
7003 .cc = cc,
69967004 .is_var_args = type_function.data.flags.is_var_args,
69977005 .is_noinline = type_function.data.flags.is_noinline,
69987006 };
......@@ -9091,18 +9099,21 @@ pub fn getFuncType(
90919099 // ask if it already exists, and if so, revert the lengths of the mutated
90929100 // arrays. This is similar to what `getOrPutTrailingString` does.
90939101 const prev_extra_len = extra.mutate.len;
9102 const packed_cc: PackedCallingConvention = .pack(key.cc orelse .auto);
9103 const cc_extra_len = packed_cc.extraLen();
90949104 const params_len: u32 = @intCast(key.param_types.len);
90959105
90969106 try extra.ensureUnusedCapacity(@typeInfo(Tag.TypeFunction).@"struct".field_names.len +
90979107 @intFromBool(key.comptime_bits != 0) +
90989108 @intFromBool(key.noalias_bits != 0) +
9109 cc_extra_len +
90999110 params_len);
91009111
91019112 const func_type_extra_index = addExtraAssumeCapacity(extra, Tag.TypeFunction{
91029113 .params_len = params_len,
91039114 .return_type = key.return_type,
91049115 .flags = .{
9105 .cc = .pack(key.cc orelse .auto),
9116 .cc = packed_cc,
91069117 .is_var_args = key.is_var_args,
91079118 .has_comptime_bits = key.comptime_bits != 0,
91089119 .has_noalias_bits = key.noalias_bits != 0,
......@@ -9112,6 +9123,18 @@ pub fn getFuncType(
91129123
91139124 if (key.comptime_bits != 0) extra.appendAssumeCapacity(.{key.comptime_bits});
91149125 if (key.noalias_bits != 0) extra.appendAssumeCapacity(.{key.noalias_bits});
9126 if (key.cc) |cc| switch (cc) {
9127 .spirv_kernel, .spirv_task => |kernel| extra.appendSliceAssumeCapacity(.{&.{
9128 kernel.x,
9129 kernel.y,
9130 kernel.z,
9131 }}),
9132 .spirv_mesh => |mesh| extra.appendSliceAssumeCapacity(.{&.{
9133 mesh.max_primitives,
9134 mesh.max_vertices,
9135 }}),
9136 else => {},
9137 };
91159138 extra.appendSliceAssumeCapacity(.{@ptrCast(key.param_types)});
91169139 errdefer extra.mutate.len = prev_extra_len;
91179140
......@@ -10704,6 +10727,7 @@ fn dumpStatsFallible(ip: *const InternPool, w: *Io.Writer, arena: Allocator) !vo
1070410727 const info = extraData(extra_list, Tag.TypeFunction, data);
1070510728 break :b @sizeOf(Tag.TypeFunction) +
1070610729 (@sizeOf(Index) * info.params_len) +
10730 (@as(u32, 4) * info.flags.cc.extraLen()) +
1070710731 (@as(u32, 4) * @intFromBool(info.flags.has_comptime_bits)) +
1070810732 (@as(u32, 4) * @intFromBool(info.flags.has_noalias_bits));
1070910733 },
......@@ -12573,12 +12597,35 @@ const PackedCallingConvention = packed struct(u18) {
1257312597 .incoming_stack_alignment = .fromByteUnits(pl.incoming_stack_alignment orelse 0),
1257412598 .extra = @intFromEnum(pl.save),
1257512599 },
12600 std.lang.CallingConvention.SpirvKernelOptions => .{
12601 .tag = tag,
12602 .incoming_stack_alignment = .none,
12603 .extra = 0,
12604 },
12605 std.lang.CallingConvention.SpirvFragmentOptions => .{
12606 .tag = tag,
12607 .incoming_stack_alignment = .none,
12608 .extra = @as(u4, @intFromEnum(pl.depth_assumption)) << 1 | @intFromBool(pl.pixel_centered_integer),
12609 },
12610 std.lang.CallingConvention.SpirvMeshOptions => .{
12611 .tag = tag,
12612 .incoming_stack_alignment = .none,
12613 .extra = @intFromEnum(pl.stage_output),
12614 },
1257612615 else => comptime unreachable,
1257712616 },
1257812617 };
1257912618 }
1258012619
12581 fn unpack(cc: PackedCallingConvention) std.lang.CallingConvention {
12620 fn extraLen(cc: PackedCallingConvention) u2 {
12621 return switch (cc.tag) {
12622 .spirv_kernel, .spirv_task => 3,
12623 .spirv_mesh => 2,
12624 else => 0,
12625 };
12626 }
12627
12628 fn unpack(cc: PackedCallingConvention, trailing: []const u32) std.lang.CallingConvention {
1258212629 return switch (cc.tag) {
1258312630 inline else => |tag| @unionInit(
1258412631 std.lang.CallingConvention,
......@@ -12616,6 +12663,20 @@ const PackedCallingConvention = packed struct(u18) {
1261612663 .incoming_stack_alignment = cc.incoming_stack_alignment.toByteUnits(),
1261712664 .save = @enumFromInt(cc.extra),
1261812665 },
12666 std.lang.CallingConvention.SpirvKernelOptions => .{
12667 .x = trailing[0],
12668 .y = trailing[1],
12669 .z = trailing[2],
12670 },
12671 std.lang.CallingConvention.SpirvFragmentOptions => .{
12672 .pixel_centered_integer = @bitCast(@as(u1, @truncate(cc.extra))),
12673 .depth_assumption = @enumFromInt(@as(u2, @truncate(cc.extra >> 1))),
12674 },
12675 std.lang.CallingConvention.SpirvMeshOptions => .{
12676 .stage_output = @enumFromInt(cc.extra),
12677 .max_primitives = trailing[0],
12678 .max_vertices = trailing[1],
12679 },
1261912680 else => comptime unreachable,
1262012681 },
1262112682 ),
src/Sema.zig+23-1
......@@ -8599,6 +8599,7 @@ fn checkReturnTypeAndCallConv(
85998599) CompileError!void {
86008600 const pt = sema.pt;
86018601 const zcu = pt.zcu;
8602 const target = zcu.getTarget();
86028603 if (opt_varargs_src) |varargs_src| {
86038604 try sema.checkCallConvSupportsVarArgs(block, varargs_src, @"callconv");
86048605 }
......@@ -8660,6 +8661,21 @@ fn checkReturnTypeAndCallConv(
86608661 .@"inline" => if (is_noinline) {
86618662 return sema.fail(block, callconv_src, "'noinline' function cannot have calling convention 'inline'", .{});
86628663 },
8664 .spirv_fragment => |fragment| {
8665 if (fragment.pixel_centered_integer and target.os.tag != .opengl) {
8666 return sema.fail(block, callconv_src, "'pixel_centered_integer' is not supported on this target", .{});
8667 }
8668 },
8669 .spirv_kernel, .spirv_task => |kernel| {
8670 if (kernel.x == 0 or kernel.y == 0 or kernel.z == 0) {
8671 return sema.fail(block, callconv_src, "kernel workgroup dimensions must be at least 1", .{});
8672 }
8673 },
8674 .spirv_mesh => |mesh| {
8675 if (mesh.max_vertices == 0 or mesh.max_primitives == 0) {
8676 return sema.fail(block, callconv_src, "mesh shader 'max_vertices' and 'max_primitives' must be at least 1", .{});
8677 }
8678 },
86638679 else => {},
86648680 }
86658681 switch (zcu.callconvSupported(@"callconv")) {
......@@ -8770,6 +8786,8 @@ fn callConvIsCallable(cc: std.lang.CallingConvention.Tag) bool {
87708786 .spirv_kernel,
87718787 .spirv_fragment,
87728788 .spirv_vertex,
8789 .spirv_task,
8790 .spirv_mesh,
87738791 => false,
87748792
87758793 else => true,
......@@ -29127,7 +29145,7 @@ fn callconvCoerceAllowed(
2912729145 switch (src_cc) {
2912829146 inline else => |src_data, tag| {
2912929147 const dest_data = @field(dest_cc, @tagName(tag));
29130 if (@TypeOf(src_data) != void) {
29148 if (@TypeOf(src_data) != void and @hasField(@TypeOf(src_data), "incoming_stack_alignment")) {
2913129149 const default_stack_align = target.stackAlignment();
2913229150 const src_stack_align = src_data.incoming_stack_alignment orelse default_stack_align;
2913329151 const dest_stack_align = dest_data.incoming_stack_alignment orelse default_stack_align;
......@@ -29156,6 +29174,10 @@ fn callconvCoerceAllowed(
2915629174 std.lang.CallingConvention.ShInterruptOptions => {
2915729175 if (src_data.save != dest_data.save) return false;
2915829176 },
29177 std.lang.CallingConvention.SpirvKernelOptions,
29178 std.lang.CallingConvention.SpirvFragmentOptions,
29179 std.lang.CallingConvention.SpirvMeshOptions,
29180 => {},
2915929181 else => comptime unreachable,
2916029182 }
2916129183 },
src/Zcu.zig+1
......@@ -4699,6 +4699,7 @@ pub fn callconvSupported(zcu: *Zcu, cc: std.lang.CallingConvention) union(enum)
46994699 .stage2_spirv => switch (cc) {
47004700 .spirv_device, .spirv_kernel => true,
47014701 .spirv_fragment, .spirv_vertex => target.os.tag == .vulkan or target.os.tag == .opengl,
4702 .spirv_task, .spirv_mesh => target.os.tag == .vulkan,
47024703 else => false,
47034704 },
47044705 };
src/codegen/llvm.zig+6
......@@ -4445,6 +4445,10 @@ pub fn toLlvmCallConv(cc: std.lang.CallingConvention, target: *const std.Target)
44454445 std.lang.CallingConvention.CommonOptions,
44464446 => .{ pl.incoming_stack_alignment, 0, 0 },
44474447 std.lang.CallingConvention.X86RegparmOptions => .{ pl.incoming_stack_alignment, pl.register_params, 0 },
4448 std.lang.CallingConvention.SpirvKernelOptions,
4449 std.lang.CallingConvention.SpirvFragmentOptions,
4450 std.lang.CallingConvention.SpirvMeshOptions,
4451 => .{ null, 0, 0 },
44484452 else => @compileError("TODO: toLlvmCallConv" ++ @tagName(pl)),
44494453 },
44504454 };
......@@ -4588,6 +4592,8 @@ pub fn toLlvmCallConvTag(cc_tag: std.lang.CallingConvention.Tag, target: *const
45884592 .spirv_kernel,
45894593 .spirv_fragment,
45904594 .spirv_vertex,
4595 .spirv_task,
4596 .spirv_mesh,
45914597 => null,
45924598 };
45934599}
src/codegen/spirv/CodeGen.zig+3-16
......@@ -1442,6 +1442,8 @@ fn resolveType(cg: *CodeGen, ty: Type, repr: Repr) Error!Id {
14421442 .spirv_fragment,
14431443 .spirv_vertex,
14441444 .spirv_device,
1445 .spirv_task,
1446 .spirv_mesh,
14451447 => {},
14461448 else => unreachable,
14471449 }
......@@ -2542,15 +2544,6 @@ fn generateTestEntryPoint(
25422544 cg.module.error_buffer = spv_err_decl_index;
25432545 }
25442546
2545 try cg.module.sections.execution_modes.emit(gpa, .OpExecutionMode, .{
2546 .entry_point = kernel_id,
2547 .mode = .{ .local_size = .{
2548 .x_size = 1,
2549 .y_size = 1,
2550 .z_size = 1,
2551 } },
2552 });
2553
25542547 const void_ty_id = try cg.resolveType(.void, .direct);
25552548 const kernel_proto_ty_id = try cg.module.functionType(void_ty_id, &.{});
25562549 try section.emit(gpa, .OpFunction, .{
......@@ -2599,13 +2592,7 @@ fn generateTestEntryPoint(
25992592 // point name is the same as a different OpName.
26002593 const test_name = try std.fmt.allocPrint(cg.module.arena, "test {s}", .{name});
26012594
2602 const execution_mode: spec.ExecutionModel = switch (target.os.tag) {
2603 .vulkan, .opengl => .gl_compute,
2604 .opencl, .amdhsa => .kernel,
2605 else => unreachable,
2606 };
2607
2608 try cg.module.declareEntryPoint(spv_decl_index, test_name, execution_mode, null);
2595 try cg.module.declareEntryPoint(spv_decl_index, test_name, .{ .spirv_kernel = .{ .x = 1, .y = 1, .z = 1 } });
26092596}
26102597
26112598fn intFromBool(cg: *CodeGen, value: Temporary, result_ty: Type) !Temporary {
src/codegen/spirv/Module.zig+76-20
......@@ -132,15 +132,10 @@ pub const Decl = struct {
132132 end_dep: usize = 0,
133133};
134134
135/// This models a kernel entry point.
136135pub const EntryPoint = struct {
137 /// The declaration that should be exported.
138136 decl_index: Decl.Index,
139 /// The name of the kernel to be exported.
140137 name: []const u8,
141 /// Calling Convention
142 exec_model: spec.ExecutionModel,
143 exec_mode: ?spec.ExecutionMode = null,
138 cc: std.builtin.CallingConvention,
144139};
145140
146141const StructType = struct {
......@@ -320,25 +315,89 @@ fn entryPoints(module: *Module) !Section {
320315 interface.items.len = 0;
321316 seen.setRangeValue(.{ .start = 0, .end = module.decls.items.len }, false);
322317
318 const exec_model: spec.ExecutionModel = switch (target.os.tag) {
319 .vulkan, .opengl => switch (entry_point.cc) {
320 .spirv_vertex => .vertex,
321 .spirv_fragment => .fragment,
322 .spirv_kernel => .gl_compute,
323 .spirv_task => .task_ext,
324 .spirv_mesh => .mesh_ext,
325 // TODO: We should integrate with the Linkage capability and export this function
326 .spirv_device => continue,
327 else => unreachable,
328 },
329 .opencl => switch (entry_point.cc) {
330 .spirv_kernel => .kernel,
331 // TODO: We should integrate with the Linkage capability and export this function
332 .spirv_device => continue,
333 else => unreachable,
334 },
335 else => unreachable,
336 };
323337 try module.addEntryPointDeps(entry_point.decl_index, &seen, &interface);
324338 try entry_points.emit(module.gpa, .OpEntryPoint, .{
325 .execution_model = entry_point.exec_model,
339 .execution_model = exec_model,
326340 .entry_point = entry_point_id,
327341 .name = entry_point.name,
328342 .interface = interface.items,
329343 });
330344
331 if (entry_point.exec_mode == null and entry_point.exec_model == .fragment) {
332 switch (target.os.tag) {
333 .vulkan, .opengl => |tag| {
345 switch (entry_point.cc) {
346 .spirv_kernel, .spirv_task => |kernel| {
347 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
348 .entry_point = entry_point_id,
349 .mode = .{ .local_size = .{
350 .x_size = kernel.x,
351 .y_size = kernel.y,
352 .z_size = kernel.z,
353 } },
354 });
355 },
356 .spirv_fragment => |fragment| {
357 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
358 .entry_point = entry_point_id,
359 .mode = if (target.os.tag == .vulkan) .origin_upper_left else .origin_lower_left,
360 });
361 if (fragment.pixel_centered_integer) {
334362 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
335363 .entry_point = entry_point_id,
336 .mode = if (tag == .vulkan) .origin_upper_left else .origin_lower_left,
364 .mode = .pixel_center_integer,
337365 });
338 },
339 .opencl => {},
340 else => unreachable,
341 }
366 }
367
368 const exec_mode: ?spec.ExecutionMode.Extended = switch (fragment.depth_assumption) {
369 .none => null,
370 .greater => .depth_greater,
371 .less => .depth_less,
372 .unchanged => .depth_unchanged,
373 };
374 if (exec_mode) |mode| {
375 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
376 .entry_point = entry_point_id,
377 .mode = mode,
378 });
379 }
380 },
381 .spirv_mesh => |mesh| {
382 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
383 .entry_point = entry_point_id,
384 .mode = .{ .output_vertices = .{ .vertex_count = mesh.max_vertices } },
385 });
386 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
387 .entry_point = entry_point_id,
388 .mode = .{ .output_primitives_ext = .{ .primitive_count = mesh.max_primitives } },
389 });
390
391 try module.sections.execution_modes.emit(module.gpa, .OpExecutionMode, .{
392 .entry_point = entry_point_id,
393 .mode = switch (mesh.stage_output) {
394 .output_points => .output_points,
395 .output_lines => .output_lines_ext,
396 .output_triangles => .output_triangles_ext,
397 },
398 });
399 },
400 else => {}, // TODO: should this be unreachable?
342401 }
343402 }
344403
......@@ -925,15 +984,12 @@ pub fn declareEntryPoint(
925984 module: *Module,
926985 decl_index: Decl.Index,
927986 name: []const u8,
928 exec_model: spec.ExecutionModel,
929 exec_mode: ?spec.ExecutionMode,
987 cc: std.builtin.CallingConvention,
930988) !void {
931989 const gop = try module.entry_points.getOrPut(module.gpa, module.declPtr(decl_index).result_id);
932990 gop.value_ptr.decl_index = decl_index;
933991 gop.value_ptr.name = name;
934 gop.value_ptr.exec_model = exec_model;
935 // Might've been set by assembler
936 if (!gop.found_existing) gop.value_ptr.exec_mode = exec_mode;
992 gop.value_ptr.cc = cc;
937993}
938994
939995pub fn debugName(module: *Module, target: Id, name: []const u8) !void {
src/link/SpirV.zig+2-20
......@@ -179,35 +179,17 @@ pub fn updateExports(
179179 },
180180 };
181181 const nav_ty = ip.getNav(nav_index).resolved.?.type;
182 const target = zcu.getTarget();
183182 if (ip.isFunctionType(nav_ty)) {
184183 const spv_decl_index = try linker.module.resolveNav(ip, nav_index);
185184 const cc = Type.fromInterned(nav_ty).fnCallingConvention(zcu);
186 const exec_model: spec.ExecutionModel = switch (target.os.tag) {
187 .vulkan, .opengl => switch (cc) {
188 .spirv_vertex => .vertex,
189 .spirv_fragment => .fragment,
190 .spirv_kernel => .gl_compute,
191 // TODO: We should integrate with the Linkage capability and export this function
192 .spirv_device => return,
193 else => unreachable,
194 },
195 .opencl => switch (cc) {
196 .spirv_kernel => .kernel,
197 // TODO: We should integrate with the Linkage capability and export this function
198 .spirv_device => return,
199 else => unreachable,
200 },
201 else => unreachable,
202 };
185 if (cc == .spirv_device) return;
203186
204187 for (export_indices) |export_idx| {
205188 const exp = export_idx.ptr(zcu);
206189 try linker.module.declareEntryPoint(
207190 spv_decl_index,
208191 exp.opts.name.toSlice(ip),
209 exec_model,
210 null,
192 cc,
211193 );
212194 }
213195 }
test/cases/callconv_spirv.zig created+11
......@@ -0,0 +1,11 @@
1export fn vert() callconv(.spirv_vertex) void {}
2export fn frag() callconv(.{ .spirv_fragment = .{ .depth_assumption = .greater } }) void {}
3export fn comp() callconv(.{ .spirv_kernel = .{ .x = 8, .y = 8, .z = 1 } }) void {}
4export fn task() callconv(.{ .spirv_task = .{ .x = 1, .y = 1, .z = 1 } }) void {}
5export fn mesh() callconv(.{ .spirv_mesh = .{ .stage_output = .output_lines, .max_primitives = 1, .max_vertices = 2 } }) void {}
6
7// compile
8// output_mode=Obj
9// backend=selfhosted
10// target=spirv64-vulkan
11// emit_bin=false
test/cases/compile_errors/callconv_spirv_invalid_options.zig created+29
......@@ -0,0 +1,29 @@
1const F1 = fn () callconv(.{ .spirv_kernel = .{ .x = 0, .y = 1, .z = 1 } }) void;
2const F2 = fn () callconv(.{ .spirv_task = .{ .x = 1, .y = 0, .z = 1 } }) void;
3const F3 = fn () callconv(.{ .spirv_mesh = .{ .max_vertices = 0 } }) void;
4const F4 = fn () callconv(.{ .spirv_fragment = .{ .pixel_centered_integer = true } }) void;
5export fn entry1() void {
6 const a: F1 = undefined;
7 _ = a;
8}
9export fn entry2() void {
10 const a: F2 = undefined;
11 _ = a;
12}
13export fn entry3() void {
14 const a: F3 = undefined;
15 _ = a;
16}
17export fn entry4() void {
18 const a: F4 = undefined;
19 _ = a;
20}
21
22// error
23// backend=selfhosted
24// target=spirv64-vulkan
25//
26// :1:28: error: kernel workgroup dimensions must be at least 1
27// :2:28: error: kernel workgroup dimensions must be at least 1
28// :3:28: error: mesh shader 'max_vertices' and 'max_primitives' must be at least 1
29// :4:28: error: 'pixel_centered_integer' is not supported on this target
test/cases/compile_errors/callconv_spirv_mesh_task_require_vulkan.zig created+17
......@@ -0,0 +1,17 @@
1const F1 = fn () callconv(.{ .spirv_task = .{ .x = 1, .y = 1, .z = 1 } }) void;
2const F2 = fn () callconv(.{ .spirv_mesh = .{} }) void;
3export fn entry1() void {
4 const a: F1 = undefined;
5 _ = a;
6}
7export fn entry2() void {
8 const a: F2 = undefined;
9 _ = a;
10}
11
12// error
13// backend=selfhosted
14// target=spirv64-opengl
15//
16// :1:28: error: calling convention 'spirv_task' not supported by compiler backend 'stage2_spirv'
17// :2:28: error: calling convention 'spirv_mesh' not supported by compiler backend 'stage2_spirv'
test/cases/compile_errors/callconv_spirv_on_unsupported_platform.zig created+29
......@@ -0,0 +1,29 @@
1const F1 = fn () callconv(.{ .spirv_fragment = .{} }) void;
2const F2 = fn () callconv(.spirv_vertex) void;
3const F3 = fn () callconv(.{ .spirv_task = .{ .x = 1, .y = 1, .z = 1 } }) void;
4const F4 = fn () callconv(.{ .spirv_mesh = .{} }) void;
5export fn entry1() void {
6 const a: F1 = undefined;
7 _ = a;
8}
9export fn entry2() void {
10 const a: F2 = undefined;
11 _ = a;
12}
13export fn entry3() void {
14 const a: F3 = undefined;
15 _ = a;
16}
17export fn entry4() void {
18 const a: F4 = undefined;
19 _ = a;
20}
21
22// error
23// backend=selfhosted
24// target=spirv64-opencl
25//
26// :1:28: error: calling convention 'spirv_fragment' not supported by compiler backend 'stage2_spirv'
27// :2:28: error: calling convention 'spirv_vertex' not supported by compiler backend 'stage2_spirv'
28// :3:28: error: calling convention 'spirv_task' not supported by compiler backend 'stage2_spirv'
29// :4:28: error: calling convention 'spirv_mesh' not supported by compiler backend 'stage2_spirv'