# Pre-RFC: New built-in attributes that map to new LLVM attributes useful on specific targets

**URL:** <https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932>\
**Category:** compiler\
**Created:** [May 28, 2024, 8:24pm UTC](https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932 "2024-05-28T20:24:35Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![rickwebiii](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rickwebiii/32/12220_2.png) [@rickwebiii](https://internals.rust-lang.org/u/rickwebiii)\
**Post date:** [May 28, 2024, 8:24pm UTC](https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932/1 "2024-05-28T20:24:35Z")

</div>

I'm working through adding a new target triple to LLVM and Rust and need to add a few features that will probably only be useful to this target. This target has a funky architecture and to ensure correct compilation, I need to add new LLVM attributes and macros in rustc that produce them. I've tried following `#[inline]` and `#[cold]` as examples of how to do this, but to no avail. Does anyone have any pointers on how to accomplish this?

---

<div class="post-metadata">

**Author:** ![CAD97](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/cad97/32/3460_2.png) [@CAD97](https://internals.rust-lang.org/u/CAD97)\
**Post date:** [May 28, 2024, 9:19pm UTC](https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932/2 "2024-05-28T21:19:31Z")

</div>

Looking at `#[ffi_pure]` might be easier then `#[inline]` since that more directly corresponds to just sticking on a backend function attribute.

---

<div class="post-metadata">

**Author:** ![rickwebiii](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rickwebiii/32/12220_2.png) [@rickwebiii](https://internals.rust-lang.org/u/rickwebiii)\
**Post date:** [May 28, 2024, 9:45pm UTC](https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932/3 "2024-05-28T21:45:14Z")

</div>

Awesome, thanks!

---

<div class="post-metadata">

**Author:** ![rickwebiii](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/rickwebiii/32/12220_2.png) [@rickwebiii](https://internals.rust-lang.org/u/rickwebiii)\
**Post date:** [May 29, 2024, 12:44am UTC](https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932/4 "2024-05-29T00:44:15Z")

</div>

Ok, that wasn't too bad for a simple fn attribute. A summary of what I did for others to find in the future:

- The actual builtin attribute gets added in `compiler/rustc_feature/src/builtin_attrs.rs`. If doing this for real, you'll want a `gated!()` macro so it's only available in unstable.
- Add a pre-interned string in `compiler/rustc_span/src/symbol.rs` by adding your attribute in the `Symbols` list
- Add a bitflag for your attribute in `compiler/rustc_middle/src/middle/codegen_fn_attrs.rs`
- Add some LLVM FFI attribute stuff in `compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp`, `compiler/rustc_codegen_llvm/src/llvm/ffi.rs`, `compiler/rustc_llvm/llvm-wrapper/LLVMWrapper.h`,
- Map the symbol to a bitflag in `compiler/rustc_codegen_ssa/src/codegen_attrs.rs`
- Add the LLVM attribute to the list of LLVM attributes in `compiler/rustc_codegen_llvm/src/attributes.rs`

This elides the LLVM changes I had to make and is just enough to plumb the attribute through to LLVM bytecode. You'll have to do more work to gate the feature in nightly Rust, fail gracefully in the gcc and cranelift backends, validate your attribute only appears on `fn`s, etc.

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/internals.rust-lang.org/system/32/14092_2.png) [@system](https://internals.rust-lang.org/u/system)\
**Post date:** [August 27, 2024, 12:44am UTC](https://internals.rust-lang.org/t/pre-rfc-new-built-in-attributes-that-map-to-new-llvm-attributes-useful-on-specific-targets/20932/5 "2024-08-27T00:44:30Z")

</div>

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.
