Skip to content

Pointer arithmetic affects the alignment factor#3955

Merged
andrewrk merged 4 commits into
ziglang:masterfrom
LemonBoy:fix-1528
Jan 9, 2020
Merged

Pointer arithmetic affects the alignment factor#3955
andrewrk merged 4 commits into
ziglang:masterfrom
LemonBoy:fix-1528

Conversation

@LemonBoy

Copy link
Copy Markdown
Contributor

Closes #1528

@andrewrk andrewrk left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This case regressed:

test "hi" {
    var ptr: [*]align(8) u32 = undefined;

    const ptr2 = ptr + 1;
    @compileLog(@TypeOf(ptr2));
}

expected: [*]u32
found: [*]align(1) u32

@LemonBoy

LemonBoy commented Jan 8, 2020

Copy link
Copy Markdown
Contributor Author

This case regressed:

Hm? You're increasing ptr address by 4 (since it's an array of u32) and that makes it not aligned to 8 anymore

@andrewrk

andrewrk commented Jan 8, 2020

Copy link
Copy Markdown
Member

Yes but it should still be aligned to 4, e.g. it should not be underaligned. Maybe this is a better example:

test "extra aligned ptr" {
    var x: [2]u32 align(8) = [_]u32{1, 2};

    const result = addTwoNumbers(&x);
    @import("std").testing.expect(result == 3);
}

fn addTwoNumbers(ptr: [*]align(8) u32) u32 {
    return fetch(ptr + 0) + fetch(ptr + 1);
}

fn fetch(ptr: [*]u32) u32 {
    return ptr[0];
}

This correctly passes in master branch.

@LemonBoy

LemonBoy commented Jan 8, 2020

Copy link
Copy Markdown
Contributor Author

Yes but it should still be aligned to 4, e.g. it should not be underaligned.

Gotcha, I've added some code to do so ;)

Comment thread src/ir.cpp Outdated
Comment on lines +15805 to +15808
// The resulting pointer is aligned to the lcd between the
// offset (an arbitrary number) and the alignment factor (always
// a power of two, non zero)
uint32_t new_align = 1 << ctzll(byte_offset | align_bytes);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👏 👏 👏

much sophisticated, very wow 🐶

Comment thread test/stage1/behavior/pointers.zig Outdated
Comment on lines +304 to +305
const ptr4 = ptr + x; // runtime-known addend
expect(@typeInfo(@TypeOf(ptr4)).Pointer.alignment == 1);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wait, this should still be aligned to u32. x is runtime known but the number of bytes the pointer marches forward is @sizeOf(u32) * x which is guaranteed to be divisible by @sizeOf(u32).

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, I focused too much on the comptime-known branch and forgot the other one, now it should be fine!

@andrewrk
andrewrk merged commit 5e345ff into ziglang:master Jan 9, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

pointer math fails to modify alignment of type

2 participants