Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Incorrect floating point optimizations with ocamlopt #7890

vicuna opened this Issue Jan 2, 2019 · 3 comments


None yet
3 participants
Copy link

vicuna commented Jan 2, 2019

Original bug ID: 7890
Reporter: sfuric
Status: new
Resolution: open
Priority: normal
Severity: major
Version: 4.07.0
Category: back end (clambda to assembly)
Related to: #3526
Monitored by: @nojb @shindere @gasche @yakobowski

Bug description

ocamlopt incorrectly assumes that multiple occurrences of the same floating point expressions necessarily reduce to the same value. This is not always the case since rounding mode can be changed by programs.

Steps to reproduce

Compile files in attached archive with:
ocamlopt -c roundingMode_stubs.c
ocamlopt -c roundingMode.mli
ocamlopt -c
ocamlopt roundingMode.cmx roundingMode_stubs.o -o not_correct
ocamlopt roundingMode.cmx roundingMode_stubs.o -o correct

Executing ./not_correct or ./correct should give the same result (they do with ocamlc).

File attachments


This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Jan 2, 2019

Comment author: @lpw25

I could be wrong, but I thought that was what the compiler's -no-float-const-prop option was for. So I don't think this is a bug, but the expected behaviour.


This comment has been minimized.

Copy link
Collaborator Author

vicuna commented Jan 2, 2019

Comment author: sfuric

I wasn't aware of this option (there are so many of them).
But I would certainly have preferred the opposite default behavior, i.e. no floating point "optimizations" by default, because it breaks functional abstraction (my example precisely shows that by factorizing out common operations the behavior of the program is changed), and it also breaks compatibility with ocamlc.
Why not having a -float-const-prop option instead, I mean require users to explicitly ask for constant propagation (knowing that it may change the semantics)?

@vicuna vicuna added the back-end label Mar 14, 2019

@nojb nojb added the floating-point label Mar 16, 2019


This comment has been minimized.

Copy link

xavierleroy commented Mar 17, 2019

Currently, OCaml is very consistent w.r.t. this rounding mode business: the standard library provides no way to change the rounding mode, and the optimizer assumes the rounding mode is not changed (unless -no-float-const-prop is given).

If you use external libraries that change the rounding mode, it is your responsibility, or the library's responsibility, to set -no-float-const-prop.

This behavior was discussed and agreed on with the members of the Caml Consortium, some of which have a need to change the FP rounding mode but are happy with -no-float-const-prop. We will repsect this decision.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.