Skip to content

Commit ed351b8

Browse files
pratlucasIanWood1
authored andcommitted
Re-land: [Analysis] Ensure use of strict fp exceptions in ConstantFolding (llvm#137652)
To perform constant folding in math operations, the implementation of the ConstantFolding Analysis relies on the use of the math functions from the host's libm. In particular, it relies on checking the value of errno and IEEE exceptions to determine when an operation is safe to be constant-folded. On some platforms, such as BSD or Darwin, math library functions don't set errno, so the ConstantFolding check depends only on the value of IEEE exceptions. As the FP exception behaviour is set to `ignore` by default, the compiler can perform optimisations that would get in the way of such checks being performed correctly. This patch sets the FP exception behaviour to `strict` when compiling the `ConstantFolding.cpp` source file, ensuring the value of IEEE exceptions can be reliably used by its implementation. This re-lands the changes from llvm#136139, but using the `-ftrapping-math` compile option instead of `-ffp-exception-behavior` for GCC support.
1 parent 8957551 commit ed351b8

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

llvm/lib/Analysis/CMakeLists.txt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,17 @@ if (DEFINED LLVM_HAVE_TF_AOT OR LLVM_HAVE_TFLITE)
2323
endif()
2424
endif()
2525

26+
# The implementation of ConstantFolding.cpp relies on the use of math functions
27+
# from the host. In particular, it relies on the detection of floating point
28+
# exceptions originating from such math functions to prevent invalid cases
29+
# from being constant folded. Therefore, we must ensure that fp exceptions are
30+
# handled correctly.
31+
if (MSVC)
32+
set_source_files_properties(ConstantFolding.cpp PROPERTIES COMPILE_OPTIONS "/fp:except")
33+
elseif(CMAKE_CXX_COMPILER_ID MATCHES "GNU|Clang")
34+
set_source_files_properties(ConstantFolding.cpp PROPERTIES COMPILE_OPTIONS "-ftrapping-math")
35+
endif()
36+
2637
add_llvm_component_library(LLVMAnalysis
2738
AliasAnalysis.cpp
2839
AliasAnalysisEvaluator.cpp

0 commit comments

Comments
 (0)