AVX2 with JUCE

Hi,

I’ve been developing a plugin and I’ve been trying to use AVX2 for optimisation. When I try to build it in xcode it gives me the following error message:

Always_inline function ‘_mm256_load_ps’ requires target feature ‘sse4.2’, but would be inlined into function ‘processBlock’ that is compiled without support for ‘sse4.2’

I am currently including <immintrin.h> and have configured “Enable Additional Vector Extensions” to AVX2, but neither of these resolve the issue.

Could someone tell whether it is possible to use AVX instructions within JUCE and if so how?
Is it something that has to be changed or added in the Projucer configuration-specific build settings?

Yes, it’s certainly possible. From the error message I suspect that you’ve not enabled AVX2 extensions for all the targets in your solution. Try setting the “Enable Additional Vector Extensions” option for the “Shared Code” target as well as “VST”, “VST3”, …

Bear in mind this will compile the whole target for AVX, so only newer computers can use the plugin.

Apple clang made a breaking change by turning this into an error. Previously, you could mix any instruction set with any global setting, such that you could have a optimized code paths for specific instructions sets. Unfortunately, this can no longer be done, unless you compile separate binaries for each code path. Probably also why JUCE won’t have AVX nor AVX2 nor AVX512 support for FloatVectorOperations, etc…

You can make the error go away using the target() __attribute__, but your code will be dead slow, as clang creates sse/avx transitions each instruction.

1 Like

Thank you for the advices, I managed to solve the issue!

Is there a way to set the “Enable Additional Vector Extensions” option from inside Projucer? Right now, everytime I overwrite the project with Projucer I have to set it in XCode and I was wondering if there is a way to skip this step by Projucer setting it for me.

I don’t have a Mac at hand right now to test but this is what I would do:

  • save the Projucer project to overwrite the Xcode project
  • “save”/copy the Xcode project somewhere (for instance using git add )
  • open the Xcode project
  • set the “Enable Additional Vector Extensions” option inside Xcode
  • save the Xcode project again
  • compare the new Xcode project and the previous state, the only difference should be the Xcode setting for “Enable Additional Vector Extensions”
  • add that setting to the “Custom Xcode Flags” field in Projucer

I hope that helps.

2 Likes

Great idea, thank you, I will check it out!

On this topic. I have 2 projucers that I have compiled as they always overwrite my CUDA settings. What would be great is if we could define custom project XML and be able to select along with a IDE. That way we could do all kinds of custom configs and would fix these niggly problems when using more vendor specific settings.

It seems it worked and the compilation is not complaining now after fresh resave and compile. (Although it’s interesting, as if I open the project settings in XCode, the vector extensions are not set explicitely… but anyway it compiles now, so thanks for the tip!)

Could you share what you added to the “Custom Xcode Flags” field in Projucer? (as I said, I don’t have a Mac at hand, but I’m still curious)

CLANG_X86_VECTOR_INSTRUCTIONS = sse4.1

1 Like

I would like to resurrect this thread. I’m at the same exact spot trying to compile Intel AVX2 vector instructions in XCode for the Mac and have no clue how to get started.

I rewrote much of my audio processor using the raw Intel AVX2 instructions in Windows compiled with Visual Studio and it is working perfectly with huge speed gains, basically the plugin processes 8 detune voices for the cpu price of 2.

In Windows Visual Studio this is easy. You include the following:
#include <intrin.h>
#include <immintrin.h>
#include <xmmintrin.h>
#include <math.h>

And set configuration properties, C/C++ Code Generation, Enable Parallel Code Generation to Yes, and Enable Enhanced Instruction Set to Advanced Vector Extensions 2.

I think I also had to install the Intel Compiler for use by Visual Studio.

However in XCode I can’t find a single article online how to do it.

XCode does not know what a “__m256” eight float wide register variable is which is basically what AVX2 depends on.

Do you need to install an XCode compatible Intel compiler first?

Where is the XCode setting “Enable Additional Vector Extensions”. I can’t seem to find that in the build settings anywhere.

If anyone got this working and could post a quick from the ground up tutorial here it would be fantastic. Many Thanks!

Fathom Synth

-mavx2 -mfma in compile flags

Also, for almost all cases you only need to include immintrin.h, in all platforms.