Sys.opaque_identity and [@inline never]

Is there a difference in the optimization the compiler can do between

let f () = e
[@inline never]

let () =
...
let x = f () in
...

and

...
let x = Sys.opaque_identity e in
...

The short answer is yes.

There are some examples on PR#9412, although those differences will disappear if this PR is merged, but there is also the fact that e is not compiled in the same context in both cases and some optimizations might trigger or not depending on such context.

However, keep in mind that in recent versions of the compiler (since 4.08), f is likely to be inlined even with the [@inline never] annotation, because it is considered local and triggers an optimization introduced in PR#2143. This will make the first piece of code equivalent to let x = e, which is probably not what you want in this case. This can be disabled using the additional [@local never] attribute.