Cycles build fails due to FMA and/or AVX2 test issue on MacOS

Hello.

If you download the latest release of Blender 2.9 and activate debug menu (Preferences > Experimental > Cycles Debug), AVX2 is enabled (at least on my computer) and rendering works.

Now if I try to compile Blender and Cycles from sources using latest master, I get this error message:

[100%] Building CXX object intern/cycles/test/CMakeFiles/cycles_test.dir/util_avxf_avx2_test.cpp.o
[100%] Linking CXX executable ../../bin/Blender.app/Contents/MacOS/Blender
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/util_avxf_avx2_test.cpp:22:
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/./util_avxf_test.h:18:
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_system.h:20:
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_string.h:24:
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_vector.h:24:
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_aligned_malloc.h:20:
In file included from /Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_types.h:160:
/Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_avxf.h:354:10: error: always_inline function '_mm256_fmsub_ps' requires target
      feature 'fma', but would be inlined into function 'msub' that is compiled without support for 'fma'
  return _mm256_fmsub_ps(a, b, c);
         ^
/Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_avxf.h:337:10: error: always_inline function '_mm256_fmadd_ps' requires target
      feature 'fma', but would be inlined into function 'madd' that is compiled without support for 'fma'
  return _mm256_fmadd_ps(a, b, c);
         ^
/Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_avxf.h:346:10: error: always_inline function '_mm256_fnmadd_ps' requires target
      feature 'fma', but would be inlined into function 'nmadd' that is compiled without support for 'fma'
  return _mm256_fnmadd_ps(a, b, c);
         ^
/Users/some_user/blender_repo/blender/intern/cycles/test/../util/util_avxf.h:290:10: error: always_inline function '_mm256_permutevar8x32_ps' requires
      target feature 'avx2', but would be inlined into function 'permute' that is compiled without support for 'avx2'
  return _mm256_permutevar8x32_ps(a, _mm256_set_epi32(i7, i6, i5, i4, i3, i2, i1, i0));
         ^
4 errors generated.
make[2]: *** [intern/cycles/test/CMakeFiles/cycles_test.dir/util_avxf_avx2_test.cpp.o] Error 1
make[1]: *** [intern/cycles/test/CMakeFiles/cycles_test.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

Apparently some people had a similar issue before but I have no idea how to handle this with Blender/Cycles. Somebody wrote this about FMA:
Due to a quirk in Clang, not all of AVX 2 is enabled, so to get FMA support, I had to go into Build Phases->Compile Sources and add the flag -mfma. Then I got fma instructions to compile. Note that it looks like this process has to be done for each source file.

I’m only using cmake, not xcode here. How to set such a flag?

I wanted to see if there’s a way to disable FMA for testing but I only found below about disabling AVX.

I used sysctl -a | grep machdep.cpu.features to see if AVX is supported and it only lists AVX1.0. I can still use the latest release and render with Cycles and AVX2.

According to this thread, one can disable AVX with the command line by setting an environment variable such as CYCLES_CPU_NO_AVX2 but there’s nothing about FMA.

The environment variable is for runtime selection of the kernel, it will still be build, just not used if you have that variable set, it will make no difference for the build process.

OK, thanks for the explanation. If anyone has an understanding of what the potential issue is, I’m all ears.

Possibly by editing this piece of code https://developer.blender.org/diffusion/B/browse/master/intern/cycles/CMakeLists.txt$127-141
, running cmake ./ in the build directory, followed by make -j 8 install.

Did you perhaps use make developer to setup the Blender build?

I’m guessing both WITH_GTESTS and WITH_CYCLES_NATIVE_ONLY are enabled because of that, and that in this case the tests are being compiled without the -mfma flag.

If my guess is correct, it should be fixed by this commit:
https://developer.blender.org/rBa9f2641

Thanks for the fix! I initially tried make developer and then make release but the problem was the same. With the fix however, now everything builds, launches and renders fine.