authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2022-12-01 23:26:02+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-04-09 01:51:51+02:00
log34b98ee372f8974e21cd34b3ca7588b13d93e31a
treeefd4176707e500d7521fd90a9515ba24ee6e84c7
parentf1229e0f00114e38001df8b57725de9c3e13a06d
signaturelock-open Commit is signed but in an unrecognized format.

spirv: start lowering non-function decls

Start to lower decls which are not functions. These generate an OpVariable instruction by which they can be used later on.

2 files changed, 57 insertions(+), 46 deletions(-)

src/codegen/spirv.zig+53-42
......@@ -225,7 +225,21 @@ pub const DeclGen = struct {
225225 /// Fetch the result-id for a previously generated instruction or constant.
226226 fn resolve(self: *DeclGen, inst: Air.Inst.Ref) !IdRef {
227227 if (self.air.value(inst)) |val| {
228 return self.genConstant(self.air.typeOf(inst), val, .direct);
228 const ty = self.air.typeOf(inst);
229 if (ty.zigTypeTag() == .Fn) {
230 const fn_decl_index = switch (val.tag()) {
231 .extern_fn => val.castTag(.extern_fn).?.data.owner_decl,
232 .function => val.castTag(.function).?.data.owner_decl,
233 else => unreachable,
234 };
235 const decl = self.module.declPtr(fn_decl_index);
236 self.module.markDeclAlive(decl);
237 return self.ids.get(fn_decl_index).?;
238 }
239
240 const result_id = self.spv.allocId();
241 try self.genConstant(result_id, ty, val, .direct);
242 return result_id;
229243 }
230244 const index = Air.refToIndex(inst).?;
231245 return self.inst_results.get(index).?; // Assertion means instruction does not dominate usage.
......@@ -339,7 +353,7 @@ pub const DeclGen = struct {
339353 };
340354 }
341355
342 fn constInt(self: *DeclGen, ty_ref: SpvType.Ref, value: anytype) !IdRef {
356 fn genConstInt(self: *DeclGen, ty_ref: SpvType.Ref, result_id: IdRef, value: anytype) !void {
343357 const ty = self.spv.typeRefType(ty_ref);
344358 const ty_id = self.typeId(ty_ref);
345359
......@@ -356,51 +370,44 @@ pub const DeclGen = struct {
356370 },
357371 };
358372
359 return try self.spv.emitConstant(ty_id, literal);
373 try self.spv.emitConstant(ty_id, result_id, literal);
374 }
375
376 fn constInt(self: *DeclGen, ty_ref: SpvType.Ref, value: anytype) !IdRef {
377 const result_id = self.spv.allocId();
378 try self.genConstInt(ty_ref, result_id, value);
379 return result_id;
360380 }
361381
362382 /// Generate a constant representing `val`.
363383 /// TODO: Deduplication?
364 fn genConstant(self: *DeclGen, ty: Type, val: Value, repr: Repr) Error!IdRef {
365 if (ty.zigTypeTag() == .Fn) {
366 const fn_decl_index = switch (val.tag()) {
367 .extern_fn => val.castTag(.extern_fn).?.data.owner_decl,
368 .function => val.castTag(.function).?.data.owner_decl,
369 else => unreachable,
370 };
371 const decl = self.module.declPtr(fn_decl_index);
372 self.module.markDeclAlive(decl);
373 return self.ids.get(fn_decl_index).?;
374 }
375
384 fn genConstant(self: *DeclGen, result_id: IdRef, ty: Type, val: Value, repr: Repr) Error!void {
376385 const target = self.getTarget();
377386 const section = &self.spv.sections.types_globals_constants;
378387 const result_ty_ref = try self.resolveType(ty, repr);
379388 const result_ty_id = self.typeId(result_ty_ref);
380389
390 log.debug("genConstant: ty = {}, val = {}", .{ ty.fmtDebug(), val.fmtDebug() });
391
381392 if (val.isUndef()) {
382 const result_id = self.spv.allocId();
383393 try section.emit(self.spv.gpa, .OpUndef, .{ .id_result_type = result_ty_id, .id_result = result_id });
384 return result_id;
385394 }
386395
387396 switch (ty.zigTypeTag()) {
388397 .Int => {
389398 const int_bits = if (ty.isSignedInt()) @bitCast(u64, val.toSignedInt(target)) else val.toUnsignedInt(target);
390 return self.constInt(result_ty_ref, int_bits);
399 try self.genConstInt(result_ty_ref, result_id, int_bits);
391400 },
392401 .Bool => switch (repr) {
393402 .direct => {
394 const result_id = self.spv.allocId();
395403 const operands = .{ .id_result_type = result_ty_id, .id_result = result_id };
396404 if (val.toBool()) {
397405 try section.emit(self.spv.gpa, .OpConstantTrue, operands);
398406 } else {
399407 try section.emit(self.spv.gpa, .OpConstantFalse, operands);
400408 }
401 return result_id;
402409 },
403 .indirect => return try self.constInt(result_ty_ref, @boolToInt(val.toBool())),
410 .indirect => try self.genConstInt(result_ty_ref, result_id, @boolToInt(val.toBool())),
404411 },
405412 .Float => {
406413 // At this point we are guaranteed that the target floating point type is supported, otherwise the function
......@@ -415,7 +422,7 @@ pub const DeclGen = struct {
415422 else => unreachable,
416423 };
417424
418 return try self.spv.emitConstant(result_ty_id, literal);
425 try self.spv.emitConstant(result_ty_id, result_id, literal);
419426 },
420427 .Array => switch (val.tag()) {
421428 .aggregate => { // todo: combine with Vector
......@@ -425,15 +432,14 @@ pub const DeclGen = struct {
425432 const constituents = try self.spv.gpa.alloc(IdRef, len);
426433 defer self.spv.gpa.free(constituents);
427434 for (elem_vals[0..len], 0..) |elem_val, i| {
428 constituents[i] = try self.genConstant(elem_ty, elem_val, repr);
435 constituents[i] = self.spv.allocId();
436 try self.genConstant(constituents[i], elem_ty, elem_val, repr);
429437 }
430 const result_id = self.spv.allocId();
431438 try section.emit(self.spv.gpa, .OpConstantComposite, .{
432439 .id_result_type = result_ty_id,
433440 .id_result = result_id,
434441 .constituents = constituents,
435442 });
436 return result_id;
437443 },
438444 .repeated => {
439445 const elem_val = val.castTag(.repeated).?.data;
......@@ -442,20 +448,20 @@ pub const DeclGen = struct {
442448 const constituents = try self.spv.gpa.alloc(IdRef, len);
443449 defer self.spv.gpa.free(constituents);
444450
445 const elem_val_id = try self.genConstant(elem_ty, elem_val, repr);
451 const elem_val_id = self.spv.allocId();
452 try self.genConstant(elem_val_id, elem_ty, elem_val, repr);
446453 for (constituents[0..len]) |*elem| {
447454 elem.* = elem_val_id;
448455 }
449456 if (ty.sentinel()) |sentinel| {
450 constituents[len] = try self.genConstant(elem_ty, sentinel, repr);
457 constituents[len] = self.spv.allocId();
458 try self.genConstant(constituents[len], elem_ty, sentinel, repr);
451459 }
452 const result_id = self.spv.allocId();
453460 try section.emit(self.spv.gpa, .OpConstantComposite, .{
454461 .id_result_type = result_ty_id,
455462 .id_result = result_id,
456463 .constituents = constituents,
457464 });
458 return result_id;
459465 },
460466 else => return self.todo("array constant with tag {s}", .{@tagName(val.tag())}),
461467 },
......@@ -468,22 +474,21 @@ pub const DeclGen = struct {
468474 const elem_refs = try self.gpa.alloc(IdRef, vector_len);
469475 defer self.gpa.free(elem_refs);
470476 for (elem_refs, 0..) |*elem, i| {
471 elem.* = try self.genConstant(elem_ty, elem_vals[i], repr);
477 elem.* = self.spv.allocId();
478 try self.genConstant(elem.*, elem_ty, elem_vals[i], repr);
472479 }
473 const result_id = self.spv.allocId();
474480 try section.emit(self.spv.gpa, .OpConstantComposite, .{
475481 .id_result_type = result_ty_id,
476482 .id_result = result_id,
477483 .constituents = elem_refs,
478484 });
479 return result_id;
480485 },
481486 else => return self.todo("vector constant with tag {s}", .{@tagName(val.tag())}),
482487 },
483488 .Enum => {
484489 var int_buffer: Value.Payload.U64 = undefined;
485490 const int_val = val.enumToInt(ty, &int_buffer).toUnsignedInt(target); // TODO: composite integer constants
486 return self.constInt(result_ty_ref, int_val);
491 return self.genConstInt(result_ty_ref, result_id, int_val);
487492 },
488493 .Struct => {
489494 const constituents = if (ty.isSimpleTupleOrAnonStruct()) blk: {
......@@ -495,7 +500,9 @@ pub const DeclGen = struct {
495500 for (tuple.types, 0..) |field_ty, i| {
496501 const field_val = tuple.values[i];
497502 if (field_val.tag() != .unreachable_value or !field_ty.hasRuntimeBits()) continue;
498 constituents[member_index] = try self.genConstant(field_ty, field_val, repr);
503 const member_id = self.spv.allocId();
504 try self.genConstant(member_id, field_ty, field_val, repr);
505 constituents[member_index] = member_id;
499506 member_index += 1;
500507 }
501508
......@@ -513,7 +520,9 @@ pub const DeclGen = struct {
513520 var member_index: usize = 0;
514521 for (struct_ty.fields.values(), 0..) |field, i| {
515522 if (field.is_comptime or !field.ty.hasRuntimeBits()) continue;
516 constituents[member_index] = try self.genConstant(field.ty, field_vals[i], repr);
523 const member_id = self.spv.allocId();
524 try self.genConstant(member_id, field.ty, field_vals[i], repr);
525 constituents[member_index] = member_id;
517526 member_index += 1;
518527 }
519528
......@@ -521,16 +530,17 @@ pub const DeclGen = struct {
521530 };
522531 defer self.spv.gpa.free(constituents);
523532
524 const result_id = self.spv.allocId();
525533 try section.emit(self.spv.gpa, .OpConstantComposite, .{
526534 .id_result_type = result_ty_id,
527535 .id_result = result_id,
528536 .constituents = constituents,
529537 });
530 return result_id;
538 },
539 .Fn => switch (repr) {
540 .direct => unreachable,
541 .indirect => return self.todo("function pointers", .{}),
531542 },
532543 .Void => unreachable,
533 .Fn => unreachable,
534544 else => return self.todo("constant generation of type {s}: {}", .{ @tagName(ty.zigTypeTag()), ty.fmtDebug() }),
535545 }
536546 }
......@@ -579,6 +589,7 @@ pub const DeclGen = struct {
579589
580590 /// Turn a Zig type into a SPIR-V Type, and return a reference to it.
581591 fn resolveType(self: *DeclGen, ty: Type, repr: Repr) Error!SpvType.Ref {
592 log.debug("resolveType: ty = {}", .{ty.fmtDebug()});
582593 const target = self.getTarget();
583594 switch (ty.zigTypeTag()) {
584595 .Void, .NoReturn => return try self.spv.resolveType(SpvType.initTag(.void)),
......@@ -808,8 +819,7 @@ pub const DeclGen = struct {
808819 .name = fqn,
809820 });
810821 } else {
811 // TODO
812 // return self.todo("generate decl type {}", .{decl.ty.zigTypeTag()});
822 try self.genConstant(result_id, decl.ty, decl.val, .direct);
813823 }
814824 }
815825
......@@ -1417,7 +1427,8 @@ pub const DeclGen = struct {
14171427 .Packed => unreachable, // TODO
14181428 else => {
14191429 const u32_ty_id = self.typeId(try self.intType(.unsigned, 32));
1420 const field_index_id = try self.spv.emitConstant(u32_ty_id, .{ .uint32 = field_index });
1430 const field_index_id = self.spv.allocId();
1431 try self.spv.emitConstant(u32_ty_id, field_index_id, .{ .uint32 = field_index });
14211432 const result_id = self.spv.allocId();
14221433 const result_type_id = try self.resolveTypeId(result_ptr_ty);
14231434 const indexes = [_]IdRef{field_index_id};
......@@ -1960,7 +1971,7 @@ pub const DeclGen = struct {
19601971 return null;
19611972 }
19621973
1963 fn airCall(self: *DeclGen, inst: Air.Inst.Index, modifier: std.builtin.CallOptions.Modifier) !?IdRef {
1974 fn airCall(self: *DeclGen, inst: Air.Inst.Index, modifier: std.builtin.CallModifier) !?IdRef {
19641975 _ = modifier;
19651976
19661977 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
src/codegen/spirv/Module.zig+4-4
......@@ -353,7 +353,8 @@ pub fn emitType(self: *Module, ty: Type) error{OutOfMemory}!IdResultType {
353353
354354 const size_type = Type.initTag(.u32);
355355 const size_type_id = try self.resolveTypeId(size_type);
356 const length_id = try self.emitConstant(size_type_id, .{ .uint32 = info.length });
356 const length_id = self.allocId();
357 try self.emitConstant(size_type_id, length_id, .{ .uint32 = info.length });
357358
358359 try types.emit(self.gpa, .OpTypeArray, .{
359360 .id_result = result_id,
......@@ -558,15 +559,14 @@ fn decorateStruct(self: *Module, target: IdRef, info: *const Type.Payload.Struct
558559pub fn emitConstant(
559560 self: *Module,
560561 ty_id: spec.IdRef,
562 result_id: IdRef,
561563 value: spec.LiteralContextDependentNumber,
562) !IdRef {
563 const result_id = self.allocId();
564) !void {
564565 try self.sections.types_globals_constants.emit(self.gpa, .OpConstant, .{
565566 .id_result_type = ty_id,
566567 .id_result = result_id,
567568 .value = value,
568569 });
569 return result_id;
570570}
571571
572572/// Decorate a result-id.