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

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?

1 Like

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

1 Like

Awesome, thanks!

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 fns, etc.

2 Likes

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