Commit 02c138fe70

Robin Voetter <robin@voetter.nl>
2021-01-18 23:47:25
SPIR-V: Add glsl450 and vulkan spir-v operating system definitions
1 parent b2b87b5
Changed files (6)
lib/std/zig/cross_target.zig
@@ -131,6 +131,8 @@ pub const CrossTarget = struct {
             .emscripten,
             .uefi,
             .opencl,
+            .glsl450,
+            .vulkan,
             .other,
             => {
                 self.os_version_min = .{ .none = {} };
@@ -732,6 +734,8 @@ pub const CrossTarget = struct {
             .emscripten,
             .uefi,
             .opencl,
+            .glsl450,
+            .vulkan,
             .other,
             => return error.InvalidOperatingSystemVersion,
 
lib/std/target.zig
@@ -57,7 +57,9 @@ pub const Target = struct {
             wasi,
             emscripten,
             uefi,
-            opencl, // SPIR-V on OpenCL
+            opencl,
+            glsl450,
+            vulkan,
             other,
 
             pub fn isDarwin(tag: Tag) bool {
@@ -249,7 +251,9 @@ pub const Target = struct {
                     .wasi,
                     .emscripten,
                     .uefi,
-                    .opencl,
+                    .opencl, // TODO: OpenCL versions
+                    .glsl450, // TODO: GLSL versions
+                    .vulkan,
                     .other,
                     => return .{ .none = {} },
 
@@ -406,6 +410,8 @@ pub const Target = struct {
                 .emscripten,
                 .uefi,
                 .opencl,
+                .glsl450,
+                .vulkan,
                 .other,
                 => false,
             };
@@ -497,7 +503,9 @@ pub const Target = struct {
                 .wasi,
                 .emscripten,
                 => return .musl,
-                .opencl, // TODO: Where should this go?
+                .opencl, // TODO: SPIR-V ABIs with Linkage capability
+                .glsl450,
+                .vulkan,
                 => return .none,
             }
         }
@@ -1367,6 +1375,8 @@ pub const Target = struct {
             .windows,
             .emscripten,
             .opencl,
+            .glsl450,
+            .vulkan,
             .other,
             => return false,
             else => return true,
@@ -1547,6 +1557,8 @@ pub const Target = struct {
             .emscripten,
             .wasi,
             .opencl,
+            .glsl450,
+            .vulkan,
             .other,
             => return result,
 
src/codegen/llvm.zig
@@ -69,6 +69,8 @@ pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 {
         .renderscript64 => "renderscript64",
         .ve => "ve",
         .spu_2 => return error.LLVMBackendDoesNotSupportSPUMarkII,
+        .spirv32 => return error.LLVMBackendDoesNotSupportSPIRV,
+        .spirv64 => return error.LLVMBackendDoesNotSupportSPIRV,
     };
     // TODO Add a sub-arch for some architectures depending on CPU features.
 
@@ -109,6 +111,9 @@ pub fn targetTriple(allocator: *Allocator, target: std.Target) ![:0]u8 {
         .wasi => "wasi",
         .emscripten => "emscripten",
         .uefi => "windows",
+        .opencl => return error.LLVMBackendDoesNotSupportOpenCL,
+        .glsl450 => return error.LLVMBackendDoesNotSupportGLSL450,
+        .vulkan => return error.LLVMBackendDoesNotSupportVulkan,
         .other => "unknown",
     };
 
src/link/SpirV.zig
@@ -12,6 +12,8 @@ const trace = @import("../tracy.zig").trace;
 const build_options = @import("build_options");
 const spec = @import("../codegen/spirv/spec.zig");
 
+//! SPIR-V Documentation: https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html
+
 pub const FnData = struct {
     id: ?u32 = null,
     code: std.ArrayListUnmanaged(u32) = .{},
@@ -32,6 +34,22 @@ pub fn createEmpty(gpa: *Allocator, options: link.Options) !*SpirV {
             .allocator = gpa,
         },
     };
+
+    // TODO: Figure out where to put all of these
+    switch (options.target.cpu.arch) {
+        .spirv32, .spirv64 => {},
+        else => return error.TODOArchNotSupported,
+    }
+
+    switch (options.target.os.tag) {
+        .opencl, .glsl450, .vulkan => {},
+        else => return error.TODOOsNotSupported,
+    }
+
+    if (options.target.abi != .none) {
+        return error.TODOAbiNotSupported;
+    }
+
     return spirv;
 }
 
@@ -119,6 +137,8 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void {
         switch (decl.typed_value) {
             .most_recent => |tvm| {
                 const fn_data = &decl.fn_link.spirv;
+
+                // TODO: This could probably be more efficient.
                 for (fn_data.code.items) |word| {
                     try writer.writeIntLittle(u32, word);
                 }
src/target.zig
@@ -189,7 +189,7 @@ pub fn supportsStackProbing(target: std.Target) bool {
 
 pub fn osToLLVM(os_tag: std.Target.Os.Tag) llvm.OSType {
     return switch (os_tag) {
-        .freestanding, .other => .UnknownOS,
+        .freestanding, .other, .opencl, .glsl450, .vulkan => .UnknownOS,
         .windows, .uefi => .Win32,
         .ananas => .Ananas,
         .cloudabi => .CloudABI,
@@ -280,7 +280,7 @@ pub fn archToLLVM(arch_tag: std.Target.Cpu.Arch) llvm.ArchType {
         .renderscript32 => .renderscript32,
         .renderscript64 => .renderscript64,
         .ve => .ve,
-        .spu_2 => .UnknownArch,
+        .spu_2, .spirv32, .spirv64 => .UnknownArch,
     };
 }
 
src/type.zig
@@ -3598,6 +3598,8 @@ pub const CType = enum {
             .hermit,
             .hurd,
             .opencl,
+            .glsl450,
+            .vulkan,
             => @panic("TODO specify the C integer and float type sizes for this OS"),
         }
     }