diff options
| author | Robin Voetter <robin@voetter.nl> | 2021-05-15 01:04:02 +0200 |
|---|---|---|
| committer | Robin Voetter <robin@voetter.nl> | 2021-05-16 14:13:23 +0200 |
| commit | de6df2bc12c1cec81bb3c562a9395098c92d8239 (patch) | |
| tree | bb12821965274723ae2ddec27657650498967e9f /src | |
| parent | 6461b9516371d22d347c27b5fcd2e8c22fafd03d (diff) | |
| download | zig-de6df2bc12c1cec81bb3c562a9395098c92d8239.tar.gz zig-de6df2bc12c1cec81bb3c562a9395098c92d8239.zip | |
SPIR-V: Restructure codegen a bit
Diffstat (limited to 'src')
| -rw-r--r-- | src/codegen/spirv.zig | 64 | ||||
| -rw-r--r-- | src/link/SpirV.zig | 36 |
2 files changed, 68 insertions, 32 deletions
diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 077e71d4e1..9ceaf107d8 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -6,6 +6,7 @@ const spec = @import("spirv/spec.zig"); const Module = @import("../Module.zig"); const Decl = Module.Decl; const Type = @import("../type.zig").Type; +const LazySrcLoc = Module.LazySrcLoc; pub const TypeMap = std.HashMap(Type, u32, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); @@ -15,30 +16,24 @@ pub fn writeInstruction(code: *std.ArrayList(u32), instr: spec.Opcode, args: []c try code.appendSlice(args); } +/// This structure represents a SPIR-V binary module being compiled, and keeps track of relevant information +/// such as code for the different logical sections, and the next result-id. pub const SPIRVModule = struct { - next_result_id: u32 = 0, - - target: std.Target, - - types: TypeMap, - + next_result_id: u32, types_and_globals: std.ArrayList(u32), fn_decls: std.ArrayList(u32), - pub fn init(target: std.Target, allocator: *Allocator) SPIRVModule { + pub fn init(allocator: *Allocator) SPIRVModule { return .{ - .target = target, - .types = TypeMap.init(allocator), + .next_result_id = 0, .types_and_globals = std.ArrayList(u32).init(allocator), .fn_decls = std.ArrayList(u32).init(allocator), }; } pub fn deinit(self: *SPIRVModule) void { - self.fn_decls.deinit(); self.types_and_globals.deinit(); - self.types.deinit(); - self.* = undefined; + self.fn_decls.deinit(); } pub fn allocResultId(self: *SPIRVModule) u32 { @@ -49,21 +44,40 @@ pub const SPIRVModule = struct { pub fn resultIdBound(self: *SPIRVModule) u32 { return self.next_result_id; } +}; + +/// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. +pub const DeclGen = struct { + module: *Module, + spv: *SPIRVModule, + + types: TypeMap, + + decl: *Decl, + error_msg: ?*Module.ErrorMsg, + + fn fail(self: *DeclGen, src: LazySrcLoc, comptime format: []const u8, args: anytype) error{ AnalysisFail, OutOfMemory } { + @setCold(true); + const src_loc = src.toSrcLocWithDecl(self.decl); + self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, format, args); + return error.AnalysisFail; + } - pub fn getOrGenType(self: *SPIRVModule, t: Type) !u32 { + pub fn getOrGenType(self: *DeclGen, t: Type) !u32 { // We can't use getOrPut here so we can recursively generate types. if (self.types.get(t)) |already_generated| { return already_generated; } - const result = self.allocResultId(); + const result = self.spv.allocResultId(); switch (t.zigTypeTag()) { - .Void => try writeInstruction(&self.types_and_globals, .OpTypeVoid, &[_]u32{ result }), - .Bool => try writeInstruction(&self.types_and_globals, .OpTypeBool, &[_]u32{ result }), + .Void => try writeInstruction(&self.spv.types_and_globals, .OpTypeVoid, &[_]u32{ result }), + .Bool => try writeInstruction(&self.spv.types_and_globals, .OpTypeBool, &[_]u32{ result }), .Int => { - const int_info = t.intInfo(self.target); - try writeInstruction(&self.types_and_globals, .OpTypeInt, &[_]u32{ + const int_info = t.intInfo(self.module.getTarget()); + // TODO: Capabilities. + try writeInstruction(&self.spv.types_and_globals, .OpTypeInt, &[_]u32{ result, int_info.bits, switch (int_info.signedness) { @@ -72,8 +86,8 @@ pub const SPIRVModule = struct { }, }); }, - // TODO: Verify that floatBits() will be correct. - .Float => try writeInstruction(&self.types_and_globals, .OpTypeFloat, &[_]u32{ result, t.floatBits(self.target) }), + // TODO: Capabilities. + .Float => try writeInstruction(&self.spv.types_and_globals, .OpTypeFloat, &[_]u32{ result, t.floatBits(self.module.getTarget()) }), .Null, .Undefined, .EnumLiteral, @@ -84,23 +98,23 @@ pub const SPIRVModule = struct { .BoundFn => unreachable, // this type will be deleted from the language. - else => return error.TODO, + else => |tag| return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type with tag {}", .{ tag }), } try self.types.put(t, result); return result; } - pub fn gen(self: *SPIRVModule, decl: *Decl) !void { - const typed_value = decl.typed_value.most_recent.typed_value; + pub fn gen(self: *DeclGen) !void { + const typed_value = self.decl.typed_value.most_recent.typed_value; switch (typed_value.ty.zigTypeTag()) { .Fn => { - log.debug("Generating code for function '{s}'", .{ std.mem.spanZ(decl.name) }); + log.debug("Generating code for function '{s}'", .{ std.mem.spanZ(self.decl.name) }); _ = try self.getOrGenType(typed_value.ty.fnReturnType()); }, - else => return error.TODO, + else => |tag| return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: generate decl with tag {}", .{ tag }), } } }; diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 95c747c170..8d5feafa5a 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -118,8 +118,8 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { const module = self.base.options.module.?; const target = comp.getTarget(); - var spirv_module = codegen.SPIRVModule.init(target, self.base.allocator); - defer spirv_module.deinit(); + var spv = codegen.SPIRVModule.init(self.base.allocator); + defer spv.deinit(); // Allocate an ID for every declaration before generating code, // so that we can access them before processing them. @@ -132,19 +132,41 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { if (decl.typed_value != .most_recent) continue; - decl.fn_link.spirv.id = spirv_module.allocResultId(); + decl.fn_link.spirv.id = spv.allocResultId(); log.debug("Allocating id {} to '{s}'", .{ decl.fn_link.spirv.id, std.mem.spanZ(decl.name) }); } } // Now, actually generate the code for all declarations. { + // We are just going to re-use this same DeclGen for every Decl, and we are just going to + // change the decl. Otherwise, we would have to keep a separate `types`, and re-construct this + // structure every time. + var decl_gen = codegen.DeclGen{ + .module = module, + .spv = &spv, + .types = codegen.TypeMap.init(self.base.allocator), + .decl = undefined, + .error_msg = undefined, + }; + + defer decl_gen.types.deinit(); + for (module.decl_table.items()) |entry| { const decl = entry.value; if (decl.typed_value != .most_recent) continue; - try spirv_module.gen(decl); + decl_gen.decl = decl; + decl_gen.error_msg = null; + + decl_gen.gen() catch |err| switch (err) { + error.AnalysisFail => { + try module.failed_decls.put(module.gpa, decl, decl_gen.error_msg.?); + return; + }, + else => |e| return e, + }; } } @@ -155,7 +177,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { spec.magic_number, (spec.version.major << 16) | (spec.version.minor << 8), 0, // TODO: Register Zig compiler magic number. - spirv_module.resultIdBound(), // ID bound. + spv.resultIdBound(), // ID bound. 0, // Schema (currently reserved for future use in the SPIR-V spec). }); @@ -166,8 +188,8 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // follows the SPIR-V logical module format! var all_buffers = [_]std.os.iovec_const{ wordsToIovConst(binary.items), - wordsToIovConst(spirv_module.types_and_globals.items), - wordsToIovConst(spirv_module.fn_decls.items), + wordsToIovConst(spv.types_and_globals.items), + wordsToIovConst(spv.fn_decls.items), }; const file = self.base.file.?; |
