From 9576576039216c433d90f72feae6965f983d26d2 Mon Sep 17 00:00:00 2001 From: Anthony Roberts Date: Mon, 12 Aug 2024 16:50:07 +0200 Subject: [PATCH] Windows: Enable clang-cl for ARM64 This gets Windows ARM64 to compile with clang-cl, which gives up to 40% performance improvements in certain scenes rendered with cycles, compared to MSVC. This is all tested using LLVM 18.1.8 and a VS2022 `vcvarsall` window. Subsequent PRs with various lib version updates, etc to go in at a later point. Pull Request: https://projects.blender.org/blender/blender/pulls/124182 --- CMakeLists.txt | 10 ++++++++++ build_files/cmake/platform/platform_win32.cmake | 4 ++-- build_files/windows/configure_ninja.cmd | 11 ++++++++--- extern/lzma/LzFind.c | 2 +- extern/lzma/README.blender | 3 ++- extern/lzma/patches/lzma.diff | 13 +++++++++++++ intern/cycles/CMakeLists.txt | 2 ++ 7 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 extern/lzma/patches/lzma.diff diff --git a/CMakeLists.txt b/CMakeLists.txt index ea5b786a688..ce3c004f15d 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -410,6 +410,10 @@ option(WITH_OPENMP "Enable OpenMP (has to be supported by the compiler)" ON) if(UNIX AND NOT APPLE) option(WITH_OPENMP_STATIC "Link OpenMP statically (only used by the release environment)" OFF) mark_as_advanced(WITH_OPENMP_STATIC) +elseif(WIN32 AND CMAKE_C_COMPILER_ID MATCHES "Clang" AND CMAKE_SYSTEM_PROCESSOR STREQUAL "ARM64") + # At time of testing (LLVM 18.1.6) OMP is not included in public LLVM builds for Windows ARM64 + set(WITH_OPENMP OFF) + set(WITH_OPENMP_STATIC OFF) endif() if(WITH_GHOST_X11) @@ -2167,6 +2171,9 @@ elseif(CMAKE_C_COMPILER_ID MATCHES "Clang") C_WARN_CLANG_CL_BITFIELD_ENUM_CONVERSION -Wno-bitfield-enum-conversion # 1 C_WARN_CLANG_CL_UNUSED_LAMBDA_CAPTURE -Wno-unused-lambda-capture # 1 C_WARN_CLANG_CL_SHADOW_FIELD_IN_CONSTRUCTOR_MODIFIED -Wno-shadow-field-in-constructor-modified # 1 + # And some additional ones that came up when using LLVM 18.1.8 on Windows ARM64 + C_WARN_CLANG_CL_SWITCH_DEFAULT -Wno-switch-default + C_WARN_CLANG_CL_NAN_INFINITY_DISABLED -Wno-nan-infinity-disabled ) add_check_cxx_compiler_flags( @@ -2298,6 +2305,9 @@ elseif(CMAKE_C_COMPILER_ID MATCHES "Clang") CXX_WARN_CLANG_CL_BITFIELD_ENUM_CONVERSION -Wno-bitfield-enum-conversion # 1 CXX_WARN_CLANG_CL_UNUSED_LAMBDA_CAPTURE -Wno-unused-lambda-capture # 1 CXX_WARN_CLANG_CL_SHADOW_FIELD_IN_CONSTRUCTOR_MODIFIED -Wno-shadow-field-in-constructor-modified # 1 + # And some additional ones that came up when using LLVM 18.1.8 on Windows ARM64 + CXX_WARN_CLANG_CL_SWITCH_DEFAULT -Wno-switch-default + CXX_WARN_CLANG_CL_NAN_INFINITY_DISABLED -Wno-nan-infinity-disabled ) endif() diff --git a/build_files/cmake/platform/platform_win32.cmake b/build_files/cmake/platform/platform_win32.cmake index 324507f9c6e..afd3ca588b7 100644 --- a/build_files/cmake/platform/platform_win32.cmake +++ b/build_files/cmake/platform/platform_win32.cmake @@ -180,8 +180,8 @@ remove_cc_flag( ) if(MSVC_CLANG) # Clangs version of cl doesn't support all flags - string(APPEND CMAKE_CXX_FLAGS " ${CXX_WARN_FLAGS} /nologo /J /Gd /EHsc -Wno-unused-command-line-argument -Wno-microsoft-enum-forward-reference ") - string(APPEND CMAKE_C_FLAGS " /nologo /J /Gd -Wno-unused-command-line-argument -Wno-microsoft-enum-forward-reference") + string(APPEND CMAKE_CXX_FLAGS " ${CXX_WARN_FLAGS} /nologo /J /Gd /EHsc -Wno-unused-command-line-argument -Wno-microsoft-enum-forward-reference /clang:-funsigned-char /clang:-fno-strict-aliasing /clang:-ffp-contract=off") + string(APPEND CMAKE_C_FLAGS " /nologo /J /Gd -Wno-unused-command-line-argument -Wno-microsoft-enum-forward-reference /clang:-funsigned-char /clang:-fno-strict-aliasing /clang:-ffp-contract=off") else() string(APPEND CMAKE_CXX_FLAGS " /nologo /J /Gd /MP /EHsc /bigobj") string(APPEND CMAKE_C_FLAGS " /nologo /J /Gd /MP /bigobj") diff --git a/build_files/windows/configure_ninja.cmd b/build_files/windows/configure_ninja.cmd index 684673b7611..897b08aacf2 100644 --- a/build_files/windows/configure_ninja.cmd +++ b/build_files/windows/configure_ninja.cmd @@ -37,9 +37,14 @@ set LLVM_DIR= :DetectionComplete set CC=%LLVM_DIR%\bin\clang-cl set CXX=%LLVM_DIR%\bin\clang-cl - rem build and tested against 2019 16.2 - set CFLAGS=-m64 -fmsc-version=1922 - set CXXFLAGS=-m64 -fmsc-version=1922 + if "%PROCESSOR_ARCHITECTURE%" == "ARM64" ( + set CFLAGS=-m64 + set CXXFLAGS=-m64 + ) else ( + rem build and tested against 2019 16.2 + set CFLAGS=-m64 -fmsc-version=1922 + set CXXFLAGS=-m64 -fmsc-version=1922 + ) ) if "%WITH_ASAN%"=="1" ( diff --git a/extern/lzma/LzFind.c b/extern/lzma/LzFind.c index 0fbd5aae563..94b4879cfdc 100644 --- a/extern/lzma/LzFind.c +++ b/extern/lzma/LzFind.c @@ -625,7 +625,7 @@ void MatchFinder_Init(CMatchFinder *p) #endif #endif - #if defined(_MSC_VER) && defined(MY_CPU_ARM64) + #if defined(_MSC_VER) && defined(MY_CPU_ARM64) && !defined(__clang__) #include #else #include diff --git a/extern/lzma/README.blender b/extern/lzma/README.blender index 48ac4ab1ee4..a26095c2134 100644 --- a/extern/lzma/README.blender +++ b/extern/lzma/README.blender @@ -2,7 +2,8 @@ Project: LZMA SDK URL: https://www.7-zip.org/sdk.html License: Public Domain Upstream version: 23.01 -Local modifications: No code changes +Local modifications: +* Update LzFind.c to find the correct NEON header when using clang-cl + Windows ARM64 - Took only files needed for Blender: C source for raw LZMA1 encoder/decoder. - CMakeLists.txt is made for Blender codebase diff --git a/extern/lzma/patches/lzma.diff b/extern/lzma/patches/lzma.diff new file mode 100644 index 00000000000..1bebb7b11e2 --- /dev/null +++ b/extern/lzma/patches/lzma.diff @@ -0,0 +1,13 @@ +diff --git a/extern/lzma/LzFind.c b/extern/lzma/LzFind.c +index 0fbd5aae563..94b4879cfdc 100644 +--- a/extern/lzma/LzFind.c ++++ b/extern/lzma/LzFind.c +@@ -625,7 +625,7 @@ void MatchFinder_Init(CMatchFinder *p) + #endif + #endif + +- #if defined(_MSC_VER) && defined(MY_CPU_ARM64) ++ #if defined(_MSC_VER) && defined(MY_CPU_ARM64) && !defined(__clang__) + #include + #else + #include diff --git a/intern/cycles/CMakeLists.txt b/intern/cycles/CMakeLists.txt index afeddff83ac..f7bac39e430 100644 --- a/intern/cycles/CMakeLists.txt +++ b/intern/cycles/CMakeLists.txt @@ -446,6 +446,8 @@ endif() if(WITH_CYCLES_BLENDER) # Not needed to make cycles automated tests pass with -march=native. # However Blender itself needs this flag. + # Note: the clang-cl style removal must go first, to avoid a dangling "/clang:" + remove_cc_flag("/clang:-ffp-contract=off") remove_cc_flag("-ffp-contract=off") add_definitions(-DWITH_BLENDER_GUARDEDALLOC) add_subdirectory(blender)