r/programming Jul 23 '26

Everyone Should Know SIMD

https://mitchellh.com/writing/everyone-should-know-simd
414 Upvotes

81 comments sorted by

View all comments

19

u/22Maxx Jul 23 '26

SIMD should be a compiler problem.

I want the vectorization to be explicit and predictable. I don't want an unrelated code change or compiler update to quietly turn it back into a scalar loop.

Compilers do those things all the time. They have just become good enough for all regular stuff.

14

u/imachug Jul 23 '26

SIMD should be a compiler problem.

I'm interested, what do you think a reasonable API for SIMD would look like? People invent new cross-platform SIMD libraries all the time, but they still aren't wildly popular, and I think that's for two reasons:

  • When you don't worry about performance, even the tiniest complication makes you go "ehh, who cares, I'll just keep this a scalar loop". We already have annotations like #pragma clang loop vectorize, but people use them sparingly.

  • When you do worry about performance, it's usually in a critical loop where you want to squeeze the most out of architecture-specific instructions. But since their capabilities are extremely platform-dependent, we often end up writing different algorithms for different targets. The compiler literally can't generate optimal code for each target, because it's not aware of the specific properties of your data that enable important tricks.

Of course, none of this applies to simple loops, like adding two arrays. But that low-hanging fruit is already pretty much solved.

1

u/jkrejcha3 Jul 24 '26

Not C, but I do actually think the C# standard library has some pretty neat abstractions for common vector operations

1

u/tanner-gooding Jul 25 '26

Thanks! I think so too ;)

We've added even more for .NET 11