Find a better solution for what the "no-builtins" attribute is meant to address
- Dominant language
- LLVM
- Stars
- 40.5k
- Forks
- 18.7k
- PR merge metrics
- PR metrics pending
Description
Clang adds the "no-builtins" attribute (not to be confused with `nobuiltin` which is documented in the langref) to all functions in a TU when `-fno-builtin[-foo]` is used. The attribute was originally landed in https://reviews.llvm.org/D74162 and was meant to ensure that builtin-looking functions like `memcpy` don't end up being replaced with calls to the functions themselves when implementing the function (ie. libc's `memcpy` which might call things which look like `memcpy` or use the `memcpy` intrinsic aren't replaced under the hood with libcalls to `memcpy` in its own definition).
The reason for adding it makes sense, but this looks too large of a hammer and prevents some inlining from LTO. For us, this prevents cross-language LTO inlining between rust and c++ code. One of the requirements for inlining a callee function into a caller is ensuring the callee and caller functions have "compatible" attributes. In freestanding mode, clang adds "no-builtins" to every function but rust has no way of emitting this attribute because only clang emits it.
Not sure if we want to keep "no-builtins" as a long term solution to the libc issue, but at the very least we should do the following:
- Rename "no-builtins[-foo]" to something like "inhibit-target-libcall[-foo]" which is more descriptive of what this is actually used for
- Add documentation for this somewhere. The only way for someone to figure out what this does is by combing through the code, which ideally we wouldn't need to do.
- Maybe formalize this into a dedicated IR attribute with its own enum value instead of just having it be a raw string. (I didn't even know IR could take arbitrary strings as attributes until finding out about this.) If this will be sticking around, rust will also need a way to emit this to facilitate cross-language inlining.
Contributor guide
Research direction
No file or test is named. Start by tracing Clang's `no-builtins` handling and the IR attribute compatibility checks described in the issue, then compare the libc protection need with cross-language LTO; done means an agreed replacement or retention plan, documentation, and, if retained, a defined way for Rust to emit it.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- cpp, rust
- Domain
- compilers
- Issue type
- Refactor
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Quiet
- Clarity
- Mostly clear
- Newbie friendliness
- 35/100