authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-20 18:02:30+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2023-05-20 18:02:30+02:00
logc92cc5798f0ea72ab1c77ae12c6124e5ab090730
treed94372b5d0575be57dde8bfb072d4fd519951490
parent65157d30ab90de01da583dd97ee2409d8ad8aeb0
signaturelock-open Commit is signed but in an unrecognized format.

spirv: make constant handle float, errorset, errorunion

This is in preparation of removing indirect lowering again. Also modifies constant() to accept a repr so that both direct as well as indirect representations can be generated. Indirect is not yet used, but will be used for globals.

2 files changed, 76 insertions(+), 13 deletions(-)

src/codegen/spirv.zig+66-13
......@@ -242,7 +242,7 @@ pub const DeclGen = struct {
242242 return self.spv.declPtr(spv_decl_index).result_id;
243243 }
244244
245 return try self.constant(ty, val);
245 return try self.constant(ty, val, .direct);
246246 }
247247 const index = Air.refToIndex(inst).?;
248248 return self.inst_results.get(index).?; // Assertion means instruction does not dominate usage.
......@@ -1021,14 +1021,16 @@ pub const DeclGen = struct {
10211021 /// the constant is more complicated however, it needs to be lowered to an indirect constant, which
10221022 /// is then loaded using OpLoad. Such values are loaded into the UniformConstant storage class by default.
10231023 /// This function should only be called during function code generation.
1024 fn constant(self: *DeclGen, ty: Type, val: Value) !IdRef {
1024 fn constant(self: *DeclGen, ty: Type, val: Value, repr: Repr) !IdRef {
10251025 const target = self.getTarget();
10261026 const section = &self.spv.sections.types_globals_constants;
1027 const result_ty_ref = try self.resolveType(ty, .direct);
1027 const result_ty_ref = try self.resolveType(ty, repr);
10281028 const result_ty_id = self.typeId(result_ty_ref);
1029 const result_id = self.spv.allocId();
1029
1030 log.debug("constant: ty = {}, val = {}", .{ ty.fmt(self.module), val.fmtValue(ty, self.module) });
10301031
10311032 if (val.isUndef()) {
1033 const result_id = self.spv.allocId();
10321034 try section.emit(self.spv.gpa, .OpUndef, .{
10331035 .id_result_type = result_ty_id,
10341036 .id_result = result_id,
......@@ -1039,24 +1041,76 @@ pub const DeclGen = struct {
10391041 switch (ty.zigTypeTag()) {
10401042 .Int => {
10411043 if (ty.isSignedInt()) {
1042 try self.genConstInt(result_ty_ref, result_id, val.toSignedInt(target));
1044 return try self.constInt(result_ty_ref, val.toSignedInt(target));
10431045 } else {
1044 try self.genConstInt(result_ty_ref, result_id, val.toUnsignedInt(target));
1046 return try self.constInt(result_ty_ref, val.toUnsignedInt(target));
1047 }
1048 },
1049 .Bool => switch (repr) {
1050 .direct => {
1051 const result_id = self.spv.allocId();
1052 const operands = .{ .id_result_type = result_ty_id, .id_result = result_id };
1053 if (val.toBool()) {
1054 try section.emit(self.spv.gpa, .OpConstantTrue, operands);
1055 } else {
1056 try section.emit(self.spv.gpa, .OpConstantFalse, operands);
1057 }
1058 return result_id;
1059 },
1060 .indirect => return try self.constInt(result_ty_ref, @boolToInt(val.toBool())),
1061 },
1062 .Float => {
1063 const result_id = self.spv.allocId();
1064 switch (ty.floatBits(target)) {
1065 16 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f16) }),
1066 32 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float32 = val.toFloat(f32) }),
1067 64 => try self.spv.emitConstant(result_ty_id, result_id, .{ .float64 = val.toFloat(f64) }),
1068 80, 128 => unreachable, // TODO
1069 else => unreachable,
10451070 }
1071 return result_id;
10461072 },
1047 .Bool => {
1048 const operands = .{ .id_result_type = result_ty_id, .id_result = result_id };
1049 if (val.toBool()) {
1050 try section.emit(self.spv.gpa, .OpConstantTrue, operands);
1073 .ErrorSet => {
1074 const value = switch (val.tag()) {
1075 .@"error" => blk: {
1076 const err_name = val.castTag(.@"error").?.data.name;
1077 const kv = try self.module.getErrorValue(err_name);
1078 break :blk @intCast(u16, kv.value);
1079 },
1080 .zero => 0,
1081 else => unreachable,
1082 };
1083
1084 return try self.constInt(result_ty_ref, value);
1085 },
1086 .ErrorUnion => {
1087 const payload_ty = ty.errorUnionPayload();
1088 const is_pl = val.errorUnionIsPayload();
1089 const error_val = if (!is_pl) val else Value.initTag(.zero);
1090
1091 const eu_layout = self.errorUnionLayout(payload_ty);
1092 if (!eu_layout.payload_has_bits) {
1093 return try self.constant(Type.anyerror, error_val, repr);
1094 }
1095
1096 const payload_val = if (val.castTag(.eu_payload)) |pl| pl.data else Value.initTag(.undef);
1097
1098 var members: [2]IdRef = undefined;
1099 if (eu_layout.error_first) {
1100 members[0] = try self.constant(Type.anyerror, error_val, .indirect);
1101 members[1] = try self.constant(payload_ty, payload_val, .indirect);
10511102 } else {
1052 try section.emit(self.spv.gpa, .OpConstantFalse, operands);
1103 members[0] = try self.constant(payload_ty, payload_val, .indirect);
1104 members[1] = try self.constant(Type.anyerror, error_val, .indirect);
10531105 }
1106 return try self.spv.constComposite(result_ty_ref, &members);
10541107 },
10551108 // TODO: We can handle most pointers here (decl refs etc), because now they emit an extra
10561109 // OpVariable that is not really required.
10571110 else => {
10581111 // The value cannot be generated directly, so generate it as an indirect constant,
10591112 // and then perform an OpLoad.
1113 const result_id = self.spv.allocId();
10601114 const alignment = ty.abiAlignment(target);
10611115 const spv_decl_index = try self.spv.allocDecl(.global);
10621116
......@@ -1078,10 +1132,9 @@ pub const DeclGen = struct {
10781132 });
10791133 // TODO: Convert bools? This logic should hook into `load`. It should be a dead
10801134 // path though considering .Bool is handled above.
1135 return result_id;
10811136 },
10821137 }
1083
1084 return result_id;
10851138 }
10861139
10871140 /// Turn a Zig type into a SPIR-V Type, and return its type result-id.
src/codegen/spirv/Module.zig+10
......@@ -774,6 +774,16 @@ pub fn changePtrStorageClass(self: *Module, ptr_ty_ref: Type.Ref, new_storage_cl
774774 return try self.resolveType(Type.initPayload(&payload.base));
775775}
776776
777pub fn constComposite(self: *Module, ty_ref: Type.Ref, members: []const IdRef) !IdRef {
778 const result_id = self.allocId();
779 try self.sections.types_globals_constants.emit(self.gpa, .OpSpecConstantComposite, .{
780 .id_result_type = self.typeId(ty_ref),
781 .id_result = result_id,
782 .constituents = members,
783 });
784 return result_id;
785}
786
777787pub fn emitConstant(
778788 self: *Module,
779789 ty_id: IdRef,