From bcda3c5b828fcb631e739e9c820a56aaaf0fd4df Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Tue, 18 May 2021 00:33:05 +0200 Subject: SPIR-V: Use Value.toFloat instead of switching on value tag when generating float constants --- src/codegen/spirv.zig | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 52b2884255..bd5de3b84d 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -230,6 +230,7 @@ pub const DeclGen = struct { /// Generate a constant representing `val`. /// TODO: Deduplication? fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 { + const target = self.module.getTarget(); const code = &self.spv.types_globals_constants; const result_id = self.spv.allocResultId(); const result_type_id = try self.getOrGenType(ty); @@ -250,11 +251,11 @@ pub const DeclGen = struct { // f16 and f32 require one word of storage. f64 requires 2, low-order first. - switch (val.tag()) { - .float_16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.castTag(.float_16).?.data) }), - .float_32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.castTag(.float_32).?.data) }), - .float_64 => { - const float_bits = @bitCast(u64, val.castTag(.float_64).?.data); + switch (ty.floatBits(target)) { + 16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }), + 32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }), + 64 => { + const float_bits = @bitCast(u64, val.toFloat(f64)); try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @@ -262,9 +263,9 @@ pub const DeclGen = struct { @truncate(u32, float_bits >> 32), }); }, - .float_128 => unreachable, // Filtered out in the call to getOrGenType. - // TODO: What tags do we need to handle here anyway? - else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: float constant generation of value {s}\n", .{val.tag()}), + 128 => unreachable, // Filtered out in the call to getOrGenType. + // TODO: Insert case for long double when the layout for that is determined. + else => unreachable, } }, else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}), -- cgit v1.2.3 From 9ddd7f4a60c70c1bf146c2fd0c35b32098755f77 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Tue, 18 May 2021 13:22:44 +0200 Subject: SPIR-V: Put types in SPIRVModule, some general restructuring --- src/codegen/spirv.zig | 94 +++++++++++++++++++++++++++++++++++---------------- src/link/SpirV.zig | 11 +++--- 2 files changed, 70 insertions(+), 35 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index bd5de3b84d..b30b1eda85 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -15,7 +15,7 @@ const ir = @import("../ir.zig"); const Inst = ir.Inst; pub const TypeMap = std.HashMap(Type, u32, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); -pub const ValueMap = std.AutoHashMap(*Inst, u32); +pub const InstMap = std.AutoHashMap(*Inst, u32); pub fn writeOpcode(code: *std.ArrayList(u32), opcode: Opcode, arg_count: u32) !void { const word_count = arg_count + 1; @@ -27,24 +27,34 @@ pub fn writeInstruction(code: *std.ArrayList(u32), opcode: Opcode, args: []const 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. +/// This structure represents a SPIR-V (binary) module being compiled, and keeps track of all relevant information. +/// That includes the actual instructions, the current result-id bound, and data structures for querying result-id's +/// of data which needs to be persistent over different calls to Decl code generation. pub const SPIRVModule = struct { next_result_id: u32, - types_globals_constants: std.ArrayList(u32), - fn_decls: std.ArrayList(u32), - pub fn init(allocator: *Allocator) SPIRVModule { + binary: struct { + types_globals_constants: std.ArrayList(u32), + fn_decls: std.ArrayList(u32), + }, + + types: TypeMap, + + pub fn init(gpa: *Allocator) SPIRVModule { return .{ .next_result_id = 1, // 0 is an invalid SPIR-V result ID. - .types_globals_constants = std.ArrayList(u32).init(allocator), - .fn_decls = std.ArrayList(u32).init(allocator), + .binary = .{ + .types_globals_constants = std.ArrayList(u32).init(gpa), + .fn_decls = std.ArrayList(u32).init(gpa), + }, + .types = TypeMap.init(gpa), }; } pub fn deinit(self: *SPIRVModule) void { - self.types_globals_constants.deinit(); - self.fn_decls.deinit(); + self.binary.types_globals_constants.deinit(); + self.binary.fn_decls.deinit(); + self.types.deinit(); } pub fn allocResultId(self: *SPIRVModule) u32 { @@ -59,18 +69,29 @@ pub const SPIRVModule = struct { /// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. pub const DeclGen = struct { + /// The parent module. module: *Module, + + /// The SPIR-V module code should be put in. spv: *SPIRVModule, + /// An array of function argument result-ids. Each index corresponds with the function argument of the same index. args: std.ArrayList(u32), + + /// A counter to keep track of how many `arg` instructions we've seen yet. next_arg_index: u32, - types: TypeMap, - values: ValueMap, + /// A map keeping track of which instruction generated which result-id. + inst_results: InstMap, + /// The decl we are currently generating code for. decl: *Decl, + + /// If `gen` returned `Error.AnalysisFail`, this contains an explanatory message. Memory is owned by + /// `module.gpa`. error_msg: ?*Module.ErrorMsg, + /// Possible errors the `gen` function may return. const Error = error{ AnalysisFail, OutOfMemory }; /// This structure is used to return information about a type typically used for arithmetic operations. @@ -129,7 +150,7 @@ pub const DeclGen = struct { return self.genConstant(inst.ty, val); } - return self.values.get(inst).?; // Instruction does not dominate all uses! + return self.inst_results.get(inst).?; // Instruction does not dominate all uses! } /// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need @@ -145,7 +166,7 @@ pub const DeclGen = struct { fn backingIntBits(self: *DeclGen, bits: u16) ?u16 { const target = self.module.getTarget(); - // TODO: Figure out what to do with u0/i0. + // The backend will never be asked to compiler a 0-bit integer, so we won't have to handle those in this function. std.debug.assert(bits != 0); // 8, 16 and 64-bit integers require the Int8, Int16 and Inr64 capabilities respectively. @@ -194,7 +215,6 @@ pub const DeclGen = struct { fn arithmeticTypeInfo(self: *DeclGen, ty: Type) !ArithmeticTypeInfo { const target = self.module.getTarget(); - return switch (ty.zigTypeTag()) { .Bool => ArithmeticTypeInfo{ .bits = 1, // Doesn't matter for this class. @@ -231,7 +251,7 @@ pub const DeclGen = struct { /// TODO: Deduplication? fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 { const target = self.module.getTarget(); - const code = &self.spv.types_globals_constants; + const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); const result_type_id = try self.getOrGenType(ty); @@ -276,12 +296,12 @@ pub const DeclGen = struct { fn getOrGenType(self: *DeclGen, ty: Type) Error!u32 { // We can't use getOrPut here so we can recursively generate types. - if (self.types.get(ty)) |already_generated| { + if (self.spv.types.get(ty)) |already_generated| { return already_generated; } const target = self.module.getTarget(); - const code = &self.spv.types_globals_constants; + const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); switch (ty.zigTypeTag()) { @@ -345,7 +365,7 @@ pub const DeclGen = struct { i = 0; while (i < params) : (i += 1) { - const param_type_id = self.types.get(ty.fnParamType(i)).?; + const param_type_id = self.spv.types.get(ty.fnParamType(i)).?; try code.append(param_type_id); } }, @@ -373,10 +393,11 @@ pub const DeclGen = struct { else => |tag| return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type {}s", .{tag}), } - try self.types.putNoClobber(ty, result_id); + try self.spv.types.putNoClobber(ty, result_id); return result_id; } +<<<<<<< HEAD pub fn gen(self: *DeclGen) !void { const decl = self.decl; const result_id = decl.fn_link.spirv.id; @@ -386,6 +407,17 @@ pub const DeclGen = struct { const prototype_id = try self.getOrGenType(decl.ty); try writeInstruction(&self.spv.fn_decls, .OpFunction, &[_]u32{ self.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. +======= + pub fn gen(self: *DeclGen) Error!void { + const result_id = self.decl.fn_link.spirv.id; + const tv = self.decl.typed_value.most_recent.typed_value; + + if (tv.val.castTag(.function)) |func_payload| { + std.debug.assert(tv.ty.zigTypeTag() == .Fn); + const prototype_id = try self.getOrGenType(tv.ty); + try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]u32{ + self.spv.types.get(tv.ty.fnReturnType()).?, // This type should be generated along with the prototype. +>>>>>>> 09e563b75 (SPIR-V: Put types in SPIRVModule, some general restructuring) result_id, @bitCast(u32, spec.FunctionControl{}), // TODO: We can set inline here if the type requires it. prototype_id, @@ -396,18 +428,22 @@ pub const DeclGen = struct { try self.args.ensureCapacity(params); while (i < params) : (i += 1) { +<<<<<<< HEAD const param_type_id = self.types.get(decl.ty.fnParamType(i)).?; +======= + const param_type_id = self.spv.types.get(tv.ty.fnParamType(i)).?; +>>>>>>> 09e563b75 (SPIR-V: Put types in SPIRVModule, some general restructuring) const arg_result_id = self.spv.allocResultId(); - try writeInstruction(&self.spv.fn_decls, .OpFunctionParameter, &[_]u32{ param_type_id, arg_result_id }); + try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionParameter, &[_]u32{ param_type_id, arg_result_id }); self.args.appendAssumeCapacity(arg_result_id); } // TODO: This could probably be done in a better way... const root_block_id = self.spv.allocResultId(); - _ = try writeInstruction(&self.spv.fn_decls, .OpLabel, &[_]u32{root_block_id}); + _ = try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]u32{root_block_id}); try self.genBody(func_payload.data.body); - try writeInstruction(&self.spv.fn_decls, .OpFunctionEnd, &[_]u32{}); + try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]u32{}); } else { return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: generate decl type {}", .{decl.ty.zigTypeTag()}); } @@ -417,7 +453,7 @@ pub const DeclGen = struct { for (body.instructions) |inst| { const maybe_result_id = try self.genInst(inst); if (maybe_result_id) |result_id| - try self.values.putNoClobber(inst, result_id); + try self.inst_results.putNoClobber(inst, result_id); } } @@ -510,7 +546,7 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.fn_decls, opcode, &[_]u32{ result_type_id, result_id, lhs_id, rhs_id }); + try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]u32{ result_type_id, result_id, lhs_id, rhs_id }); // TODO: Trap on overflow? Probably going to be annoying. // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap. @@ -535,7 +571,7 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.fn_decls, opcode, &[_]u32{ result_type_id, result_id, operand_id }); + try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]u32{ result_type_id, result_id, operand_id }); return result_id; } @@ -548,19 +584,19 @@ pub const DeclGen = struct { fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?u32 { const operand_id = try self.resolve(inst.operand); // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.fn_decls, .OpReturnValue, &[_]u32{operand_id}); + try writeInstruction(&self.spv.binary.fn_decls, .OpReturnValue, &[_]u32{operand_id}); return null; } fn genRetVoid(self: *DeclGen) !?u32 { // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.fn_decls, .OpReturn, &[_]u32{}); + try writeInstruction(&self.spv.binary.fn_decls, .OpReturn, &[_]u32{}); return null; } fn genUnreach(self: *DeclGen) !?u32 { // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.fn_decls, .OpUnreachable, &[_]u32{}); + try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]u32{}); return null; } }; diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index c98c5d689d..603a9160ff 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -157,20 +157,19 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { .spv = &spv, .args = std.ArrayList(u32).init(self.base.allocator), .next_arg_index = undefined, - .types = codegen.TypeMap.init(self.base.allocator), - .values = codegen.ValueMap.init(self.base.allocator), + .inst_results = codegen.InstMap.init(self.base.allocator), .decl = undefined, .error_msg = undefined, }; - defer decl_gen.values.deinit(); - defer decl_gen.types.deinit(); + defer decl_gen.inst_results.deinit(); defer decl_gen.args.deinit(); for (self.decl_table.items()) |entry| { const decl = entry.key; if (!decl.has_tv) continue; + // Reset the decl_gen, but retain allocated resources. decl_gen.args.items.len = 0; decl_gen.next_arg_index = 0; decl_gen.decl = decl; @@ -204,8 +203,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(spv.types_globals_constants.items), - wordsToIovConst(spv.fn_decls.items), + wordsToIovConst(spv.binary.types_globals_constants.items), + wordsToIovConst(spv.binary.fn_decls.items), }; const file = self.base.file.?; -- cgit v1.2.3 From c190b2ff83308a6680b9d4587d742c253dcdee5d Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Tue, 18 May 2021 13:31:22 +0200 Subject: SPIR-V: ResultId and Word aliases to improve code clarity --- src/codegen/spirv.zig | 115 ++++++++++++++++++++++---------------------------- src/link/SpirV.zig | 25 ++++++----- 2 files changed, 65 insertions(+), 75 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index b30b1eda85..1356ccc6bf 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -14,16 +14,19 @@ const LazySrcLoc = Module.LazySrcLoc; const ir = @import("../ir.zig"); const Inst = ir.Inst; -pub const TypeMap = std.HashMap(Type, u32, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); -pub const InstMap = std.AutoHashMap(*Inst, u32); +pub const Word = u32; +pub const ResultId = u32; -pub fn writeOpcode(code: *std.ArrayList(u32), opcode: Opcode, arg_count: u32) !void { - const word_count = arg_count + 1; +pub const TypeMap = std.HashMap(Type, ResultId, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); +pub const InstMap = std.AutoHashMap(*Inst, ResultId); + +pub fn writeOpcode(code: *std.ArrayList(Word), opcode: Opcode, arg_count: u16) !void { + const word_count: Word = arg_count + 1; try code.append((word_count << 16) | @enumToInt(opcode)); } -pub fn writeInstruction(code: *std.ArrayList(u32), opcode: Opcode, args: []const u32) !void { - try writeOpcode(code, opcode, @intCast(u32, args.len)); +pub fn writeInstruction(code: *std.ArrayList(Word), opcode: Opcode, args: []const Word) !void { + try writeOpcode(code, opcode, @intCast(u16, args.len)); try code.appendSlice(args); } @@ -31,11 +34,11 @@ pub fn writeInstruction(code: *std.ArrayList(u32), opcode: Opcode, args: []const /// That includes the actual instructions, the current result-id bound, and data structures for querying result-id's /// of data which needs to be persistent over different calls to Decl code generation. pub const SPIRVModule = struct { - next_result_id: u32, + next_result_id: ResultId, binary: struct { - types_globals_constants: std.ArrayList(u32), - fn_decls: std.ArrayList(u32), + types_globals_constants: std.ArrayList(Word), + fn_decls: std.ArrayList(Word), }, types: TypeMap, @@ -44,8 +47,8 @@ pub const SPIRVModule = struct { return .{ .next_result_id = 1, // 0 is an invalid SPIR-V result ID. .binary = .{ - .types_globals_constants = std.ArrayList(u32).init(gpa), - .fn_decls = std.ArrayList(u32).init(gpa), + .types_globals_constants = std.ArrayList(Word).init(gpa), + .fn_decls = std.ArrayList(Word).init(gpa), }, .types = TypeMap.init(gpa), }; @@ -57,12 +60,12 @@ pub const SPIRVModule = struct { self.types.deinit(); } - pub fn allocResultId(self: *SPIRVModule) u32 { + pub fn allocResultId(self: *SPIRVModule) Word { defer self.next_result_id += 1; return self.next_result_id; } - pub fn resultIdBound(self: *SPIRVModule) u32 { + pub fn resultIdBound(self: *SPIRVModule) Word { return self.next_result_id; } }; @@ -76,7 +79,7 @@ pub const DeclGen = struct { spv: *SPIRVModule, /// An array of function argument result-ids. Each index corresponds with the function argument of the same index. - args: std.ArrayList(u32), + args: std.ArrayList(ResultId), /// A counter to keep track of how many `arg` instructions we've seen yet. next_arg_index: u32, @@ -145,7 +148,7 @@ pub const DeclGen = struct { return error.AnalysisFail; } - fn resolve(self: *DeclGen, inst: *Inst) !u32 { + fn resolve(self: *DeclGen, inst: *Inst) !ResultId { if (inst.value()) |val| { return self.genConstant(inst.ty, val); } @@ -249,21 +252,21 @@ pub const DeclGen = struct { /// Generate a constant representing `val`. /// TODO: Deduplication? - fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 { + fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!ResultId { const target = self.module.getTarget(); const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); const result_type_id = try self.getOrGenType(ty); if (val.isUndef()) { - try writeInstruction(code, .OpUndef, &[_]u32{ result_type_id, result_id }); + try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id }); return result_id; } switch (ty.zigTypeTag()) { .Bool => { const opcode: Opcode = if (val.toBool()) .OpConstantTrue else .OpConstantFalse; - try writeInstruction(code, opcode, &[_]u32{ result_type_id, result_id }); + try writeInstruction(code, opcode, &[_]Word{ result_type_id, result_id }); }, .Float => { // At this point we are guaranteed that the target floating point type is supported, otherwise the function @@ -272,15 +275,15 @@ pub const DeclGen = struct { // f16 and f32 require one word of storage. f64 requires 2, low-order first. switch (ty.floatBits(target)) { - 16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }), - 32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }), + 16 => try writeInstruction(code, .OpConstant, &[_]Word{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }), + 32 => try writeInstruction(code, .OpConstant, &[_]Word{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }), 64 => { const float_bits = @bitCast(u64, val.toFloat(f64)); - try writeInstruction(code, .OpConstant, &[_]u32{ + try writeInstruction(code, .OpConstant, &[_]Word{ result_type_id, result_id, - @truncate(u32, float_bits), - @truncate(u32, float_bits >> 32), + @truncate(Word, float_bits), + @truncate(Word, float_bits >> 32), }); }, 128 => unreachable, // Filtered out in the call to getOrGenType. @@ -294,7 +297,7 @@ pub const DeclGen = struct { return result_id; } - fn getOrGenType(self: *DeclGen, ty: Type) Error!u32 { + fn getOrGenType(self: *DeclGen, ty: Type) Error!ResultId { // We can't use getOrPut here so we can recursively generate types. if (self.spv.types.get(ty)) |already_generated| { return already_generated; @@ -305,8 +308,8 @@ pub const DeclGen = struct { const result_id = self.spv.allocResultId(); switch (ty.zigTypeTag()) { - .Void => try writeInstruction(code, .OpTypeVoid, &[_]u32{result_id}), - .Bool => try writeInstruction(code, .OpTypeBool, &[_]u32{result_id}), + .Void => try writeInstruction(code, .OpTypeVoid, &[_]Word{result_id}), + .Bool => try writeInstruction(code, .OpTypeBool, &[_]Word{result_id}), .Int => { const int_info = ty.intInfo(target); const backing_bits = self.backingIntBits(int_info.bits) orelse { @@ -315,7 +318,7 @@ pub const DeclGen = struct { }; // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here. - try writeInstruction(code, .OpTypeInt, &[_]u32{ + try writeInstruction(code, .OpTypeInt, &[_]Word{ result_id, backing_bits, switch (int_info.signedness) { @@ -340,7 +343,7 @@ pub const DeclGen = struct { return self.fail(.{ .node_offset = 0 }, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); } - try writeInstruction(code, .OpTypeFloat, &[_]u32{ result_id, bits }); + try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits }); }, .Fn => { // We only support zig-calling-convention functions, no varargs. @@ -360,7 +363,7 @@ pub const DeclGen = struct { const return_type_id = try self.getOrGenType(ty.fnReturnType()); // result id + result type id + parameter type ids. - try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u32, ty.fnParamLen())); + try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen())); try code.appendSlice(&.{ result_id, return_type_id }); i = 0; @@ -397,7 +400,6 @@ pub const DeclGen = struct { return result_id; } -<<<<<<< HEAD pub fn gen(self: *DeclGen) !void { const decl = self.decl; const result_id = decl.fn_link.spirv.id; @@ -405,21 +407,10 @@ pub const DeclGen = struct { if (decl.val.castTag(.function)) |func_payload| { std.debug.assert(decl.ty.zigTypeTag() == .Fn); const prototype_id = try self.getOrGenType(decl.ty); - try writeInstruction(&self.spv.fn_decls, .OpFunction, &[_]u32{ - self.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. -======= - pub fn gen(self: *DeclGen) Error!void { - const result_id = self.decl.fn_link.spirv.id; - const tv = self.decl.typed_value.most_recent.typed_value; - - if (tv.val.castTag(.function)) |func_payload| { - std.debug.assert(tv.ty.zigTypeTag() == .Fn); - const prototype_id = try self.getOrGenType(tv.ty); - try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]u32{ - self.spv.types.get(tv.ty.fnReturnType()).?, // This type should be generated along with the prototype. ->>>>>>> 09e563b75 (SPIR-V: Put types in SPIRVModule, some general restructuring) + try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ + self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. result_id, - @bitCast(u32, spec.FunctionControl{}), // TODO: We can set inline here if the type requires it. + @bitCast(Word, spec.FunctionControl{}), // TODO: We can set inline here if the type requires it. prototype_id, }); @@ -428,22 +419,18 @@ pub const DeclGen = struct { try self.args.ensureCapacity(params); while (i < params) : (i += 1) { -<<<<<<< HEAD - const param_type_id = self.types.get(decl.ty.fnParamType(i)).?; -======= - const param_type_id = self.spv.types.get(tv.ty.fnParamType(i)).?; ->>>>>>> 09e563b75 (SPIR-V: Put types in SPIRVModule, some general restructuring) + const param_type_id = self.spv.types.get(decl.ty.fnParamType(i)).?; const arg_result_id = self.spv.allocResultId(); - try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionParameter, &[_]u32{ param_type_id, arg_result_id }); + try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionParameter, &[_]Word{ param_type_id, arg_result_id }); self.args.appendAssumeCapacity(arg_result_id); } // TODO: This could probably be done in a better way... const root_block_id = self.spv.allocResultId(); - _ = try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]u32{root_block_id}); + _ = try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]Word{root_block_id}); try self.genBody(func_payload.data.body); - try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]u32{}); + try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]Word{}); } else { return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: generate decl type {}", .{decl.ty.zigTypeTag()}); } @@ -457,7 +444,7 @@ pub const DeclGen = struct { } } - fn genInst(self: *DeclGen, inst: *Inst) !?u32 { + fn genInst(self: *DeclGen, inst: *Inst) !?ResultId { return switch (inst.tag) { .add, .addwrap => try self.genBinOp(inst.castTag(.add).?), .sub, .subwrap => try self.genBinOp(inst.castTag(.sub).?), @@ -487,7 +474,7 @@ pub const DeclGen = struct { }; } - fn genBinOp(self: *DeclGen, inst: *Inst.BinOp) !u32 { + fn genBinOp(self: *DeclGen, inst: *Inst.BinOp) !ResultId { // TODO: Will lhs and rhs have the same type? const lhs_id = try self.resolve(inst.lhs); const rhs_id = try self.resolve(inst.rhs); @@ -546,7 +533,7 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]u32{ result_type_id, result_id, lhs_id, rhs_id }); + try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); // TODO: Trap on overflow? Probably going to be annoying. // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap. @@ -557,7 +544,7 @@ pub const DeclGen = struct { return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: strange integer operation mask", .{}); } - fn genUnOp(self: *DeclGen, inst: *Inst.UnOp) !u32 { + fn genUnOp(self: *DeclGen, inst: *Inst.UnOp) !ResultId { const operand_id = try self.resolve(inst.operand); const result_id = self.spv.allocResultId(); @@ -571,32 +558,32 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]u32{ result_type_id, result_id, operand_id }); + try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, operand_id }); return result_id; } - fn genArg(self: *DeclGen) u32 { + fn genArg(self: *DeclGen) ResultId { defer self.next_arg_index += 1; return self.args.items[self.next_arg_index]; } - fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?u32 { + fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { const operand_id = try self.resolve(inst.operand); // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.binary.fn_decls, .OpReturnValue, &[_]u32{operand_id}); + try writeInstruction(&self.spv.binary.fn_decls, .OpReturnValue, &[_]Word{operand_id}); return null; } - fn genRetVoid(self: *DeclGen) !?u32 { + fn genRetVoid(self: *DeclGen) !?ResultId { // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.binary.fn_decls, .OpReturn, &[_]u32{}); + try writeInstruction(&self.spv.binary.fn_decls, .OpReturn, &[_]Word{}); return null; } - fn genUnreach(self: *DeclGen) !?u32 { + fn genUnreach(self: *DeclGen) !?ResultId { // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]u32{}); + try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{}); return null; } }; diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 603a9160ff..2614e9ddfa 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -31,15 +31,18 @@ const Module = @import("../Module.zig"); const Compilation = @import("../Compilation.zig"); const link = @import("../link.zig"); const codegen = @import("../codegen/spirv.zig"); +const Word = codegen.Word; +const ResultId = codegen.ResultId; const trace = @import("../tracy.zig").trace; const build_options = @import("build_options"); const spec = @import("../codegen/spirv/spec.zig"); // TODO: Should this struct be used at all rather than just a hashmap of aux data for every decl? pub const FnData = struct { -// We're going to fill these in flushModule, and we're going to fill them unconditionally, -// so just set it to undefined. -id: u32 = undefined }; + // We're going to fill these in flushModule, and we're going to fill them unconditionally, + // so just set it to undefined. + id: ResultId = undefined +}; base: link.File, @@ -155,7 +158,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { var decl_gen = codegen.DeclGen{ .module = module, .spv = &spv, - .args = std.ArrayList(u32).init(self.base.allocator), + .args = std.ArrayList(codegen.Word).init(self.base.allocator), .next_arg_index = undefined, .inst_results = codegen.InstMap.init(self.base.allocator), .decl = undefined, @@ -185,10 +188,10 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { } } - var binary = std.ArrayList(u32).init(self.base.allocator); + var binary = std.ArrayList(Word).init(self.base.allocator); defer binary.deinit(); - try binary.appendSlice(&[_]u32{ + try binary.appendSlice(&[_]Word{ spec.magic_number, (spec.version.major << 16) | (spec.version.minor << 8), 0, // TODO: Register Zig compiler magic number. @@ -220,7 +223,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { try file.pwritevAll(&all_buffers, 0); } -fn writeCapabilities(binary: *std.ArrayList(u32), target: std.Target) !void { +fn writeCapabilities(binary: *std.ArrayList(Word), target: std.Target) !void { // TODO: Integrate with a hypothetical feature system const cap: spec.Capability = switch (target.os.tag) { .opencl => .Kernel, @@ -229,10 +232,10 @@ fn writeCapabilities(binary: *std.ArrayList(u32), target: std.Target) !void { else => unreachable, // TODO }; - try codegen.writeInstruction(binary, .OpCapability, &[_]u32{@enumToInt(cap)}); + try codegen.writeInstruction(binary, .OpCapability, &[_]Word{@enumToInt(cap)}); } -fn writeMemoryModel(binary: *std.ArrayList(u32), target: std.Target) !void { +fn writeMemoryModel(binary: *std.ArrayList(Word), target: std.Target) !void { const addressing_model = switch (target.os.tag) { .opencl => switch (target.cpu.arch) { .spirv32 => spec.AddressingModel.Physical32, @@ -250,12 +253,12 @@ fn writeMemoryModel(binary: *std.ArrayList(u32), target: std.Target) !void { else => unreachable, }; - try codegen.writeInstruction(binary, .OpMemoryModel, &[_]u32{ + try codegen.writeInstruction(binary, .OpMemoryModel, &[_]Word{ @enumToInt(addressing_model), @enumToInt(memory_model), }); } -fn wordsToIovConst(words: []const u32) std.os.iovec_const { +fn wordsToIovConst(words: []const Word) std.os.iovec_const { const bytes = std.mem.sliceAsBytes(words); return .{ .iov_base = bytes.ptr, -- cgit v1.2.3 From b8444d2c51f334e3afec74ff80b051ed797ab480 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 20 May 2021 02:56:20 +0200 Subject: SPIR-V: Preliminary integer constant encoding --- src/codegen/spirv.zig | 38 ++++++++++++++++++++++++++++++++++++-- 1 file changed, 36 insertions(+), 2 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 1356ccc6bf..a97a5aa393 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -264,6 +264,40 @@ pub const DeclGen = struct { } switch (ty.zigTypeTag()) { + .Int => { + const int_info = ty.intInfo(target); + const backing_bits = self.backingIntBits(int_info.bits) orelse { + // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits. + return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite int constants for {}", .{ty}); + }; + + // We can just use toSignedInt/toUnsignedInt here as it returns u64 - a type large enough to hold any + // SPIR-V native type (up to i/u64 with Int64). If SPIR-V ever supports native ints of a larger size, this + // might need to be updated. + std.debug.assert(self.largestSupportedIntBits() <= std.meta.bitCount(u64)); + var int_bits = if (ty.isSignedInt()) @bitCast(u64, val.toSignedInt()) else val.toUnsignedInt(); + + // Mask the low bits which make up the actual integer. This is to make sure that negative values + // only use the actual bits of the type. + // TODO: Should this be the backing type bits or the actual type bits? + int_bits &= (@as(u64, 1) << @intCast(u6, backing_bits)) - 1; + + switch (backing_bits) { + 0 => unreachable, + 1...32 => try writeInstruction(code, .OpConstant, &[_]Word{ + result_type_id, + result_id, + @truncate(u32, int_bits), + }), + 33...64 => try writeInstruction(code, .OpConstant, &[_]Word{ + result_type_id, + result_id, + @truncate(u32, int_bits), + @truncate(u32, int_bits >> @bitSizeOf(u32)), + }), + else => unreachable, // backing_bits is bounded by largestSupportedIntBits. + } + }, .Bool => { const opcode: Opcode = if (val.toBool()) .OpConstantTrue else .OpConstantFalse; try writeInstruction(code, opcode, &[_]Word{ result_type_id, result_id }); @@ -282,8 +316,8 @@ pub const DeclGen = struct { try writeInstruction(code, .OpConstant, &[_]Word{ result_type_id, result_id, - @truncate(Word, float_bits), - @truncate(Word, float_bits >> 32), + @truncate(u32, float_bits), + @truncate(u32, float_bits >> @bitSizeOf(u32)), }); }, 128 => unreachable, // Filtered out in the call to getOrGenType. -- cgit v1.2.3 From 6a121d9ccde34556ffb1baf3b8543defdf6136e0 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 20 May 2021 13:05:32 +0200 Subject: SPIR-V: Split out genCmp from genBinOp --- src/codegen/spirv.zig | 79 +++++++++++++++++++++++++++++++++++++-------------- src/link/SpirV.zig | 2 +- 2 files changed, 58 insertions(+), 23 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index a97a5aa393..6266aa1c3e 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -348,7 +348,7 @@ pub const DeclGen = struct { const int_info = ty.intInfo(target); const backing_bits = self.backingIntBits(int_info.bits) orelse { // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits. - return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite ints {}", .{ty}); + return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite int {}", .{ty}); }; // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here. @@ -487,12 +487,12 @@ pub const DeclGen = struct { .bit_and => try self.genBinOp(inst.castTag(.bit_and).?), .bit_or => try self.genBinOp(inst.castTag(.bit_or).?), .xor => try self.genBinOp(inst.castTag(.xor).?), - .cmp_eq => try self.genBinOp(inst.castTag(.cmp_eq).?), - .cmp_neq => try self.genBinOp(inst.castTag(.cmp_neq).?), - .cmp_gt => try self.genBinOp(inst.castTag(.cmp_gt).?), - .cmp_gte => try self.genBinOp(inst.castTag(.cmp_gte).?), - .cmp_lt => try self.genBinOp(inst.castTag(.cmp_lt).?), - .cmp_lte => try self.genBinOp(inst.castTag(.cmp_lte).?), + .cmp_eq => try self.genCmp(inst.castTag(.cmp_eq).?), + .cmp_neq => try self.genCmp(inst.castTag(.cmp_neq).?), + .cmp_gt => try self.genCmp(inst.castTag(.cmp_gt).?), + .cmp_gte => try self.genCmp(inst.castTag(.cmp_gte).?), + .cmp_lt => try self.genCmp(inst.castTag(.cmp_lt).?), + .cmp_lte => try self.genCmp(inst.castTag(.cmp_lte).?), .bool_and => try self.genBinOp(inst.castTag(.bool_and).?), .bool_or => try self.genBinOp(inst.castTag(.bool_or).?), .not => try self.genUnOp(inst.castTag(.not).?), @@ -504,7 +504,7 @@ pub const DeclGen = struct { .ret => self.genRet(inst.castTag(.ret).?), .retvoid => self.genRetVoid(), .unreach => self.genUnreach(), - else => self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement inst {}", .{inst.tag}), + else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), }; } @@ -528,13 +528,14 @@ pub const DeclGen = struct { const info = try self.arithmeticTypeInfo(inst.lhs.ty); if (info.class == .composite_integer) - return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: binary operations for composite integers", .{}); + return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); + else if (info.class == .strange_integer) + return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for strange integers", .{}); const is_bool = info.class == .bool; const is_float = info.class == .float; const is_signed = info.signedness == .signed; - // **Note**: All these operations must be valid for vectors of floats, integers and bools as well! - // For floating points, we generally want ordered operations (which return false if either operand is nan). + // **Note**: All these operations must be valid for vectors as well! const opcode = switch (inst.base.tag) { // The regular integer operations are all defined for wrapping. Since theyre only relevant for integers, // we can just switch on both cases here. @@ -551,16 +552,6 @@ pub const DeclGen = struct { .bit_and => Opcode.OpBitwiseAnd, .bit_or => Opcode.OpBitwiseOr, .xor => Opcode.OpBitwiseXor, - // Int/bool/float -> bool operations. - .cmp_eq => if (is_float) Opcode.OpFOrdEqual else if (is_bool) Opcode.OpLogicalEqual else Opcode.OpIEqual, - .cmp_neq => if (is_float) Opcode.OpFOrdNotEqual else if (is_bool) Opcode.OpLogicalNotEqual else Opcode.OpINotEqual, - // Int/float -> bool operations. - // TODO: Verify that these OpFOrd type operations produce the right value. - // TODO: Is there a more fundamental difference between OpU and OpS operations here than just the type? - .cmp_gt => if (is_float) Opcode.OpFOrdGreaterThan else if (is_signed) Opcode.OpSGreaterThan else Opcode.OpUGreaterThan, - .cmp_gte => if (is_float) Opcode.OpFOrdGreaterThanEqual else if (is_signed) Opcode.OpSGreaterThanEqual else Opcode.OpUGreaterThanEqual, - .cmp_lt => if (is_float) Opcode.OpFOrdLessThan else if (is_signed) Opcode.OpSLessThan else Opcode.OpULessThan, - .cmp_lte => if (is_float) Opcode.OpFOrdLessThanEqual else if (is_signed) Opcode.OpSLessThanEqual else Opcode.OpULessThanEqual, // Bool -> bool operations. .bool_and => Opcode.OpLogicalAnd, .bool_or => Opcode.OpLogicalOr, @@ -575,7 +566,51 @@ pub const DeclGen = struct { if (info.class != .strange_integer) return result_id; - return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: strange integer operation mask", .{}); + return self.fail(inst.base.src, "TODO: SPIR-V backend: strange integer operation mask", .{}); + } + + fn genCmp(self: *DeclGen, inst: *Inst.BinOp) !ResultId { + const lhs_id = try self.resolve(inst.lhs); + const rhs_id = try self.resolve(inst.rhs); + + const result_id = self.spv.allocResultId(); + const result_type_id = try self.getOrGenType(inst.base.ty); + + // All of these operations should be 2 equal types -> bool + std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty)); + std.debug.assert(inst.base.ty.tag() == .bool); + + // Comparisons are generally applicable to both scalar and vector operations in SPIR-V, but int and float + // versions of operations require different opcodes. + // Since inst.base.ty is always bool and so not very useful, and because both arguments must be the same, just get the info + // from either of the operands. + const info = try self.arithmeticTypeInfo(inst.lhs.ty); + + if (info.class == .composite_integer) + return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); + else if (info.class == .strange_integer) + return self.fail(inst.base.src, "TODO: SPIR-V backend: comparison for strange integers", .{}); + + const is_bool = info.class == .bool; + const is_float = info.class == .float; + const is_signed = info.signedness == .signed; + + // **Note**: All these operations must be valid for vectors as well! + // For floating points, we generally want ordered operations (which return false if either operand is nan). + const opcode = switch (inst.base.tag) { + .cmp_eq => if (is_float) Opcode.OpFOrdEqual else if (is_bool) Opcode.OpLogicalEqual else Opcode.OpIEqual, + .cmp_neq => if (is_float) Opcode.OpFOrdNotEqual else if (is_bool) Opcode.OpLogicalNotEqual else Opcode.OpINotEqual, + // TODO: Verify that these OpFOrd type operations produce the right value. + // TODO: Is there a more fundamental difference between OpU and OpS operations here than just the type? + .cmp_gt => if (is_float) Opcode.OpFOrdGreaterThan else if (is_signed) Opcode.OpSGreaterThan else Opcode.OpUGreaterThan, + .cmp_gte => if (is_float) Opcode.OpFOrdGreaterThanEqual else if (is_signed) Opcode.OpSGreaterThanEqual else Opcode.OpUGreaterThanEqual, + .cmp_lt => if (is_float) Opcode.OpFOrdLessThan else if (is_signed) Opcode.OpSLessThan else Opcode.OpULessThan, + .cmp_lte => if (is_float) Opcode.OpFOrdLessThanEqual else if (is_signed) Opcode.OpSLessThanEqual else Opcode.OpULessThanEqual, + else => unreachable, + }; + + try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); + return result_id; } fn genUnOp(self: *DeclGen, inst: *Inst.UnOp) !ResultId { diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 2614e9ddfa..d27a0d25d2 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -41,7 +41,7 @@ const spec = @import("../codegen/spirv/spec.zig"); pub const FnData = struct { // We're going to fill these in flushModule, and we're going to fill them unconditionally, // so just set it to undefined. - id: ResultId = undefined + id: ResultId = undefined, }; base: link.File, -- cgit v1.2.3 From 63d0576f1ccfd6ec1538459c6ac69b9f892b2142 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 20 May 2021 14:03:04 +0200 Subject: SPIR-V: Preliminary alloc/store/load generation --- src/codegen/spirv.zig | 93 +++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 79 insertions(+), 14 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 6266aa1c3e..682ee6e425 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -256,7 +256,7 @@ pub const DeclGen = struct { const target = self.module.getTarget(); const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); - const result_type_id = try self.getOrGenType(ty); + const result_type_id = try self.genType(ty); if (val.isUndef()) { try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id }); @@ -304,7 +304,7 @@ pub const DeclGen = struct { }, .Float => { // At this point we are guaranteed that the target floating point type is supported, otherwise the function - // would have exited at getOrGenType(ty). + // would have exited at genType(ty). // f16 and f32 require one word of storage. f64 requires 2, low-order first. @@ -320,7 +320,7 @@ pub const DeclGen = struct { @truncate(u32, float_bits >> @bitSizeOf(u32)), }); }, - 128 => unreachable, // Filtered out in the call to getOrGenType. + 128 => unreachable, // Filtered out in the call to genType. // TODO: Insert case for long double when the layout for that is determined. else => unreachable, } @@ -331,7 +331,7 @@ pub const DeclGen = struct { return result_id; } - fn getOrGenType(self: *DeclGen, ty: Type) Error!ResultId { + fn genType(self: *DeclGen, ty: Type) Error!ResultId { // We can't use getOrPut here so we can recursively generate types. if (self.spv.types.get(ty)) |already_generated| { return already_generated; @@ -391,10 +391,10 @@ pub const DeclGen = struct { const params = ty.fnParamLen(); var i: usize = 0; while (i < params) : (i += 1) { - _ = try self.getOrGenType(ty.fnParamType(i)); + _ = try self.genType(ty.fnParamType(i)); } - const return_type_id = try self.getOrGenType(ty.fnReturnType()); + const return_type_id = try self.genType(ty.fnReturnType()); // result id + result type id + parameter type ids. try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen())); @@ -406,6 +406,8 @@ pub const DeclGen = struct { try code.append(param_type_id); } }, + // When recursively generating a type, we cannot infer the pointer's storage class. See genPointerType. + .Pointer => return self.fail(.{ .node_offset = 0 }, "Cannot create pointer with unkown storage class", .{}), .Vector => { // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations // which work on them), so simply use those. @@ -434,13 +436,31 @@ pub const DeclGen = struct { return result_id; } + /// SPIR-V requires pointers to have a storage class (address space), and so we have a special function for that. + /// TODO: The result of this needs to be cached. + fn genPointerType(self: *DeclGen, ty: Type, storage_class: spec.StorageClass) !ResultId { + std.debug.assert(ty.zigTypeTag() == .Pointer); + + const code = &self.spv.binary.types_globals_constants; + const result_id = self.spv.allocResultId(); + + // TODO: There are many constraints which are ignored for now: We may only create pointers to certain types, and to other types + // if more capabilities are enabled. For example, we may only create pointers to f16 if Float16Buffer is enabled. + // These also relates to the pointer's address space. + const child_id = try self.genType(ty.elemType()); + + try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id }); + + return result_id; + } + pub fn gen(self: *DeclGen) !void { const decl = self.decl; const result_id = decl.fn_link.spirv.id; if (decl.val.castTag(.function)) |func_payload| { std.debug.assert(decl.ty.zigTypeTag() == .Fn); - const prototype_id = try self.getOrGenType(decl.ty); + const prototype_id = try self.genType(decl.ty); try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. result_id, @@ -496,13 +516,17 @@ pub const DeclGen = struct { .bool_and => try self.genBinOp(inst.castTag(.bool_and).?), .bool_or => try self.genBinOp(inst.castTag(.bool_or).?), .not => try self.genUnOp(inst.castTag(.not).?), + .alloc => try self.genAlloc(inst.castTag(.alloc).?), .arg => self.genArg(), // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them // throughout the IR. .breakpoint => null, + .constant => unreachable, .dbg_stmt => null, + .load => try self.genLoad(inst.castTag(.load).?), .ret => self.genRet(inst.castTag(.ret).?), .retvoid => self.genRetVoid(), + .store => try self.genStore(inst.castTag(.store).?), .unreach => self.genUnreach(), else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), }; @@ -514,7 +538,7 @@ pub const DeclGen = struct { const rhs_id = try self.resolve(inst.rhs); const result_id = self.spv.allocResultId(); - const result_type_id = try self.getOrGenType(inst.base.ty); + const result_type_id = try self.genType(inst.base.ty); // TODO: Is the result the same as the argument types? // This is supposed to be the case for SPIR-V. @@ -527,10 +551,11 @@ pub const DeclGen = struct { // instead. const info = try self.arithmeticTypeInfo(inst.lhs.ty); - if (info.class == .composite_integer) + if (info.class == .composite_integer) { return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); - else if (info.class == .strange_integer) + } else if (info.class == .strange_integer) { return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for strange integers", .{}); + } const is_bool = info.class == .bool; const is_float = info.class == .float; @@ -574,7 +599,7 @@ pub const DeclGen = struct { const rhs_id = try self.resolve(inst.rhs); const result_id = self.spv.allocResultId(); - const result_type_id = try self.getOrGenType(inst.base.ty); + const result_type_id = try self.genType(inst.base.ty); // All of these operations should be 2 equal types -> bool std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty)); @@ -586,10 +611,11 @@ pub const DeclGen = struct { // from either of the operands. const info = try self.arithmeticTypeInfo(inst.lhs.ty); - if (info.class == .composite_integer) + if (info.class == .composite_integer) { return self.fail(inst.base.src, "TODO: SPIR-V backend: binary operations for composite integers", .{}); - else if (info.class == .strange_integer) + } else if (info.class == .strange_integer) { return self.fail(inst.base.src, "TODO: SPIR-V backend: comparison for strange integers", .{}); + } const is_bool = info.class == .bool; const is_float = info.class == .float; @@ -617,7 +643,7 @@ pub const DeclGen = struct { const operand_id = try self.resolve(inst.operand); const result_id = self.spv.allocResultId(); - const result_type_id = try self.getOrGenType(inst.base.ty); + const result_type_id = try self.genType(inst.base.ty); const info = try self.arithmeticTypeInfo(inst.operand.ty); @@ -632,11 +658,37 @@ pub const DeclGen = struct { return result_id; } + fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId { + const storage_class = spec.StorageClass.Function; + const result_type_id = try self.genPointerType(inst.base.ty, storage_class); + const result_id = self.spv.allocResultId(); + + try writeInstruction(&self.spv.binary.fn_decls, .OpVariable, &[_]Word{ result_type_id, result_id, @enumToInt(storage_class) }); + + return result_id; + } + fn genArg(self: *DeclGen) ResultId { defer self.next_arg_index += 1; return self.args.items[self.next_arg_index]; } + fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { + const operand_id = try self.resolve(inst.operand); + + const result_type_id = try self.genType(inst.base.ty); + const result_id = self.spv.allocResultId(); + + const operands = if (inst.base.ty.isVolatilePtr()) + &[_]Word{ result_type_id, result_id, operand_id, @bitCast(u32, spec.MemoryAccess{.Volatile = true}) } + else + &[_]Word{ result_type_id, result_id, operand_id}; + + try writeInstruction(&self.spv.binary.fn_decls, .OpLoad, operands); + + return result_id; + } + fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { const operand_id = try self.resolve(inst.operand); // TODO: This instruction needs to be the last in a block. Is that guaranteed? @@ -650,6 +702,19 @@ pub const DeclGen = struct { return null; } + fn genStore(self: *DeclGen, inst: *Inst.BinOp) !?ResultId { + const dst_ptr_id = try self.resolve(inst.lhs); + const src_val_id = try self.resolve(inst.rhs); + + const operands = if (inst.lhs.ty.isVolatilePtr()) + &[_]Word{ dst_ptr_id, src_val_id, @bitCast(u32, spec.MemoryAccess{.Volatile = true}) } + else + &[_]Word{ dst_ptr_id, src_val_id }; + + try writeInstruction(&self.spv.binary.fn_decls, .OpStore, operands); + return null; + } + fn genUnreach(self: *DeclGen) !?ResultId { // TODO: This instruction needs to be the last in a block. Is that guaranteed? try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{}); -- cgit v1.2.3 From 5edc5f973089aaec5a62c37a3b7d0470a90d45e3 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 20 May 2021 17:50:17 +0200 Subject: SPIR-V: Pass source location to genType and genConstant for better error reporting --- src/codegen/spirv.zig | 46 +++++++++++++++++++++++----------------------- 1 file changed, 23 insertions(+), 23 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 682ee6e425..e2cd3fe619 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -150,7 +150,7 @@ pub const DeclGen = struct { fn resolve(self: *DeclGen, inst: *Inst) !ResultId { if (inst.value()) |val| { - return self.genConstant(inst.ty, val); + return self.genConstant(inst.src, inst.ty, val); } return self.inst_results.get(inst).?; // Instruction does not dominate all uses! @@ -252,11 +252,11 @@ pub const DeclGen = struct { /// Generate a constant representing `val`. /// TODO: Deduplication? - fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!ResultId { + fn genConstant(self: *DeclGen, src: LazySrcLoc, ty: Type, val: Value) Error!ResultId { const target = self.module.getTarget(); const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); - const result_type_id = try self.genType(ty); + const result_type_id = try self.genType(src, ty); if (val.isUndef()) { try writeInstruction(code, .OpUndef, &[_]Word{ result_type_id, result_id }); @@ -268,7 +268,7 @@ pub const DeclGen = struct { const int_info = ty.intInfo(target); const backing_bits = self.backingIntBits(int_info.bits) orelse { // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits. - return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite int constants for {}", .{ty}); + return self.fail(src, "TODO: SPIR-V backend: implement composite int constants for {}", .{ty}); }; // We can just use toSignedInt/toUnsignedInt here as it returns u64 - a type large enough to hold any @@ -325,13 +325,13 @@ pub const DeclGen = struct { else => unreachable, } }, - else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}), + else => return self.fail(src, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}), } return result_id; } - fn genType(self: *DeclGen, ty: Type) Error!ResultId { + fn genType(self: *DeclGen, src: LazySrcLoc, ty: Type) Error!ResultId { // We can't use getOrPut here so we can recursively generate types. if (self.spv.types.get(ty)) |already_generated| { return already_generated; @@ -348,7 +348,7 @@ pub const DeclGen = struct { const int_info = ty.intInfo(target); const backing_bits = self.backingIntBits(int_info.bits) orelse { // Integers too big for any native type are represented as "composite integers": An array of largestSupportedIntBits. - return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement composite int {}", .{ty}); + return self.fail(src, "TODO: SPIR-V backend: implement composite int {}", .{ty}); }; // TODO: If backing_bits != int_info.bits, a duplicate type might be generated here. @@ -374,7 +374,7 @@ pub const DeclGen = struct { }; if (!supported) { - return self.fail(.{ .node_offset = 0 }, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); + return self.fail(src, "Floating point width of {} bits is not supported for the current SPIR-V feature set", .{bits}); } try writeInstruction(code, .OpTypeFloat, &[_]Word{ result_id, bits }); @@ -382,19 +382,19 @@ pub const DeclGen = struct { .Fn => { // We only support zig-calling-convention functions, no varargs. if (ty.fnCallingConvention() != .Unspecified) - return self.fail(.{ .node_offset = 0 }, "Unsupported calling convention for SPIR-V", .{}); + return self.fail(src, "Unsupported calling convention for SPIR-V", .{}); if (ty.fnIsVarArgs()) - return self.fail(.{ .node_offset = 0 }, "VarArgs unsupported for SPIR-V", .{}); + return self.fail(src, "VarArgs unsupported for SPIR-V", .{}); // In order to avoid a temporary here, first generate all the required types and then simply look them up // when generating the function type. const params = ty.fnParamLen(); var i: usize = 0; while (i < params) : (i += 1) { - _ = try self.genType(ty.fnParamType(i)); + _ = try self.genType(src, ty.fnParamType(i)); } - const return_type_id = try self.genType(ty.fnReturnType()); + const return_type_id = try self.genType(src, ty.fnReturnType()); // result id + result type id + parameter type ids. try writeOpcode(code, .OpTypeFunction, 2 + @intCast(u16, ty.fnParamLen())); @@ -407,7 +407,7 @@ pub const DeclGen = struct { } }, // When recursively generating a type, we cannot infer the pointer's storage class. See genPointerType. - .Pointer => return self.fail(.{ .node_offset = 0 }, "Cannot create pointer with unkown storage class", .{}), + .Pointer => return self.fail(src, "Cannot create pointer with unkown storage class", .{}), .Vector => { // Although not 100% the same, Zig vectors map quite neatly to SPIR-V vectors (including many integer and float operations // which work on them), so simply use those. @@ -417,7 +417,7 @@ pub const DeclGen = struct { // is adequate at all for this. // TODO: Vectors are not yet supported by the self-hosted compiler itself it seems. - return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type Vector", .{}); + return self.fail(src, "TODO: SPIR-V backend: implement type Vector", .{}); }, .Null, .Undefined, @@ -429,7 +429,7 @@ pub const DeclGen = struct { .BoundFn => unreachable, // this type will be deleted from the language. - else => |tag| return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: implement type {}s", .{tag}), + else => |tag| return self.fail(src, "TODO: SPIR-V backend: implement type {}s", .{tag}), } try self.spv.types.putNoClobber(ty, result_id); @@ -438,7 +438,7 @@ pub const DeclGen = struct { /// SPIR-V requires pointers to have a storage class (address space), and so we have a special function for that. /// TODO: The result of this needs to be cached. - fn genPointerType(self: *DeclGen, ty: Type, storage_class: spec.StorageClass) !ResultId { + fn genPointerType(self: *DeclGen, src: LazySrcLoc, ty: Type, storage_class: spec.StorageClass) !ResultId { std.debug.assert(ty.zigTypeTag() == .Pointer); const code = &self.spv.binary.types_globals_constants; @@ -447,7 +447,7 @@ pub const DeclGen = struct { // TODO: There are many constraints which are ignored for now: We may only create pointers to certain types, and to other types // if more capabilities are enabled. For example, we may only create pointers to f16 if Float16Buffer is enabled. // These also relates to the pointer's address space. - const child_id = try self.genType(ty.elemType()); + const child_id = try self.genType(src, ty.elemType()); try writeInstruction(code, .OpTypePointer, &[_]Word{ result_id, @enumToInt(storage_class), child_id }); @@ -460,7 +460,7 @@ pub const DeclGen = struct { if (decl.val.castTag(.function)) |func_payload| { std.debug.assert(decl.ty.zigTypeTag() == .Fn); - const prototype_id = try self.genType(decl.ty); + const prototype_id = try self.genType(.{ .node_offset = 0 }, decl.ty); try writeInstruction(&self.spv.binary.fn_decls, .OpFunction, &[_]Word{ self.spv.types.get(decl.ty.fnReturnType()).?, // This type should be generated along with the prototype. result_id, @@ -538,7 +538,7 @@ pub const DeclGen = struct { const rhs_id = try self.resolve(inst.rhs); const result_id = self.spv.allocResultId(); - const result_type_id = try self.genType(inst.base.ty); + const result_type_id = try self.genType(inst.base.src, inst.base.ty); // TODO: Is the result the same as the argument types? // This is supposed to be the case for SPIR-V. @@ -599,7 +599,7 @@ pub const DeclGen = struct { const rhs_id = try self.resolve(inst.rhs); const result_id = self.spv.allocResultId(); - const result_type_id = try self.genType(inst.base.ty); + const result_type_id = try self.genType(inst.base.src, inst.base.ty); // All of these operations should be 2 equal types -> bool std.debug.assert(inst.rhs.ty.eql(inst.lhs.ty)); @@ -643,7 +643,7 @@ pub const DeclGen = struct { const operand_id = try self.resolve(inst.operand); const result_id = self.spv.allocResultId(); - const result_type_id = try self.genType(inst.base.ty); + const result_type_id = try self.genType(inst.base.src, inst.base.ty); const info = try self.arithmeticTypeInfo(inst.operand.ty); @@ -660,7 +660,7 @@ pub const DeclGen = struct { fn genAlloc(self: *DeclGen, inst: *Inst.NoOp) !ResultId { const storage_class = spec.StorageClass.Function; - const result_type_id = try self.genPointerType(inst.base.ty, storage_class); + const result_type_id = try self.genPointerType(inst.base.src, inst.base.ty, storage_class); const result_id = self.spv.allocResultId(); try writeInstruction(&self.spv.binary.fn_decls, .OpVariable, &[_]Word{ result_type_id, result_id, @enumToInt(storage_class) }); @@ -676,7 +676,7 @@ pub const DeclGen = struct { fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { const operand_id = try self.resolve(inst.operand); - const result_type_id = try self.genType(inst.base.ty); + const result_type_id = try self.genType(inst.base.src, inst.base.ty); const result_id = self.spv.allocResultId(); const operands = if (inst.base.ty.isVolatilePtr()) -- cgit v1.2.3 From 46184ab85eaf32be6e6fcbaac2202a2d58a37cf7 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 20 May 2021 19:15:43 +0200 Subject: SPIR-V: branching --- src/codegen/spirv.zig | 152 ++++++++++++++++++++++++++++++++++++++++++++++++-- src/link/SpirV.zig | 6 ++ 2 files changed, 152 insertions(+), 6 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index e2cd3fe619..4cb0068473 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -20,6 +20,16 @@ pub const ResultId = u32; pub const TypeMap = std.HashMap(Type, ResultId, Type.hash, Type.eql, std.hash_map.default_max_load_percentage); pub const InstMap = std.AutoHashMap(*Inst, ResultId); +const IncomingBlock = struct { + src_label_id: ResultId, + break_value_id: ResultId, +}; + +pub const BlockMap = std.AutoHashMap(*Inst.Block, struct { + label_id: ResultId, + incoming_blocks: *std.ArrayListUnmanaged(IncomingBlock), +}); + pub fn writeOpcode(code: *std.ArrayList(Word), opcode: Opcode, arg_count: u16) !void { const word_count: Word = arg_count + 1; try code.append((word_count << 16) | @enumToInt(opcode)); @@ -87,6 +97,12 @@ pub const DeclGen = struct { /// A map keeping track of which instruction generated which result-id. inst_results: InstMap, + /// We need to keep track of result ids for block labels, as well as the 'incoming' blocks for a block. + blocks: BlockMap, + + /// The label of the SPIR-V block we are currently generating. + current_block_label_id: ResultId, + /// The decl we are currently generating code for. decl: *Decl, @@ -156,6 +172,11 @@ pub const DeclGen = struct { return self.inst_results.get(inst).?; // Instruction does not dominate all uses! } + fn beginSPIRVBlock(self: *DeclGen, label_id: ResultId) !void { + try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]Word{label_id}); + self.current_block_label_id = label_id; + } + /// SPIR-V requires enabling specific integer sizes through capabilities, and so if they are not enabled, we need /// to emulate them in other instructions/types. This function returns, given an integer bit width (signed or unsigned, sign /// included), the width of the underlying type which represents it, given the enabled features for the current target. @@ -325,7 +346,8 @@ pub const DeclGen = struct { else => unreachable, } }, - else => return self.fail(src, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}), + .Void => unreachable, + else => return self.fail(src, "TODO: SPIR-V backend: constant generation of type {}", .{ty}), } return result_id; @@ -481,7 +503,7 @@ pub const DeclGen = struct { // TODO: This could probably be done in a better way... const root_block_id = self.spv.allocResultId(); - _ = try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]Word{root_block_id}); + try self.beginSPIRVBlock(root_block_id); try self.genBody(func_payload.data.body); try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]Word{}); @@ -490,7 +512,7 @@ pub const DeclGen = struct { } } - fn genBody(self: *DeclGen, body: ir.Body) !void { + fn genBody(self: *DeclGen, body: ir.Body) Error!void { for (body.instructions) |inst| { const maybe_result_id = try self.genInst(inst); if (maybe_result_id) |result_id| @@ -518,16 +540,21 @@ pub const DeclGen = struct { .not => try self.genUnOp(inst.castTag(.not).?), .alloc => try self.genAlloc(inst.castTag(.alloc).?), .arg => self.genArg(), + .block => try self.genBlock(inst.castTag(.block).?), + .br => try self.genBr(inst.castTag(.br).?), + .br_void => try self.genBrVoid(inst.castTag(.br_void).?), // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them // throughout the IR. .breakpoint => null, + .condbr => try self.genCondBr(inst.castTag(.condbr).?), .constant => unreachable, .dbg_stmt => null, .load => try self.genLoad(inst.castTag(.load).?), - .ret => self.genRet(inst.castTag(.ret).?), - .retvoid => self.genRetVoid(), + .loop => try self.genLoop(inst.castTag(.loop).?), + .ret => try self.genRet(inst.castTag(.ret).?), + .retvoid => try self.genRetVoid(), .store => try self.genStore(inst.castTag(.store).?), - .unreach => self.genUnreach(), + .unreach => try self.genUnreach(), else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), }; } @@ -673,6 +700,103 @@ pub const DeclGen = struct { return self.args.items[self.next_arg_index]; } + fn genBlock(self: *DeclGen, inst: *Inst.Block) !?ResultId { + // In IR, a block doesn't really define an entry point like a block, but more like a scope that breaks can jump out of and + // "return" a value from. This cannot be directly modelled in SPIR-V, so in a block instruction, we're going to split up + // the current block by first generating the code of the block, then a label, and then generate the rest of the current + // ir.Block in a different SPIR-V block. + + const label_id = self.spv.allocResultId(); + + // 4 chosen as arbitrary initial capacity. + var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.module.gpa, 4); + + try self.blocks.putNoClobber(inst, .{ + .label_id = label_id, + .incoming_blocks = &incoming_blocks, + }); + defer { + self.blocks.removeAssertDiscard(inst); + incoming_blocks.deinit(self.module.gpa); + } + + try self.genBody(inst.body); + try self.beginSPIRVBlock(label_id); + + // If this block didn't produce a value, simply return here. + if (!inst.base.ty.hasCodeGenBits()) + return null; + + // Combine the result from the blocks using the Phi instruction. + + const result_id = self.spv.allocResultId(); + + // TODO: OpPhi is limited in the types that it may produce, such as pointers. Figure out which other types + // are not allowed to be created from a phi node, and throw an error for those. For now, genType already throws + // an error for pointers. + const result_type_id = try self.genType(inst.base.src, inst.base.ty); + + try writeOpcode(&self.spv.binary.fn_decls, .OpPhi, 2 + @intCast(u16, incoming_blocks.items.len * 2)); // result type + result + variable/parent... + + for (incoming_blocks.items) |incoming| { + try self.spv.binary.fn_decls.appendSlice(&[_]Word{ incoming.break_value_id, incoming.src_label_id }); + } + + return result_id; + } + + fn genBr(self: *DeclGen, inst: *Inst.Br) !?ResultId { + // TODO: This instruction needs to be the last in a block. Is that guaranteed? + const target = self.blocks.get(inst.block).?; + + // TODO: For some reason, br is emitted with void parameters. + if (inst.operand.ty.hasCodeGenBits()) { + const operand_id = try self.resolve(inst.operand); + // current_block_label_id should not be undefined here, lest there is a br or br_void in the function's body. + try target.incoming_blocks.append(self.module.gpa, .{ + .src_label_id = self.current_block_label_id, + .break_value_id = operand_id + }); + } + + try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{target.label_id}); + + return null; + } + + fn genBrVoid(self: *DeclGen, inst: *Inst.BrVoid) !?ResultId { + // TODO: This instruction needs to be the last in a block. Is that guaranteed? + const target = self.blocks.get(inst.block).?; + // Don't need to add this to the incoming block list, as there is no value to insert in the phi node anyway. + try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{target.label_id}); + return null; + } + + fn genCondBr(self: *DeclGen, inst: *Inst.CondBr) !?ResultId { + // TODO: This instruction needs to be the last in a block. Is that guaranteed? + const condition_id = try self.resolve(inst.condition); + + // These will always generate a new SPIR-V block, since they are ir.Body and not ir.Block. + const then_label_id = self.spv.allocResultId(); + const else_label_id = self.spv.allocResultId(); + + // TODO: We can generate OpSelectionMerge here if we know the target block that both of these will resolve to, + // but i don't know if those will always resolve to the same block. + + try writeInstruction(&self.spv.binary.fn_decls, .OpBranchConditional, &[_]Word{ + condition_id, + then_label_id, + else_label_id, + }); + + try self.beginSPIRVBlock(then_label_id); + try self.genBody(inst.then_body); + try self.beginSPIRVBlock(else_label_id); + try self.genBody(inst.else_body); + + return null; + } + fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { const operand_id = try self.resolve(inst.operand); @@ -689,6 +813,22 @@ pub const DeclGen = struct { return result_id; } + fn genLoop(self: *DeclGen, inst: *Inst.Loop) !?ResultId { + // TODO: This instruction needs to be the last in a block. Is that guaranteed? + const loop_label_id = self.spv.allocResultId(); + + // Jump to the loop entry point + try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{ loop_label_id }); + + // TODO: Look into OpLoopMerge. + + try self.beginSPIRVBlock(loop_label_id); + try self.genBody(inst.body); + + try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{ loop_label_id }); + return null; + } + fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { const operand_id = try self.resolve(inst.operand); // TODO: This instruction needs to be the last in a block. Is that guaranteed? diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index d27a0d25d2..daa94f932a 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -161,12 +161,15 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { .args = std.ArrayList(codegen.Word).init(self.base.allocator), .next_arg_index = undefined, .inst_results = codegen.InstMap.init(self.base.allocator), + .blocks = codegen.BlockMap.init(self.base.allocator), + .current_block_label_id = undefined, .decl = undefined, .error_msg = undefined, }; defer decl_gen.inst_results.deinit(); defer decl_gen.args.deinit(); + defer decl_gen.blocks.deinit(); for (self.decl_table.items()) |entry| { const decl = entry.key; @@ -175,6 +178,9 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // Reset the decl_gen, but retain allocated resources. decl_gen.args.items.len = 0; decl_gen.next_arg_index = 0; + decl_gen.inst_results.clearRetainingCapacity(); + decl_gen.blocks.clearRetainingCapacity(); + decl_gen.current_block_label_id = undefined; decl_gen.decl = decl; decl_gen.error_msg = null; -- cgit v1.2.3 From e3be1a1e88bc76d5886122048e44673b692e6db6 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Thu, 20 May 2021 20:35:52 +0200 Subject: SPIR-V: DeclGen constructor/destructor --- src/codegen/spirv.zig | 41 ++++++++++++++++++++++++++++++++++++++++- src/link/SpirV.zig | 40 ++++++---------------------------------- 2 files changed, 46 insertions(+), 35 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 4cb0068473..6687ffd970 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -157,6 +157,45 @@ pub const DeclGen = struct { class: Class, }; + /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, only set when `gen` is called. + pub fn init(gpa: *Allocator, module: *Module, spv: *SPIRVModule) DeclGen { + return .{ + .module = module, + .spv = spv, + .args = std.ArrayList(ResultId).init(gpa), + .next_arg_index = undefined, + .inst_results = InstMap.init(gpa), + .blocks = BlockMap.init(gpa), + .current_block_label_id = undefined, + .decl = undefined, + .error_msg = undefined, + }; + } + + /// Generate the code for `decl`. If a reportable error occured during code generation, + /// a message is returned by this function. Callee owns the memory. If this function returns such + /// a reportable error, it is valid to be called again for a different decl. + pub fn gen(self: *DeclGen, decl: *Decl) !?*Module.ErrorMsg { + // Reset internal resources, we don't want to re-allocate these. + self.args.items.len = 0; + self.next_arg_index = 0; + self.inst_results.clearRetainingCapacity(); + self.blocks.clearRetainingCapacity(); + self.current_block_label_id = undefined; + self.decl = decl; + self.error_msg = null; + + try self.genDecl(); + return self.error_msg; + } + + /// Free resources owned by the DeclGen. + pub fn deinit(self: *DeclGen) void { + self.args.deinit(); + self.inst_results.deinit(); + self.blocks.deinit(); + } + fn fail(self: *DeclGen, src: LazySrcLoc, comptime format: []const u8, args: anytype) Error { @setCold(true); const src_loc = src.toSrcLocWithDecl(self.decl); @@ -476,7 +515,7 @@ pub const DeclGen = struct { return result_id; } - pub fn gen(self: *DeclGen) !void { + fn genDecl(self: *DeclGen) !void { const decl = self.decl; const result_id = decl.fn_link.spirv.id; diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index daa94f932a..cae29eb2d7 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -152,45 +152,17 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // 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 `args` and `types`, and re-construct this - // structure every time. - var decl_gen = codegen.DeclGen{ - .module = module, - .spv = &spv, - .args = std.ArrayList(codegen.Word).init(self.base.allocator), - .next_arg_index = undefined, - .inst_results = codegen.InstMap.init(self.base.allocator), - .blocks = codegen.BlockMap.init(self.base.allocator), - .current_block_label_id = undefined, - .decl = undefined, - .error_msg = undefined, - }; - - defer decl_gen.inst_results.deinit(); - defer decl_gen.args.deinit(); - defer decl_gen.blocks.deinit(); + var decl_gen = codegen.DeclGen.init(self.base.allocator, module, &spv); + defer decl_gen.deinit(); for (self.decl_table.items()) |entry| { const decl = entry.key; if (!decl.has_tv) continue; - // Reset the decl_gen, but retain allocated resources. - decl_gen.args.items.len = 0; - decl_gen.next_arg_index = 0; - decl_gen.inst_results.clearRetainingCapacity(); - decl_gen.blocks.clearRetainingCapacity(); - decl_gen.current_block_label_id = undefined; - 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, - }; + if (try decl_gen.gen(decl)) |msg| { + try module.failed_decls.put(module.gpa, decl, msg); + return; // TODO: Attempt to generate more decls? + } } } -- cgit v1.2.3 From 6634abfd2669a902a86f2c61dbc011310e1f31c4 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 21 May 2021 02:08:14 +0200 Subject: SPIR-V: Debug line info/source info --- src/codegen/spirv.zig | 116 ++++++++++++++++++++++++++++++++++++++++++-------- src/link/SpirV.zig | 50 +++++++++++----------- 2 files changed, 122 insertions(+), 44 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 6687ffd970..e9b4c7c384 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -40,34 +40,92 @@ pub fn writeInstruction(code: *std.ArrayList(Word), opcode: Opcode, args: []cons try code.appendSlice(args); } +pub fn writeInstructionWithString(code: *std.ArrayList(Word), opcode: Opcode, args: []const Word, str: []const u8) !void { + // Str needs to be written zero-terminated, so we need to add one to the length. + const zero_terminated_len = str.len + 1; + const str_words = (zero_terminated_len + @sizeOf(Word) - 1) / @sizeOf(Word); + + try writeOpcode(code, opcode, @intCast(u16, args.len + str_words)); + try code.ensureUnusedCapacity(args.len + str_words); + code.appendSliceAssumeCapacity(args); + + // TODO: Not actually sure whether this is correct for big-endian. + // See https://www.khronos.org/registry/spir-v/specs/unified1/SPIRV.html#Literal + var i: usize = 0; + while (i < zero_terminated_len) : (i += @sizeOf(Word)) { + var word: Word = 0; + + var j: usize = 0; + while (j < @sizeOf(Word) and i + j < str.len) : (j += 1) { + word |= @as(Word, str[i + j]) << @intCast(std.math.Log2Int(Word), j * std.meta.bitCount(u8)); + } + + code.appendAssumeCapacity(word); + } +} + /// This structure represents a SPIR-V (binary) module being compiled, and keeps track of all relevant information. /// That includes the actual instructions, the current result-id bound, and data structures for querying result-id's /// of data which needs to be persistent over different calls to Decl code generation. pub const SPIRVModule = struct { + /// A general-purpose allocator which may be used to allocate temporary resources required for compilation. + gpa: *Allocator, + + /// The parent module. + module: *Module, + + /// SPIR-V instructions return result-ids. This variable holds the module-wide counter for these. next_result_id: ResultId, + /// Code of the actual SPIR-V binary, divided into the relevant logical sections. + /// Note: To save some bytes, these could also be unmanaged, but since there is only one instance of SPIRVModule + /// and this removes some clutter in the rest of the backend, it's fine like this. binary: struct { + /// OpCapability and OpExtension instructions (in that order). + capabilities_and_extensions: std.ArrayList(Word), + + /// OpString, OpSourceExtension, OpSource, OpSourceContinued. + debug_strings: std.ArrayList(Word), + + /// Type declaration instructions, constant instructions, global variable declarations, OpUndef instructions. types_globals_constants: std.ArrayList(Word), + + /// Regular functions. fn_decls: std.ArrayList(Word), }, + /// Global type cache to reduce the amount of generated types. types: TypeMap, - pub fn init(gpa: *Allocator) SPIRVModule { + /// Cache for results of OpString instructions for module file names fed to OpSource. + /// Since OpString is pretty much only used for those, we don't need to keep track of all strings, + /// just the ones for OpLine. Note that OpLine needs the result of OpString, and not that of OpSource. + file_names: std.StringHashMap(ResultId), + + pub fn init(gpa: *Allocator, module: *Module) SPIRVModule { return .{ + .gpa = gpa, + .module = module, .next_result_id = 1, // 0 is an invalid SPIR-V result ID. .binary = .{ + .capabilities_and_extensions = std.ArrayList(Word).init(gpa), + .debug_strings = std.ArrayList(Word).init(gpa), .types_globals_constants = std.ArrayList(Word).init(gpa), .fn_decls = std.ArrayList(Word).init(gpa), }, .types = TypeMap.init(gpa), + .file_names = std.StringHashMap(ResultId).init(gpa), }; } pub fn deinit(self: *SPIRVModule) void { - self.binary.types_globals_constants.deinit(); - self.binary.fn_decls.deinit(); + self.file_names.deinit(); self.types.deinit(); + + self.binary.fn_decls.deinit(); + self.binary.types_globals_constants.deinit(); + self.binary.debug_strings.deinit(); + self.binary.capabilities_and_extensions.deinit(); } pub fn allocResultId(self: *SPIRVModule) Word { @@ -78,13 +136,26 @@ pub const SPIRVModule = struct { pub fn resultIdBound(self: *SPIRVModule) Word { return self.next_result_id; } + + fn resolveSourceFileName(self: *SPIRVModule, decl: *Decl) !ResultId { + const path = decl.namespace.file_scope.sub_file_path; + const result = try self.file_names.getOrPut(path); + if (!result.found_existing) { + result.entry.value = self.allocResultId(); + try writeInstructionWithString(&self.binary.debug_strings, .OpString, &[_]Word{result.entry.value}, path); + try writeInstruction(&self.binary.debug_strings, .OpSource, &[_]Word{ + @enumToInt(spec.SourceLanguage.Unknown), // TODO: Register Zig source language. + 0, // TODO: Zig version as u32? + result.entry.value, + }); + } + + return result.entry.value; + } }; /// This structure is used to compile a declaration, and contains all relevant meta-information to deal with that. pub const DeclGen = struct { - /// The parent module. - module: *Module, - /// The SPIR-V module code should be put in. spv: *SPIRVModule, @@ -158,9 +229,8 @@ pub const DeclGen = struct { }; /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, only set when `gen` is called. - pub fn init(gpa: *Allocator, module: *Module, spv: *SPIRVModule) DeclGen { + pub fn init(gpa: *Allocator, spv: *SPIRVModule) DeclGen { return .{ - .module = module, .spv = spv, .args = std.ArrayList(ResultId).init(gpa), .next_arg_index = undefined, @@ -196,10 +266,14 @@ pub const DeclGen = struct { self.blocks.deinit(); } + fn getTarget(self: *DeclGen) std.Target { + return self.spv.module.getTarget(); + } + fn fail(self: *DeclGen, src: LazySrcLoc, comptime format: []const u8, args: anytype) Error { @setCold(true); const src_loc = src.toSrcLocWithDecl(self.decl); - self.error_msg = try Module.ErrorMsg.create(self.module.gpa, src_loc, format, args); + self.error_msg = try Module.ErrorMsg.create(self.spv.module.gpa, src_loc, format, args); return error.AnalysisFail; } @@ -227,7 +301,7 @@ pub const DeclGen = struct { /// TODO: This probably needs an ABI-version as well (especially in combination with SPV_INTEL_arbitrary_precision_integers). /// TODO: Should the result of this function be cached? fn backingIntBits(self: *DeclGen, bits: u16) ?u16 { - const target = self.module.getTarget(); + const target = self.getTarget(); // The backend will never be asked to compiler a 0-bit integer, so we won't have to handle those in this function. std.debug.assert(bits != 0); @@ -262,7 +336,7 @@ pub const DeclGen = struct { /// is no way of knowing whether those are actually supported. /// TODO: Maybe this should be cached? fn largestSupportedIntBits(self: *DeclGen) u16 { - const target = self.module.getTarget(); + const target = self.getTarget(); return if (Target.spirv.featureSetHas(target.cpu.features, .Int64)) 64 else @@ -277,7 +351,7 @@ pub const DeclGen = struct { } fn arithmeticTypeInfo(self: *DeclGen, ty: Type) !ArithmeticTypeInfo { - const target = self.module.getTarget(); + const target = self.getTarget(); return switch (ty.zigTypeTag()) { .Bool => ArithmeticTypeInfo{ .bits = 1, // Doesn't matter for this class. @@ -313,7 +387,7 @@ pub const DeclGen = struct { /// Generate a constant representing `val`. /// TODO: Deduplication? fn genConstant(self: *DeclGen, src: LazySrcLoc, ty: Type, val: Value) Error!ResultId { - const target = self.module.getTarget(); + const target = self.getTarget(); const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); const result_type_id = try self.genType(src, ty); @@ -398,7 +472,7 @@ pub const DeclGen = struct { return already_generated; } - const target = self.module.getTarget(); + const target = self.getTarget(); const code = &self.spv.binary.types_globals_constants; const result_id = self.spv.allocResultId(); @@ -587,7 +661,7 @@ pub const DeclGen = struct { .breakpoint => null, .condbr => try self.genCondBr(inst.castTag(.condbr).?), .constant => unreachable, - .dbg_stmt => null, + .dbg_stmt => try self.genDbgStmt(inst.castTag(.dbg_stmt).?), .load => try self.genLoad(inst.castTag(.load).?), .loop => try self.genLoop(inst.castTag(.loop).?), .ret => try self.genRet(inst.castTag(.ret).?), @@ -748,7 +822,7 @@ pub const DeclGen = struct { const label_id = self.spv.allocResultId(); // 4 chosen as arbitrary initial capacity. - var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.module.gpa, 4); + var incoming_blocks = try std.ArrayListUnmanaged(IncomingBlock).initCapacity(self.spv.gpa, 4); try self.blocks.putNoClobber(inst, .{ .label_id = label_id, @@ -756,7 +830,7 @@ pub const DeclGen = struct { }); defer { self.blocks.removeAssertDiscard(inst); - incoming_blocks.deinit(self.module.gpa); + incoming_blocks.deinit(self.spv.gpa); } try self.genBody(inst.body); @@ -792,7 +866,7 @@ pub const DeclGen = struct { if (inst.operand.ty.hasCodeGenBits()) { const operand_id = try self.resolve(inst.operand); // current_block_label_id should not be undefined here, lest there is a br or br_void in the function's body. - try target.incoming_blocks.append(self.module.gpa, .{ + try target.incoming_blocks.append(self.spv.gpa, .{ .src_label_id = self.current_block_label_id, .break_value_id = operand_id }); @@ -836,6 +910,12 @@ pub const DeclGen = struct { return null; } + fn genDbgStmt(self: *DeclGen, inst: *Inst.DbgStmt) !?ResultId { + const src_fname_id = try self.spv.resolveSourceFileName(self.decl); + try writeInstruction(&self.spv.binary.fn_decls, .OpLine, &[_]Word{ src_fname_id, inst.line, inst.column }); + return null; + } + fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { const operand_id = try self.resolve(inst.operand); diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index cae29eb2d7..3ecdf4725f 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -132,7 +132,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { const module = self.base.options.module.?; const target = comp.getTarget(); - var spv = codegen.SPIRVModule.init(self.base.allocator); + var spv = codegen.SPIRVModule.init(self.base.allocator, module); defer spv.deinit(); // Allocate an ID for every declaration before generating code, @@ -152,7 +152,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // Now, actually generate the code for all declarations. { - var decl_gen = codegen.DeclGen.init(self.base.allocator, module, &spv); + var decl_gen = codegen.DeclGen.init(self.base.allocator, &spv); defer decl_gen.deinit(); for (self.decl_table.items()) |entry| { @@ -166,39 +166,45 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { } } - var binary = std.ArrayList(Word).init(self.base.allocator); - defer binary.deinit(); + try writeCapabilities(&spv.binary.capabilities_and_extensions, target); + try writeMemoryModel(&spv.binary.capabilities_and_extensions, target); - try binary.appendSlice(&[_]Word{ + const header = [_]Word{ spec.magic_number, (spec.version.major << 16) | (spec.version.minor << 8), 0, // TODO: Register Zig compiler magic number. - spv.resultIdBound(), // ID bound. + spv.resultIdBound(), 0, // Schema (currently reserved for future use in the SPIR-V spec). - }); - - try writeCapabilities(&binary, target); - try writeMemoryModel(&binary, target); + }; // Note: The order of adding sections to the final binary // follows the SPIR-V logical module format! - var all_buffers = [_]std.os.iovec_const{ - wordsToIovConst(binary.items), - wordsToIovConst(spv.binary.types_globals_constants.items), - wordsToIovConst(spv.binary.fn_decls.items), + const buffers = &[_][]const Word{ + &header, + spv.binary.capabilities_and_extensions.items, + spv.binary.debug_strings.items, + spv.binary.types_globals_constants.items, + spv.binary.fn_decls.items, }; - const file = self.base.file.?; - const bytes = std.mem.sliceAsBytes(binary.items); + var iovc_buffers: [buffers.len]std.os.iovec_const = undefined; + for (iovc_buffers) |*iovc, i| { + const bytes = std.mem.sliceAsBytes(buffers[i]); + iovc.* = .{ + .iov_base = bytes.ptr, + .iov_len = bytes.len + }; + } var file_size: u64 = 0; - for (all_buffers) |iov| { + for (iovc_buffers) |iov| { file_size += iov.iov_len; } + const file = self.base.file.?; try file.seekTo(0); try file.setEndPos(file_size); - try file.pwritevAll(&all_buffers, 0); + try file.pwritevAll(&iovc_buffers, 0); } fn writeCapabilities(binary: *std.ArrayList(Word), target: std.Target) !void { @@ -235,11 +241,3 @@ fn writeMemoryModel(binary: *std.ArrayList(Word), target: std.Target) !void { @enumToInt(addressing_model), @enumToInt(memory_model), }); } - -fn wordsToIovConst(words: []const Word) std.os.iovec_const { - const bytes = std.mem.sliceAsBytes(words); - return .{ - .iov_base = bytes.ptr, - .iov_len = bytes.len, - }; -} -- cgit v1.2.3 From 228f71fa0ce25a5c473496dd2b947ae05ab9bed8 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 21 May 2021 02:45:11 +0200 Subject: SPIR-V: Generate locals at the start of a function --- src/codegen/spirv.zig | 62 +++++++++++++++++++++++++++++++++------------------ src/link/SpirV.zig | 2 +- 2 files changed, 41 insertions(+), 23 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index e9b4c7c384..0e356b891f 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -174,6 +174,12 @@ pub const DeclGen = struct { /// The label of the SPIR-V block we are currently generating. current_block_label_id: ResultId, + /// The actual instructions for this function. We need to declare all locals in the first block, and because we don't + /// know which locals there are going to be, we're just going to generate everything after the locals-section in this array. + /// Note: It will not contain OpFunction, OpFunctionParameter, OpVariable and the initial OpLabel. These will be generated + /// into spv.binary.fn_decls directly. + code: std.ArrayList(Word), + /// The decl we are currently generating code for. decl: *Decl, @@ -229,14 +235,15 @@ pub const DeclGen = struct { }; /// Initialize the common resources of a DeclGen. Some fields are left uninitialized, only set when `gen` is called. - pub fn init(gpa: *Allocator, spv: *SPIRVModule) DeclGen { + pub fn init(spv: *SPIRVModule) DeclGen { return .{ .spv = spv, - .args = std.ArrayList(ResultId).init(gpa), + .args = std.ArrayList(ResultId).init(spv.gpa), .next_arg_index = undefined, - .inst_results = InstMap.init(gpa), - .blocks = BlockMap.init(gpa), + .inst_results = InstMap.init(spv.gpa), + .blocks = BlockMap.init(spv.gpa), .current_block_label_id = undefined, + .code = std.ArrayList(Word).init(spv.gpa), .decl = undefined, .error_msg = undefined, }; @@ -252,6 +259,7 @@ pub const DeclGen = struct { self.inst_results.clearRetainingCapacity(); self.blocks.clearRetainingCapacity(); self.current_block_label_id = undefined; + self.code.items.len = 0; self.decl = decl; self.error_msg = null; @@ -264,6 +272,7 @@ pub const DeclGen = struct { self.args.deinit(); self.inst_results.deinit(); self.blocks.deinit(); + self.code.deinit(); } fn getTarget(self: *DeclGen) std.Target { @@ -286,7 +295,7 @@ pub const DeclGen = struct { } fn beginSPIRVBlock(self: *DeclGen, label_id: ResultId) !void { - try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]Word{label_id}); + try writeInstruction(&self.code, .OpLabel, &[_]Word{label_id}); self.current_block_label_id = label_id; } @@ -616,9 +625,16 @@ pub const DeclGen = struct { // TODO: This could probably be done in a better way... const root_block_id = self.spv.allocResultId(); - try self.beginSPIRVBlock(root_block_id); + + // We need to generate the label directly in the fn_decls here because we're going to write the local variables after + // here. Since we're not generating in self.code, we're just going to bypass self.beginSPIRVBlock here. + try writeInstruction(&self.spv.binary.fn_decls, .OpLabel, &[_]Word{root_block_id}); + self.current_block_label_id = root_block_id; + try self.genBody(func_payload.data.body); + // Append the actual code into the fn_decls section. + try self.spv.binary.fn_decls.appendSlice(self.code.items); try writeInstruction(&self.spv.binary.fn_decls, .OpFunctionEnd, &[_]Word{}); } else { return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: generate decl type {}", .{decl.ty.zigTypeTag()}); @@ -723,7 +739,7 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); + try writeInstruction(&self.code, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); // TODO: Trap on overflow? Probably going to be annoying. // TODO: Look into SPV_KHR_no_integer_wrap_decoration which provides NoSignedWrap/NoUnsignedWrap. @@ -775,7 +791,7 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); + try writeInstruction(&self.code, opcode, &[_]Word{ result_type_id, result_id, lhs_id, rhs_id }); return result_id; } @@ -793,7 +809,7 @@ pub const DeclGen = struct { else => unreachable, }; - try writeInstruction(&self.spv.binary.fn_decls, opcode, &[_]Word{ result_type_id, result_id, operand_id }); + try writeInstruction(&self.code, opcode, &[_]Word{ result_type_id, result_id, operand_id }); return result_id; } @@ -803,6 +819,8 @@ pub const DeclGen = struct { const result_type_id = try self.genPointerType(inst.base.src, inst.base.ty, storage_class); const result_id = self.spv.allocResultId(); + // Rather than generating into code here, we're just going to generate directly into the fn_decls section so that + // variable declarations appear in the first block of the function. try writeInstruction(&self.spv.binary.fn_decls, .OpVariable, &[_]Word{ result_type_id, result_id, @enumToInt(storage_class) }); return result_id; @@ -849,10 +867,10 @@ pub const DeclGen = struct { // an error for pointers. const result_type_id = try self.genType(inst.base.src, inst.base.ty); - try writeOpcode(&self.spv.binary.fn_decls, .OpPhi, 2 + @intCast(u16, incoming_blocks.items.len * 2)); // result type + result + variable/parent... + try writeOpcode(&self.code, .OpPhi, 2 + @intCast(u16, incoming_blocks.items.len * 2)); // result type + result + variable/parent... for (incoming_blocks.items) |incoming| { - try self.spv.binary.fn_decls.appendSlice(&[_]Word{ incoming.break_value_id, incoming.src_label_id }); + try self.code.appendSlice(&[_]Word{ incoming.break_value_id, incoming.src_label_id }); } return result_id; @@ -872,7 +890,7 @@ pub const DeclGen = struct { }); } - try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{target.label_id}); + try writeInstruction(&self.code, .OpBranch, &[_]Word{target.label_id}); return null; } @@ -881,7 +899,7 @@ pub const DeclGen = struct { // TODO: This instruction needs to be the last in a block. Is that guaranteed? const target = self.blocks.get(inst.block).?; // Don't need to add this to the incoming block list, as there is no value to insert in the phi node anyway. - try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{target.label_id}); + try writeInstruction(&self.code, .OpBranch, &[_]Word{target.label_id}); return null; } @@ -896,7 +914,7 @@ pub const DeclGen = struct { // TODO: We can generate OpSelectionMerge here if we know the target block that both of these will resolve to, // but i don't know if those will always resolve to the same block. - try writeInstruction(&self.spv.binary.fn_decls, .OpBranchConditional, &[_]Word{ + try writeInstruction(&self.code, .OpBranchConditional, &[_]Word{ condition_id, then_label_id, else_label_id, @@ -912,7 +930,7 @@ pub const DeclGen = struct { fn genDbgStmt(self: *DeclGen, inst: *Inst.DbgStmt) !?ResultId { const src_fname_id = try self.spv.resolveSourceFileName(self.decl); - try writeInstruction(&self.spv.binary.fn_decls, .OpLine, &[_]Word{ src_fname_id, inst.line, inst.column }); + try writeInstruction(&self.code, .OpLine, &[_]Word{ src_fname_id, inst.line, inst.column }); return null; } @@ -927,7 +945,7 @@ pub const DeclGen = struct { else &[_]Word{ result_type_id, result_id, operand_id}; - try writeInstruction(&self.spv.binary.fn_decls, .OpLoad, operands); + try writeInstruction(&self.code, .OpLoad, operands); return result_id; } @@ -937,27 +955,27 @@ pub const DeclGen = struct { const loop_label_id = self.spv.allocResultId(); // Jump to the loop entry point - try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{ loop_label_id }); + try writeInstruction(&self.code, .OpBranch, &[_]Word{ loop_label_id }); // TODO: Look into OpLoopMerge. try self.beginSPIRVBlock(loop_label_id); try self.genBody(inst.body); - try writeInstruction(&self.spv.binary.fn_decls, .OpBranch, &[_]Word{ loop_label_id }); + try writeInstruction(&self.code, .OpBranch, &[_]Word{ loop_label_id }); return null; } fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { const operand_id = try self.resolve(inst.operand); // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.binary.fn_decls, .OpReturnValue, &[_]Word{operand_id}); + try writeInstruction(&self.code, .OpReturnValue, &[_]Word{operand_id}); return null; } fn genRetVoid(self: *DeclGen) !?ResultId { // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.binary.fn_decls, .OpReturn, &[_]Word{}); + try writeInstruction(&self.code, .OpReturn, &[_]Word{}); return null; } @@ -970,13 +988,13 @@ pub const DeclGen = struct { else &[_]Word{ dst_ptr_id, src_val_id }; - try writeInstruction(&self.spv.binary.fn_decls, .OpStore, operands); + try writeInstruction(&self.code, .OpStore, operands); return null; } fn genUnreach(self: *DeclGen) !?ResultId { // TODO: This instruction needs to be the last in a block. Is that guaranteed? - try writeInstruction(&self.spv.binary.fn_decls, .OpUnreachable, &[_]Word{}); + try writeInstruction(&self.code, .OpUnreachable, &[_]Word{}); return null; } }; diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 3ecdf4725f..57da89dede 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -152,7 +152,7 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { // Now, actually generate the code for all declarations. { - var decl_gen = codegen.DeclGen.init(self.base.allocator, &spv); + var decl_gen = codegen.DeclGen.init(&spv); defer decl_gen.deinit(); for (self.decl_table.items()) |entry| { -- cgit v1.2.3 From cba97e47730ff42df1da23e7019350a2d9e1a312 Mon Sep 17 00:00:00 2001 From: Robin Voetter Date: Fri, 21 May 2021 02:59:12 +0200 Subject: SPIR-V: Make functions which always return a null result return void instead --- src/codegen/spirv.zig | 63 +++++++++++++++++++++------------------------------ src/link/SpirV.zig | 1 - 2 files changed, 26 insertions(+), 38 deletions(-) (limited to 'src/codegen') diff --git a/src/codegen/spirv.zig b/src/codegen/spirv.zig index 0e356b891f..52112e2f61 100644 --- a/src/codegen/spirv.zig +++ b/src/codegen/spirv.zig @@ -643,14 +643,12 @@ pub const DeclGen = struct { fn genBody(self: *DeclGen, body: ir.Body) Error!void { for (body.instructions) |inst| { - const maybe_result_id = try self.genInst(inst); - if (maybe_result_id) |result_id| - try self.inst_results.putNoClobber(inst, result_id); + try self.genInst(inst); } } - fn genInst(self: *DeclGen, inst: *Inst) !?ResultId { - return switch (inst.tag) { + fn genInst(self: *DeclGen, inst: *Inst) !void { + const result_id = switch (inst.tag) { .add, .addwrap => try self.genBinOp(inst.castTag(.add).?), .sub, .subwrap => try self.genBinOp(inst.castTag(.sub).?), .mul, .mulwrap => try self.genBinOp(inst.castTag(.mul).?), @@ -669,23 +667,25 @@ pub const DeclGen = struct { .not => try self.genUnOp(inst.castTag(.not).?), .alloc => try self.genAlloc(inst.castTag(.alloc).?), .arg => self.genArg(), - .block => try self.genBlock(inst.castTag(.block).?), - .br => try self.genBr(inst.castTag(.br).?), - .br_void => try self.genBrVoid(inst.castTag(.br_void).?), + .block => (try self.genBlock(inst.castTag(.block).?)) orelse return, + .br => return try self.genBr(inst.castTag(.br).?), + .br_void => return try self.genBrVoid(inst.castTag(.br_void).?), // TODO: Breakpoints won't be supported in SPIR-V, but the compiler seems to insert them // throughout the IR. - .breakpoint => null, - .condbr => try self.genCondBr(inst.castTag(.condbr).?), + .breakpoint => return, + .condbr => return try self.genCondBr(inst.castTag(.condbr).?), .constant => unreachable, - .dbg_stmt => try self.genDbgStmt(inst.castTag(.dbg_stmt).?), + .dbg_stmt => return try self.genDbgStmt(inst.castTag(.dbg_stmt).?), .load => try self.genLoad(inst.castTag(.load).?), - .loop => try self.genLoop(inst.castTag(.loop).?), - .ret => try self.genRet(inst.castTag(.ret).?), - .retvoid => try self.genRetVoid(), - .store => try self.genStore(inst.castTag(.store).?), - .unreach => try self.genUnreach(), - else => self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), + .loop => return try self.genLoop(inst.castTag(.loop).?), + .ret => return try self.genRet(inst.castTag(.ret).?), + .retvoid => return try self.genRetVoid(), + .store => return try self.genStore(inst.castTag(.store).?), + .unreach => return try self.genUnreach(), + else => return self.fail(inst.src, "TODO: SPIR-V backend: implement inst {s}", .{@tagName(inst.tag)}), }; + + try self.inst_results.putNoClobber(inst, result_id); } fn genBinOp(self: *DeclGen, inst: *Inst.BinOp) !ResultId { @@ -876,7 +876,7 @@ pub const DeclGen = struct { return result_id; } - fn genBr(self: *DeclGen, inst: *Inst.Br) !?ResultId { + fn genBr(self: *DeclGen, inst: *Inst.Br) !void { // TODO: This instruction needs to be the last in a block. Is that guaranteed? const target = self.blocks.get(inst.block).?; @@ -891,19 +891,16 @@ pub const DeclGen = struct { } try writeInstruction(&self.code, .OpBranch, &[_]Word{target.label_id}); - - return null; } - fn genBrVoid(self: *DeclGen, inst: *Inst.BrVoid) !?ResultId { + fn genBrVoid(self: *DeclGen, inst: *Inst.BrVoid) !void { // TODO: This instruction needs to be the last in a block. Is that guaranteed? const target = self.blocks.get(inst.block).?; // Don't need to add this to the incoming block list, as there is no value to insert in the phi node anyway. try writeInstruction(&self.code, .OpBranch, &[_]Word{target.label_id}); - return null; } - fn genCondBr(self: *DeclGen, inst: *Inst.CondBr) !?ResultId { + fn genCondBr(self: *DeclGen, inst: *Inst.CondBr) !void { // TODO: This instruction needs to be the last in a block. Is that guaranteed? const condition_id = try self.resolve(inst.condition); @@ -924,14 +921,11 @@ pub const DeclGen = struct { try self.genBody(inst.then_body); try self.beginSPIRVBlock(else_label_id); try self.genBody(inst.else_body); - - return null; } - fn genDbgStmt(self: *DeclGen, inst: *Inst.DbgStmt) !?ResultId { + fn genDbgStmt(self: *DeclGen, inst: *Inst.DbgStmt) !void { const src_fname_id = try self.spv.resolveSourceFileName(self.decl); try writeInstruction(&self.code, .OpLine, &[_]Word{ src_fname_id, inst.line, inst.column }); - return null; } fn genLoad(self: *DeclGen, inst: *Inst.UnOp) !ResultId { @@ -950,7 +944,7 @@ pub const DeclGen = struct { return result_id; } - fn genLoop(self: *DeclGen, inst: *Inst.Loop) !?ResultId { + fn genLoop(self: *DeclGen, inst: *Inst.Loop) !void { // TODO: This instruction needs to be the last in a block. Is that guaranteed? const loop_label_id = self.spv.allocResultId(); @@ -963,23 +957,20 @@ pub const DeclGen = struct { try self.genBody(inst.body); try writeInstruction(&self.code, .OpBranch, &[_]Word{ loop_label_id }); - return null; } - fn genRet(self: *DeclGen, inst: *Inst.UnOp) !?ResultId { + fn genRet(self: *DeclGen, inst: *Inst.UnOp) !void { const operand_id = try self.resolve(inst.operand); // TODO: This instruction needs to be the last in a block. Is that guaranteed? try writeInstruction(&self.code, .OpReturnValue, &[_]Word{operand_id}); - return null; } - fn genRetVoid(self: *DeclGen) !?ResultId { + fn genRetVoid(self: *DeclGen) !void { // TODO: This instruction needs to be the last in a block. Is that guaranteed? try writeInstruction(&self.code, .OpReturn, &[_]Word{}); - return null; } - fn genStore(self: *DeclGen, inst: *Inst.BinOp) !?ResultId { + fn genStore(self: *DeclGen, inst: *Inst.BinOp) !void { const dst_ptr_id = try self.resolve(inst.lhs); const src_val_id = try self.resolve(inst.rhs); @@ -989,12 +980,10 @@ pub const DeclGen = struct { &[_]Word{ dst_ptr_id, src_val_id }; try writeInstruction(&self.code, .OpStore, operands); - return null; } - fn genUnreach(self: *DeclGen) !?ResultId { + fn genUnreach(self: *DeclGen) !void { // TODO: This instruction needs to be the last in a block. Is that guaranteed? try writeInstruction(&self.code, .OpUnreachable, &[_]Word{}); - return null; } }; diff --git a/src/link/SpirV.zig b/src/link/SpirV.zig index 57da89dede..a84a777e61 100644 --- a/src/link/SpirV.zig +++ b/src/link/SpirV.zig @@ -146,7 +146,6 @@ pub fn flushModule(self: *SpirV, comp: *Compilation) !void { if (!decl.has_tv) continue; decl.fn_link.spirv.id = spv.allocResultId(); - log.debug("Allocating id {} to '{s}'", .{ decl.fn_link.spirv.id, std.mem.spanZ(decl.name) }); } } -- cgit v1.2.3