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

Nikita Popov via cfe-commits cfe-commits at lists.llvm.org
Fri Jan 19 05:35:50 PST 2024


nikic wrote:

@bwendling I think you are reading the GCC docs too pedantically. In particular, they also say

> If there are multiple objects ptr can point to and all of them are known at compile time, the returned number is the maximum of remaining byte counts in those objects if type & 2 is 0 and minimum if nonzero.

which makes it abundantly clear that what you get is an upper bound or lower bound, respectively. -1 and 0 are just the upper and lower bounds if you have no useful information at all. If you want to check whether the size is *exactly* known, you'll have to compare both bounds. Outside of doing that, you can never assume that the bound is precise.

Btw, it looks like your initial example gets 48 for both modes on GCC as well? https://c.godbolt.org/z/EfGWv4Wrh

> All of these are explicit in the LLVM IR. Is the worry that they've been changed from some transformations? Or are there others I'm missing?

Apart from the fact that what you are doing is simply illegal under our IR semantics, a practical case where this will likely compute incorrect results are unions. For unions, clang will use the type of the union member with the largest size as the alloca type, regardless of which union member is active. I haven't tried, but your patch will probably compute the subobject size based on that arbitrarily picked member, rather than the one being accessed.

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


More information about the cfe-commits mailing list