authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-18 00:33:05+02:00
committergravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2021-05-22 16:11:56+02:00
logbcda3c5b828fcb631e739e9c820a56aaaf0fd4df
treec23cdeb9817b03c46c138afe68c901fe6da25eb4
parentf4101c1153980b887e9aa8850ac0a9dd88192140

SPIR-V: Use Value.toFloat instead of switching on value tag when generating float constants


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

src/codegen/spirv.zig+9-8
......@@ -230,6 +230,7 @@ pub const DeclGen = struct {
230230 /// Generate a constant representing `val`.
231231 /// TODO: Deduplication?
232232 fn genConstant(self: *DeclGen, ty: Type, val: Value) Error!u32 {
233 const target = self.module.getTarget();
233234 const code = &self.spv.types_globals_constants;
234235 const result_id = self.spv.allocResultId();
235236 const result_type_id = try self.getOrGenType(ty);
......@@ -250,11 +251,11 @@ pub const DeclGen = struct {
250251
251252 // f16 and f32 require one word of storage. f64 requires 2, low-order first.
252253
253 switch (val.tag()) {
254 .float_16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.castTag(.float_16).?.data) }),
255 .float_32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.castTag(.float_32).?.data) }),
256 .float_64 => {
257 const float_bits = @bitCast(u64, val.castTag(.float_64).?.data);
254 switch (ty.floatBits(target)) {
255 16 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u16, val.toFloat(f16)) }),
256 32 => try writeInstruction(code, .OpConstant, &[_]u32{ result_type_id, result_id, @bitCast(u32, val.toFloat(f32)) }),
257 64 => {
258 const float_bits = @bitCast(u64, val.toFloat(f64));
258259 try writeInstruction(code, .OpConstant, &[_]u32{
259260 result_type_id,
260261 result_id,
......@@ -262,9 +263,9 @@ pub const DeclGen = struct {
262263 @truncate(u32, float_bits >> 32),
263264 });
264265 },
265 .float_128 => unreachable, // Filtered out in the call to getOrGenType.
266 // TODO: What tags do we need to handle here anyway?
267 else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: float constant generation of value {s}\n", .{val.tag()}),
266 128 => unreachable, // Filtered out in the call to getOrGenType.
267 // TODO: Insert case for long double when the layout for that is determined.
268 else => unreachable,
268269 }
269270 },
270271 else => return self.fail(.{ .node_offset = 0 }, "TODO: SPIR-V backend: constant generation of type {s}\n", .{ty.zigTypeTag()}),