authorgravatar for robin@voetter.nlRobin Voetter <robin@voetter.nl> 2024-04-06 13:37:25+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-04-06 13:37:25+02:00
log39420838061a9049fbc889212836a9d4d2ab9af4
treede835335172000e497871f9593bac17bcff882c0
parent3eeb70540d7f40526b4f4549deb6e2bc792bb3b2
parent436f53f55d3191bfa56418d98130d763fa5a6b22
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #18984 from alichraghi/vector

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

11 files changed, 241 insertions(+), 75 deletions(-)

src/codegen/spirv.zig+198-38
......@@ -1016,7 +1016,7 @@ const DeclGen = struct {
10161016 const elem_ty = Type.fromInterned(array_type.child);
10171017 const elem_ty_ref = try self.resolveType(elem_ty, .indirect);
10181018
1019 const constituents = try self.gpa.alloc(IdRef, @as(u32, @intCast(ty.arrayLenIncludingSentinel(mod))));
1019 const constituents = try self.gpa.alloc(IdRef, @intCast(ty.arrayLenIncludingSentinel(mod)));
10201020 defer self.gpa.free(constituents);
10211021
10221022 switch (aggregate.storage) {
......@@ -1736,7 +1736,6 @@ const DeclGen = struct {
17361736 .EnumLiteral,
17371737 .ComptimeFloat,
17381738 .ComptimeInt,
1739 .Type,
17401739 => unreachable, // Must be comptime.
17411740
17421741 else => |tag| return self.todo("Implement zig type '{}'", .{tag}),
......@@ -2316,21 +2315,23 @@ const DeclGen = struct {
23162315 .sub, .sub_wrap, .sub_optimized => try self.airArithOp(inst, .OpFSub, .OpISub, .OpISub),
23172316 .mul, .mul_wrap, .mul_optimized => try self.airArithOp(inst, .OpFMul, .OpIMul, .OpIMul),
23182317
2318
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,
2323 // TODO: Check that this is the right operation.
23242326 .div_trunc,
2325 .div_trunc_optimized,
2326 => try self.airArithOp(inst, .OpFDiv, .OpSDiv, .OpUDiv),
2327 // TODO: Check if this is the right operation
2328 .rem,
2329 .rem_optimized,
2330 => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem),
2327 .div_trunc_optimized => try self.airArithOp(inst, .OpFDiv, .OpSDiv, .OpUDiv),
2328 .rem, .rem_optimized => try self.airArithOp(inst, .OpFRem, .OpSRem, .OpSRem),
2329 .mod, .mod_optimized => try self.airArithOp(inst, .OpFMod, .OpSMod, .OpSMod),
2330
23312331
23322332 .add_with_overflow => try self.airAddSubOverflow(inst, .OpIAdd, .OpULessThan, .OpSLessThan),
23332333 .sub_with_overflow => try self.airAddSubOverflow(inst, .OpISub, .OpUGreaterThan, .OpSGreaterThan),
2334 .mul_with_overflow => try self.airMulOverflow(inst),
23342335 .shl_with_overflow => try self.airShlOverflow(inst),
23352336
23362337 .mul_add => try self.airMulAdd(inst),
......@@ -2340,7 +2341,7 @@ const DeclGen = struct {
23402341
23412342 .splat => try self.airSplat(inst),
23422343 .reduce, .reduce_optimized => try self.airReduce(inst),
2343 .shuffle => try self.airShuffle(inst),
2344 .shuffle => try self.airShuffle(inst),
23442345
23452346 .ptr_add => try self.airPtrAdd(inst),
23462347 .ptr_sub => try self.airPtrSub(inst),
......@@ -2661,6 +2662,95 @@ const DeclGen = struct {
26612662 }
26622663 }
26632664
2665 fn airDivFloor(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2666 const bin_op = self.air.instructions.items(.data)[@intFromEnum(inst)].bin_op;
2667 const lhs_id = try self.resolve(bin_op.lhs);
2668 const rhs_id = try self.resolve(bin_op.rhs);
2669 const ty = self.typeOfIndex(inst);
2670 const ty_ref = try self.resolveType(ty, .direct);
2671 const info = self.arithmeticTypeInfo(ty);
2672 switch (info.class) {
2673 .composite_integer => unreachable, // TODO
2674 .integer, .strange_integer => {
2675 const zero_id = try self.constInt(ty_ref, 0);
2676 const one_id = try self.constInt(ty_ref, 1);
2677
2678 // (a ^ b) > 0
2679 const bin_bitwise_id = try self.binOpSimple(ty, lhs_id, rhs_id, .OpBitwiseXor);
2680 const is_positive_id = try self.cmp(.gt, Type.bool, ty, bin_bitwise_id, zero_id);
2681
2682 // a / b
2683 const positive_div_id = try self.arithOp(ty, lhs_id, rhs_id, .OpFDiv, .OpSDiv, .OpUDiv);
2684
2685 // - (abs(a) + abs(b) - 1) / abs(b)
2686 const lhs_abs = try self.abs(ty, ty, lhs_id);
2687 const rhs_abs = try self.abs(ty, ty, rhs_id);
2688 const negative_div_lhs = try self.arithOp(
2689 ty,
2690 try self.arithOp(ty, lhs_abs, rhs_abs, .OpFAdd, .OpIAdd, .OpIAdd),
2691 one_id,
2692 .OpFSub,
2693 .OpISub,
2694 .OpISub,
2695 );
2696 const negative_div_id = try self.arithOp(ty, negative_div_lhs, rhs_abs, .OpFDiv, .OpSDiv, .OpUDiv);
2697 const negated_negative_div_id = self.spv.allocId();
2698 try self.func.body.emit(self.spv.gpa, .OpSNegate, .{
2699 .id_result_type = self.typeId(ty_ref),
2700 .id_result = negated_negative_div_id,
2701 .operand = negative_div_id,
2702 });
2703
2704 const result_id = self.spv.allocId();
2705 try self.func.body.emit(self.spv.gpa, .OpSelect, .{
2706 .id_result_type = self.typeId(ty_ref),
2707 .id_result = result_id,
2708 .condition = is_positive_id,
2709 .object_1 = positive_div_id,
2710 .object_2 = negated_negative_div_id,
2711 });
2712 return result_id;
2713 },
2714 .float => {
2715 const div_id = try self.arithOp(ty, lhs_id, rhs_id, .OpFDiv, .OpSDiv, .OpUDiv);
2716 return try self.floor(ty, div_id);
2717 },
2718 .bool => unreachable,
2719 }
2720 }
2721
2722 fn airFloor(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2723 const un_op = self.air.instructions.items(.data)[@intFromEnum(inst)].un_op;
2724 const operand_id = try self.resolve(un_op);
2725 const result_ty = self.typeOfIndex(inst);
2726 return try self.floor(result_ty, operand_id);
2727 }
2728
2729 fn floor(self: *DeclGen, ty: Type, operand_id: IdRef) !IdRef {
2730 const target = self.getTarget();
2731 const ty_ref = try self.resolveType(ty, .direct);
2732 const ext_inst: Word = switch (target.os.tag) {
2733 .opencl => 25,
2734 .vulkan => 8,
2735 else => unreachable,
2736 };
2737 const set_id = switch (target.os.tag) {
2738 .opencl => try self.spv.importInstructionSet(.@"OpenCL.std"),
2739 .vulkan => try self.spv.importInstructionSet(.@"GLSL.std.450"),
2740 else => unreachable,
2741 };
2742
2743 const result_id = self.spv.allocId();
2744 try self.func.body.emit(self.spv.gpa, .OpExtInst, .{
2745 .id_result_type = self.typeId(ty_ref),
2746 .id_result = result_id,
2747 .set = set_id,
2748 .instruction = .{ .inst = ext_inst },
2749 .id_ref_4 = &.{operand_id},
2750 });
2751 return result_id;
2752 }
2753
26642754 fn airArithOp(
26652755 self: *DeclGen,
26662756 inst: Air.Inst.Index,
......@@ -2668,7 +2758,6 @@ const DeclGen = struct {
26682758 comptime sop: Opcode,
26692759 comptime uop: Opcode,
26702760 ) !?IdRef {
2671
26722761 // LHS and RHS are guaranteed to have the same type, and AIR guarantees
26732762 // the result to be the same as the LHS and RHS, which matches SPIR-V.
26742763 const ty = self.typeOfIndex(inst);
......@@ -2700,8 +2789,8 @@ const DeclGen = struct {
27002789 return self.todo("binary operations for composite integers", .{});
27012790 },
27022791 .integer, .strange_integer => switch (info.signedness) {
2703 .signed => @as(usize, 1),
2704 .unsigned => @as(usize, 2),
2792 .signed => 1,
2793 .unsigned => 2,
27052794 },
27062795 .float => 0,
27072796 .bool => unreachable,
......@@ -2737,12 +2826,16 @@ const DeclGen = struct {
27372826 }
27382827
27392828 fn airAbs(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
2740 const target = self.getTarget();
27412829 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
27422830 const operand_id = try self.resolve(ty_op.operand);
27432831 // Note: operand_ty may be signed, while ty is always unsigned!
27442832 const operand_ty = self.typeOf(ty_op.operand);
27452833 const result_ty = self.typeOfIndex(inst);
2834 return try self.abs(result_ty, operand_ty, operand_id);
2835 }
2836
2837 fn abs(self: *DeclGen, result_ty: Type, operand_ty: Type, operand_id: IdRef) !IdRef {
2838 const target = self.getTarget();
27462839 const operand_info = self.arithmeticTypeInfo(operand_ty);
27472840
27482841 var wip = try self.elementWise(result_ty, false);
......@@ -2907,6 +3000,61 @@ const DeclGen = struct {
29073000 );
29083001 }
29093002
3003 fn airMulOverflow(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
3004 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
3005 const extra = self.air.extraData(Air.Bin, ty_pl.payload).data;
3006 const lhs = try self.resolve(extra.lhs);
3007 const rhs = try self.resolve(extra.rhs);
3008
3009 const result_ty = self.typeOfIndex(inst);
3010 const operand_ty = self.typeOf(extra.lhs);
3011 const ov_ty = result_ty.structFieldType(1, self.module);
3012
3013 const info = self.arithmeticTypeInfo(operand_ty);
3014 switch (info.class) {
3015 .composite_integer => return self.todo("overflow ops for composite integers", .{}),
3016 .strange_integer, .integer => {},
3017 .float, .bool => unreachable,
3018 }
3019
3020 var wip_result = try self.elementWise(operand_ty, true);
3021 defer wip_result.deinit();
3022 var wip_ov = try self.elementWise(ov_ty, true);
3023 defer wip_ov.deinit();
3024
3025 const zero_id = try self.constInt(wip_result.ty_ref, 0);
3026 const zero_ov_id = try self.constInt(wip_ov.ty_ref, 0);
3027 const one_ov_id = try self.constInt(wip_ov.ty_ref, 1);
3028
3029 for (wip_result.results, wip_ov.results, 0..) |*result_id, *ov_id, i| {
3030 const lhs_elem_id = try wip_result.elementAt(operand_ty, lhs, i);
3031 const rhs_elem_id = try wip_result.elementAt(operand_ty, rhs, i);
3032
3033 result_id.* = try self.arithOp(wip_result.ty, lhs_elem_id, rhs_elem_id, .OpFMul, .OpIMul, .OpIMul);
3034
3035 // (a != 0) and (x / a != b)
3036 const not_zero_id = try self.cmp(.neq, Type.bool, wip_result.ty, lhs_elem_id, zero_id);
3037 const res_rhs_id = try self.arithOp(wip_result.ty, result_id.*, lhs_elem_id, .OpFDiv, .OpSDiv, .OpUDiv);
3038 const res_rhs_not_rhs_id = try self.cmp(.neq, Type.bool, wip_result.ty, res_rhs_id, rhs_elem_id);
3039 const cond_id = try self.binOpSimple(Type.bool, not_zero_id, res_rhs_not_rhs_id, .OpLogicalAnd);
3040
3041 ov_id.* = self.spv.allocId();
3042 try self.func.body.emit(self.spv.gpa, .OpSelect, .{
3043 .id_result_type = wip_ov.ty_id,
3044 .id_result = ov_id.*,
3045 .condition = cond_id,
3046 .object_1 = one_ov_id,
3047 .object_2 = zero_ov_id,
3048 });
3049 }
3050
3051 return try self.constructStruct(
3052 result_ty,
3053 &.{ operand_ty, ov_ty },
3054 &.{ try wip_result.finalize(), try wip_ov.finalize() },
3055 );
3056 }
3057
29103058 fn airShlOverflow(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
29113059 const mod = self.module;
29123060 const ty_pl = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_pl;
......@@ -3692,19 +3840,22 @@ const DeclGen = struct {
36923840 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
36933841 const operand_ty = self.typeOf(ty_op.operand);
36943842 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);
3843 const result_ty = self.typeOfIndex(inst);
3844 const result_ty_ref = try self.resolveType(result_ty, .direct);
3845 return try self.floatFromInt(result_ty_ref, operand_ty, operand_id);
3846 }
36983847
3848 fn floatFromInt(self: *DeclGen, result_ty_ref: CacheRef, operand_ty: Type, operand_id: IdRef) !IdRef {
3849 const operand_info = self.arithmeticTypeInfo(operand_ty);
36993850 const result_id = self.spv.allocId();
37003851 switch (operand_info.signedness) {
37013852 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertSToF, .{
3702 .id_result_type = dest_ty_id,
3853 .id_result_type = self.typeId(result_ty_ref),
37033854 .id_result = result_id,
37043855 .signed_value = operand_id,
37053856 }),
37063857 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertUToF, .{
3707 .id_result_type = dest_ty_id,
3858 .id_result_type = self.typeId(result_ty_ref),
37083859 .id_result = result_id,
37093860 .unsigned_value = operand_id,
37103861 }),
......@@ -3715,19 +3866,22 @@ const DeclGen = struct {
37153866 fn airIntFromFloat(self: *DeclGen, inst: Air.Inst.Index) !?IdRef {
37163867 const ty_op = self.air.instructions.items(.data)[@intFromEnum(inst)].ty_op;
37173868 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);
3869 const result_ty = self.typeOfIndex(inst);
3870 return try self.intFromFloat(result_ty, operand_id);
3871 }
37213872
3873 fn intFromFloat(self: *DeclGen, result_ty: Type, operand_id: IdRef) !IdRef {
3874 const result_info = self.arithmeticTypeInfo(result_ty);
3875 const result_ty_ref = try self.resolveType(result_ty, .direct);
37223876 const result_id = self.spv.allocId();
3723 switch (dest_info.signedness) {
3877 switch (result_info.signedness) {
37243878 .signed => try self.func.body.emit(self.spv.gpa, .OpConvertFToS, .{
3725 .id_result_type = dest_ty_id,
3879 .id_result_type = self.typeId(result_ty_ref),
37263880 .id_result = result_id,
37273881 .float_value = operand_id,
37283882 }),
37293883 .unsigned => try self.func.body.emit(self.spv.gpa, .OpConvertFToU, .{
3730 .id_result_type = dest_ty_id,
3884 .id_result_type = self.typeId(result_ty_ref),
37313885 .id_result = result_id,
37323886 .float_value = operand_id,
37333887 }),
......@@ -5237,20 +5391,21 @@ const DeclGen = struct {
52375391
52385392 fn airSwitchBr(self: *DeclGen, inst: Air.Inst.Index) !void {
52395393 const mod = self.module;
5394 const target = self.getTarget();
52405395 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
52415396 const cond_ty = self.typeOf(pl_op.operand);
52425397 const cond = try self.resolve(pl_op.operand);
5243 const cond_indirect = try self.convertToIndirect(cond_ty, cond);
5398 var cond_indirect = try self.convertToIndirect(cond_ty, cond);
52445399 const switch_br = self.air.extraData(Air.SwitchBr, pl_op.payload);
52455400
52465401 const cond_words: u32 = switch (cond_ty.zigTypeTag(mod)) {
5247 .Bool => 1,
5402 .Bool, .ErrorSet => 1,
52485403 .Int => blk: {
52495404 const bits = cond_ty.intInfo(mod).bits;
52505405 const backing_bits = self.backingIntBits(bits) orelse {
52515406 return self.todo("implement composite int switch", .{});
52525407 };
5253 break :blk if (backing_bits <= 32) @as(u32, 1) else 2;
5408 break :blk if (backing_bits <= 32) 1 else 2;
52545409 },
52555410 .Enum => blk: {
52565411 const int_ty = cond_ty.intTagType(mod);
......@@ -5258,10 +5413,14 @@ const DeclGen = struct {
52585413 const backing_bits = self.backingIntBits(int_info.bits) orelse {
52595414 return self.todo("implement composite int switch", .{});
52605415 };
5261 break :blk if (backing_bits <= 32) @as(u32, 1) else 2;
5416 break :blk if (backing_bits <= 32) 1 else 2;
5417 },
5418 .Pointer => blk: {
5419 cond_indirect = try self.intFromPtr(cond_indirect);
5420 break :blk target.ptrBitWidth() / 32;
52625421 },
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.
5422 // TODO: Figure out which types apply here, and work around them as we can only do integers.
5423 else => return self.todo("implement switch for type {s}", .{@tagName(cond_ty.zigTypeTag(mod))}),
52655424 };
52665425
52675426 const num_cases = switch_br.data.cases_len;
......@@ -5308,7 +5467,7 @@ const DeclGen = struct {
53085467 for (0..num_cases) |case_i| {
53095468 // SPIR-V needs a literal here, which' width depends on the case condition.
53105469 const case = self.air.extraData(Air.SwitchBr.Case, extra_index);
5311 const items = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[case.end..][0..case.data.items_len]));
5470 const items: []const Air.Inst.Ref = @ptrCast(self.air.extra[case.end..][0..case.data.items_len]);
53125471 const case_body = self.air.extra[case.end + items.len ..][0..case.data.body_len];
53135472 extra_index = case.end + case.data.items_len + case_body.len;
53145473
......@@ -5316,13 +5475,14 @@ const DeclGen = struct {
53165475
53175476 for (items) |item| {
53185477 const value = (try self.air.value(item, mod)) orelse unreachable;
5319 const int_val = switch (cond_ty.zigTypeTag(mod)) {
5320 .Bool, .Int => if (cond_ty.isSignedInt(mod)) @as(u64, @bitCast(value.toSignedInt(mod))) else value.toUnsignedInt(mod),
5478 const int_val: u64 = switch (cond_ty.zigTypeTag(mod)) {
5479 .Bool, .Int => if (cond_ty.isSignedInt(mod)) @bitCast(value.toSignedInt(mod)) else value.toUnsignedInt(mod),
53215480 .Enum => blk: {
53225481 // TODO: figure out of cond_ty is correct (something with enum literals)
53235482 break :blk (try value.intFromEnum(cond_ty, mod)).toUnsignedInt(mod); // TODO: composite integer constants
53245483 },
53255484 .ErrorSet => value.getErrorInt(mod),
5485 .Pointer => value.toUnsignedInt(mod),
53265486 else => unreachable,
53275487 };
53285488 const int_lit: spec.LiteralContextDependentNumber = switch (cond_words) {
......@@ -5438,14 +5598,14 @@ const DeclGen = struct {
54385598 const extra = self.air.extraData(Air.Asm, ty_pl.payload);
54395599
54405600 const is_volatile = @as(u1, @truncate(extra.data.flags >> 31)) != 0;
5441 const clobbers_len = @as(u31, @truncate(extra.data.flags));
5601 const clobbers_len: u31 = @truncate(extra.data.flags);
54425602
54435603 if (!is_volatile and self.liveness.isUnused(inst)) return null;
54445604
54455605 var extra_i: usize = extra.end;
5446 const outputs = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]));
5606 const outputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.outputs_len]);
54475607 extra_i += outputs.len;
5448 const inputs = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]));
5608 const inputs: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra_i..][0..extra.data.inputs_len]);
54495609 extra_i += inputs.len;
54505610
54515611 if (outputs.len > 1) {
......@@ -5567,7 +5727,7 @@ const DeclGen = struct {
55675727 const mod = self.module;
55685728 const pl_op = self.air.instructions.items(.data)[@intFromEnum(inst)].pl_op;
55695729 const extra = self.air.extraData(Air.Call, pl_op.payload);
5570 const args = @as([]const Air.Inst.Ref, @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]));
5730 const args: []const Air.Inst.Ref = @ptrCast(self.air.extra[extra.end..][0..extra.data.args_len]);
55715731 const callee_ty = self.typeOf(pl_op.operand);
55725732 const zig_fn_ty = switch (callee_ty.zigTypeTag(mod)) {
55735733 .Fn => callee_ty,
src/codegen/spirv/Assembler.zig+29-9
......@@ -256,10 +256,18 @@ fn todo(self: *Assembler, comptime fmt: []const u8, args: anytype) Error {
256256/// If this function returns `error.AssembleFail`, an explanatory
257257/// error message has already been emitted into `self.errors`.
258258fn processInstruction(self: *Assembler) !void {
259 const result = switch (self.inst.opcode) {
259 const result: AsmValue = switch (self.inst.opcode) {
260260 .OpEntryPoint => {
261261 return self.fail(0, "cannot export entry points via OpEntryPoint, export the kernel using callconv(.Kernel)", .{});
262262 },
263 .OpExtInstImport => blk: {
264 const set_name_offset = self.inst.operands.items[1].string;
265 const set_name = std.mem.sliceTo(self.inst.string_bytes.items[set_name_offset..], 0);
266 const set_tag = std.meta.stringToEnum(spec.InstructionSet, set_name) orelse {
267 return self.fail(set_name_offset, "unknown instruction set: {s}", .{set_name});
268 };
269 break :blk .{ .value = try self.spv.importInstructionSet(set_tag) };
270 },
263271 else => switch (self.inst.opcode.class()) {
264272 .TypeDeclaration => try self.processTypeInstruction(),
265273 else => if (try self.processGenericInstruction()) |result|
......@@ -309,7 +317,7 @@ fn processTypeInstruction(self: *Assembler) !AsmValue {
309317 return self.fail(0, "{} is not a valid bit count for floats (expected 16, 32 or 64)", .{bits});
310318 },
311319 }
312 break :blk try self.spv.resolve(.{ .float_type = .{ .bits = @as(u16, @intCast(bits)) } });
320 break :blk try self.spv.resolve(.{ .float_type = .{ .bits = @intCast(bits) } });
313321 },
314322 .OpTypeVector => try self.spv.resolve(.{ .vector_type = .{
315323 .component_type = try self.resolveTypeRef(operands[1].ref_id),
......@@ -364,6 +372,7 @@ fn processGenericInstruction(self: *Assembler) !?AsmValue {
364372 .OpExecutionMode, .OpExecutionModeId => &self.spv.sections.execution_modes,
365373 .OpVariable => switch (@as(spec.StorageClass, @enumFromInt(operands[2].value))) {
366374 .Function => &self.func.prologue,
375 .UniformConstant => &self.spv.sections.types_globals_constants,
367376 else => {
368377 // This is currently disabled because global variables are required to be
369378 // emitted in the proper order, and this should be honored in inline assembly
......@@ -473,14 +482,14 @@ fn parseInstruction(self: *Assembler) !void {
473482 self.inst.string_bytes.shrinkRetainingCapacity(0);
474483
475484 const lhs_result_tok = self.currentToken();
476 const maybe_lhs_result = if (self.eatToken(.result_id_assign)) blk: {
485 const maybe_lhs_result: ?AsmValue.Ref = if (self.eatToken(.result_id_assign)) blk: {
477486 const name = self.tokenText(lhs_result_tok)[1..];
478487 const entry = try self.value_map.getOrPut(self.gpa, name);
479488 try self.expectToken(.equals);
480489 if (!entry.found_existing) {
481490 entry.value_ptr.* = .just_declared;
482491 }
483 break :blk @as(AsmValue.Ref, @intCast(entry.index));
492 break :blk @intCast(entry.index);
484493 } else null;
485494
486495 const opcode_tok = self.currentToken();
......@@ -550,6 +559,7 @@ fn parseOperand(self: *Assembler, kind: spec.OperandKind) Error!void {
550559 .LiteralInteger => try self.parseLiteralInteger(),
551560 .LiteralString => try self.parseString(),
552561 .LiteralContextDependentNumber => try self.parseContextDependentNumber(),
562 .LiteralExtInstInteger => try self.parseLiteralExtInstInteger(),
553563 .PairIdRefIdRef => try self.parsePhiSource(),
554564 else => return self.todo("parse operand of type {s}", .{@tagName(kind)}),
555565 },
......@@ -641,7 +651,7 @@ fn parseRefId(self: *Assembler) !void {
641651 entry.value_ptr.* = .unresolved_forward_reference;
642652 }
643653
644 const index = @as(AsmValue.Ref, @intCast(entry.index));
654 const index: AsmValue.Ref = @intCast(entry.index);
645655 try self.inst.operands.append(self.gpa, .{ .ref_id = index });
646656}
647657
......@@ -660,6 +670,16 @@ fn parseLiteralInteger(self: *Assembler) !void {
660670 try self.inst.operands.append(self.gpa, .{ .literal32 = value });
661671}
662672
673fn parseLiteralExtInstInteger(self: *Assembler) !void {
674 const tok = self.currentToken();
675 try self.expectToken(.value);
676 const text = self.tokenText(tok);
677 const value = std.fmt.parseInt(u32, text, 0) catch {
678 return self.fail(tok.start, "'{s}' is not a valid 32-bit integer literal", .{text});
679 };
680 try self.inst.operands.append(self.gpa, .{ .literal32 = value });
681}
682
663683fn parseString(self: *Assembler) !void {
664684 const tok = self.currentToken();
665685 try self.expectToken(.string);
......@@ -673,7 +693,7 @@ fn parseString(self: *Assembler) !void {
673693 else
674694 text[1..];
675695
676 const string_offset = @as(u32, @intCast(self.inst.string_bytes.items.len));
696 const string_offset: u32 = @intCast(self.inst.string_bytes.items.len);
677697 try self.inst.string_bytes.ensureUnusedCapacity(self.gpa, literal.len + 1);
678698 self.inst.string_bytes.appendSliceAssumeCapacity(literal);
679699 self.inst.string_bytes.appendAssumeCapacity(0);
......@@ -730,9 +750,9 @@ fn parseContextDependentInt(self: *Assembler, signedness: std.builtin.Signedness
730750
731751 // Note, we store the sign-extended version here.
732752 if (width <= @bitSizeOf(spec.Word)) {
733 try self.inst.operands.append(self.gpa, .{ .literal32 = @as(u32, @truncate(@as(u128, @bitCast(int)))) });
753 try self.inst.operands.append(self.gpa, .{ .literal32 = @truncate(@as(u128, @bitCast(int))) });
734754 } else {
735 try self.inst.operands.append(self.gpa, .{ .literal64 = @as(u64, @truncate(@as(u128, @bitCast(int)))) });
755 try self.inst.operands.append(self.gpa, .{ .literal64 = @truncate(@as(u128, @bitCast(int))) });
736756 }
737757 return;
738758 }
......@@ -753,7 +773,7 @@ fn parseContextDependentFloat(self: *Assembler, comptime width: u16) !void {
753773 return self.fail(tok.start, "'{s}' is not a valid {}-bit float literal", .{ text, width });
754774 };
755775
756 const float_bits = @as(Int, @bitCast(value));
776 const float_bits: Int = @bitCast(value);
757777 if (width <= @bitSizeOf(spec.Word)) {
758778 try self.inst.operands.append(self.gpa, .{ .literal32 = float_bits });
759779 } else {
src/codegen/spirv/Module.zig+4-4
......@@ -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}
......@@ -500,9 +500,9 @@ pub fn declPtr(self: *Module, index: Decl.Index) *Decl {
500500
501501/// Declare ALL dependencies for a decl.
502502pub fn declareDeclDeps(self: *Module, decl_index: Decl.Index, deps: []const Decl.Index) !void {
503 const begin_dep = @as(u32, @intCast(self.decl_deps.items.len));
503 const begin_dep: u32 = @intCast(self.decl_deps.items.len);
504504 try self.decl_deps.appendSlice(self.gpa, deps);
505 const end_dep = @as(u32, @intCast(self.decl_deps.items.len));
505 const end_dep: u32 = @intCast(self.decl_deps.items.len);
506506
507507 const decl = self.declPtr(decl_index);
508508 decl.begin_dep = begin_dep;
src/codegen/spirv/Section.zig+10-10
......@@ -115,8 +115,8 @@ pub fn writeWords(section: *Section, words: []const Word) void {
115115
116116pub fn writeDoubleWord(section: *Section, dword: DoubleWord) void {
117117 section.writeWords(&.{
118 @as(Word, @truncate(dword)),
119 @as(Word, @truncate(dword >> @bitSizeOf(Word))),
118 @truncate(dword),
119 @truncate(dword >> @bitSizeOf(Word)),
120120 });
121121}
122122
......@@ -196,12 +196,12 @@ fn writeString(section: *Section, str: []const u8) void {
196196
197197fn writeContextDependentNumber(section: *Section, operand: spec.LiteralContextDependentNumber) void {
198198 switch (operand) {
199 .int32 => |int| section.writeWord(@as(Word, @bitCast(int))),
200 .uint32 => |int| section.writeWord(@as(Word, @bitCast(int))),
201 .int64 => |int| section.writeDoubleWord(@as(DoubleWord, @bitCast(int))),
202 .uint64 => |int| section.writeDoubleWord(@as(DoubleWord, @bitCast(int))),
203 .float32 => |float| section.writeWord(@as(Word, @bitCast(float))),
204 .float64 => |float| section.writeDoubleWord(@as(DoubleWord, @bitCast(float))),
199 .int32 => |int| section.writeWord(@bitCast(int)),
200 .uint32 => |int| section.writeWord(@bitCast(int)),
201 .int64 => |int| section.writeDoubleWord(@bitCast(int)),
202 .uint64 => |int| section.writeDoubleWord(@bitCast(int)),
203 .float32 => |float| section.writeWord(@bitCast(float)),
204 .float64 => |float| section.writeDoubleWord(@bitCast(float)),
205205 }
206206}
207207
......@@ -274,8 +274,8 @@ fn operandSize(comptime Operand: type, operand: Operand) usize {
274274 spec.LiteralString => std.math.divCeil(usize, operand.len + 1, @sizeOf(Word)) catch unreachable, // Add one for zero-terminator
275275
276276 spec.LiteralContextDependentNumber => switch (operand) {
277 .int32, .uint32, .float32 => @as(usize, 1),
278 .int64, .uint64, .float64 => @as(usize, 2),
277 .int32, .uint32, .float32 => 1,
278 .int64, .uint64, .float64 => 2,
279279 },
280280
281281 // TODO: Where this type is used (OpSpecConstantOp) is currently not correct in the spec
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/for.zig-1
......@@ -226,7 +226,6 @@ test "else continue outer for" {
226226
227227test "for loop with else branch" {
228228 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
229 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
230229
231230 {
232231 var x = [_]u32{ 1, 2 };
test/behavior/hasdecl.zig-4
......@@ -12,8 +12,6 @@ const Bar = struct {
1212};
1313
1414test "@hasDecl" {
15 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
16
1715 try expect(@hasDecl(Foo, "public_thing"));
1816 try expect(!@hasDecl(Foo, "private_thing"));
1917 try expect(!@hasDecl(Foo, "no_thing"));
......@@ -24,8 +22,6 @@ test "@hasDecl" {
2422}
2523
2624test "@hasDecl using a sliced string literal" {
27 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
28
2925 try expect(@hasDecl(@This(), "std") == true);
3026 try expect(@hasDecl(@This(), "std"[0..0]) == false);
3127 try expect(@hasDecl(@This(), "std"[0..1]) == false);
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/math.zig-3
......@@ -788,7 +788,6 @@ test "small int addition" {
788788test "basic @mulWithOverflow" {
789789 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
790790 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
791 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
792791
793792 {
794793 var a: u8 = 86;
......@@ -821,7 +820,6 @@ test "basic @mulWithOverflow" {
821820test "extensive @mulWithOverflow" {
822821 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
823822 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
824 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
825823
826824 {
827825 var a: u5 = 3;
......@@ -998,7 +996,6 @@ test "@mulWithOverflow bitsize > 32" {
998996 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
999997 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
1000998 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1001 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
1002999
10031000 {
10041001 var a: u62 = 3;
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 {
test/behavior/vector.zig-1
......@@ -1136,7 +1136,6 @@ test "@mulWithOverflow" {
11361136 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
11371137 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
11381138 if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO
1139 if (builtin.zig_backend == .stage2_spirv64) return error.SkipZigTest;
11401139
11411140 const S = struct {
11421141 fn doTheTest() !void {