From 07e6648e1230721b9d2c8ed298719a12de9e1d33 Mon Sep 17 00:00:00 2001 From: Michael Klimenko Date: Wed, 2 Aug 2023 21:50:35 +0200 Subject: [PATCH] Enable Intel oneAPI DPC++/C++ compiler build (#16587) Last week I fixed error #16484 found when trying to build onnxruntime with the icpx compiler. Another thing I found out is that icpx uses -ffast-math flag by default. You can check it by running the compiler with -v flag like following: ```bash # Setup the environment . /opt/intel/oneapi/setvars.sh # Compile any file to see all the implicit flags icpx -v main.cpp ``` This leads to a bunch of warnings during the build like: ```bash In file included from /mnt/f/wsl_home/onnxruntime/onnxruntime/test/providers/cpu/tensor/upsample_op_test.cc:5: In file included from /mnt/f/wsl_home/onnxruntime/onnxruntime/test/providers/provider_test_utils.h:6: In file included from /mnt/f/wsl_home/onnxruntime/onnxruntime/test/providers/checkers.h:10: In file included from /mnt/f/wsl_home/onnxruntime/onnxruntime/core/util/math_cpuonly.h:68: In file included from /mnt/f/wsl_home/onnxruntime/build/Linux/RelWithDebInfo/_deps/eigen-src/Eigen/Core:172: /mnt/f/wsl_home/onnxruntime/build/Linux/RelWithDebInfo/_deps/eigen-src/Eigen/src/Core/MathFunctions.h:1019:12: warning: comparison with NaN always evaluates to false in fast floating point modes [-Wtautological-constant-compare] return isnan EIGEN_NOT_A_MACRO (x); ^~~~~~~~~~~~~~~~~~~~~~~~~~~ ``` And some tests are failing as well, usually with infinities involved. To list a few: ```bash # ... 1: [ FAILED ] IsInfTest.test_isinf_float 1: [ FAILED ] IsInfTest.test_isinf_double 1: [ FAILED ] IsInfTest.test_isinf_positive_float 1: [ FAILED ] IsInfTest.test_isinf_positive_double 1: [ FAILED ] IsInfTest.test_isinf_negative_float 1: [ FAILED ] IsInfTest.test_isinf_negative_double 1: [ FAILED ] IsNaNOpTest.IsNaNFloat 1: [ FAILED ] IsNaNOpTest.IsNaNDouble # ... ``` This PR adds a quick global check for the IntelLLVM compiler, as in the way its name is reported by CMake and then, depending on the compiler driver, sets either MSVC-like or GCC-like switch to disable fast-maths. Probably a bit cleaner solution would be to use ```target_compile_options(${TARGET} PRIVATE MEOW)``` instead of a global-wide ```set(CMAKE_CXX_FLAGS MEOW)```, but then we'd be required to add it to all the individual targets and execution providers and this will lead to a lot of code duplication. --- cmake/CMakeLists.txt | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/cmake/CMakeLists.txt b/cmake/CMakeLists.txt index 104b8ac213..db5f817fdb 100644 --- a/cmake/CMakeLists.txt +++ b/cmake/CMakeLists.txt @@ -30,6 +30,17 @@ endif() # Project project(onnxruntime C CXX ASM) +# Disable fast-math for Intel oneAPI compiler +if("${CMAKE_CXX_COMPILER_ID}" MATCHES "IntelLLVM") + if("${CMAKE_CXX_COMPILER_ID}" MATCHES "MSVC-like") + # Using icx-cl compiler driver with MSVC-like arguments + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /fp:precise") + else() + # Using icpx compiler driver + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -fno-fast-math") + endif() +endif() + # Needed for Java set(CMAKE_C_STANDARD 99)