From a7563e453dce0cc256a0c40af434731f2cf7dcaf Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Sat, 8 Apr 2023 19:02:31 +0200 Subject: [PATCH] spirv: minimal start code For SPIR-V, only export the main function if it is actually declared. Kernel entry points will often have parameters and more than one kernel declared. In general, SPIR-V binaries should mostly be compiled as libraries and not as executables. However, this start code is required so that we can build test executables. Note that a call to isSpirV() would emit the code for that function, even though the call is at comptime. To save that function from being emitted the checks are just inlined manually. --- lib/std/start.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/lib/std/start.zig b/lib/std/start.zig index 1b686e43f5..377a317df2 100644 --- a/lib/std/start.zig +++ b/lib/std/start.zig @@ -24,7 +24,9 @@ pub const simplified_logic = builtin.zig_backend == .stage2_aarch64 or builtin.zig_backend == .stage2_arm or builtin.zig_backend == .stage2_riscv64 or - builtin.zig_backend == .stage2_sparc64; + builtin.zig_backend == .stage2_sparc64 or + builtin.cpu.arch == .spirv32 or + builtin.cpu.arch == .spirv64; comptime { // No matter what, we import the root file, so that any export, test, comptime @@ -43,6 +45,9 @@ comptime { } } else if (builtin.os.tag == .wasi and @hasDecl(root, "main")) { @export(wasiMain2, .{ .name = "_start" }); + } else if (builtin.os.tag == .opencl) { + if (@hasDecl(root, "main")) + @export(spirvMain2, .{ .name = "main" }); } else { if (!@hasDecl(root, "_start")) { @export(_start2, .{ .name = "_start" }); @@ -127,6 +132,10 @@ fn wasiMain2() callconv(.C) noreturn { } } +fn spirvMain2() callconv(.Kernel) void { + root.main(); +} + fn wWinMainCRTStartup2() callconv(.C) noreturn { root.main(); exit2(0);