Once again positively surprised how well single-pass compilers synergize with modern branch predictors. The CPU time distribution in my single-pass compiler is roughly 40% lexing + 40% parsing and semantic analysis + 20% code generation (x86-64). The lexer is a conventional switch-based lexer and its hot paths are well optimized but don't use any crazy tricks: it processes input at ~0.75 GB/s on one thread. But 40% is enough that I thought I'd compare with a full AVX-512 SIMD lexer.
The main cause of the lackluster speed-up in the full compiler is a massive increase in branch mispredicts in the parser, despite identical branching structure to the old code. Basically, most of the mispredicted branches we previously paid for in the branch-based lexer were now appearing in the parser. I knew some of that would happen from the last time I did this kind of analysis years ago but seeing the net impact is still kind of shocking.
If you don't quite see what's going on, when you're running a single-pass compiler like this, you're finely interleaving the execution of all the different logical stages like lexing, parsing, semantic analysis and code generation. While that adds pressure to instruction and op caches, BTBs, etc, it also lets you condition on the recent branch history across stages. So the predictor can condition the predictions for parser branches based on recent branches in the lexer.
Anyway, this made me want to do a more precise analysis I didn't do back in the day. How many extra branch mispredicts appear in the lexer (not the parser) if you don't run it interleaved with the parser (the lexer branch predictions get to condition on parser branches, too). The lexer runs significantly faster (if you attribute time by instruction sampling) when interleaved with the parser and there's about 20% fewer mispredicts in the same lexer when run interleaved than when run standalone.
The TL; DR is that if you're writing a very fast compiler like this where lexing is a significant time cost, it's almost certainly not worth it to use even a hyper-optimized SIMD lexer. After all the experimentation back in the day, that was my final conclusion as well. The 5% is not worth giving up the flexibility of a conventional lexer, and that's just about the best case given that this is a micro-optimized full AVX-512 lexer on just about the fastest single core machine you can buy.
@pervognsen did you also experience a different maximum achievable clock when using the AVX-512 path and was it big enough to impact performance? One lesson learnt from vectorizing non-explicitly parallel code is that a good chunk of the speedup gets eaten up by the reduction in clockspeed and this has a knock-on effect on throughtput: hardware threads not using the vector units get bogged down because they share a core with an AVX-using core. In multi-threaded applications this has the added cost of indirectly slowing down your critical sections (which are almost always scalar), further hampering overall throughput.
@gabrielesvelto No, not a major difference that I noticed in the Zen 5 series but I also have a good cooling setup (temp on all-core workload never goes over ~80 C).