authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2023-10-18 02:30:25+03:30
committergravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2023-10-18 02:31:16+03:30
loge5d5c1d423c73c94d6c3e9d26bd904eb3807b489
tree1caa6b04f4b39f01cd9a090ba1f1516016c93873
parent364c54460f3e6fb770a19c5572b0951dd903f19d

spirv: switch on bool


4 files changed, 5 insertions(+), 6 deletions(-)

src/codegen/spirv.zig+5-3
...@@ -4098,11 +4098,13 @@ const DeclGen = struct {...@@ -4098,11 +4098,13 @@ const DeclGen = struct {
4098 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {4098 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {
4099 const mod = self.module;4099 const mod = self.module;
4100 const pl_op = self.air.instructions.items(.data)[inst].pl_op;4100 const pl_op = self.air.instructions.items(.data)[inst].pl_op;
4101 const cond = try self.resolve(pl_op.operand);
4102 const cond_ty = self.typeOf(pl_op.operand);4101 const cond_ty = self.typeOf(pl_op.operand);
4102 const cond = try self.resolve(pl_op.operand);
4103 const cond_indirect = try self.convertToIndirect(cond_ty, cond);
4103 const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload);4104 const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload);
41044105
4105 const cond_words: u32 = switch (cond_ty.zigTypeTag(mod)) {4106 const cond_words: u32 = switch (cond_ty.zigTypeTag(mod)) {
4107 .Bool => 1,
4106 .Int => blk: {4108 .Int => blk: {
4107 const bits = cond_ty.intInfo(mod).bits;4109 const bits = cond_ty.intInfo(mod).bits;
4108 const backing_bits = self.backingIntBits(bits) orelse {4110 const backing_bits = self.backingIntBits(bits) orelse {
...@@ -4146,7 +4148,7 @@ const DeclGen = struct {...@@ -4146,7 +4148,7 @@ const DeclGen = struct {
41464148
4147 // Emit the instruction before generating the blocks.4149 // Emit the instruction before generating the blocks.
4148 try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions);4150 try self.func.body.emitRaw(self.spv.gpa, .OpSwitch, 2 + (cond_words + 1) * num_conditions);
4149 self.func.body.writeOperand(IdRef, cond);4151 self.func.body.writeOperand(IdRef, cond_indirect);
4150 self.func.body.writeOperand(IdRef, default);4152 self.func.body.writeOperand(IdRef, default);
41514153
4152 // Emit each of the cases4154 // Emit each of the cases
...@@ -4167,7 +4169,7 @@ const DeclGen = struct {...@@ -4167,7 +4169,7 @@ const DeclGen = struct {
4167 return self.todo("switch on runtime value???", .{});4169 return self.todo("switch on runtime value???", .{});
4168 };4170 };
4169 const int_val = switch (cond_ty.zigTypeTag(mod)) {4171 const int_val = switch (cond_ty.zigTypeTag(mod)) {
4170 .Int => if (cond_ty.isSignedInt(mod)) @as(u64, @bitCast(value.toSignedInt(mod))) else value.toUnsignedInt(mod),4172 .Bool, .Int => if (cond_ty.isSignedInt(mod)) @as(u64, @bitCast(value.toSignedInt(mod))) else value.toUnsignedInt(mod),
4171 .Enum => blk: {4173 .Enum => blk: {
4172 // TODO: figure out of cond_ty is correct (something with enum literals)4174 // TODO: figure out of cond_ty is correct (something with enum literals)
4173 break :blk (try value.intFromEnum(cond_ty, mod)).toUnsignedInt(mod); // TODO: composite integer constants4175 break :blk (try value.intFromEnum(cond_ty, mod)).toUnsignedInt(mod); // TODO: composite integer constants
test/behavior/basic.zig-1
...@@ -715,7 +715,6 @@ test "result location is optional inside error union" {...@@ -715,7 +715,6 @@ test "result location is optional inside error union" {
715 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO715 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
716 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO716 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
717 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO717 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
718 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
719718
720 const x = maybe(true) catch unreachable;719 const x = maybe(true) catch unreachable;
721 try expect(x.? == 42);720 try expect(x.? == 42);
test/behavior/inline_switch.zig-1
...@@ -72,7 +72,6 @@ test "inline switch unions" {...@@ -72,7 +72,6 @@ test "inline switch unions" {
72test "inline else bool" {72test "inline else bool" {
73 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO73 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
74 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO74 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
75 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
7675
77 var a = true;76 var a = true;
78 switch (a) {77 switch (a) {
test/behavior/switch.zig-1
...@@ -118,7 +118,6 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {...@@ -118,7 +118,6 @@ fn trueIfBoolFalseOtherwise(comptime T: type) bool {
118118
119test "switching on booleans" {119test "switching on booleans" {
120 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO120 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
121 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
122121
123 try testSwitchOnBools();122 try testSwitchOnBools();
124 try comptime testSwitchOnBools();123 try comptime testSwitchOnBools();