optimize(no) is a very implementation-oriented directive. It’d be interesting to see how practical it would be to capture intent instead. Addressing the use cases raised here:
When implementing security-sensitive algorithms compiler optimisations are often disabled so that asymmetries are avoided. These might otherwise be used in side-channel attacks.
LLVM doesn’t guarantee that optnone will disable all optimizations. As one example, IPSCCP and other interprocedural optimizations can move code out of an optnone function into a non-optnone function, and noinline doesn’t disable them. As another, fast-isel sometimes falls back to selectiondag-isel, and selectiondag-isel does some optimizations automatically. If you’re doing crypto or similar and really need to be sure about timings, perhaps we should talk something like #[timing_sensitive] so that we can have a conversation with implementors about what’s needed to actually implement that reliably.
Imagine you are porting a large codebase from one compiler version to another and the new version contains… surprises.
and
You found that the optimizer takes a lot of time for no/little gain on some part of your code (arguably a bug, but still needs working around)
Thought experiment: what if you required the attribute to have a compiler identifier/version? Something like #optimize(no, LLVM, 4.3), which would only disable optimizations for a specific compiler/version (or perhaps range of versions)? That might discourage these attributes from applying in contexts where the original intent doesn’t apply.
Improved debug info when you debug in release mode (all but the lowest optimization level tends to make a total mess of it, and even mild optimizations can impact debug info quality somewhat).
There are ways to do optimization that don’t disrupt debugging, eg. work on the -Og flag, that optimize(no) would preclude. Would something like #[single_step_debugging], capture the intent here?
The optimizer does improve performance, but the code is rarely used so you don’t want to spend time on optimizing it (while still optimizing everything else)
Rust already covers this with #[cold].