Following Matt's last AoC video I decided to check out if the same optimization worked in Rust, namely the sum of the first N natural numbers being reduced to the formula N * (N + 1) / 1.
And it does! Here's the Godbolt link to it.
Like Clang, the optimization seems to be happening before the LLVM IR is being generated. In particular, going by this playground, I would think it is happening between the MIR and LLVM IR being emitted.
Could someone please point to which crate/file is responsible for such optimizations in the rustc?
The optimization is performed by LLVM in both rustc and clang. The reason you see it performed when you run with --emit=llvm-ir is because that still runs LLVM optimizations before emitting the LLVM IR.
On godbolt you can add an "Opt pipeline" tab to see how every LLVM optimization affects the output. In this case it's IndVarSimplifyPass Compiler Explorer
cargo-remark is also useful for showing compiler “remarks” saying what optimizations were applied. (You can also just look at what cargo command cargo-remark runs and do it yourself.)