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 {
3131
3232 pub fn init(allocator: *Allocator) SPIRVModule {
3333 return .{
34 .next_result_id = 0,
34 .next_result_id = 1, // 0 is an invalid SPIR-V result ID.
3535 .types_and_globals = std.ArrayList(u32).init(allocator),
3636 .fn_decls = std.ArrayList(u32).init(allocator),
3737 };
......@@ -146,17 +146,14 @@ pub const DeclGen = struct {
146146 16 => Target.spirv.featureSetHas(target.cpu.features, .Float16),
147147 32 => true,
148148 64 => Target.spirv.featureSetHas(target.cpu.features, .Float64),
149 else => false
149 else => false,
150150 };
151151
152152 if (!supported) {
153153 return self.fail(.{.node_offset = 0}, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{ bits });
154154 }
155155
156 try writeInstruction(code, .OpTypeFloat, &.{
157 result_id,
158 bits
159 });
156 try writeInstruction(code, .OpTypeFloat, &[_]u32{ result_id, bits });
160157 },
161158 .Fn => {
162159 // We only support zig-calling-convention functions, no varargs.
......@@ -195,7 +192,7 @@ pub const DeclGen = struct {
195192
196193 .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 }),
199196 }
200197
201198 try self.types.put(ty, result_id);
......@@ -203,11 +200,23 @@ pub const DeclGen = struct {
203200 }
204201
205202 pub fn gen(self: *DeclGen) !void {
203 const result_id = self.decl.fn_link.spirv.id;
206204 const tv = self.decl.typed_value.most_recent.typed_value;
207205
208206 if (tv.val.castTag(.function)) |func_payload| {
209207 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{});
211220 } else {
212221 return self.fail(.{.node_offset = 0}, "TODO: SPIR-V backend: generate decl type {}", .{ tv.ty.zigTypeTag() });
213222 }