[clang] [CodeGen] Revamp counted_by calculations (PR #70606)

Yeoul Na via cfe-commits cfe-commits at lists.llvm.org
Fri Nov 3 13:07:43 PDT 2023


rapidsna wrote:

> With this PR I get:
> 
> ```
> $ clang -O2 ~/llvm/bdos.c
> $ ./a.out
> 400
> 400
> 404
> ```

Oh I see. 

> I think the first two `printf` s in your code seem correct with 40, because they're both looking at only the FAM, not the entire struct.

Interesting. So this sounds different from non-fam array cases. When the struct is not a flexible array member, e.g., `struct s {int a; int arr[3]; int b;};`  `__bdos(&p->arr[0], 0)` would return the whole object size instead of the array-only size (12). https://godbolt.org/z/1e9nKPYb4 Was this difference part of the specification?

> struct flex *p = (struct flex *)malloc(sizeof(struct flex) + sizeof(int) * 10);

I just wanted to call out that this case is interesting because the size derived from malloc and __counted_by compete each other. And __counted_by always wins. This conforms to how `-fbounds-safety` will see the size of objects, so sounds like we are all happy with it.

Similarly, this will also mean that , when we support `__counted_by` for pointers, the following will be the case.

```
int arr[10];
int *__counted_by(4) p = arr;

__bdos(arr, 0); // returns 4 * sizeof(int) instead of 10 * sizeof(int)

``` 

https://github.com/llvm/llvm-project/pull/70606


More information about the cfe-commits mailing list