<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Pipelining on Tom's Lab Notes</title><link>https://vtbl.dev/tags/pipelining/</link><description>Recent content in Pipelining on Tom's Lab Notes</description><generator>Hugo -- gohugo.io</generator><language>en</language><lastBuildDate>Mon, 26 Oct 2026 00:00:00 +0000</lastBuildDate><atom:link href="https://vtbl.dev/tags/pipelining/index.xml" rel="self" type="application/rss+xml"/><item><title>A practical illustration of the impact of instruction pipelining</title><link>https://vtbl.dev/posts/pipelining/</link><pubDate>Mon, 26 Oct 2026 00:00:00 +0000</pubDate><guid>https://vtbl.dev/posts/pipelining/</guid><description>&lt;p>During a university course in fall 2025 we were tasked to optimise a 3x3 convolution under certain conditions (1300x1300 32 bit integers for the input, optimisations off i.e. &lt;code>-O0&lt;/code>, stride 1, padding 0, overflow had to be handled somehow but was up to us to decide how) on a platform of our choice.
I picked a Raspberry Pi 3B+ which has an Arm Cortex-A53 processor clocked at 1.4 GHz with 1 GB LPDDR2 SDRAM.
In this post I will use the single threaded solution that I came up with for that project and will use it as a starting off point to see how much further that solution can be improved.
After a short introduction of the solution that I came up with and one way in which it is lacking, I will explain how the performance of that solution can be improved through pipelining.&lt;/p>
&lt;h1 id="introduction">Introduction
&lt;/h1>&lt;p>The optimised algorithm I came up with back then combined all the standard improvements that a compiler would ordinarily make such as loop unrolling, vector instructions and caching loads with a more efficient memory access strategy.
A standard SIMD 3x3 convolution loads each input row up to 3 times.
This causes a lot of wasted cycles loading data into registers, performing cache lookups, or worse re-loading from DRAM.
This can be avoided by reusing values from a previous step and forming overlapping windows from the values that are in-register.
This requires one memory load for each iteration as opposed to three.
This is visualised in the figure below.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/pipelining/sliding_window.svg"
alt="Figure created with the help of generative AI">&lt;figcaption>
&lt;p>Figure created with the help of generative AI&lt;/p>
&lt;/figcaption>
&lt;/figure>
&lt;p>This allowed a significant increase in operational intensity as displayed by the roofline diagram below.
Memory bandwidth was benchmarked using STREAM&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> and the L1 cache bandwidth was determined using a small self made microbenchmark.
The &amp;rsquo;naive scalar&amp;rsquo; version here refers to the standard algorithm with 4 nested for loops written in C with &lt;code>-O0&lt;/code> as discussed.
So the performance difference between it and the hand-written SIMD implementation is partially due to several optimisations a compiler would ordinarily do aside from vectorisation.&lt;/p>
&lt;figure class="centered-fig">&lt;img src="https://vtbl.dev/images/pipelining/roofline.svg">
&lt;/figure>
&lt;p>The best performing solution had been implemented with inline assembly for the computation and C for the loop as the code generated by gcc with &lt;code>-O0&lt;/code> had a number of unnecessary loads and stores, but it was still written to be as readable as possible by separating different computational steps from each other and not interleaving loads and multiply-accumulate (MAC)s.
This however came at the cost of performance, as the Arm Cortex-A53 is an in-order CPU, so the order of the instructions is essential.
This post explores how much.&lt;/p>
&lt;h2 id="tools-and-disclaimer">Tools and Disclaimer
&lt;/h2>&lt;p>The &lt;code>llvm-mca&lt;/code> tool (version 23.1.2) has been used to improve upon the original implementation with the options: &lt;code>-mcpu=cortex-a53 -iterations=100 -timeline&lt;/code>.
It can simulate the timings of a set of instructions for a particular processor, allowing me to look at the timings of the instructions while also not requiring access to the real hardware (the Raspberry Pi originally used for this project is dead unfortunately and I don&amp;rsquo;t have a replacement at this time).
I want to stress here that this is a simulation that makes a number of assumptions.&lt;/p>
&lt;p>These assumptions can cause significant inaccuracies compared to the real hardware.
For the original implementation, which this post uses as its starting point, &lt;code>llvm-mca&lt;/code> predicts an execution time of 39.1 ms at 1.4 GHz; 26,204 cycles per 100 iterations, i.e. 262 cycles per 8 outputs with 161 passes per row (remainder is handled separately), times 1298 output rows results in 54.8 million cycles.&lt;/p>
&lt;p>However the actually measured time was 23.3 ms.&lt;/p>
&lt;p>So this method is not suited to derive real world performance from the simulation. It should be suited however to compare different implementations to each other.&lt;/p>
&lt;p>For this post I will focus on the inner loop only and not on the whole routine.
The computation in the inner loop is where the majority of the time is spent and to keep the post short, the rest (outer loop, branching) is declared out of scope.
Perhaps I will look at the function as a whole in a future post.&lt;/p>
&lt;p>Reading the timing diagrams of &lt;code>llvm-mca&lt;/code> is relatively straightforward. Each row is one instruction and each column is one cycle (a &lt;code>.&lt;/code> marks every 5th).
&lt;code>D&lt;/code> marks dispatch, &lt;code>e&lt;/code> execution and &lt;code>E&lt;/code> is the cycle in which the instruction has finished executing.
So a column with no &lt;code>D&lt;/code> marks a cycle in which no instruction has been dispatched.&lt;/p>
&lt;h1 id="pipelining">Pipelining
&lt;/h1>&lt;p>Every instruction has a latency $\mathcal{L}$, where $\mathcal{L}$ is the number of cycles between the instruction being issued and being completed.
It is common for instructions to have a latency greater than one, so with an average $\mathcal{L} = 4$, one instruction would be completed every 4 cycles if one were to wait for an instruction to complete before starting the next one, resulting in 0.25 instructions per cycle (IPC).
That is not necessary however, as instructions are executed in different stages&lt;sup id="fnref:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>, so it is possible to start working on one instruction before the previous one has been completed. This is called instruction-level parallelism (ILP).&lt;/p>
&lt;p>The table below shows an ideal 5-stage pipeline, adapted from Bakhvalov&lt;sup id="fnref1:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>, with the stages fetch (IF), decode (ID), execute (EXE), memory access (MEM) and write back (WB).&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Instr. \ Cycle&lt;/th>
&lt;th>1&lt;/th>
&lt;th>2&lt;/th>
&lt;th>3&lt;/th>
&lt;th>4&lt;/th>
&lt;th>5&lt;/th>
&lt;th>6&lt;/th>
&lt;th>7&lt;/th>
&lt;th>8&lt;/th>
&lt;th>9&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>x&lt;/td>
&lt;td>IF&lt;/td>
&lt;td>ID&lt;/td>
&lt;td>EXE&lt;/td>
&lt;td>MEM&lt;/td>
&lt;td>WB&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>x+1&lt;/td>
&lt;td>&lt;/td>
&lt;td>IF&lt;/td>
&lt;td>ID&lt;/td>
&lt;td>EXE&lt;/td>
&lt;td>MEM&lt;/td>
&lt;td>WB&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>x+2&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>IF&lt;/td>
&lt;td>ID&lt;/td>
&lt;td>EXE&lt;/td>
&lt;td>MEM&lt;/td>
&lt;td>WB&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>x+3&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>IF&lt;/td>
&lt;td>ID&lt;/td>
&lt;td>EXE&lt;/td>
&lt;td>MEM&lt;/td>
&lt;td>WB&lt;/td>
&lt;td>&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>x+4&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>&lt;/td>
&lt;td>IF&lt;/td>
&lt;td>ID&lt;/td>
&lt;td>EXE&lt;/td>
&lt;td>MEM&lt;/td>
&lt;td>WB&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>(Instruction) pipelining is a technique employed by hardware designers to achieve some ILP, increasing the IPC to 1 in optimal cases for a single issue CPU and even above one if more than one instruction can be issued at the same time, despite higher instruction latencies.&lt;/p>
&lt;p>Data, control and structural hazards can prevent pipelining, so for optimal performance, it is important to structure code in such a way that avoids these.
As the code we will be optimising has no branches, control hazards can be put aside.
So data and structural hazards will be the number one reason for poor performance.&lt;/p>
&lt;p>A structural hazard occurs when multiple instructions need the same execution unit at the same time.
The Arm Cortex-A53 for example is a dual issue CPU (meaning it can issue 2 instructions at the same time) but with only one NEON arithmetic unit. Thus two instructions that require that unit (such as a MAC, which will be the majority of instructions) cannot be issued at the same time.
This results in stalls of one cycle and effectively only one instruction being issued/cycle for most of the algorithm, except for the case where a load/store can be interleaved. But more on that later.&lt;/p>
&lt;p>A data hazard appears when an instruction needs the result of an earlier one.
So it has to wait for the result of the earlier instruction to be available before it can start executing, which can cause stalls that take multiple cycles depending on the latency of the earlier instruction and the number of independent instructions in between.
Reusing a register for unrelated computations can cause similar stalls. This is called a false-dependency.&lt;/p>
&lt;h2 id="optimisation">Optimisation
&lt;/h2>&lt;p>The original implementation has an IPC of 0.23. That means on average one instruction finishes every ~4.3 cycles.
For the aforementioned reason, only one SIMD arithmetic instruction can be dispatched at a time.&lt;/p>
&lt;p>The inner loop consists of 60 instructions. 8 of those are (SIMD) loads and stores and the remaining 52 are SIMD arithmetic instructions.
The loads and stores can be dispatched simultaneously to the arithmetic instructions, which results in an IPC ceiling of $\frac{60}{52} = 1.15$ for the implementation.
So the examined section is about 5 times slower than it theoretically could be.&lt;/p>
&lt;p>The reason is clear when looking at the timing diagram below of one arithmetic block with 1 multiply and 8 MAC operations.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-asm" data-lang="asm">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>[1,9] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . . . . . . . . . . &lt;span style="color:#d3869b">smull&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v0.2s&lt;/span>, &lt;span style="color:#d3869b">v20.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span>[1,10] . . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . . . . . . . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v1.2s&lt;/span>, &lt;span style="color:#d3869b">v10.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span>[1,11] . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . . . . . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v2.2s&lt;/span>, &lt;span style="color:#d3869b">v11.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>[1,12] . . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . . . . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v3.2s&lt;/span>, &lt;span style="color:#d3869b">v21.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span>[1,13] . . . . . &lt;span style="color:#b8bb26;font-weight:bold">.DeeeeeE&lt;/span> . . . . . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v4.2s&lt;/span>, &lt;span style="color:#d3869b">v13.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span>[1,14] . . . . . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v5.2s&lt;/span>, &lt;span style="color:#d3869b">v14.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span>[1,15] . . . . . . . . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v6.2s&lt;/span>, &lt;span style="color:#d3869b">v22.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">8&lt;/span>&lt;span>[1,16] . . . . . . . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v7.2s&lt;/span>, &lt;span style="color:#d3869b">v16.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">9&lt;/span>&lt;span>[1,17] . . . . . . . . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v8.2s&lt;/span>, &lt;span style="color:#d3869b">v17.2s&lt;/span>
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Each of these has a latency of 6 in the model, and they all depend on the result of the previous operation, since they all add to the same output register. This dependency chain forms a chain of data hazards resulting in each instruction having to wait until the previous one has been completed and thus no pipelining is possible.
That causes the CPU to stall and 5 cycles being wasted for each operation.&lt;/p>
&lt;p>To optimally take advantage of pipelining, the minimum number of independent instructions required is equal to the latency of the instructions.
And since $\mathcal{L}=6$, 6 independent instructions are required to keep the CPU busy.
This can be achieved by interleaving various other instructions in between the M(AC) operations, such as the (M)AC operations of the second result or the &lt;code>ext&lt;/code> instructions that build some of the inputs. Loads can essentially be interleaved for free, as they can be issued at the same time as an arithmetic instruction.
An example of this is shown below.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-asm" data-lang="asm">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>[1,9] . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v1.2s&lt;/span>, &lt;span style="color:#d3869b">v10.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span>[1,10] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . &lt;span style="color:#d3869b">smlal2&lt;/span> &lt;span style="color:#d3869b">v19.2d&lt;/span>, &lt;span style="color:#d3869b">v1.4s&lt;/span>, &lt;span style="color:#d3869b">v10.4s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span>[1,11] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . &lt;span style="color:#d3869b">ext&lt;/span> &lt;span style="color:#d3869b">v13.16b&lt;/span>, &lt;span style="color:#d3869b">v21.16b&lt;/span>, &lt;span style="color:#d3869b">v12.16b&lt;/span>, &lt;span style="color:#928374;font-style:italic">#4
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,12] . &lt;span style="color:#b8bb26;font-weight:bold">.DeeeeeE&lt;/span> . . . &lt;span style="color:#d3869b">ext&lt;/span> &lt;span style="color:#d3869b">v14.16b&lt;/span>, &lt;span style="color:#d3869b">v21.16b&lt;/span>, &lt;span style="color:#d3869b">v12.16b&lt;/span>, &lt;span style="color:#928374;font-style:italic">#8
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,13] . . &lt;span style="color:#fabd2f">DeeeE&lt;/span> . . . &lt;span style="color:#d3869b">ld1&lt;/span> { &lt;span style="color:#d3869b">v20.4s&lt;/span> }, [&lt;span style="color:#d3869b">x3&lt;/span>]
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span>[1,14] . . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v29.2d&lt;/span>, &lt;span style="color:#d3869b">v3.2s&lt;/span>, &lt;span style="color:#d3869b">v12.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span>[1,15] . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">smlal2&lt;/span> &lt;span style="color:#d3869b">v30.2d&lt;/span>, &lt;span style="color:#d3869b">v3.4s&lt;/span>, &lt;span style="color:#d3869b">v12.4s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">8&lt;/span>&lt;span>[1,16] . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">smlal&lt;/span> &lt;span style="color:#d3869b">v18.2d&lt;/span>, &lt;span style="color:#d3869b">v2.2s&lt;/span>, &lt;span style="color:#d3869b">v11.2s&lt;/span>
&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">9&lt;/span>&lt;span>[1,17] . . &lt;span style="color:#b8bb26;font-weight:bold">.DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">ext&lt;/span> &lt;span style="color:#d3869b">v16.16b&lt;/span>, &lt;span style="color:#d3869b">v22.16b&lt;/span>, &lt;span style="color:#d3869b">v15.16b&lt;/span>, &lt;span style="color:#928374;font-style:italic">#4
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This is the same instruction window as before but as is clearly visible: drastically different instructions are executed in that segment.
This enables an (almost) optimal usage of the CPU for this segment and increases the IPC of the analysed section from 0.23 to 0.66.
Note that the structural hazards could not be avoided for the most part, as previously explained.&lt;/p>
&lt;p>However, 0.66 is far from the ceiling of 1.15 calculated earlier.
There are two reasons for this.&lt;/p>
&lt;p>First; there is a stall between instruction 12 and 13. This is because the in-order model in the simulation tool requires instructions to finish in-order as well&lt;sup id="fnref:3">&lt;a href="#fn:3" class="footnote-ref" role="doc-noteref">3&lt;/a>&lt;/sup>.
A load instruction such as &lt;code>ld1 { v20.4s }, [x3]&lt;/code> only has a latency of 4, so it would finish before the previous &lt;code>ext&lt;/code> instruction, which, like the other arithmetic instructions, has a latency of 6.
So in the model it was stalled by one cycle so that it does not finish before the previous instruction.
It is not clear however if a CPU would behave like that as well as there are no hazards or dependencies here.
The instructions don&amp;rsquo;t access the same registers or memory regions, so it would be safe to execute them without the stall.
So this might just be a simplification of the simulation tool and explain some of the more pessimistic estimations.&lt;/p>
&lt;p>Second; at the end of the examined section, the number of independent instructions reduces to 4 as all the inputs have been set up (&lt;code>ld1&lt;/code> and &lt;code>ext&lt;/code>), leaving only the M(AC) instructions. Calculating the final results also adds dependencies.&lt;/p>
&lt;h2 id="optimising-the-tail-end">Optimising the tail-end
&lt;/h2>&lt;p>At the end of each iteration of the inner loop, the intermediate 64 bit values are narrowed to 32 bit and combined into one register, which is then stored.
The &lt;code>sqxtn&lt;/code> and &lt;code>sqxtn2&lt;/code> instructions used to do the &amp;rsquo;narrow and combine&amp;rsquo; operation write to different parts of the same register, so there is a direct dependency there which once again stalls the pipeline.
In the original implementation the same result register is also used for the two results, which adds a false-dependency.&lt;/p>
&lt;p>The timeline and instructions of this procedure are displayed below.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-asm" data-lang="asm">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>[1,53] &lt;span style="color:#b8bb26;font-weight:bold">.DeeeeeE&lt;/span> . . . . . &lt;span style="color:#d3869b">smlal2&lt;/span> &lt;span style="color:#d3869b">v30.2d&lt;/span>, &lt;span style="color:#d3869b">v8.4s&lt;/span>, &lt;span style="color:#d3869b">v28.4s&lt;/span> &lt;span style="color:#928374;font-style:italic">; last MAC
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,54] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . . . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v31.2s&lt;/span>, &lt;span style="color:#d3869b">v18.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow lower half part A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,55] . . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . . &lt;span style="color:#d3869b">sqxtn2&lt;/span> &lt;span style="color:#d3869b">v31.4s&lt;/span>, &lt;span style="color:#d3869b">v19.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow upper half part A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,56] . . . &lt;span style="color:#fabd2f">DeeeE&lt;/span> . . . &lt;span style="color:#d3869b">st1&lt;/span> { &lt;span style="color:#d3869b">v31.4s&lt;/span> }, [&lt;span style="color:#d3869b">x6&lt;/span>] &lt;span style="color:#928374;font-style:italic">; store result A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,57] . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v31.2s&lt;/span>, &lt;span style="color:#d3869b">v29.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow lower half part B
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,58] . . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . &lt;span style="color:#d3869b">sqxtn2&lt;/span> &lt;span style="color:#d3869b">v31.4s&lt;/span>, &lt;span style="color:#d3869b">v30.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow upper half part B
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,59] . . . . . &lt;span style="color:#b8bb26;font-weight:bold">.DeeeE&lt;/span> &lt;span style="color:#d3869b">st1&lt;/span> { &lt;span style="color:#d3869b">v31.4s&lt;/span> }, [&lt;span style="color:#d3869b">x7&lt;/span>] &lt;span style="color:#928374;font-style:italic">; store result B
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>Using different result registers for the two results removes the false-dependency and increases the IPC to 0.72 with the following timeline.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-asm" data-lang="asm">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>[1,53] . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . . &lt;span style="color:#d3869b">smlal2&lt;/span> &lt;span style="color:#d3869b">v30.2d&lt;/span>, &lt;span style="color:#d3869b">v8.4s&lt;/span>, &lt;span style="color:#d3869b">v28.4s&lt;/span> &lt;span style="color:#928374;font-style:italic">; last MAC
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,54] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v10.2s&lt;/span>, &lt;span style="color:#d3869b">v18.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow lower half part A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,55] . . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v31.2s&lt;/span>, &lt;span style="color:#d3869b">v29.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow lower half part B
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,56] . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">sqxtn2&lt;/span> &lt;span style="color:#d3869b">v10.4s&lt;/span>, &lt;span style="color:#d3869b">v19.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow upper half part A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,57] . . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . &lt;span style="color:#d3869b">sqxtn2&lt;/span> &lt;span style="color:#d3869b">v31.4s&lt;/span>, &lt;span style="color:#d3869b">v30.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate and narrow upper half part B
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,58] . . . &lt;span style="color:#b8bb26;font-weight:bold">.DeeeE&lt;/span> . &lt;span style="color:#d3869b">st1&lt;/span> { &lt;span style="color:#d3869b">v10.4s&lt;/span> }, [&lt;span style="color:#d3869b">x6&lt;/span>] &lt;span style="color:#928374;font-style:italic">; store result A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,59] . . . . &lt;span style="color:#fabd2f">DeeeE&lt;/span> &lt;span style="color:#d3869b">st1&lt;/span> { &lt;span style="color:#d3869b">v31.4s&lt;/span> }, [&lt;span style="color:#d3869b">x7&lt;/span>] &lt;span style="color:#928374;font-style:italic">; store result B
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>The remaining dependency is caused by instruction choice and can be avoided by slightly changing the instructions used without changing the actual computation.
Instead of a &amp;rsquo;narrow and combine&amp;rsquo;, each intermediate value is narrowed down in its own register.
Then instead of a simple &lt;code>st1&lt;/code>, &lt;code>stp&lt;/code> is used to store a pair of values, thereby combining upper and lower parts of a result during the store.
The saturate instructions (&lt;code>sqxtn&lt;/code>) saturate the 64 bit intermediate values down to 32 bits, and the &lt;code>stp&lt;/code> instruction then stores the lower 64 bits (the two saturated 32-bit result values) of the two registers to the designated memory address.&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" style="color:#ebdbb2;background-color:#282828;-moz-tab-size:2;-o-tab-size:2;tab-size:2;">&lt;code class="language-asm" data-lang="asm">&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">1&lt;/span>&lt;span>[1,53] . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . . &lt;span style="color:#d3869b">smlal2&lt;/span> &lt;span style="color:#d3869b">v30.2d&lt;/span>, &lt;span style="color:#d3869b">v8.4s&lt;/span>, &lt;span style="color:#d3869b">v28.4s&lt;/span> &lt;span style="color:#928374;font-style:italic">; last MAC
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">2&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,54] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v18.2s&lt;/span>, &lt;span style="color:#d3869b">v18.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate in place
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">3&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,55] . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v19.2s&lt;/span>, &lt;span style="color:#d3869b">v19.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate in place
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">4&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,56] . . &lt;span style="color:#fabd2f">DeeeeeE.&lt;/span> . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v29.2s&lt;/span>, &lt;span style="color:#d3869b">v29.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate in place
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">5&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,57] . . &lt;span style="color:#fabd2f">DeeeeeE&lt;/span> . &lt;span style="color:#d3869b">sqxtn&lt;/span> &lt;span style="color:#d3869b">v30.2s&lt;/span>, &lt;span style="color:#d3869b">v30.2d&lt;/span> &lt;span style="color:#928374;font-style:italic">; saturate in place
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">6&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,58] . . &lt;span style="color:#b8bb26;font-weight:bold">.DeeeE&lt;/span> . &lt;span style="color:#d3869b">stp&lt;/span> &lt;span style="color:#d3869b">d18&lt;/span>, &lt;span style="color:#d3869b">d19&lt;/span>, [&lt;span style="color:#d3869b">x6&lt;/span>] &lt;span style="color:#928374;font-style:italic">; store result A
&lt;/span>&lt;/span>&lt;/span>&lt;span style="display:flex;">&lt;span style="white-space:pre;-webkit-user-select:none;user-select:none;margin-right:0.4em;padding:0 0.4em 0 0.4em;color:#756d59">7&lt;/span>&lt;span>&lt;span style="color:#928374;font-style:italic">&lt;/span>[1,59] . . . &lt;span style="color:#fabd2f">DeeeE&lt;/span> &lt;span style="color:#d3869b">stp&lt;/span> &lt;span style="color:#d3869b">d29&lt;/span>, &lt;span style="color:#d3869b">d30&lt;/span>, [&lt;span style="color:#d3869b">x7&lt;/span>] &lt;span style="color:#928374;font-style:italic">; store result B
&lt;/span>&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;p>This reduces the total simulation cycle count to 7804 and the examined section reaches an IPC of 0.77.&lt;/p>
&lt;h2 id="results-and-future-work">Results and Future Work
&lt;/h2>&lt;p>Just reordering the instructions and a simple tweak changing how the results are stored caused an over 3x increase in the number of instructions processed per cycle according to the simulation.
The next step would of course be to check how far that translates onto real hardware. &lt;br>
The following table presents the results at each step.&lt;/p>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Version&lt;/th>
&lt;th>Total cycles (100 iterations)&lt;/th>
&lt;th>IPC&lt;/th>
&lt;th>Speedup&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>Original&lt;/td>
&lt;td>26,204&lt;/td>
&lt;td>0.23&lt;/td>
&lt;td>1.00x&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Interleaved&lt;/td>
&lt;td>9,104&lt;/td>
&lt;td>0.66&lt;/td>
&lt;td>2.88x&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Separate result registers&lt;/td>
&lt;td>8,304&lt;/td>
&lt;td>0.72&lt;/td>
&lt;td>3.16x&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>Final (&lt;code>sqxtn&lt;/code> + &lt;code>stp&lt;/code>)&lt;/td>
&lt;td>7,804&lt;/td>
&lt;td>0.77&lt;/td>
&lt;td>3.36x&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>Some minor improvements might be achieved by fine tuning the instruction order a bit more, however the current implementation should be close to or at the optimal order, with the remaining gaps coming from dependencies in the tail and the model&amp;rsquo;s in-order stalls.
Further speedups might be possible to achieve by reducing critical paths by calculating partial sums, however that would increase the number of registers required, as each intermediate result would use up an additional register.&lt;/p>
&lt;p>Finally reading inputs/writing results could also be interleaved, reducing the tail dependencies.
As demonstrated, the tail is the cause of the longest remaining stalls in the examined section, and the next iteration does not depend on the results of the previous one.
By starting with the subsequent iteration while saturating and storing the results of the current iteration (also called software pipelining&lt;sup id="fnref2:2">&lt;a href="#fn:2" class="footnote-ref" role="doc-noteref">2&lt;/a>&lt;/sup>) it is possible to interleave the instructions at the tail-end better, potentially increasing IPC.
However, that requires restructuring the entire algorithm.
It would also require special handling of edge cases (first and last iteration) and would thus require changes that reach outside the bounds of the inner loop.&lt;/p>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>John D. McCalpin, STREAM: Sustainable Memory Bandwidth in High Performance Computers, technical report, University of Virginia, 1991 (&lt;a class="link" href="https://www.cs.virginia.edu/stream/" target="_blank" rel="noopener"
>https://www.cs.virginia.edu/stream/&lt;/a>)&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:2">
&lt;p>Denis Bakhvalov, Performance Analysis and Tuning on Modern CPUs, 2nd edition, 2024, Section 3.2 &amp;ldquo;Pipelining&amp;rdquo; (&lt;a class="link" href="https://github.com/dendibakh/perf-book" target="_blank" rel="noopener"
>https://github.com/dendibakh/perf-book&lt;/a>)&amp;#160;&lt;a href="#fnref:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&amp;#160;&lt;a href="#fnref1:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&amp;#160;&lt;a href="#fnref2:2" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;li id="fn:3">
&lt;p>LLVM 23.1.2, &lt;code>llvm/lib/MCA/Stages/InOrderIssueStage.cpp&lt;/code> (&lt;a class="link" href="https://github.com/llvm/llvm-project/blob/llvmorg-23.1.2/llvm/lib/MCA/Stages/InOrderIssueStage.cpp#L142-L144" target="_blank" rel="noopener"
>https://github.com/llvm/llvm-project/blob/llvmorg-23.1.2/llvm/lib/MCA/Stages/InOrderIssueStage.cpp#L142-L144&lt;/a>)&amp;#160;&lt;a href="#fnref:3" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description></item></channel></rss>