From afa74c6b213efb1ff85b86ce4a9edd5cc03e5a9b Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Thu, 30 Jan 2025 11:58:32 -0500 Subject: Sema: introduce all_vector_instructions backend feature Sema is arbitrarily scalarizing some operations, which means that when I try to implement vectorized versions of those operations in a backend, they are impossible to test due to Sema not producing them. Now, I can implement them and then temporarily enable the new feature for that backend in order to test them. Once the backend supports all of them, the feature can be permanently enabled. This also deletes the Air instructions `int_from_bool` and `int_from_ptr`, which are just bitcasts with a fixed result type, since changing `un_op` to `ty_op` takes up the same amount of memory. --- src/Sema.zig | 103 ++++++++++++++++++++++++++++++++++++++++------------------- 1 file changed, 70 insertions(+), 33 deletions(-) (limited to 'src/Sema.zig') diff --git a/src/Sema.zig b/src/Sema.zig index bc9d5ea55f..4a11dd4544 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -10054,6 +10054,8 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! }; return sema.failWithOwnedErrorMsg(block, msg); } + const len = if (is_vector) operand_ty.vectorLen(zcu) else undefined; + const dest_ty: Type = if (is_vector) try pt.vectorType(.{ .child = .usize_type, .len = len }) else .usize; if (try sema.resolveValueIntable(operand)) |operand_val| ct: { if (!is_vector) { if (operand_val.isUndef(zcu)) { @@ -10064,8 +10066,6 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! (try operand_val.toUnsignedIntSema(pt)), )).toIntern()); } - const len = operand_ty.vectorLen(zcu); - const dest_ty = try pt.vectorType(.{ .child = .usize_type, .len = len }); const new_elems = try sema.arena.alloc(InternPool.Index, len); for (new_elems, 0..) |*new_elem, i| { const ptr_val = try operand_val.elemValue(pt, i); @@ -10089,16 +10089,14 @@ fn zirIntFromPtr(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! } try sema.requireRuntimeBlock(block, block.nodeOffset(inst_data.src_node), ptr_src); try sema.validateRuntimeValue(block, ptr_src, operand); - if (!is_vector) { - return block.addUnOp(.int_from_ptr, operand); + if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) { + return block.addBitCast(dest_ty, operand); } - const len = operand_ty.vectorLen(zcu); - const dest_ty = try pt.vectorType(.{ .child = .usize_type, .len = len }); const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); for (new_elems, 0..) |*new_elem, i| { const idx_ref = try pt.intRef(Type.usize, i); const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); - new_elem.* = try block.addUnOp(.int_from_ptr, old_elem); + new_elem.* = try block.addBitCast(.usize, old_elem); } return block.addAggregateInit(dest_ty, new_elems); } @@ -10585,7 +10583,7 @@ fn zirFloatCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A if (dst_bits >= src_bits) { return sema.coerce(block, dest_ty, operand, operand_src); } - if (!is_vector) { + if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) { return block.addTyOp(.fptrunc, dest_ty, operand); } const vec_len = operand_ty.vectorLen(zcu); @@ -14762,7 +14760,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai }); const many_ty = slice_ty.slicePtrFieldType(zcu); - const many_alloc = try block.addTyOp(.bitcast, many_ty, mutable_alloc); + const many_alloc = try block.addBitCast(many_ty, mutable_alloc); // lhs_dest_slice = dest[0..lhs.len] const slice_ty_ref = Air.internedToRef(slice_ty.toIntern()); @@ -14812,7 +14810,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store); } - return block.addTyOp(.bitcast, constant_alloc_ty, mutable_alloc); + return block.addBitCast(constant_alloc_ty, mutable_alloc); } var elem_i: u32 = 0; @@ -14845,7 +14843,7 @@ fn zirArrayCat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Ai try sema.storePtr2(block, src, elem_ptr, src, init, lhs_src, .store); } - return block.addTyOp(.bitcast, constant_alloc_ty, mutable_alloc); + return block.addBitCast(constant_alloc_ty, mutable_alloc); } const element_refs = try sema.arena.alloc(Air.Inst.Ref, result_len); @@ -16612,8 +16610,8 @@ fn analyzeArithmetic( }; try sema.requireRuntimeBlock(block, src, runtime_src); - const lhs_int = try block.addUnOp(.int_from_ptr, lhs); - const rhs_int = try block.addUnOp(.int_from_ptr, rhs); + const lhs_int = try block.addBitCast(.usize, lhs); + const rhs_int = try block.addBitCast(.usize, rhs); const address = try block.addBinOp(.sub_wrap, lhs_int, rhs_int); return try block.addBinOp(.div_exact, address, try pt.intRef(Type.usize, elem_size)); } @@ -21231,14 +21229,14 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError if (operand_scalar_ty.toIntern() != .bool_type) { return sema.fail(block, src, "expected 'bool', found '{}'", .{operand_scalar_ty.zigTypeTag(zcu)}); } + const len = if (is_vector) operand_ty.vectorLen(zcu) else undefined; + const dest_ty: Type = if (is_vector) try pt.vectorType(.{ .child = .u1_type, .len = len }) else .u1; if (try sema.resolveValue(operand)) |val| { if (!is_vector) { if (val.isUndef(zcu)) return pt.undefRef(Type.u1); if (val.toBool()) return Air.internedToRef((try pt.intValue(Type.u1, 1)).toIntern()); return Air.internedToRef((try pt.intValue(Type.u1, 0)).toIntern()); } - const len = operand_ty.vectorLen(zcu); - const dest_ty = try pt.vectorType(.{ .child = .u1_type, .len = len }); if (val.isUndef(zcu)) return pt.undefRef(dest_ty); const new_elems = try sema.arena.alloc(InternPool.Index, len); for (new_elems, 0..) |*new_elem, i| { @@ -21256,16 +21254,14 @@ fn zirIntFromBool(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError .storage = .{ .elems = new_elems }, } })); } - if (!is_vector) { - return block.addUnOp(.int_from_bool, operand); + if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) { + return block.addBitCast(dest_ty, operand); } - const len = operand_ty.vectorLen(zcu); - const dest_ty = try pt.vectorType(.{ .child = .u1_type, .len = len }); const new_elems = try sema.arena.alloc(Air.Inst.Ref, len); for (new_elems, 0..) |*new_elem, i| { const idx_ref = try pt.intRef(Type.usize, i); const old_elem = try block.addBinOp(.array_elem_val, operand, idx_ref); - new_elem.* = try block.addUnOp(.int_from_bool, old_elem); + new_elem.* = try block.addBitCast(.u1, old_elem); } return block.addAggregateInit(dest_ty, new_elems); } @@ -22858,14 +22854,27 @@ fn zirIntFromFloat(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro .storage = .{ .repeated_elem = (try pt.intValue(dest_scalar_ty, 0)).toIntern() }, } })); } - if (!is_vector) { + if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) { const result = try block.addTyOp(if (block.float_mode == .optimized) .int_from_float_optimized else .int_from_float, dest_ty, operand); if (block.wantSafety()) { const back = try block.addTyOp(.float_from_int, operand_ty, result); - const diff = try block.addBinOp(.sub, operand, back); - const ok_pos = try block.addBinOp(if (block.float_mode == .optimized) .cmp_lt_optimized else .cmp_lt, diff, Air.internedToRef((try pt.floatValue(operand_ty, 1.0)).toIntern())); - const ok_neg = try block.addBinOp(if (block.float_mode == .optimized) .cmp_gt_optimized else .cmp_gt, diff, Air.internedToRef((try pt.floatValue(operand_ty, -1.0)).toIntern())); - const ok = try block.addBinOp(.bool_and, ok_pos, ok_neg); + const diff = try block.addBinOp(if (block.float_mode == .optimized) .sub_optimized else .sub, operand, back); + const ok = if (is_vector) ok: { + const ok_pos = try block.addCmpVector(diff, Air.internedToRef((try sema.splat(operand_ty, try pt.floatValue(operand_scalar_ty, 1.0))).toIntern()), .lt); + const ok_neg = try block.addCmpVector(diff, Air.internedToRef((try sema.splat(operand_ty, try pt.floatValue(operand_scalar_ty, -1.0))).toIntern()), .gt); + const ok = try block.addBinOp(.bit_and, ok_pos, ok_neg); + break :ok try block.addInst(.{ + .tag = .reduce, + .data = .{ .reduce = .{ + .operand = ok, + .operation = .And, + } }, + }); + } else ok: { + const ok_pos = try block.addBinOp(if (block.float_mode == .optimized) .cmp_lt_optimized else .cmp_lt, diff, Air.internedToRef((try pt.floatValue(operand_ty, 1.0)).toIntern())); + const ok_neg = try block.addBinOp(if (block.float_mode == .optimized) .cmp_gt_optimized else .cmp_gt, diff, Air.internedToRef((try pt.floatValue(operand_ty, -1.0)).toIntern())); + break :ok try block.addBinOp(.bool_and, ok_pos, ok_neg); + }; try sema.addSafetyCheck(block, src, ok, .integer_part_out_of_bounds); } return result; @@ -22917,7 +22926,7 @@ fn zirFloatFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileErro } try sema.requireRuntimeBlock(block, src, operand_src); - if (!is_vector) { + if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) { return block.addTyOp(.float_from_int, dest_ty, operand); } const len = operand_ty.vectorLen(zcu); @@ -22996,17 +23005,37 @@ fn zirPtrFromInt(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError! }); } try sema.requireRuntimeBlock(block, src, operand_src); - if (!is_vector) { + if (!is_vector or zcu.backendSupportsFeature(.all_vector_instructions)) { if (block.wantSafety() and (try elem_ty.hasRuntimeBitsSema(pt) or elem_ty.zigTypeTag(zcu) == .@"fn")) { if (!ptr_ty.isAllowzeroPtr(zcu)) { - const is_non_zero = try block.addBinOp(.cmp_neq, operand_coerced, .zero_usize); + const is_non_zero = if (is_vector) all_non_zero: { + const zero_usize = Air.internedToRef((try sema.splat(operand_ty, .zero_usize)).toIntern()); + const is_non_zero = try block.addCmpVector(operand_coerced, zero_usize, .neq); + break :all_non_zero try block.addInst(.{ + .tag = .reduce, + .data = .{ .reduce = .{ + .operand = is_non_zero, + .operation = .And, + } }, + }); + } else try block.addBinOp(.cmp_neq, operand_coerced, .zero_usize); try sema.addSafetyCheck(block, src, is_non_zero, .cast_to_null); } if (ptr_align.compare(.gt, .@"1")) { const align_bytes_minus_1 = ptr_align.toByteUnits().? - 1; - const align_minus_1 = Air.internedToRef((try pt.intValue(Type.usize, align_bytes_minus_1)).toIntern()); + const align_minus_1 = Air.internedToRef((try sema.splat(operand_ty, try pt.intValue(Type.usize, align_bytes_minus_1))).toIntern()); const remainder = try block.addBinOp(.bit_and, operand_coerced, align_minus_1); - const is_aligned = try block.addBinOp(.cmp_eq, remainder, .zero_usize); + const is_aligned = if (is_vector) all_aligned: { + const splat_zero_usize = Air.internedToRef((try sema.splat(operand_ty, .zero_usize)).toIntern()); + const is_aligned = try block.addCmpVector(remainder, splat_zero_usize, .eq); + break :all_aligned try block.addInst(.{ + .tag = .reduce, + .data = .{ .reduce = .{ + .operand = is_aligned, + .operation = .And, + } }, + }); + } else try block.addBinOp(.cmp_eq, remainder, .zero_usize); try sema.addSafetyCheck(block, src, is_aligned, .incorrect_alignment); } } @@ -23559,7 +23588,11 @@ fn ptrCastFull( if (block.wantSafety() and operand_ty.ptrAllowsZero(zcu) and !dest_ty.ptrAllowsZero(zcu) and (try Type.fromInterned(dest_info.child).hasRuntimeBitsSema(pt) or Type.fromInterned(dest_info.child).zigTypeTag(zcu) == .@"fn")) { - const ptr_int = try block.addUnOp(.int_from_ptr, ptr); + const actual_ptr = if (src_info.flags.size == .slice) + try sema.analyzeSlicePtr(block, src, ptr, operand_ty) + else + ptr; + const ptr_int = try block.addBitCast(.usize, actual_ptr); const is_non_zero = try block.addBinOp(.cmp_neq, ptr_int, .zero_usize); const ok = if (src_info.flags.size == .slice and dest_info.flags.size == .slice) ok: { const len = try sema.analyzeSliceLen(block, operand_src, ptr); @@ -23575,7 +23608,11 @@ fn ptrCastFull( { const align_bytes_minus_1 = dest_align.toByteUnits().? - 1; const align_minus_1 = Air.internedToRef((try pt.intValue(Type.usize, align_bytes_minus_1)).toIntern()); - const ptr_int = try block.addUnOp(.int_from_ptr, ptr); + const actual_ptr = if (src_info.flags.size == .slice) + try sema.analyzeSlicePtr(block, src, ptr, operand_ty) + else + ptr; + const ptr_int = try block.addBitCast(.usize, actual_ptr); const remainder = try block.addBinOp(.bit_and, ptr_int, align_minus_1); const is_aligned = try block.addBinOp(.cmp_eq, remainder, .zero_usize); const ok = if (src_info.flags.size == .slice and dest_info.flags.size == .slice) ok: { @@ -31403,7 +31440,7 @@ fn coerceCompatiblePtrs( try sema.analyzeSlicePtr(block, inst_src, inst, inst_ty) else inst; - const ptr_int = try block.addUnOp(.int_from_ptr, actual_ptr); + const ptr_int = try block.addBitCast(.usize, actual_ptr); const is_non_zero = try block.addBinOp(.cmp_neq, ptr_int, .zero_usize); const ok = if (inst_ty.isSlice(zcu)) ok: { const len = try sema.analyzeSliceLen(block, inst_src, inst); -- cgit v1.2.3