Is there a way to set per-function compiler options?


Is there a way to set different compiler options on a per-function basis? Specifically, to allow some instruction set extensions in a function that won’t be called if the instruction set extensions aren’t supported by the CPU?

Concretely, I’m interested in compiling code for 32-bit x86 without SSE2 in general but then having SSE2 enabled for a particular function.

Having to create two crates, compile them with different options and link them together seems sad.


There’s unfortunately not a way to do this directly. When I checked a few months ago, LLVM (rustc’s backend) hadn’t completed its support for this either. Building two crates or linking in some asm (:frowning:) is the easiest way at the moment.


OK. :frowning: Thanks.


In GCC the function attributes target(options) and target_clones(options) help:


Yes, clang getting support for that GNU extension is why LLVM is getting/has gotten support for per-function compiler options, however, rustc is not GCC, nor do we use the GCC backend, so it unfortunately isn’t immediately helpful.