AVX2 with JUCE


#1

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?


#2

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”, …


#3

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.


#4

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


#5

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.


#6

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.


#7

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


#8

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.


#9

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!)


#10

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)


#11

CLANG_X86_VECTOR_INSTRUCTIONS = sse4.1