authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-03-03 15:12:02-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2022-03-03 15:12:02-05:00
loge91c16e38b58611217f225d002a3321aa24bb47f
treee86a89067a08d9c14d01334b4f054b51637afdb8
parent7deadf4301d5f5f5e2b8a1a8b2dc7109e2c82181
parent365aca0e9096ef7c69b3ba2eb682caf3f7234293
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #11043 from topolarity/ptr-fixes

stage2: Improve `@ptrCast` support for sliced/optional operands

5 files changed, 53 insertions(+), 19 deletions(-)

src/Sema.zig+41-11
......@@ -12538,6 +12538,8 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1253812538 const dest_ty = try sema.resolveType(block, dest_ty_src, extra.lhs);
1253912539 const operand = sema.resolveInst(extra.rhs);
1254012540 const operand_ty = sema.typeOf(operand);
12541 const target = sema.mod.getTarget();
12542
1254112543 try sema.checkPtrType(block, dest_ty_src, dest_ty);
1254212544 try sema.checkPtrOperand(block, operand_src, operand_ty);
1254312545 if (dest_ty.isSlice()) {
......@@ -12547,7 +12549,28 @@ fn zirPtrCast(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air
1254712549 try sema.analyzeSlicePtr(block, operand_src, operand, operand_ty)
1254812550 else
1254912551 operand;
12550 return sema.coerceCompatiblePtrs(block, dest_ty, ptr, operand_src);
12552
12553 try sema.resolveTypeLayout(block, dest_ty_src, dest_ty.elemType2());
12554 const dest_align = dest_ty.ptrAlignment(target);
12555 try sema.resolveTypeLayout(block, operand_src, operand_ty.elemType2());
12556 const operand_align = operand_ty.ptrAlignment(target);
12557
12558 // If the destination is less aligned than the source, preserve the source alignment
12559 var aligned_dest_ty = if (operand_align <= dest_align) dest_ty else blk: {
12560 // Unwrap the pointer (or pointer-like optional) type, set alignment, and re-wrap into result
12561 if (dest_ty.zigTypeTag() == .Optional) {
12562 var buf: Type.Payload.ElemType = undefined;
12563 var dest_ptr_info = dest_ty.optionalChild(&buf).ptrInfo().data;
12564 dest_ptr_info.@"align" = operand_align;
12565 break :blk try Type.optional(sema.arena, try Type.ptr(sema.arena, target, dest_ptr_info));
12566 } else {
12567 var dest_ptr_info = dest_ty.ptrInfo().data;
12568 dest_ptr_info.@"align" = operand_align;
12569 break :blk try Type.ptr(sema.arena, target, dest_ptr_info);
12570 }
12571 };
12572
12573 return sema.coerceCompatiblePtrs(block, aligned_dest_ty, ptr, operand_src);
1255112574}
1255212575
1255312576fn zirTruncate(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!Air.Inst.Ref {
......@@ -15340,7 +15363,7 @@ fn unionFieldVal(
1534015363 return sema.addConstant(field.ty, tag_and_val.val);
1534115364 } else {
1534215365 const old_ty = union_ty.unionFieldType(tag_and_val.tag);
15343 const new_val = try sema.bitCastVal(block, src, tag_and_val.val, old_ty, field.ty);
15366 const new_val = try sema.bitCastVal(block, src, tag_and_val.val, old_ty, field.ty, 0);
1534415367 return sema.addConstant(field.ty, new_val);
1534515368 }
1534615369 },
......@@ -16447,7 +16470,7 @@ fn storePtrVal(
1644716470 var kit = try beginComptimePtrMutation(sema, block, src, ptr_val);
1644816471 try sema.checkComptimeVarStore(block, src, kit.decl_ref_mut);
1644916472
16450 const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, kit.ty);
16473 const bitcasted_val = try sema.bitCastVal(block, src, operand_val, operand_ty, kit.ty, 0);
1645116474
1645216475 const arena = kit.beginArena(sema.gpa);
1645316476 defer kit.finishArena();
......@@ -16741,6 +16764,8 @@ fn beginComptimePtrMutation(
1674116764const ComptimePtrLoadKit = struct {
1674216765 /// The Value of the Decl that owns this memory.
1674316766 root_val: Value,
16767 /// The Type of the Decl that owns this memory.
16768 root_ty: Type,
1674416769 /// Parent Value.
1674516770 val: Value,
1674616771 /// The Type of the parent Value.
......@@ -16771,6 +16796,7 @@ fn beginComptimePtrLoad(
1677116796 if (decl_val.tag() == .variable) return error.RuntimeLoad;
1677216797 return ComptimePtrLoadKit{
1677316798 .root_val = decl_val,
16799 .root_ty = decl.ty,
1677416800 .val = decl_val,
1677516801 .ty = decl.ty,
1677616802 .byte_offset = 0,
......@@ -16783,6 +16809,7 @@ fn beginComptimePtrLoad(
1678316809 if (decl_val.tag() == .variable) return error.RuntimeLoad;
1678416810 return ComptimePtrLoadKit{
1678516811 .root_val = decl_val,
16812 .root_ty = decl.ty,
1678616813 .val = decl_val,
1678716814 .ty = decl.ty,
1678816815 .byte_offset = 0,
......@@ -16817,6 +16844,7 @@ fn beginComptimePtrLoad(
1681716844 };
1681816845 return ComptimePtrLoadKit{
1681916846 .root_val = parent.root_val,
16847 .root_ty = parent.ty,
1682016848 .val = try parent.val.elemValue(sema.arena, elem_ptr.index),
1682116849 .ty = elem_ty,
1682216850 .byte_offset = byte_offset,
......@@ -16832,6 +16860,7 @@ fn beginComptimePtrLoad(
1683216860 }
1683316861 return ComptimePtrLoadKit{
1683416862 .root_val = parent.root_val,
16863 .root_ty = parent.ty,
1683516864 .val = parent.val,
1683616865 .ty = parent.ty,
1683716866 .byte_offset = parent.byte_offset,
......@@ -16859,6 +16888,7 @@ fn beginComptimePtrLoad(
1685916888 };
1686016889 return ComptimePtrLoadKit{
1686116890 .root_val = parent.root_val,
16891 .root_ty = parent.ty,
1686216892 .val = try parent.val.fieldValue(sema.arena, field_index),
1686316893 .ty = parent.ty.structFieldType(field_index),
1686416894 .byte_offset = byte_offset,
......@@ -16870,6 +16900,7 @@ fn beginComptimePtrLoad(
1687016900 const parent = try beginComptimePtrLoad(sema, block, src, err_union_ptr);
1687116901 return ComptimePtrLoadKit{
1687216902 .root_val = parent.root_val,
16903 .root_ty = parent.root_ty,
1687316904 .val = parent.val.castTag(.eu_payload).?.data,
1687416905 .ty = parent.ty.errorUnionPayload(),
1687516906 .byte_offset = null,
......@@ -16881,6 +16912,7 @@ fn beginComptimePtrLoad(
1688116912 const parent = try beginComptimePtrLoad(sema, block, src, opt_ptr);
1688216913 return ComptimePtrLoadKit{
1688316914 .root_val = parent.root_val,
16915 .root_ty = parent.root_ty,
1688416916 .val = parent.val.castTag(.opt_payload).?.data,
1688516917 .ty = try parent.ty.optionalChildAlloc(sema.arena),
1688616918 .byte_offset = null,
......@@ -16918,7 +16950,7 @@ fn bitCast(
1691816950
1691916951 // TODO validate the type size and other compile errors
1692016952 if (try sema.resolveMaybeUndefVal(block, inst_src, inst)) |val| {
16921 const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty);
16953 const result_val = try sema.bitCastVal(block, inst_src, val, old_ty, dest_ty, 0);
1692216954 return sema.addConstant(dest_ty, result_val);
1692316955 }
1692416956 try sema.requireRuntimeBlock(block, inst_src);
......@@ -16932,6 +16964,7 @@ pub fn bitCastVal(
1693216964 val: Value,
1693316965 old_ty: Type,
1693416966 new_ty: Type,
16967 buffer_offset: usize,
1693516968) !Value {
1693616969 if (old_ty.eql(new_ty)) return val;
1693716970
......@@ -16942,7 +16975,7 @@ pub fn bitCastVal(
1694216975 const buffer = try sema.gpa.alloc(u8, abi_size);
1694316976 defer sema.gpa.free(buffer);
1694416977 val.writeToMemory(old_ty, target, buffer);
16945 return Value.readFromMemory(new_ty, target, buffer, sema.arena);
16978 return Value.readFromMemory(new_ty, target, buffer[buffer_offset..], sema.arena);
1694616979}
1694716980
1694816981fn coerceArrayPtrToSlice(
......@@ -19808,13 +19841,10 @@ fn pointerDeref(sema: *Sema, block: *Block, src: LazySrcLoc, ptr_val: Value, ptr
1980819841 // The Type it is stored as in the compiler has an ABI size greater or equal to
1980919842 // the ABI size of `load_ty`. We may perform the bitcast based on
1981019843 // `parent.val` alone (more efficient).
19811 return try sema.bitCastVal(block, src, parent.val, parent.ty, load_ty);
19844 return try sema.bitCastVal(block, src, parent.val, parent.ty, load_ty, 0);
19845 } else {
19846 return try sema.bitCastVal(block, src, parent.root_val, parent.root_ty, load_ty, parent.byte_offset.?);
1981219847 }
19813
19814 // The Type it is stored as in the compiler has an ABI size less than the ABI size
19815 // of `load_ty`. The bitcast must be performed based on the `parent.root_val`
19816 // and reinterpreted starting at `parent.byte_offset`.
19817 return sema.fail(block, src, "TODO: implement bitcast with index offset", .{});
1981819848}
1981919849
1982019850/// Used to convert a u64 value to a usize value, emitting a compile error if the number
src/codegen/llvm.zig+1-1
......@@ -2882,7 +2882,7 @@ pub const FuncGen = struct {
28822882 const bin_op = self.air.extraData(Air.Bin, ty_pl.payload).data;
28832883 const ptr_ty = self.air.typeOf(bin_op.lhs);
28842884 const elem_ty = ptr_ty.childType();
2885 if (!elem_ty.hasRuntimeBits()) return null;
2885 if (!elem_ty.hasRuntimeBits()) return self.dg.lowerPtrToVoid(ptr_ty);
28862886
28872887 const base_ptr = try self.resolveInst(bin_op.lhs);
28882888 const rhs = try self.resolveInst(bin_op.rhs);
src/type.zig+1-1
......@@ -2245,6 +2245,7 @@ pub const Type = extern union {
22452245 .prefetch_options,
22462246 .export_options,
22472247 .extern_options,
2248 .@"opaque",
22482249 => return 1,
22492250
22502251 .fn_noreturn_no_args, // represents machine code; not a pointer
......@@ -2432,7 +2433,6 @@ pub const Type = extern union {
24322433 .noreturn,
24332434 .inferred_alloc_const,
24342435 .inferred_alloc_mut,
2435 .@"opaque",
24362436 .var_args_param,
24372437 .bound_fn,
24382438 => unreachable,
test/behavior/optional.zig+4-1
......@@ -273,7 +273,10 @@ test "0-bit child type coerced to optional return ptr result location" {
273273}
274274
275275test "0-bit child type coerced to optional" {
276 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
276 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
277 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
278 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
279 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
277280
278281 const S = struct {
279282 fn doTheTest() !void {
test/behavior/ptrcast.zig+6-5
......@@ -4,7 +4,9 @@ const expect = std.testing.expect;
44const native_endian = builtin.target.cpu.arch.endian();
55
66test "reinterpret bytes as integer with nonzero offset" {
7 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
7 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
8 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
9 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
810
911 try testReinterpretBytesAsInteger();
1012 comptime try testReinterpretBytesAsInteger();
......@@ -43,7 +45,6 @@ fn testReinterpretBytesAsExternStruct() !void {
4345test "reinterpret struct field at comptime" {
4446 if (builtin.zig_backend == .stage2_wasm) return error.SkipZigTest; // TODO
4547 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
46 if (builtin.zig_backend == .stage2_x86_64) return error.SkipZigTest; // TODO
4748 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
4849 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
4950
......@@ -67,8 +68,6 @@ const Bytes = struct {
6768};
6869
6970test "comptime ptrcast keeps larger alignment" {
70 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
71
7271 comptime {
7372 const a: u32 = 1234;
7473 const p = @ptrCast([*]const u8, &a);
......@@ -77,7 +76,9 @@ test "comptime ptrcast keeps larger alignment" {
7776}
7877
7978test "implicit optional pointer to optional anyopaque pointer" {
80 if (builtin.zig_backend != .stage1) return error.SkipZigTest; // TODO
79 if (builtin.zig_backend == .stage2_c) return error.SkipZigTest; // TODO
80 if (builtin.zig_backend == .stage2_arm) return error.SkipZigTest; // TODO
81 if (builtin.zig_backend == .stage2_aarch64) return error.SkipZigTest; // TODO
8182
8283 var buf: [4]u8 = "aoeu".*;
8384 var x: ?[*]u8 = &buf;