[PATCH] D43142: Experimental pass to convert all floating point operations to the equivalent constrained intrinsics

Kevin P. Neal via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 2 09:27:58 PDT 2018


kpn added a comment.

In https://reviews.llvm.org/D43142#1285069, @cameron.mcinally wrote:

> In https://reviews.llvm.org/D43142#1284977, @andrew.w.kaylor wrote:
>
> > I thought what we said was that if constrained FP intrinsics were used anywhere in a function then they had to be used everywhere in the function. So if you inline a function with FENV_ACCESS=ON into a function with FENV_ACCESS=OFF then the FP operations in the destination function need to be converted to constrained intrinsics, and if you inline a function with FENV_ACCESS=OFF into a function with FENV_ACCESS=ON then the FP operations in the function being inlined need to be converted. This conversion will have a detrimental effect on optimizations, so it would be nice to factor that into the inling cost, but at this point I'm mostly concerned about generating correct code.
>
>
> I'm realizing that FENV_ACCESS is poorly designed.
>
> Let's take this small example:
>
>   #include <fenv.h>
>   #include <stdio.h>
>  
>   void bar();
>  
>   #pragma STDC FENV_ACCESS ON
>   int main() {
>     feenableexcept(FE_DIVBYZERO);
>     bar();
>     float x = 1.0/0.0;
>     printf("main x=%lf\n", x);
>   }
>  
>   #pragma STDC FENV_ACCESS OFF
>   void bar() {
>     float x = 1.0/0.0;
>     printf("bar x=%lf\n", x);
>   }
>
>
> The FDiv in bar() would access the FP environment since main() enabled exceptions. But, since bar() is preceded with FENV_ACCESS=OFF, the Standard says that accessing the FP environment in bar() is undefined behavior. Since it's undefined behavior, the compiler can optimize as it wishes, including inlining bar() without modifying the FDiv.


If all optimizations including constant folding, or at least optimizations on floating point, are delayed until after inlining then there's no problem. And the optimizations applied to an inlined functions don't have to be the same as the optimizations on the standalone function. The copy of bar() inlined into main() can inherit the FENV_ACCESS ON of main() despite the standalone version of bar() having it OFF.


https://reviews.llvm.org/D43142





More information about the llvm-commits mailing list