Reading FP Environment status from IR

Hi All,

We would like to prevent certain optimizations (like hoisting an fdiv instruction) when the application has set the floating point environment flags using APIs like
feraiseexcept or if it uses #pragma STDC FENV_ACCESS.

Is there any way to know this at LLVM-IR level?

Regards,
Raghesh

We have intrinsic versions of the floating point instructions to suppress optimizations that are unsafe when exceptions are enabled. https://llvm.org/docs/LangRef.html#constrained-floating-point-intrinsics Currently I think it prevents nearly all optimizations.

Clang should emit these for some targets like X86 when it sees #pragma STDC FENV_ACCESS. Clang can only do this for targets that have implemented proper support for the constrained intrinsics in the backend.

I’d use the clang -ffp-exception-behavior=strict or =maytrap options instead of the #pragma. I’m not convinced the #pragma always works correctly and emits the correct metadata for constrained intrinsics. It’s OK for now, but I’m working on optimizations for LLVM that rely on correct metadata. You’ll get correct metadata with the command line option.

The first phab ticket for optimizations is https://reviews.llvm.org/D99962 with more on the way. It’s an EarlyCSE change. If anyone wants to jump in as a reviewer I won’t mind at all.