llvm / llvm/llvm-project

[Clang/LLVM] Missing annotations for operator new

Open
#218,452 0 comments 1 reaction 0 assignees View on GitHub
clang libc++ llvm:ir
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

We're currently in the process of introducing new variant of `operator new` in libc++. Specifically #215813, which introduces `__new_at_least` (name still open to being changed of course). This function returns an `__allocation_result` consisting of a pointer and the actually allocated size (`{ptr, i64}` in LLVM IR). This is quite nice for `vector` and `string`, which can make use of any memory allocated additionally. Unfortunately, we currently can't tell the compiler much about this great new function.

What we're currently missing compared to an actual `operator new` call are (to my knowledge) the following LLVM IR attributes (ordered most to least severe based on my knowledge):
- [ ] `noalias` on the returned pointer
- [ ] `allocsize`
- [ ] `"alloc-family"=""`
- [ ] `allockind("alloc,uninitialized")`
- [ ] `nonnull` on the returned pointer (can probably be emulated via a `@llvm.assume`)
- [ ] `allocalign` (can probably be emulated via a `@llvm.assume`)

I believe none of these can be represented in LLVM IR currently.

On the Clang side, we already have `[[clang::malloc_span]]`, which is appropriate for the `noalias`. Everything else will have to be exposed somehow.

It would be great if we could extend LLVM and Clang to bring `__new_at_least` to allow the same optimizations as `operator new` does today. How exactly that is done is of course up to discussion, especially the last two bullets, which I expect can use `@llvm.assume` with bundles either via `__builtin_assume` and then having appropriate folds in InstCombine or by having some Clang attributes/builtins which emit the appropriate assumes.

CC @ldionne @fhahn @nikic @dtcxzyw @AaronBallman @erichkeane

Contributor guide

Open the contributing guide

Research direction

Start with the __new_at_least change referenced in #215813 and the existing Clang [[clang::malloc_span]] handling. Compare the missing LLVM IR attributes with operator new, and inspect the proposed __builtin_assume, @llvm.assume, and InstCombine entry points; done means __new_at_least supports the required operator-new optimizations.

Written by the indexing model from the issue text.

Assessment

Domain
compilers
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Active
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.