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 {...@@ -75,7 +75,8 @@ sections: struct {
75 execution_modes: Section = .{},75 execution_modes: Section = .{},
76 /// OpString, OpSourcExtension, OpSource, OpSourceContinued.76 /// OpString, OpSourcExtension, OpSource, OpSourceContinued.
77 debug_strings: Section = .{},77 debug_strings: Section = .{},
78 // OpName, OpMemberName - skip for now.78 // OpName, OpMemberName.
79 debug_names: Section = .{},
79 // OpModuleProcessed - skip for now.80 // OpModuleProcessed - skip for now.
80 /// Annotation instructions (OpDecorate etc).81 /// Annotation instructions (OpDecorate etc).
81 annotations: Section = .{},82 annotations: Section = .{},
...@@ -115,6 +116,7 @@ pub fn deinit(self: *Module) void {...@@ -115,6 +116,7 @@ pub fn deinit(self: *Module) void {
115 self.sections.entry_points.deinit(self.gpa);116 self.sections.entry_points.deinit(self.gpa);
116 self.sections.execution_modes.deinit(self.gpa);117 self.sections.execution_modes.deinit(self.gpa);
117 self.sections.debug_strings.deinit(self.gpa);118 self.sections.debug_strings.deinit(self.gpa);
119 self.sections.debug_names.deinit(self.gpa);
118 self.sections.annotations.deinit(self.gpa);120 self.sections.annotations.deinit(self.gpa);
119 self.sections.types_globals_constants.deinit(self.gpa);121 self.sections.types_globals_constants.deinit(self.gpa);
120 self.sections.functions.deinit(self.gpa);122 self.sections.functions.deinit(self.gpa);
...@@ -154,6 +156,7 @@ pub fn flush(self: Module, file: std.fs.File) !void {...@@ -154,6 +156,7 @@ pub fn flush(self: Module, file: std.fs.File) !void {
154 self.sections.entry_points.toWords(),156 self.sections.entry_points.toWords(),
155 self.sections.execution_modes.toWords(),157 self.sections.execution_modes.toWords(),
156 self.sections.debug_strings.toWords(),158 self.sections.debug_strings.toWords(),
159 self.sections.debug_names.toWords(),
157 self.sections.annotations.toWords(),160 self.sections.annotations.toWords(),
158 self.sections.types_globals_constants.toWords(),161 self.sections.types_globals_constants.toWords(),
159 self.sections.functions.toWords(),162 self.sections.functions.toWords(),
...@@ -244,12 +247,25 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {...@@ -244,12 +247,25 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {
244 const result_id = self.allocId();247 const result_id = self.allocId();
245 const ref_id = result_id.toRef();248 const ref_id = result_id.toRef();
246 const types = &self.sections.types_globals_constants;249 const types = &self.sections.types_globals_constants;
250 const debug_names = &self.sections.debug_names;
247 const annotations = &self.sections.annotations;251 const annotations = &self.sections.annotations;
248 const result_id_operand = .{ .id_result = result_id };252 const result_id_operand = .{ .id_result = result_id };
249253
250 switch (ty.tag()) {254 switch (ty.tag()) {
251 .void => try types.emit(self.gpa, .OpTypeVoid, result_id_operand),255 .void => {
252 .bool => try types.emit(self.gpa, .OpTypeBool, result_id_operand),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 },
253 .u8,269 .u8,
254 .u16,270 .u16,
255 .u32,271 .u32,
...@@ -260,6 +276,7 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {...@@ -260,6 +276,7 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {
260 .i64,276 .i64,
261 .int,277 .int,
262 => {278 => {
279 const bits = ty.intFloatBits();
263 const signedness: spec.LiteralInteger = switch (ty.intSignedness()) {280 const signedness: spec.LiteralInteger = switch (ty.intSignedness()) {
264 .unsigned => 0,281 .unsigned => 0,
265 .signed => 1,282 .signed => 1,
...@@ -267,14 +284,37 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {...@@ -267,14 +284,37 @@ pub fn emitType(self: *Module, ty: Type) !IdResultType {
267284
268 try types.emit(self.gpa, .OpTypeInt, .{285 try types.emit(self.gpa, .OpTypeInt, .{
269 .id_result = result_id,286 .id_result = result_id,
270 .width = ty.intFloatBits(),287 .width = bits,
271 .signedness = signedness,288 .signedness = signedness,
272 });289 });
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 });
273 },317 },
274 .f16, .f32, .f64 => try types.emit(self.gpa, .OpTypeFloat, .{
275 .id_result = result_id,
276 .width = ty.intFloatBits(),
277 }),
278 .vector => try types.emit(self.gpa, .OpTypeVector, .{318 .vector => try types.emit(self.gpa, .OpTypeVector, .{
279 .id_result = result_id,319 .id_result = result_id,
280 .component_type = self.typeResultId(ty.childType()).toRef(),320 .component_type = self.typeResultId(ty.childType()).toRef(),