Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Translate c pointer alignment #2318

Merged
merged 1 commit into from
Apr 30, 2019

Conversation

LemonBoy
Copy link
Contributor

I hope @raulgrell doesn't mind but I ended up needing this for some translate-c magic to work so I went ahead and wrote this PR.

Closes #2313

@raulgrell
Copy link
Contributor

Just saw this, I don't mind at all - this way I get to compare our solutions =)

@raulgrell
Copy link
Contributor

Your implementation addresses the first two questions I had posted on my PR:

If the bit size is known, should it be included directly instead of calling @Alignof on the target type?
Yes

Should I extract the alignment comparison into a function? Or is the duplication ok?
Yes and more

If the alignment is not known and/or the returned alignment is 0, should the alignCasts be skipped? This is the case for void *. I think alignCast requires a power of 2 alignment.

Regarding dividing the bit alignment by 8 - is it guaranteed to be a multiple of 8 or could it consider an alignment of 17 bits to not be smaller than one of 18 bits because the division truncates the remainder? I'll make a comment on the commit

const clang::PointerType *ptr_ty = reinterpret_cast<const clang::PointerType*>(ty);
ZigClangQualType base_qt = bitcast(ptr_ty->getPointeeType());
// Convert from bits to bytes
return ZigClangASTContext_getTypeAlign(c->ctx, base_qt) / 8;
Copy link
Contributor

Choose a reason for hiding this comment

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

is the type alignment guaranteed to be multiple of 8? Could integer div interfere with ptr alignment comparison?

\\ var p: ?*c_void = undefined;
\\ {
\\ var to_char: [*c]u8 = @ptrCast([*c]u8, p);
\\ var to_short: [*c]c_short = @ptrCast([*c]c_short, @alignCast(2, p));
Copy link
Member

Choose a reason for hiding this comment

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

maybe this is easier said than done but this would be better translated as @alignCast(@alignOf(c_short), p) rather than hard coding 2.

@LemonBoy
Copy link
Contributor Author

If the alignment is not known and/or the returned alignment is 0, should the alignCasts be skipped? This is the case for void *. I think alignCast requires a power of 2 alignment.

getTypeAlign is a safer bet than getTypeAlignIfKnown here, the former returns 8 for void so we're good.

Regarding dividing the bit alignment by 8 - is it guaranteed to be a multiple of 8 or could it consider an alignment of 17 bits to not be smaller than one of 18 bits because the division truncates the remainder? I'll make a comment on the commit

Common wisdom is that it is indeed safe and that's also what llvm does in getTypeAlignInChars. We're only interested in the number of bytes (or octets) hence the division by 8.

maybe this is easier said than done but this would be better translated as @alignCast(@Alignof(c_short), p) rather than hard coding 2.

I've left the hard-coded number since it also serves as a sanity check to catch types with mismatched alignment.

No idea about the test failure, as always it's osx that's failing.

@andrewrk
Copy link
Member

andrewrk commented Apr 25, 2019

maybe this is easier said than done but this would be better translated as @alignCast(@Alignof(c_short), p) rather than hard coding 2.

I've left the hard-coded number since it also serves as a sanity check to catch types with mismatched alignment.

I think it's actually problematic, because, for example, the alignment of short in C can be different on different targets. So using the value 2 instead of @alignOf(c_short) loses the portability that the previous code had. It's a bit of a footgun.

No idea about the test failure, as always it's osx that's failing.

Don't worry about that one - it's one of the things I'm addressing with the coroutine rewriting.

@LemonBoy LemonBoy force-pushed the translate-c-pointer-alignment branch 2 times, most recently from 15027ae to e4fda63 Compare April 26, 2019 21:50
@@ -1334,7 +1334,7 @@ pub fn addCases(cases: *tests.TranslateCContext) void {
\\}
,
\\fn ptrcast(a: [*c]c_int) [*c]f32 {
\\ return @ptrCast([*c]f32, a);
\\ return @ptrCast([*c]f32, @alignCast(@alignOf([*c]f32), a));
Copy link
Member

Choose a reason for hiding this comment

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

This is not quite right, it should be @alignOf(f32).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Duh, I must've missed some type unwrapping in the last rewrite. Sorry for the noise, the patch should be ready to go now.

Avoid producing Zig code that doesn't compile due to mismatched
alignments between pointers.

Always emit a @Alignof instead of hardcoding the alignment value
returned by LLVM for portability sake of the generated code.
@LemonBoy LemonBoy force-pushed the translate-c-pointer-alignment branch from e4fda63 to 7c41146 Compare April 29, 2019 22:45
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.

translate-c and pointer alignment
3 participants