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

contracted floating-point expressions #373

Open
monniaux opened this issue Oct 18, 2020 · 2 comments
Open

contracted floating-point expressions #373

monniaux opened this issue Oct 18, 2020 · 2 comments

Comments

@monniaux
Copy link
Contributor

monniaux commented Oct 18, 2020

The C standard allows a compiler to "contract" compound floating-point expressions by removing intermediate rounding points. Concretely, an expression a*b+c may be rewritten into fma(a, b, c).

It has come to my attention that part of CompCert's low performance compared to gcc on certain kinds of programs (matrix and other numerical computations) can be attributed to gcc, by default, contracting expressions.

Since this optimization modifies semantics, it cannot be implemented in the verified parts of CompCert, but I think it could be implemented in the unverified parts of the front-end (those that deal with varargs, structure passing and so on).

On gcc and clang, this optimization may be turned off and on using -ffp-contract=off/on/fast. A similar option could be used. The C standard mandates a #pragma STDC FP_CONTRACT but gcc does not implement it yet.

@xavierleroy
Copy link
Contributor

I'm uneasy about this suggestion.

CompCert prides itself of compiling FP computations exactly as written in the source, without excess precision, "contraction", reassociation, or any of that stuff allowed by the C and Fortran standards that makes it so painful to conduct a precise FP analysis of the code, as you observed previously. To me, this predictability of FP computations is worth more than improving benchmark figures.

Now, you're right that FP contraction can be controlled by the users, and off by default. I also agree that programming with explicit invocations of __builtin_fma is syntactically heavy and unpleasant. I'm still on the fence.

Concering #pragma STDC FP_CONTRACT, CompCert could honor it at the level of whole functions, but not at the statement level. That's because CompCert's C elaborator currently processes pragmas at "top-level" of a source file, but not inside functions.

@andrew-appel
Copy link

I have worked on and continue to work on the verification of C programs that use floating point. Indeed I need "precise FP analysis of the code". But I do this analysis at the Clight level, not the Csem level. So indeed, if this transformation were done before Clight (or at least, before Clight2), then it would not substantially interfere with proving programs correct -- especially if it could be turned on or off using the pragma.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants