authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-01-21 01:39:20+01:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-02-04 19:09:29+01:00
log77ef78a0ef00392c4e157ebc170d6c4d98f586fb
treebcb69a0c37a295ef01065e04df4ec07e0a5b9cb8
parent54ec9365498635aa127ff13dfbdd3942890b53d0
signaturebadge-check Signed by SSH key SHA256:ZS52FNyUv2WUXvO4njmVaFVO46RHojFuOrxRc4LuKzg

spirv: clean up arithmeticTypeInfo a bit

- No longer returns an error - Returns more useful vector info

1 files changed, 31 insertions(+), 38 deletions(-)

src/codegen/spirv.zig+31-38
......@@ -373,8 +373,9 @@ const DeclGen = struct {
373373 /// For `composite_integer` this is 0 (TODO)
374374 backing_bits: u16,
375375
376 /// Whether the type is a vector.
377 is_vector: bool,
376 /// Null if this type is a scalar, or the length
377 /// of the vector otherwise.
378 vector_len: ?u32,
378379
379380 /// Whether the inner type is signed. Only relevant for integers.
380381 signedness: std.builtin.Signedness,
......@@ -597,32 +598,37 @@ const DeclGen = struct {
597598 return self.backingIntBits(ty) == null;
598599 }
599600
600 fn arithmeticTypeInfo(self: *DeclGen, ty: Type) !ArithmeticTypeInfo {
601 fn arithmeticTypeInfo(self: *DeclGen, ty: Type) ArithmeticTypeInfo {
601602 const mod = self.module;
602603 const target = self.getTarget();
603 return switch (ty.zigTypeTag(mod)) {
604 var scalar_ty = ty.scalarType(mod);
605 if (scalar_ty.zigTypeTag(mod) == .Enum) {
606 scalar_ty = scalar_ty.intTagType(mod);
607 }
608 const vector_len = if (ty.isVector(mod)) ty.vectorLen(mod) else null;
609 return switch (scalar_ty.zigTypeTag(mod)) {
604610 .Bool => ArithmeticTypeInfo{
605611 .bits = 1, // Doesn't matter for this class.
606612 .backing_bits = self.backingIntBits(1).?,
607 .is_vector = false,
613 .vector_len = vector_len,
608614 .signedness = .unsigned, // Technically, but doesn't matter for this class.
609615 .class = .bool,
610616 },
611617 .Float => ArithmeticTypeInfo{
612 .bits = ty.floatBits(target),
613 .backing_bits = ty.floatBits(target), // TODO: F80?
614 .is_vector = false,
618 .bits = scalar_ty.floatBits(target),
619 .backing_bits = scalar_ty.floatBits(target), // TODO: F80?
620 .vector_len = vector_len,
615621 .signedness = .signed, // Technically, but doesn't matter for this class.
616622 .class = .float,
617623 },
618624 .Int => blk: {
619 const int_info = ty.intInfo(mod);
625 const int_info = scalar_ty.intInfo(mod);
620626 // TODO: Maybe it's useful to also return this value.
621627 const maybe_backing_bits = self.backingIntBits(int_info.bits);
622628 break :blk ArithmeticTypeInfo{
623629 .bits = int_info.bits,
624630 .backing_bits = maybe_backing_bits orelse 0,
625 .is_vector = false,
631 .vector_len = vector_len,
626632 .signedness = int_info.signedness,
627633 .class = if (maybe_backing_bits) |backing_bits|
628634 if (backing_bits == int_info.bits)
......@@ -633,22 +639,9 @@ const DeclGen = struct {
633639 .composite_integer,
634640 };
635641 },
636 .Enum => return self.arithmeticTypeInfo(ty.intTagType(mod)),
637 // As of yet, there is no vector support in the self-hosted compiler.
638 .Vector => blk: {
639 const child_type = ty.childType(mod);
640 const child_ty_info = try self.arithmeticTypeInfo(child_type);
641 break :blk ArithmeticTypeInfo{
642 .bits = child_ty_info.bits,
643 .backing_bits = child_ty_info.backing_bits,
644 .is_vector = true,
645 .signedness = child_ty_info.signedness,
646 .class = child_ty_info.class,
647 };
648 },
649 // TODO: For which types is this the case?
650 // else => self.todo("implement arithmeticTypeInfo for {}", .{ty.fmt(self.module)}),
651 else => unreachable,
642 .Enum => unreachable,
643 .Vector => unreachable,
644 else => unreachable, // Unhandled arithmetic type
652645 };
653646 }
654647
......@@ -2336,7 +2329,7 @@ const DeclGen = struct {
23362329 const shift_ty = self.typeOf(bin_op.rhs);
23372330 const scalar_shift_ty_ref = try self.resolveType(shift_ty.scalarType(mod), .direct);
23382331
2339 const info = try self.arithmeticTypeInfo(result_ty);
2332 const info = self.arithmeticTypeInfo(result_ty);
23402333 switch (info.class) {
23412334 .composite_integer => return self.todo("shift ops for composite integers", .{}),
23422335 .integer, .strange_integer => {},
......@@ -2393,7 +2386,7 @@ const DeclGen = struct {
23932386
23942387 fn minMax(self: *DeclGen, result_ty: Type, op: std.math.CompareOperator, lhs_id: IdRef, rhs_id: IdRef) !IdRef {
23952388 const result_ty_ref = try self.resolveType(result_ty, .direct);
2396 const info = try self.arithmeticTypeInfo(result_ty);
2389 const info = self.arithmeticTypeInfo(result_ty);
23972390
23982391 // TODO: Use fmin for OpenCL
23992392 const cmp_id = try self.cmp(op, Type.bool, result_ty, lhs_id, rhs_id);
......@@ -2516,7 +2509,7 @@ const DeclGen = struct {
25162509 ) !IdRef {
25172510 // Binary operations are generally applicable to both scalar and vector operations
25182511 // in SPIR-V, but int and float versions of operations require different opcodes.
2519 const info = try self.arithmeticTypeInfo(ty);
2512 const info = self.arithmeticTypeInfo(ty);
25202513
25212514 const opcode_index: usize = switch (info.class) {
25222515 .composite_integer => {
......@@ -2579,7 +2572,7 @@ const DeclGen = struct {
25792572
25802573 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
25812574
2582 const info = try self.arithmeticTypeInfo(operand_ty);
2575 const info = self.arithmeticTypeInfo(operand_ty);
25832576 switch (info.class) {
25842577 .composite_integer => return self.todo("overflow ops for composite integers", .{}),
25852578 .strange_integer, .integer => {},
......@@ -2693,7 +2686,7 @@ const DeclGen = struct {
26932686
26942687 const bool_ty_ref = try self.resolveType(Type.bool, .direct);
26952688
2696 const info = try self.arithmeticTypeInfo(operand_ty);
2689 const info = self.arithmeticTypeInfo(operand_ty);
26972690 switch (info.class) {
26982691 .composite_integer => return self.todo("overflow shift for composite integers", .{}),
26992692 .integer, .strange_integer => {},
......@@ -2777,7 +2770,7 @@ const DeclGen = struct {
27772770 const scalar_ty_ref = try self.resolveType(scalar_ty, .direct);
27782771 const scalar_ty_id = self.typeId(scalar_ty_ref);
27792772
2780 const info = try self.arithmeticTypeInfo(operand_ty);
2773 const info = self.arithmeticTypeInfo(operand_ty);
27812774
27822775 var result_id = try self.extractField(scalar_ty, operand, 0);
27832776 const len = operand_ty.vectorLen(mod);
......@@ -3093,7 +3086,7 @@ const DeclGen = struct {
30933086 };
30943087
30953088 const opcode: Opcode = opcode: {
3096 const info = try self.arithmeticTypeInfo(op_ty);
3089 const info = self.arithmeticTypeInfo(op_ty);
30973090 const signedness = switch (info.class) {
30983091 .composite_integer => {
30993092 return self.todo("binary operations for composite integers", .{});
......@@ -3245,8 +3238,8 @@ const DeclGen = struct {
32453238 const dst_ty = self.typeOfIndex(inst);
32463239 const dst_ty_ref = try self.resolveType(dst_ty, .direct);
32473240
3248 const src_info = try self.arithmeticTypeInfo(src_ty);
3249 const dst_info = try self.arithmeticTypeInfo(dst_ty);
3241 const src_info = self.arithmeticTypeInfo(src_ty);
3242 const dst_info = self.arithmeticTypeInfo(dst_ty);
32503243
32513244 if (src_info.backing_bits == dst_info.backing_bits) {
32523245 return operand_id;
......@@ -3302,7 +3295,7 @@ const DeclGen = struct {
33023295 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
33033296 const operand_ty = self.typeOf(ty_op.operand);
33043297 const operand_id = try self.resolve(ty_op.operand);
3305 const operand_info = try self.arithmeticTypeInfo(operand_ty);
3298 const operand_info = self.arithmeticTypeInfo(operand_ty);
33063299 const dest_ty = self.typeOfIndex(inst);
33073300 const dest_ty_id = try self.resolveTypeId(dest_ty);
33083301
......@@ -3328,7 +3321,7 @@ const DeclGen = struct {
33283321 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
33293322 const operand_id = try self.resolve(ty_op.operand);
33303323 const dest_ty = self.typeOfIndex(inst);
3331 const dest_info = try self.arithmeticTypeInfo(dest_ty);
3324 const dest_info = self.arithmeticTypeInfo(dest_ty);
33323325 const dest_ty_id = try self.resolveTypeId(dest_ty);
33333326
33343327 const result_id = self.spv.allocId();
......@@ -3369,7 +3362,7 @@ const DeclGen = struct {
33693362 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
33703363 const operand_id = try self.resolve(ty_op.operand);
33713364 const result_ty = self.typeOfIndex(inst);
3372 const info = try self.arithmeticTypeInfo(result_ty);
3365 const info = self.arithmeticTypeInfo(result_ty);
33733366
33743367 var wip = try self.elementWise(result_ty);
33753368 defer wip.deinit();