authorgravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-09 11:56:45+01:00
committergravatar for thatlemon@gmail.comLemonBoy <thatlemon@gmail.com> 2020-01-09 11:56:45+01:00
logc51b79c56e32594e4fb119fc760ae38b69fb9bbb
treecf906791fd6f2df1240fade35b8c9213a3012e01
parent7ea7842ed020d5b984d14b824663891aae7549bc

Correct alignment calculation for runtime addends


2 files changed, 25 insertions(+), 18 deletions(-)

src/ir.cpp+17-17
......@@ -15776,6 +15776,10 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1577615776 return ir_const_undef(ira, &instruction->base, op1->value->type);
1577715777 }
1577815778
15779 ZigType *elem_type = op1->value->type->data.pointer.child_type;
15780 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown)))
15781 return ira->codegen->invalid_instruction;
15782
1577915783 // NOTE: this variable is meaningful iff op2_val is not null!
1578015784 uint64_t byte_offset;
1578115785 if (op2_val != nullptr) {
......@@ -15783,9 +15787,6 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1578315787 if (!ir_resolve_usize(ira, casted_op2, &elem_offset))
1578415788 return ira->codegen->invalid_instruction;
1578515789
15786 ZigType *elem_type = op1->value->type->data.pointer.child_type;
15787 if ((err = type_resolve(ira->codegen, elem_type, ResolveStatusSizeKnown)))
15788 return ira->codegen->invalid_instruction;
1578915790 byte_offset = type_size(ira->codegen, elem_type) * elem_offset;
1579015791 }
1579115792
......@@ -15795,24 +15796,23 @@ static IrInstruction *ir_analyze_bin_op_math(IrAnalyze *ira, IrInstructionBinOp
1579515796 }
1579615797
1579715798 ZigType *result_type = op1->value->type;
15798 // The resulting pointer may not be aligned anymore
15799 if (op2_val != nullptr) {
15799 // Calculate the new alignment of the pointer
15800 {
1580015801 uint32_t align_bytes;
1580115802 if ((err = resolve_ptr_align(ira, op1->value->type, &align_bytes)))
1580215803 return ira->codegen->invalid_instruction;
1580315804
15804 if (byte_offset & (align_bytes - 1)) {
15805 // The resulting pointer is aligned to the lcd between the
15806 // offset (an arbitrary number) and the alignment factor (always
15807 // a power of two, non zero)
15808 uint32_t new_align = 1 << ctzll(byte_offset | align_bytes);
15809 // Rough guard to prevent overflows
15810 assert(new_align);
15811 result_type = adjust_ptr_align(ira->codegen, result_type, new_align);
15812 }
15813 } else {
15814 // The addend is not a comptime-known value
15815 result_type = adjust_ptr_align(ira->codegen, result_type, 1);
15805 // If the addend is not a comptime-known value we can still count on
15806 // it being a multiple of the type size
15807 uint32_t addend = op2_val ? byte_offset : type_size(ira->codegen, elem_type);
15808
15809 // The resulting pointer is aligned to the lcd between the
15810 // offset (an arbitrary number) and the alignment factor (always
15811 // a power of two, non zero)
15812 uint32_t new_align = 1 << ctzll(addend | align_bytes);
15813 // Rough guard to prevent overflows
15814 assert(new_align);
15815 result_type = adjust_ptr_align(ira->codegen, result_type, new_align);
1581615816 }
1581715817
1581815818 if (op2_val != nullptr && op1_val != nullptr &&
test/stage1/behavior/pointers.zig+8-1
......@@ -302,12 +302,19 @@ test "pointer arithmetic affects the alignment" {
302302 const ptr3 = ptr + 0; // no-op
303303 expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
304304 const ptr4 = ptr + x; // runtime-known addend
305 expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 1);
305 expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
306306 }
307307 {
308308 var ptr: [*]align(8) [3]u8 = undefined;
309 var x: usize = 1;
309310
310311 const ptr1 = ptr + 17; // 3 * 17 = 51
311312 expect(@typeInfo(@TypeOf(ptr1)).Pointer.alignment == 1);
313 const ptr2 = ptr + x; // runtime-known addend
314 expect(@typeInfo(@TypeOf(ptr2)).Pointer.alignment == 1);
315 const ptr3 = ptr + 8; // 3 * 8 = 24 -> lcd(8,24) = 8
316 expect(@typeInfo(@TypeOf(ptr3)).Pointer.alignment == 8);
317 const ptr4 = ptr + 4; // 3 * 4 = 12 -> lcd(8,12) = 4
318 expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 4);
312319 }
313320}