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

Add Support For Alignment Specifier In C Struct Declarations #94

Merged
merged 4 commits into from
Nov 11, 2024

Conversation

noahmartinwilliams
Copy link

C Data structures can now be parsed that have the _Alignas and alignas keywords used in it.

@expipiplus1
Copy link
Collaborator

Thank you,

If you add a test and give the version number a minor bump then we can make a release on Hackage.

The last I remember was the the tests were a little tricky, sorry.

@noahmartinwilliams
Copy link
Author

I can't seem to figure out where the test would fit. test/harness/README says that suite is meant for "existing libraries" (which I'm guessing means compiling C libraries from other projects) and harness is for "tests documenting bugs and problems", but I can't seem to find where I would test new features. Should I just put one in harness?

I'm kind of new to Haskell and it's testing system tbh.

@expipiplus1
Copy link
Collaborator

expipiplus1 commented Aug 3, 2023

Ok, welcome, and no worries, I'll take care of the tests when this is done:

I think that this isn't quite correct however. For example it will fail when the alignment specifier is not the first specifier of the first struct member.

A careful reading of the C11 standard (draft) actually doesn't allow these on struct members (https://www.open-std.org/jtc1/sc22/wg14/www/docs/n1570.pdf) however this was corrected in a defect report https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2396.htm#dr_444

I would expect that the example here parses, for example: https://en.cppreference.com/w/c/language/_Alignas

#include <stdalign.h>
#include <stdio.h>
 
// every object of type struct sse_t will be aligned to 16-byte boundary
// (note: needs support for DR 444)
struct sse_t
{
    alignas(16) float sse_data[4];
};
 
// every object of type struct data will be aligned to 128-byte boundary
struct data
{
    char x;
    alignas(128) char cacheline[128]; // over-aligned array of char,
                                      // not array of over-aligned chars
};
 
int main(void)
{
    [printf](http://en.cppreference.com/w/c/io/fprintf)("sizeof(data) = %zu (1 byte + 127 bytes padding + 128-byte array)\n",
           sizeof(struct data));
 
    [printf](http://en.cppreference.com/w/c/io/fprintf)("alignment of sse_t is %zu\n", alignof(struct sse_t));
 
    alignas(2048) struct data d; // this instance of data is aligned even stricter
    (void)d; // suppresses "maybe unused" warning
}

@noahmartinwilliams
Copy link
Author

noahmartinwilliams commented Aug 17, 2023

Does this library support statements that declare a variable? I can't seem to find anything that would allow it to declare a local variable.
Edit:
Actually it seems that this project uses much of the grammar from the C11 standard which, confusingly enough, doesn't count variable declarations as a kind of statement. I've added a test to my version that uses the code you presented and the type checker says it works. I wasn't able to get it to print out the alignment and size of the structures though.

@tomsmeding
Copy link

Hi! Dropping in from outside -- having in language-c would be awesome for Accelerate. With this PR, and with an appropriate version bump in c2hs, c2hs can parse cuda.h from Cuda 12. With Noah's other PR to cuda this means that Accelerate can support Cuda 12, which would be great. :)

@Bodigrim
Copy link
Collaborator

Bodigrim commented Nov 8, 2024

@noahmartinwilliams could you possibly rebase?

@noahmartinwilliams
Copy link
Author

@Bodigrim , ok, rebased.

@Bodigrim
Copy link
Collaborator

@noahmartinwilliams GitHub UI shows me lots of new files, could you please take another look? Were they added by accident?

@noahmartinwilliams
Copy link
Author

@Bodigrim . Huh, that's weird. I could have sworn I only changed a few files. I honestly don't know what to do here. I guess they must have somehow been added by accident. I think I saw some of them that looked like they were from my home directory, so maybe there's a bug in git or github?

C Data structures can now be parsed that have the _Alignas and alignas
keywords used in it.
The alignas keyword can be used anywhere in a data structure declaration
in front of any attribute.
Previously the alignas keywords would only apply alignment to attributes
immediately to the right of them and not after them.
@Bodigrim
Copy link
Collaborator

No worries, I rebased myself. Thanks!

@Bodigrim Bodigrim merged commit 23d2100 into visq:master Nov 11, 2024
13 checks passed
@Bodigrim
Copy link
Collaborator

@tomsmeding @leftaroundabout before I cut a release of language-c, could someone please verify that the current master branch (commit a811096) is capable of parsing cuda.h?

tomsmeding added a commit to diku-dk/CFAL-bench that referenced this pull request Nov 12, 2024
The PR ( visq/language-c#94 ) is merged (thanks
Bodigrim!) so we can just use that directly.
@tomsmeding
Copy link

@Bodigrim Works! Checked a project that indeed fails with language-c from hackage but works with 0.10.0 from master here.

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.

4 participants