CGAL / cgal

The public CGAL repository, see the README below
https://github.com/CGAL/cgal#readme
Other
4.93k stars 1.38k forks source link

Investigate clang's -frounding-math #6924

Open mglisse opened 2 years ago

mglisse commented 2 years ago

Issue Details

Not directly an issue, but... It used to be that clang didn't support -frounding-math. However, we have enough protection through inline asm that things seemed to work fine. Recent versions of clang implement #pragma fenv and -frounding-math seems to turn it on globally. I see 2 things we should investigate

Environment

lrineau commented 2 years ago

Here is the change: https://reviews.llvm.org/D62731#change-Qs7eGduOQs42. It seems to have been introduced in clang 10.0.0.

The experiments we tried in the past about the pragma were not successful, because the effects of pragma with C++ templates was not the expected ones.

mglisse commented 2 years ago

The experiments we tried in the past about the pragma were not successful, because the effects of pragma with C++ templates was not the expected ones.

But that was with MSVC or Intel, right? At least with very basic tests (certainly nothing exhaustive though), clang seemed to do something sensible with templates. On the other hand, llvm seems to optimize essentially nothing when rounded operations are involved. Even converting 2 to double is done at runtime... And it seems to disable inlining. On the benchmark Triangulation_3/simple_2.cpp with 100000 points, -frounding-math increases the running time from .34 to .41. So for now I would recommend that clang users avoid -frounding-math.

lrineau commented 2 years ago

The experiments we tried in the past about the pragma were not successful, because the effects of pragma with C++ templates was not the expected ones.

But that was with MSVC or Intel, right? At least with very basic tests (certainly nothing exhaustive though), clang seemed to do something sensible with templates. On the other hand, llvm seems to optimize essentially nothing when rounded operations are involved. Even converting 2 to double is done at runtime... And it seems to disable inlining. On the benchmark Triangulation_3/simple_2.cpp with 100000 points, -frounding-math increases the running time from .34 to .41. So for now I would recommend that clang users avoid -frounding-math.

Maybe we should consider removing -frounding-math with gcc as well, and apply workarounds. That might also speeds up computations.

mglisse commented 2 years ago

IIRC with gcc -frounding-math doesn't cause much slowdown for CGAL (in large part because the option doesn't protect against much). On the other hand, it doesn't do much and we would probably be about as safe without it.

lrineau commented 2 years ago

IIRC with gcc -frounding-math doesn't cause much slowdown for CGAL (in large part because the option doesn't protect against much). On the other hand, it doesn't do much and we would probably be about as safe without it.

That is right. The real challenge would be to try -funsafe-math-optimizations or -ffast-math with gcc.

mglisse commented 2 years ago

Adding -ffast-math at link time brings in an object file that sets the processor mode to flush denormals to 0, irrespective of rounding modes. That doesn't seem compatible with intervals. So you would first need to put that back to normal later in the initialization. Other than that, intervals should be fine I think. I am less confident about static filters, the bounds rely on the code the way it is written, and even changing (x+y)+z to x+(y+z) could change the required bound for the filter.