authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-15 03:14:12+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-16 14:13:23+02:00
log074cb9f1daff0f9d8c3bc5736b8990aa53eb8226
treef685134133047e0eda7701b1f0fcf1e759587248
parent4403f3598a97d93466c62a91cf3a5aacc6d113a6

SPIR-V: OpFunction/OpFunctionEnd generation


1 files changed, 17 insertions(+), 8 deletions(-)

src/codegen/spirv.zig+17-8
...@@ -31,7 +31,7 @@ pub const SPIRVModule = struct {...@@ -31,7 +31,7 @@ pub const SPIRVModule = struct {
3131
32 pub fn init(allocator: *Allocator) SPIRVModule {32 pub fn init(allocator: *Allocator) SPIRVModule {
33 return .{33 return .{
34 .next_result_id = 0,34 .next_result_id = 1, // 0 is an invalid SPIR-V result ID.
35 .types_and_globals = std.ArrayList(u32).init(allocator),35 .types_and_globals = std.ArrayList(u32).init(allocator),
36 .fn_decls = std.ArrayList(u32).init(allocator),36 .fn_decls = std.ArrayList(u32).init(allocator),
37 };37 };
...@@ -146,17 +146,14 @@ pub const DeclGen = struct {...@@ -146,17 +146,14 @@ pub const DeclGen = struct {
146 16 => Target.spirv.featureSetHas(target.cpu.features, .Float16),146 16 => Target.spirv.featureSetHas(target.cpu.features, .Float16),
147 32 => true,147 32 => true,
148 64 => Target.spirv.featureSetHas(target.cpu.features, .Float64),148 64 => Target.spirv.featureSetHas(target.cpu.features, .Float64),
149 else => false149 else => false,
150 };150 };
151151
152 if (!supported) {152 if (!supported) {
153 return self.fail(.{.node_offset = 0}, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{ bits });153 return self.fail(.{.node_offset = 0}, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{ bits });
154 }154 }
155155
156 try writeInstruction(code, .OpTypeFloat, &.{156 try writeInstruction(code, .OpTypeFloat, &[_]u32{ result_id, bits });
157 result_id,
158 bits
159 });
160 },157 },
161 .Fn => {158 .Fn => {
162 // We only support zig-calling-convention functions, no varargs.159 // We only support zig-calling-convention functions, no varargs.
...@@ -195,7 +192,7 @@ pub const DeclGen = struct {...@@ -195,7 +192,7 @@ pub const DeclGen = struct {
195192
196 .BoundFn => unreachable, // this type will be deleted from the language.193 .BoundFn => unreachable, // this type will be deleted from the language.
197194
198 else => |tag| return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type {}", .{ tag }),195 else => |tag| return self.fail(.{.node_offset = 0}, "TODO: SPIR-V backend: implement type {}", .{ tag }),
199 }196 }
200197
201 try self.types.put(ty, result_id);198 try self.types.put(ty, result_id);
...@@ -203,11 +200,23 @@ pub const DeclGen = struct {...@@ -203,11 +200,23 @@ pub const DeclGen = struct {
203 }200 }
204201
205 pub fn gen(self: *DeclGen) !void {202 pub fn gen(self: *DeclGen) !void {
203 const result_id = self.decl.fn_link.spirv.id;
206 const tv = self.decl.typed_value.most_recent.typed_value;204 const tv = self.decl.typed_value.most_recent.typed_value;
207205
208 if (tv.val.castTag(.function)) |func_payload| {206 if (tv.val.castTag(.function)) |func_payload| {
209 std.debug.assert(tv.ty.zigTypeTag() == .Fn);207 std.debug.assert(tv.ty.zigTypeTag() == .Fn);
210 _ = try self.getOrGenType(tv.ty);208 const prototype_id = try self.getOrGenType(tv.ty);
209 try writeInstruction(&self.spv.fn_decls, .OpFunction, &[_]u32{
210 self.types.get(tv.ty.fnReturnType()).?, // This type should be generated along with the prototype.
211 result_id,
212 @bitCast(u32, spec.FunctionControl{}), // TODO: We can set inline here if the type requires it.
213 prototype_id,
214 });
215
216 // TODO: Parameters
217 // TODO: Body
218
219 try writeInstruction(&self.spv.fn_decls, .OpFunctionEnd, &[_]u32{});
211 } else {220 } else {
212 return self.fail(.{.node_offset = 0}, "TODO: SPIR-V backend: generate decl type {}", .{ tv.ty.zigTypeTag() });221 return self.fail(.{.node_offset = 0}, "TODO: SPIR-V backend: generate decl type {}", .{ tv.ty.zigTypeTag() });
213 }222 }