Heads-up: FloatVectorOperations::multiply might (?) produce denormals on apple silicon

Just a quick community service note.

PluginVal started failing me on denormals on release builds on Apple silicon.

From what I can tell: if you have inputs near the denormal floor, and FloatVectorOperations::multiply with a gain like 0.1f near the end of your processing chain, you can end up with denormals in your output, even with juce::ScopedNoDenormals in scope.

I think the mechanism is that ScopedNoDenormals is just for the current thread/context, and osx may deploy the vectorized multiply to another CPU context?

Not sure - but when I switched to a naive looped multiply-with-const (which presumably clang will optimize pretty hard?), my pluginval denormal failure went away. No problem seen on x86_64.

Anyone have more context on this one? Thanks.

1 Like

Perhaps vDSP uses some speicific acceleration hardware instead of NEON SIMD registers? I have seen some unofficial reports about matrix calculation in vDSP before.

1 Like

That’s super interesting and I’m not sure if it should be considered concerning or not.

If we think this through and assume that the calculation gets dispatched to some acceleator instead being calculated on the CPU and we assumed for a moment, that this accelerator would not work slower when computing denormals, then I’m not sure it it would actually be problematic? I mean if those values would be computed further on a thread with ScopedNoDenormals active, shouldn’t the next following arithmetic operation not dispatched to some library function just ignore the denormals again and all will be as expected basically?

Because as far as I understand, not the general existence of a denormal value is problematic but the slowdown that they cause for some CPU based calculations on them in case they are not deactivated for the current CPU context.

It’s still interesting if emiting denormals from the plugin is problematic to the host, which could mean that in cases like yours where that vDSP computed values are the actual output to the plugin the host would have to deal with avoiding denormal calculations, but I assume that it’s probably common practice for hosts too?

And last but not least it’s of course pure speculation that these values get emitted by some accelerator that doesn’t suffer any slowdown when computing denormals.

But it shouldn’t be too hard to write a few benchmarks and unit tests to verify this behaviour.

On what system did you see this happening, @remaincalm?

Mac mini M2, not one of the fancier ones.

In this case, the very last processing in a synth I’m working on is a master volume adjustment, so it was passing denormal-scale floats back to the host.

After having this topic open in a tab for month now, I finally found time to ask claude to write some test program to verify which vDSP functions might create denormals even with juce::ScopedNoDenormals being active in the scope. And the findings are pretty interesting. I’ll paste the core results here as a slightly reformatted AI response. Note that I use my M3 Pro equipped MacBook for testing.

Your hunch was right in principle: some vDSP calls do ignore FZ. Plain element-wise vDSP calls leak denormals once the vector is at least 512 elements long, even though ScopedNoDenormals is active and FZ is still set after the call.

Results (M3 Pro, arm64; all inputs were normal numbers, so every denormal was created by the call itself)

Call Denormals inside ScopedNoDenormals?
vDSP_vmul, vDSP_vsmul, vDSP_vma, vDSP_vadd Flushed for n ≤ 511. Every output is denormal from n = 512 up.
vDSP_mmul, vDSP_zmmul, cblas_sgemm Leak from 64×64 up; flushed at 16×16
cblas_dgemm Leaks from 16×16 up
cblas_sgemv Leaks from 256×256 up
vImageConvolve_PlanarF, default (multithreaded) 3.8 of 4.2 million outputs are denormal
vImageConvolve_PlanarF with kvImageDoNotTile (single thread) Flushed
vDSP_zvma, vDSP_conv, vvexpf, vvexp2f, vvpowf, vvexp; vDSP_vclip, vthr, vmax, vmin Flushed at every size I tried
Data moves and sign changes: vDSP_mmov, vrvrs, vsort, vabs, vneg, vnabs, vvfabsf Denormals already in the input pass through, as expected for non-arithmetic ops

JUCE code is affected. On macOS, juce::FloatVectorOperations is built on vDSP by default. I checked AudioBuffer::applyGain and FloatVectorOperations::multiply inside a ScopedNoDenormals: no denormals up to 511 samples, then every output sample was denormal from 512 samples up. So a plug-in running with 512-sample or larger blocks can get denormals from ordinary gain calls despite the guard.

What’s likely behind it (inferred, not proven):

  • The sudden switch at 512 elements: this looks like Accelerate sending larger inputs to a different engine. AMX fits, but I can’t observe AMX directly.
  • It isn’t Accelerate’s worker threads: the vDSP and BLAS results were identical with VECLIB_MAXIMUM_THREADS=1, and the FZ flag was still set after every call.

Limits: I only tested an M3 Pro. Other chips may switch engines at different sizes; M4, for example, uses SME instead of AMX. I didn’t test strided calls or double-precision element-wise calls.

I think this is very relevant knowledge and should at least be documented with the FloatVectorOperations class. Of course there is no guarantee here, that the behaviour stays consistent across architectures and I’m also a bit clueless what I should conclude from this knowledge. What do you all think?

Are denormal really annoying on ARM ?

This is indeed a relevant question, I just looked at it from a pure technical point of view because I found it interesting.

Cases that I remember where denormals caused a huge real-world performance issue were all on x86_64 but I never questioned if that wasn’t the case for ARM too. I also remember having attended an ADC talk a few years ago that mentioned it would be still an issue on ARM, but I might have remembered that incorrectly.

I just ran a quick benchmark and it seems that indeed computations on denormal values don’t seem to have a measureable performance impact on my M3, so this supports the claim that it might just not be relevant on ARM anymore. Now if that should be the case, I wonder if it still makes sense to keep a denormal check for ARM builds in pluginval?

Here’s some evidence that denormals are not a problem on Arm:

3 Likes

Given the difference between Intel and AMD, I would guess the denormal speed cost could also be avoided in x86-64. Perhaps it is too expensive in design/hardware or Intel does not care much about that :slight_smile:

So with that knowledge, would it make sense to remove the denormal check from pluginval when running on ARM @dave96? Because originally, the issue came up by a failing pluginval check.

I also think it could be worth adding a hint to the docs of juce::ScopedNoDenormals that this might not affect all calls to Apple Accelerate funtions and therefore some juce::FloatVectorOperations calls on ARM to avoid this kind of confusion.

I guess there’s two options:

  1. See why juce::ScopedNoDenormals doesn’t work on ARM and if it can be implemented
  2. ifdef the check on ARM

I’m fine with either if it’s not a problem.
If someone wants to log it as a GitHub Issue that would avoid me forgetting it.

See

vDSP might produce denormals on Apple ARM64 chips even when `juce::ScopedNoDenormals` is set · Issue #206 · Tracktion/pluginval · GitHub

From my point of view, restricting it to Apple + ARM64 makes more sense, as the problem seems to be specific to vDSP. I haven’t seen the same issue when using Google Highway (NEON) extensively on M chips (i.e., the plugin always passes pluginval tests).

1 Like

This topic is insane and relevant to me!

My entire additive synth engine (which relies on vdsp) is built around 511 harmonics, precisely because moving to 512 produces degraded performance on Apple Silicon. Something I found out via good old trial and error and documented on the forum in some old back and forth:

1 Like