authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-11-26 12:39:04+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:51:49+02:00
logdae8b4c11f6a59dc6ccd3e4fe327c43eb73c44cb
treeea5f2bba08470e13de778fd3cb429cffaf07a2c7
parent3eafe3033ef83e5b34e3ccbd6e803c7a046df390
signaturelock-open Commit is signed but in an unrecognized format.

spirv: emit OpName for some primitive types

OpName instructions assign a debug name to a type. Some basic types - bool, void, ints, and floats are given a debug name this way. TODO is to extend this to the other types.

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

src/codegen/spirv/Module.zig+48-8
......@@ -75,7 +75,8 @@ sections: struct {
7575 execution_modes: Section = .{},
7676 /// OpString, OpSourcExtension, OpSource, OpSourceContinued.
7777 debug_strings: Section = .{},
78 // OpName, OpMemberName - skip for now.
78 // OpName, OpMemberName.
79 debug_names: Section = .{},
7980 // OpModuleProcessed - skip for now.
8081 /// Annotation instructions (OpDecorate etc).
8182 annotations: Section = .{},
......@@ -115,6 +116,7 @@ pub fn deinit(self: *Module) void {
115116 self.sections.entry_points.deinit(self.gpa);
116117 self.sections.execution_modes.deinit(self.gpa);
117118 self.sections.debug_strings.deinit(self.gpa);
119 self.sections.debug_names.deinit(self.gpa);
118120 self.sections.annotations.deinit(self.gpa);
119121 self.sections.types_globals_constants.deinit(self.gpa);
120122 self.sections.functions.deinit(self.gpa);
......@@ -154,6 +156,7 @@ pub fn flush(self: Module, file: std.fs.File) !void {
154156 self.sections.entry_points.toWords(),
155157 self.sections.execution_modes.toWords(),
156158 self.sections.debug_strings.toWords(),
159 self.sections.debug_names.toWords(),
157160 self.sections.annotations.toWords(),
158161 self.sections.types_globals_constants.toWords(),
159162 self.sections.functions.toWords(),
......@@ -244,12 +247,25 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {
244247 const result_id = self.allocId();
245248 const ref_id = result_id.toRef();
246249 const types = &self.sections.types_globals_constants;
250 const debug_names = &self.sections.debug_names;
247251 const annotations = &self.sections.annotations;
248252 const result_id_operand = .{ .id_result = result_id };
249253
250254 switch (ty.tag()) {
251 .void => try types.emit(self.gpa, .OpTypeVoid, result_id_operand),
252 .bool => try types.emit(self.gpa, .OpTypeBool, result_id_operand),
255 .void => {
256 try types.emit(self.gpa, .OpTypeVoid, result_id_operand);
257 try debug_names.emit(self.gpa, .OpName, .{
258 .target = result_id.toRef(),
259 .name = "void",
260 });
261 },
262 .bool => {
263 try types.emit(self.gpa, .OpTypeBool, result_id_operand);
264 try debug_names.emit(self.gpa, .OpName, .{
265 .target = result_id.toRef(),
266 .name = "bool",
267 });
268 },
253269 .u8,
254270 .u16,
255271 .u32,
......@@ -260,6 +276,7 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {
260276 .i64,
261277 .int,
262278 => {
279 const bits = ty.intFloatBits();
263280 const signedness: spec.LiteralInteger = switch (ty.intSignedness()) {
264281 .unsigned => 0,
265282 .signed => 1,
......@@ -267,14 +284,37 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {
267284
268285 try types.emit(self.gpa, .OpTypeInt, .{
269286 .id_result = result_id,
270 .width = ty.intFloatBits(),
287 .width = bits,
271288 .signedness = signedness,
272289 });
290
291 const ui: []const u8 = switch (signedness) {
292 0 => "u",
293 1 => "i",
294 else => unreachable,
295 };
296 const name = try std.fmt.allocPrint(self.gpa, "{s}{}", .{ ui, bits });
297 defer self.gpa.free(name);
298
299 try debug_names.emit(self.gpa, .OpName, .{
300 .target = result_id.toRef(),
301 .name = name,
302 });
303 },
304 .f16, .f32, .f64 => {
305 const bits = ty.intFloatBits();
306 try types.emit(self.gpa, .OpTypeFloat, .{
307 .id_result = result_id,
308 .width = bits,
309 });
310
311 const name = try std.fmt.allocPrint(self.gpa, "f{}", .{bits});
312 defer self.gpa.free(name);
313 try debug_names.emit(self.gpa, .OpName, .{
314 .target = result_id.toRef(),
315 .name = name,
316 });
273317 },
274 .f16, .f32, .f64 => try types.emit(self.gpa, .OpTypeFloat, .{
275 .id_result = result_id,
276 .width = ty.intFloatBits(),
277 }),
278318 .vector => try types.emit(self.gpa, .OpTypeVector, .{
279319 .id_result = result_id,
280320 .component_type = self.typeResultId(ty.childType()).toRef(),