Commit a7563e453d

Robin Voetter <robin@voetter.nl>
2023-04-08 19:02:31
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.
1 parent 45b5f46
Changed files (1)
lib
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);