[clang] Reland "[AllocToken] Infer the allocated type from placement new into the result" (PR #244809)

David Li via cfe-commits cfe-commits at lists.llvm.org
Wed Nov 25 14:54:24 PST 2026


david-xl wrote:

> > Thanks for the review. Before addressing the individual comments, I measured whether this is worth it, using heap profiles from heap-partitioned production deployments. Here are some stats:
> > 
> > * About 22% of their heap gets the fallback token. About 40% of that is pointerful or mixed, so it belongs in the pointer partition anyway.
> 
> Yes, with tcmalloc's current default scheme.

True -- in general when # of partitions are limited due to fragmentation concerns.

> 
> > * This PR, together with [[AllocToken] Propagate !alloc_token through inlined malloc_span wrappers #244314](https://github.com/llvm/llvm-project/pull/244314) and [[Clang][AllocToken] Keep span variables whose size is bound to a reference #244802](https://github.com/llvm/llvm-project/pull/244802), fixes exactly one pattern in practice: absl's CordRepFlat. That is about 3% of heap. No pointerful allocation moves to the pointer-free partition.
> > * The CordRepFlat win depends on accepting CordRep::storage[3] as a trailing array. With the strict []/[0]/[1] rule you suggested, the measurable benefit drops to about zero.
> > * The same 3% can be fixed in source.  Verified that this yields !{"char", i1 false} with today's compiler.
> > * A larger share, about 6% of heap, is pointer-free buffers that no inference rule can reach: void* wrappers, posix_memalign, and __size_returning_new without a covering placement new. Those need source changes anyway.
> 
> Thanks for the details.
> 
> > So I'm leaning towards dropping (or parking) this PR and fixing the hot sites in source. The remaining argument is just generality to cover future sites that use a pointer-free placement new with a trailing array.
> > We can drop the PR and pursue source cleanups for security for now. Can always revisit it in the future if needed. WDYT?
> 
> That'd be my preference. If simple source cleanups can achieve the same, that'd be my preference rather than a potentially brittle analysis. In fact, that's what I've already done in a few places.
> 
> One option is to hint the compiler:
> 
> ```
> p = malloc(size + 0 * sizeof(T));
> ```
> 
> But that might not always work, depending on how the bytes are allocated. But for e.g. inlinable wrappers, it should be possible to propagate a hint that the object will be of type T.
> 
> > PS. there seems to be a token propagation bug found in the study so an independent fix may come up for review when confirmed.
> 
> That's reasonable.



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


More information about the cfe-commits mailing list