IntelLLVM: Unable to set oneAPI-specific C++ compiler flags in combination with Visual Studio
Hello,
I'm using Visual Studio 2022 with the Intel oneApi 2022 C++ Compiler and CMake 3.23.1. Everything works as expected expect setting some specific Intel C++ compiler flags.
E.g. For optimization Visual Studio supports Od, O1, O2 and Ox. The Intel C++ Compiler supports Od, O1, O2, Ox and O3[Intel C++].
I can set all flags expect the O3 and some optimization diagnostic flags supported by the Intel compiler. I tried it in multiple ways.
Changing the current flag like this:
MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
string(REGEX REPLACE "/O[0-4]" "/O3" CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}")
set(CMAKE_CXX_FLAGS_RELEASE "${CMAKE_CXX_FLAGS_RELEASE}" CACHE STRING "" FORCE)
MESSAGE(STATUS "CMAKE_CXX_FLAGS_RELEASE: ${CMAKE_CXX_FLAGS_RELEASE}")
The CXX_FLAG contains the Value O3, but when I open Visual Studio it is still set zo O2.
I also tried to overwrite the initial CXX flags
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cxx_flags_override.cmake")
with
if (("${CMAKE_CXX_COMPILER_ID}" MATCHES "IntelLLVM"))
set(CMAKE_CXX_FLAGS_RELEASE_INIT "/MD /O3 /Ob2 /DNDEBUG /Qopt-report:3 /Qopt-report-phase:loop /Qopt-report-file:C:/Temp/ICX.dat")
message(STATUS "Overrides default compiler flags!")
else()
message(FATAL_ERROR "Overrides not set for compiler ${CMAKE_CXX_COMPILER_ID}")
endif()
but the behavior is still the same. The flag is still O2.
A minimum CMake file looks like that:
set(CMAKE_USER_MAKE_RULES_OVERRIDE_CXX "${CMAKE_CURRENT_SOURCE_DIR}/cmake/cxx_flags_override.cmake")
project(IntelTest)
set(EXECUTABLE_NAME intelTest)
cmake_minimum_required(VERSION 3.22)
set(CMAKE_CONFIGURATION_TYPES "Debug;Release")
add_executable(${EXECUTABLE_NAME}
main.cpp
)
The main is not doing anything right now:
#include <iostream>
#if defined( __INTEL_LLVM_COMPILER )
#endif
#if defined( _MSC_VER ) && ! defined( __INTEL_LLVM_COMPILER )
#endif
int main(int argc, char** argv)
{
std::cout << "Intel Test!!!" << std::endl;
return 0;
}
If I use Ninja to build the project, the flags for optimization are accepted and the diagnostic is written to file.
REM Set Intel C++ compiler variables
call "C:\Program Files (x86)\Intel\oneAPI\compiler\2022.2.0\env\vars.bat"
REM Create the project
cmake -G Ninja -B build-ninja -DCMAKE_MAKE_PROGRAM="ninja.exe" -DCMAKE_C_COMPILER="C:/Program Files (x86)/Intel/oneAPI/compiler/2022.2.0/windows/bin/icx.exe" -DCMAKE_CXX_COMPILER="C:/Program Files (x86)/Intel/oneAPI/compiler/2022.2.0/windows/bin/icx.exe" -DCMAKE_BUILD_TYPE=Release
cd build-ninja
REM Build the project
cmake --build . --config Release --verbose --clean-first
cd ..
A discussion about the topic in the CMake community can be found here: https://discourse.cmake.org/t/intel-oneapi-c-compiler-with-visual-studio-2022/6634/3
Thanks and best regards, Dirk