Get the latest tech news

A Clang regression related to switch statements and inlining


fter my previous post, Eliminating redundant bound checks (read it for context if you haven’t already), I wanted to do a benchmark using the ‘optimized’ version of the increment() function, which didn’t contain any bound checks when compiled with Clang, even though we used .at() for indexing into the array. Here’s that last version of the function from the previous post (I made it static because I’m using a single compilation unit): My benchmark goes like this: I generate 300 million instances for first_idx, and then I call increment() with them.

Since the indexes are randomly generated, the branch predictor won’t be able to accurately predict to which of those switch case labels to jump on each iteration. After trying to add some flags (that the zig c++ toolchain sets) to the previous clang++ command without managing to improve the performance, I was thinking that this may just be a regression in Clang 19. Someone already responded saying that it’s a trade-off between two optimization passes called SROA (scalar replacement of aggregates) and SimplifyCFG (simplify control flow graph).

Get the Android app

Or read this on Hacker News

Read more on:

Photo of Clang

Clang

Photo of statements

statements

Photo of Clang regression

Clang regression

Related news:

News photo

Canonical Experimented With Rebuilding Its Ubuntu 25.04 Packages Under LLVM/Clang

News photo

Clang 20 Compiler Adds Support For Xtensa CPU Target

News photo

Triaging Clang C++ front end bugs