Thursday, 16 November 2023

[gccsdk] sizeof structs

Hello
 
I thought I'd have a go at building Python 3.12. While ro-config step works (with the patch from 3.11), trying the build fails with a static assertion "incorrect cache size".
 
From a look at the code, it seems to be related to the size of some structs .. for example
 

typedef union {
uint16_t cache;
struct {
uint8_t code;
uint8_t arg;
} op;
} _Py_CODEUNIT;
 
and
 
typedef struct {
uint16_t counter;
uint16_t version[2];
uint16_t index;
} _PyAttrCache;
 
The assert that INLINE_CACHE_ENTRIES_STORE_ATTR == 4
 
So what is that doing?
 
#define CACHE_ENTRIES(cache) (sizeof(cache)/sizeof(_Py_CODEUNIT))
#define INLINE_CACHE_ENTRIES_STORE_ATTR CACHE_ENTRIES(_PyAttrCache)
 
So from a naive point of view, it should be fine .. sizeof(_Py_CODEUNIT) is 2 bytes (a uniion of a 16 bit int or 2 8-bit ones), sizeof(_PyAttrCache) is 8 bytes (4 16-bit int (two plus two in the array).
 
However, while sizeof(_PyAttrCache) is indeed 8, sizeof(_PyCODEUNIT) is 4 (I am guessing it's being rounded up to a word).
 
I'm using gcc 4.7.4 (cross compiler). I have tried targetting a later CPU, and I did try setting the packed, aligned=2 on the _Py_CODEUNIT struct but that didn't seem to make any difference.
 
Any other suggestions? Later gcc? Some other options? I guess python probably shouldn't be assuming the sizes, but I'm not sure about getting that changed :)
 
I'm not too worried if it means python 3.12 is limited to more modern mechines. 3.8 works on everything else.
 
 
Cheers
 
Chris

No comments:

Post a Comment