So this one's a bit different.

In #12 and #13, I tested my loop-hint pass on a 1000x1000 matrix addition and got a beautiful 5.26x speedup, backed by 30 runs and tiny standard deviations. I was absolutely thrilled, assuming the only difference between the two binaries was one piece of LLVM metadata.

That wasn't true. And this post is about how I found out, what I fixed, and what my pass really does.

What I built: Control experiment, innermost-loop fix, and a strided benchmark

What I understood:

1) Two things changed, not one:

  • Here's what my two binaries were in #12 and #13:
#without pass clang++ -O0 matxAdd2D_canonical.ll -o runWithoutPass2D #with pass opt -load-pass-plugin=./build/libMyPass.so -passes="my-pass,loop-vectorize" -S matxAdd2D_canonical.ll -o matxAdd2D_vectorized.ll clang++ -O0 matxAdd2D_vectorized.ll -o runWithPass2D 
  • If you look closely, you'll see that the "without pass" binary never even went through loop-vectorize.
  • This means the two binaries differed in not one, but two ways. One had my hint and the vectoriser run on it, while the other had neither.
  • I credited the speedup to the hint, but never checked what the vectoriser does on its own.
  • The missing piece was a control: the vectoriser, with no hint.
# vectoriser on, no hint opt -passes="loop-vectorize" -S matxAdd2D_canonical.ll -o matxAdd2D_nohint_vec.ll clang++ -O0 matxAdd2D_nohint_vec.ll -o runVecNoHint 

2) The control (30 runs):

  • I added this third binary to my 30-run script, and ran all three again:
Variant Median Min Max Stdev
Vectoriser off (my old "without pass") 1138 ms 1132 ms 1157 ms 5.21 ms
Vectoriser on, no hint 216 ms 209 ms 222 ms 2.91 ms
Vectoriser on, with my hint 216.5 ms 213 ms 398 ms 33.95 ms
  • Vectoriser off → on: 5.27x. That's my old number, almost exactly.
  • No hint → with hint: 1.00x. Half a millisecond of difference. Nothing.
  • (The 398 ms is a single outlier run, which is why the stdev looks big. The median doesn't care about it, which is exactly why we compare medians.)
  • So the 5.26x was real. It just wasn't mine. It was LLVM's loop vectoriser, which happily vectorises a simple matrix addition all by itself.

3) How the IR agrees:

  • Timings can be, well... deceptive. So I compared the actual code.
  • I pulled matrix_add out of both files with llvm-extract and diffed them.
  • The only difference was one metadata node: !{!"llvm.loop.vectorize.enable", i1 true}.
  • The vector instructions themselves were identical.
  • In #13, I said 30 runs and small standard deviations proved the speedup wasn't a fluke. Which was technically true. But statistics only tell you a number is stable, and not what really caused it.

4) And then I found a bug:

  • While digging, I noticed something in my pass output. matrix_add has two loops (i outside, j inside), but the pass only ever printed one.
  • My own code said it too:
//iterating to find top-level loops in the function for (Loop *L : LI) { 
  • for (Loop *L : LI) only gives you the outermost loops.
  • LLVM's loop vectoriser works on innermost loops.
  • So my hint was sitting on the outer i loop, where the vectoriser never even looks. The inner j loop, the one that actually got vectorised, never had my hint at all.

  • The fix:

//Visiting every loop (nested ones too), but tagging only innermost ones (where LoopVectorize works) for (Loop *L : LI.getLoopsInPreorder()) { if (!L->isInnermost()) continue; 
  • getLoopsInPreorder() gives every loop, nested ones included.
  • isInnermost() keeps only the loops with nothing inside them.
  • I also made the pass skip loops that already have a vectorise directive, so it never overrides a programmer's own #pragma clang loop, and never adds the hint twice. Running the pass twice in a row confirms it:
Found the innermost loop in _Z10matrix_addPA1000_fS0_S0_ with header: for.body3 (depth 2) Tagged. ... Found the innermost loop in _Z10matrix_addPA1000_fS0_S0_ with header: for.body3 (depth 2) Already has a vectorise directive; skipping 
  • Finally. Depth 2 🥹
  • With the fix, I reran the matrix addition, and it was still 1.00x. Which makes sense, because LLVM was already vectorising that inner loop on its own.

5) So when does a hint really matter?

  • A hint like llvm.loop.vectorize.enable tells the vectoriser to skip its cost model (the bit that asks whether the vectorisation is worth it).
  • It doesn't skip legality (the part that asks whether the vectorisation is safe).
  • So a hint can only change anything on a loop that is legal to vectorise, but which LLVM thinks isn't worth it.
  • Matrix addition isn't one of those. LLVM loves matrix addition, actually.
  • So I needed a loop LLVM doesn't love, and hence picked a strided one, which reads every 8th element:
void strided_add(float *A, float *B, float *C) { for (int i = 0; i < N; i++) { C[i * STRIDE] = A[i * STRIDE] + B[i * STRIDE]; } } 
  • And the remarks confirmed it:
No hint: remark: the cost-model indicates that vectorization is not beneficial With hint: remark: vectorized loop (vectorization width: 2, interleaved count: 1) 
  • For the first time, my hint actually changed what LLVM did!

6) It looked like a win...

  • This was the same pipeline as before (30 runs, clang++ -O0 to build each binary):
Variant Median Min Max Stdev
Vectoriser off 375 ms 363 ms 381 ms 4.90 ms
Vectoriser on, no hint 375.5 ms 364 ms 497 ms 25.04 ms
Vectoriser on, with my hint 318 ms 303 ms 342 ms 8.67 ms
  • Off and no-hint are the same, since LLVM declined the loop, so both run scalar code.
  • With the hint: 1.18x faster (When LLVM's cost model said the vectorisation wasn't worth it)
    • Or so I assumed

7) ...but it wasn't:

  • All these binaries were built from IR using clang++ -O0, which produces pretty poor machine code. What if the scalar loop was just extra slow at -O0, making the vector one look good?
  • So I built the exact same IR files with -O3 instead:
clang++ -O3 -fno-vectorize -fno-slp-vectorize strided_canonical.ll -o runStridedOffO3 clang++ -O3 strided_nohint.ll -o runStridedNoHintO3 clang++ -O3 strided_hint.ll -o runStridedHintO3 
Variant Median Min Max Stdev
Vectoriser off 313.5 ms 283 ms 388 ms 22.84 ms
Vectoriser on, no hint 313.5 ms 289 ms 494 ms 36.74 ms
Vectoriser on, with my hint 317.5 ms 287 ms 383 ms 21.33 ms
  • Hint effect: 0.99x. A 4 ms difference, with runs varying by 20+ ms. Which is practically nothing 😬
  • With properly optimised code, the scalar loop is just as fast as the forced vector loop.
  • So the 1.18x really came from poor -O0 code, not from vectorisation. And LLVM's cost model was right all along.
  • Another small thing I noticed: with the hint, clang now prints a warning for the timing loop in main ("loop not vectorized: the optimizer was unable to perform the requested transformation").
    • That loop calls a function, so it can never be vectorised, but my pass tags it anyway because it's an innermost loop. This is something to fix.

8) So where does that leave things?

Test Hint effect What it means
Matrix add 1.00x LLVM already vectorises it. The 5.27x is the vectoriser's
Strided, -O0 back end 1.18x Looked like a win
Strided, -O3 0.99x It wasn't. The cost model was right
  • So my pass works, mechanically at least. It now tags the right loops, merges metadata properly, adn respects existing directives.
  • But on the loops I've tested, the hint either does nothing, or overrides a decision LLVM got right.
  • A hint gives the compiler permission, but not necessarily judgement. And knowing when vectorising actually pays off is the genuinely hard part.

What I learnt:

  • To change one thing at a time. If two things change between the binaries, we can't tell which one did the work.
  • Always run a control.
  • Check what the compiler actually did (remarks, IR, assembly), and not just the timings.
  • Statistics show stability, not causes.
  • Before believing a speedup, test it with optimised code.

What's next: Testing a few more loops LLVM might turn down (short trip counts, conditional stores), and one that's a little dangerous (like floating-point sums), where a hint might change the answer and not just the speed.

Musings:
This was quite a silly lapse on my part. Maybe I was so eager to see the speedup that I hadn't tested the pass as rigorously as I otherwise might have. But in a way, it's good this happened. It taught me to be extra cautious going ahead.

I once read somewhere that research is all about finding/discovering something we'd been earnestly looking for, for quite some time, and then spending the next couple of months trying to disprove it. Which is ideally how it should be, because as humans, we tend to be biased.

There's a part of us that always seeks familiarity, when exposure to diversity can be very helpful as well. I, too, believe in looking for evidence that challenges my view of the world. In philosophy, this is called dialectics, which speaks of how contradictions drive change. Basically, an existing idea (the thesis) clashes with an opposing force (its antithesis), blending the best of both to create a new reality (the synthesis), that eventually becomes the next starting point in an endless cycle of change.

Someone once very rightly said, "The only way to make sense out of change is to plunge into it, move with it, and join the dance."