authorgravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2024-02-18 13:39:32+03:30
committergravatar for alichraghi@proton.meAli Chraghi <alichraghi@proton.me> 2024-04-06 08:50:02+03:30
log0f75143c621b239dd79abc32de0911a68fa7caa4
tree0972b885452801b0a4c79522c4121ddd31348790
parent23f729aec96cec68be15436db2a38e37d54fbe8f

spirv: implement `@divFloor`, `@floor` and `@mod`


5 files changed, 132 insertions(+), 25 deletions(-)

src/codegen/spirv.zig+130-18
......@@ -2317,6 +2317,9 @@ const DeclGen = struct {
23172317 .mul, .mul_wrap, .mul_optimized => try self.airArithOp(inst, .OpFMul, .OpIMul, .OpIMul),
23182318
23192319 .abs => try self.airAbs(inst),
2320 .floor => try self.airFloor(inst),
2321
2322 .div_floor => try self.airDivFloor(inst),
23202323
23212324 .div_float,
23222325 .div_float_optimized,
......@@ -2328,6 +2331,11 @@ const DeclGen = struct {
23282331 .rem,
23292332 .rem_optimized,
23302333 => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem),
2334 // TODO: Check if this is the right operation
2335 .mod,
2336 .mod_optimized,
2337 => try self.airArithOp(inst, .OpFMod, .OpSMod, .OpSMod),
2338
23312339
23322340 .add_with_overflow => try self.airAddSubOverflow(inst, .OpIAdd, .OpULessThan, .OpSLessThan),
23332341 .sub_with_overflow => try self.airAddSubOverflow(inst, .OpISub, .OpUGreaterThan, .OpSGreaterThan),
......@@ -2661,6 +2669,95 @@ const DeclGen = struct {
26612669 }
26622670 }
26632671
2672 fn airDivFloor(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2673 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2674 const lhs_id = try self.resolve(bin_op.lhs);
2675 const rhs_id = try self.resolve(bin_op.rhs);
2676 const ty = self.typeOfIndex(inst);
2677 const ty_ref = try self.resolveType(ty, .direct);
2678 const info = self.arithmeticTypeInfo(ty);
2679 switch (info.class) {
2680 .composite_integer => unreachable, // TODO
2681 .integer, .strange_integer => {
2682 const zero_id = try self.constInt(ty_ref, 0);
2683 const one_id = try self.constInt(ty_ref, 1);
2684
2685 // (a ^ b) > 0
2686 const bin_bitwise_id = try self.binOpSimple(ty, lhs_id, rhs_id, .OpBitwiseXor);
2687 const is_positive_id = try self.cmp(.gt, Type.bool, ty, bin_bitwise_id, zero_id);
2688
2689 // a / b
2690 const positive_div_id = try self.arithOp(ty, lhs_id, rhs_id, .OpFDiv, .OpSDiv, .OpUDiv);
2691
2692 // - (abs(a) + abs(b) - 1) / abs(b)
2693 const lhs_abs = try self.abs(ty, ty, lhs_id);
2694 const rhs_abs = try self.abs(ty, ty, rhs_id);
2695 const negative_div_lhs = try self.arithOp(
2696 ty,
2697 try self.arithOp(ty, lhs_abs, rhs_abs, .OpFAdd, .OpIAdd, .OpIAdd),
2698 one_id,
2699 .OpFSub,
2700 .OpISub,
2701 .OpISub,
2702 );
2703 const negative_div_id = try self.arithOp(ty, negative_div_lhs, rhs_abs, .OpFDiv, .OpSDiv, .OpUDiv);
2704 const negated_negative_div_id = self.spv.allocId();
2705 try self.func.body.emit(self.spv.gpa, .OpSNegate, .{
2706 .id_result_type = self.typeId(ty_ref),
2707 .id_result = negated_negative_div_id,
2708 .operand = negative_div_id,
2709 });
2710
2711 const result_id = self.spv.allocId();
2712 try self.func.body.emit(self.spv.gpa, .OpSelect, .{
2713 .id_result_type = self.typeId(ty_ref),
2714 .id_result = result_id,
2715 .condition = is_positive_id,
2716 .object_1 = positive_div_id,
2717 .object_2 = negated_negative_div_id,
2718 });
2719 return result_id;
2720 },
2721 .float => {
2722 const div_id = try self.arithOp(ty, lhs_id, rhs_id, .OpFDiv, .OpSDiv, .OpUDiv);
2723 return try self.floor(ty, div_id);
2724 },
2725 .bool => unreachable,
2726 }
2727 }
2728
2729 fn airFloor(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2730 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
2731 const operand_id = try self.resolve(un_op);
2732 const result_ty = self.typeOfIndex(inst);
2733 return try self.floor(result_ty, operand_id);
2734 }
2735
2736 fn floor(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef {
2737 const target = self.getTarget();
2738 const ty_ref = try self.resolveType(ty, .direct);
2739 const ext_inst: Word = switch (target.os.tag) {
2740 .opencl => 25,
2741 .vulkan => 8,
2742 else => unreachable,
2743 };
2744 const set_id = switch (target.os.tag) {
2745 .opencl => try self.spv.importInstructionSet(.opencl),
2746 .vulkan => try self.spv.importInstructionSet(.glsl),
2747 else => unreachable,
2748 };
2749
2750 const result_id = self.spv.allocId();
2751 try self.func.body.emit(self.spv.gpa, .OpExtInst, .{
2752 .id_result_type = self.typeId(ty_ref),
2753 .id_result = result_id,
2754 .set = set_id,
2755 .instruction = .{ .inst = ext_inst },
2756 .id_ref_4 = &.{operand_id},
2757 });
2758 return result_id;
2759 }
2760
26642761 fn airArithOp(
26652762 self: *DeclGen,
26662763 inst: Air.Inst.Index,
......@@ -2668,7 +2765,6 @@ const DeclGen = struct {
26682765 comptime sop: Opcode,
26692766 comptime uop: Opcode,
26702767 ) !?IdRef {
2671
26722768 // LHS and RHS are guaranteed to have the same type, and AIR guarantees
26732769 // the result to be the same as the LHS and RHS, which matches SPIR-V.
26742770 const ty = self.typeOfIndex(inst);
......@@ -2737,12 +2833,16 @@ const DeclGen = struct {
27372833 }
27382834
27392835 fn airAbs(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2740 const target = self.getTarget();
27412836 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
27422837 const operand_id = try self.resolve(ty_op.operand);
27432838 // Note: operand_ty may be signed, while ty is always unsigned!
27442839 const operand_ty = self.typeOf(ty_op.operand);
27452840 const result_ty = self.typeOfIndex(inst);
2841 return try self.abs(result_ty, operand_ty, operand_id);
2842 }
2843
2844 fn abs(self: *DeclGen, result_ty: Type, operand_ty: Type, operand_id: IdRef) !IdRef {
2845 const target = self.getTarget();
27462846 const operand_info = self.arithmeticTypeInfo(operand_ty);
27472847
27482848 var wip = try self.elementWise(result_ty, false);
......@@ -3692,19 +3792,22 @@ const DeclGen = struct {
36923792 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
36933793 const operand_ty = self.typeOf(ty_op.operand);
36943794 const operand_id = try self.resolve(ty_op.operand);
3695 const operand_info = self.arithmeticTypeInfo(operand_ty);
3696 const dest_ty = self.typeOfIndex(inst);
3697 const dest_ty_id = try self.resolveTypeId(dest_ty);
3795 const result_ty = self.typeOfIndex(inst);
3796 const result_ty_ref = try self.resolveType(result_ty, .direct);
3797 return try self.floatFromInt(result_ty_ref, operand_ty, operand_id);
3798 }
36983799
3800 fn floatFromInt(self: *DeclGen, result_ty_ref: CacheRef, operand_ty: Type, operand_id: IdRef) !IdRef {
3801 const operand_info = self.arithmeticTypeInfo(operand_ty);
36993802 const result_id = self.spv.allocId();
37003803 switch (operand_info.signedness) {
37013804 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{
3702 .id_result_type = dest_ty_id,
3805 .id_result_type = self.typeId(result_ty_ref),
37033806 .id_result = result_id,
37043807 .signed_value = operand_id,
37053808 }),
37063809 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{
3707 .id_result_type = dest_ty_id,
3810 .id_result_type = self.typeId(result_ty_ref),
37083811 .id_result = result_id,
37093812 .unsigned_value = operand_id,
37103813 }),
......@@ -3715,19 +3818,22 @@ const DeclGen = struct {
37153818 fn airIntFromFloat(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
37163819 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
37173820 const operand_id = try self.resolve(ty_op.operand);
3718 const dest_ty = self.typeOfIndex(inst);
3719 const dest_info = self.arithmeticTypeInfo(dest_ty);
3720 const dest_ty_id = try self.resolveTypeId(dest_ty);
3821 const result_ty = self.typeOfIndex(inst);
3822 return try self.intFromFloat(result_ty, operand_id);
3823 }
37213824
3825 fn intFromFloat(self: *DeclGen, result_ty: Type, operand_id: IdRef) !IdRef {
3826 const result_info = self.arithmeticTypeInfo(result_ty);
3827 const result_ty_ref = try self.resolveType(result_ty, .direct);
37223828 const result_id = self.spv.allocId();
3723 switch (dest_info.signedness) {
3829 switch (result_info.signedness) {
37243830 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{
3725 .id_result_type = dest_ty_id,
3831 .id_result_type = self.typeId(result_ty_ref),
37263832 .id_result = result_id,
37273833 .float_value = operand_id,
37283834 }),
37293835 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{
3730 .id_result_type = dest_ty_id,
3836 .id_result_type = self.typeId(result_ty_ref),
37313837 .id_result = result_id,
37323838 .float_value = operand_id,
37333839 }),
......@@ -5237,14 +5343,15 @@ const DeclGen = struct {
52375343
52385344 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {
52395345 const mod = self.module;
5346 const target = self.getTarget();
52405347 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
52415348 const cond_ty = self.typeOf(pl_op.operand);
52425349 const cond = try self.resolve(pl_op.operand);
5243 const cond_indirect = try self.convertToIndirect(cond_ty, cond);
5350 var cond_indirect = try self.convertToIndirect(cond_ty, cond);
52445351 const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload);
52455352
52465353 const cond_words: u32 = switch (cond_ty.zigTypeTag(mod)) {
5247 .Bool => 1,
5354 .Bool, .ErrorSet => 1,
52485355 .Int => blk: {
52495356 const bits = cond_ty.intInfo(mod).bits;
52505357 const backing_bits = self.backingIntBits(bits) orelse {
......@@ -5260,8 +5367,12 @@ const DeclGen = struct {
52605367 };
52615368 break :blk if (backing_bits <= 32) @as(u32, 1) else 2;
52625369 },
5263 .ErrorSet => 1,
5264 else => return self.todo("implement switch for type {s}", .{@tagName(cond_ty.zigTypeTag(mod))}), // TODO: Figure out which types apply here, and work around them as we can only do integers.
5370 .Pointer => blk: {
5371 cond_indirect = try self.intFromPtr(cond_indirect);
5372 break :blk target.ptrBitWidth() / 32;
5373 },
5374 // TODO: Figure out which types apply here, and work around them as we can only do integers.
5375 else => return self.todo("implement switch for type {s}", .{@tagName(cond_ty.zigTypeTag(mod))}),
52655376 };
52665377
52675378 const num_cases = switch_br.data.cases_len;
......@@ -5316,13 +5427,14 @@ const DeclGen = struct {
53165427
53175428 for (items) |item| {
53185429 const value = (try self.air.value(item, mod)) orelse unreachable;
5319 const int_val = switch (cond_ty.zigTypeTag(mod)) {
5430 const int_val: u64 = switch (cond_ty.zigTypeTag(mod)) {
53205431 .Bool, .Int => if (cond_ty.isSignedInt(mod)) @as(u64, @bitCast(value.toSignedInt(mod))) else value.toUnsignedInt(mod),
53215432 .Enum => blk: {
53225433 // TODO: figure out of cond_ty is correct (something with enum literals)
53235434 break :blk (try value.intFromEnum(cond_ty, mod)).toUnsignedInt(mod); // TODO: composite integer constants
53245435 },
53255436 .ErrorSet => value.getErrorInt(mod),
5437 .Pointer => value.toUnsignedInt(mod),
53265438 else => unreachable,
53275439 };
53285440 const int_lit: spec.LiteralContextDependentNumber = switch (cond_words) {
src/codegen/spirv/Module.zig+2-2
......@@ -429,8 +429,8 @@ pub fn constInt(self: *Module, ty_ref: CacheRef, value: anytype) !IdRef {
429429 return try self.resolveId(.{ .int = .{
430430 .ty = ty_ref,
431431 .value = switch (ty.signedness) {
432 .signed => Value{ .int64 = @as(i64, @intCast(value)) },
433 .unsigned => Value{ .uint64 = @as(u64, @intCast(value)) },
432 .signed => Value{ .int64 = @intCast(value) },
433 .unsigned => Value{ .uint64 = @intCast(value) },
434434 },
435435 } });
436436}
test/behavior/floatop.zig-3
......@@ -1089,7 +1089,6 @@ test "@floor f16" {
10891089 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
10901090 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
10911091 if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
1092 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
10931092
10941093 try testFloor(f16);
10951094 try comptime testFloor(f16);
......@@ -1100,7 +1099,6 @@ test "@floor f32/f64" {
11001099 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11011100 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
11021101 if (builtin.zig_backend == .stage2_x86_64 and builtin.target.ofmt != .elf and builtin.target.ofmt != .macho) return error.SkipZigTest;
1103 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11041102
11051103 try testFloor(f32);
11061104 try comptime testFloor(f32);
......@@ -1162,7 +1160,6 @@ fn testFloor(comptime T: type) !void {
11621160test "@floor with vectors" {
11631161 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
11641162 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
1165 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11661163 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
11671164 if (builtin.zig_backend == .stage2_x86_64 and
11681165 !comptime std.Target.x86.featureSetHas(builtin.cpu.features, .sse4_1)) return error.SkipZigTest;
test/behavior/int_div.zig-1
......@@ -6,7 +6,6 @@ test "integer division" {
66 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest;
77 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest;
88 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
109
1110 try testDivision();
1211 try comptime testDivision();
test/behavior/switch.zig-1
......@@ -640,7 +640,6 @@ test "switch prong pointer capture alignment" {
640640test "switch on pointer type" {
641641 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
642642 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
643 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
644643
645644 const S = struct {
646645 const X = struct {