[clang] [llvm] [Clang] Correct __builtin_dynamic_object_size for subobject types (PR #78526)

Bill Wendling via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 24 16:44:15 PST 2024


bwendling wrote:

> > maybe we could add the subtype as part of the llvm.objectsize intrinsic and use that instead of grappling with the whole object's type
> 
> I'm not sure I follow; if you know the object's type, doesn't that mean you also know its size?

Not necessarily. If you have something like this:

```c
struct x {
    int a;
    char foo[2][40];
    int b;
    int c;
};

size_t f(struct x *p, int idx) {
    return __builtin_dynamic_object_size(&p->foo[idx], 1);
}
```

But in general, it would be tricky for something like:

```c
__builtin_dynamic_object_size(&((char *)p)[idx], 1);
```

which I'm not sure if that's something GCC can handle. The documentation says that if the pointer can point to multiple objects, then we can return the whole object value (or something to that affect).

> > (I don't readily know of any transformation that changes a structure's layout. Does it exist?)
> 
> Any such transform has to reason about all the uses, so the llvm.objectsize call itself would prevent the transform from happening.

Bon! :-)

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


More information about the llvm-commits mailing list