Expose tune-cpu from LLVM?

Clang has recently gained the ability to do a -mtune flag. This flag from GCC tells the compiler to emit instructions for some (probably ancient, like generic x86-64) CPU, but schedule (order) the instructions for a (probably more common, like a broadwell or a znver2) one. On the LLVM side, this is handled through a tune-cpu attribute, although at present only the x86 backend cares about it.

Rustc should make this a -C flag for users to take advantage of this feature.


Alright, so I poked around this a bit. It seems that for LLVM all that needs to be done is to tag all the functions with it like we are already doing for target-cpu. (The linker doesn't care about it, so there's nothing to be done there. Same for the LLVM target object.)

For AVR-GCC I haven't found a good way to pass in the -mtune yet, since target only accepts a String. (NUL-deliminated fields maybe? nope nope nope)

4 Likes

What's the difference with target-cpu and target-feature?

Like I said, one is for selecting the set of valid instructions, and the other is for instruction scheduling. It's a bigger thing for in-order CPUs like the typical ARM, but sadly LLVM does not have that yet.

Did you mean -mtune here?

OH SHIT yes I did. Edited. Thanks.

Good idea! The next step would be to propose a flag name an open a compiler MCP.

I'd expect the possible values to depend on the backend you're using (e.g. LLVM vs cranelift), which is worth noting in the MCP but shouldn't be a dealbreaker.