What Moves a Score: Compiler, Memory and the Vendor Number
Keep the C loop and the core fixed. Change only the compiler and the memory, and the score moves more than nine times. A quoted score means little without its fine print.
You Will Be Able To
- Explain how the compiler changes the instructions of an iteration, and why a lower CPI can still mean a slower program.
- Predict what wait cycles on data and on code do to a score.
- Read a vendor's benchmark number and name the facts that must come with it.
- Lesson
- S8.15
- Time
- 25 min
- Runs on
- Your browser
- Check
- 5 questions
A data sheet says the five-stage core scores 22,727 per MHz on our kernel. Your board runs the same core and the same kernel, and measures 2,398.
Nobody cheated. Both numbers were measured on the real core. What differs?
Before You Start
You need Benchmarks: the checksum kernel, its window, the score per MHz, CPI and IPC.
You need Load-Use Stalls: an instruction that needs the load just before it waits one cycle.
It helps to have read Memories in an SoC: flash is slower than SRAM, and code often lives in flash.
| Question | |
|---|---|
| 1 | What does the compiler decide, if the C source is fixed? |
| 2 | Can a version with a lower CPI be slower? Why? |
| 3 | What do wait cycles on data and on code do to a score? |
| 4 | Which facts must come with a vendor's number? |
| 5 | With one data wait, how many cycles does the unrolled version need for 10 iterations? |
The Idea: One Loop, Many Instruction Streams
A benchmark fixes the C source and its input. It does not fix the machine code. A compiler turns one loop into many different streams of instructions.
Some streams have fewer instructions. Some put a load early, so that nothing waits for it. Some keep every variable in memory. The core sees only the stream, so its cycle count changes with the compiler.
The memory matters as much. A load or store may wait for a slow memory. If the code itself sits in a slow memory, every instruction waits to be fetched.
So a score belongs to a whole setup. That is the compiler and its settings, the memories, the clock and the length of the run. A number quoted without them cannot be compared.
New Words
| Word | Meaning |
|---|---|
| Compiler setting | A choice given to the compiler, such as how hard it tries to make the code fast. It changes the machine code, not the source. |
| Instruction scheduling | The compiler puts instructions in an order that avoids waits, for example a load well before its use. |
| Loop unrolling | The compiler copies the body of a loop several times. Fewer branches run, and the code grows. |
| Wait cycle of an access | A cycle that a load, a store or a fetch spends waiting for a slow memory. |
| Code memory | The memory that the instructions are fetched from: SRAM, ROM or flash. |
| Fine print of a score | The facts that a score needs to mean something: compiler, settings, memories, waits, clock and run length. |
One Picture: Everything Behind One Number
- Control / FSM
- Memory
- Processor
- Clock / reset
- Peripheral
- One source
The C loop is fixed. A benchmark forbids changes to it.
- The compiler
The compiler decides how many instructions an iteration needs, and in which order they come.
- The memories
Each load and store may wait. If the code sits in a slow memory, every fetch waits.
- The score
Cycles and the clock make the score. Every box before it changed the cycles.
Read the diagram as text
- The C loop: the same source for every run
- The compiler: its version and its settings
- The instructions: how many in each iteration, and in which order
- The core: its CPI on those instructions
- The memories: wait cycles for code and for data
- The clock: the MHz that the chip runs at
- The score: one number, with all of this behind it
- The C loop → The compiler: source
- The compiler → The instructions: machine code
- The instructions → The core: runs on
- The memories → The core: waits
- The core → The score: cycles
- The clock → The score: MHz
Everything to the left of the score belongs in the fine print of a quoted number.
All four versions of the lab compute the same checksum from the same C loop:
uint32_t sum = 0;
for (int r = 0; r < ROUNDS; r++) // one round is one iteration of the benchmark
for (int i = 0; i < 8; i++)
sum = ((sum << 1) | (sum >> 31)) ^ buf[i]; // turn left by one bit, then mix in a word| Version | What it does | Instructions for each iteration |
|---|---|---|
| in memory | Keeps sum and i in memory: a load before each use, a store after each change. Like code with no optimisation. | 123 |
| plain | Keeps everything in registers. The load of a word comes just before the instruction that needs it. | 60 |
| scheduled | The same 60 instructions. The load comes first, so the checksum steps run while it finishes. | 60 |
| unrolled | Eight copies of the body, so no inner branch. Each load is placed early. | 42 |
Hand-written listings that stand for what a compiler might emit at different settings. No compiler was run. Every version leaves the same checksum in SRAM.
The Smallest Example, Worked by Hand
Take one iteration on the five-stage core, with no wait cycles. A taken branch costs 2 cycles, and a load-use costs 1.
| Version | Instructions | Taken branches | Load-use | Cycles | CPI |
|---|---|---|---|---|---|
| scheduled | 60 | 8, so 16 cycles | none | 60 + 16 = 76 | 1.27 |
| plain | 60 | 8, so 16 cycles | 8, one for each word | 60 + 16 + 8 = 84 | 1.40 |
| in memory | 123 | 8, so 16 cycles | 24, three for each word | 123 + 16 + 24 = 163 | 1.33 |
Compare "plain" with "in memory". The second has the lower CPI, and it takes almost twice as long. Its extra instructions are cheap ones, so they pull the average down.
Now add two wait cycles to each load and store. "scheduled" has 8 loads in an iteration: 76 + 16 = 92 cycles. "in memory" has 49 loads and stores: it grows far more.
The Rules
Rule 1: The compiler sets the instructions of an iteration, and their order.
The same C loop ran as 42, 60 or 123 instructions in our versions.
Rule 2: Compare cycles, not CPI.
A version with more, cheaper instructions can have a lower CPI and still be slower.
Rule 3: Each wait cycle is paid by every load and store.
Code that keeps its variables in memory pays most.
Rule 4: Code in a slow memory slows every instruction.
Each fetch waits. On our core a load-use stall then hides behind the slow fetch.
Rule 5: A score belongs to its whole setup.
Compiler and settings, code and data memory with their waits, clock, and run length.
Rule 6: Compare two scores only if they share that setup.
If one line of the fine print is missing, ask for it before you compare.
Sources for These Rules
Compiler settings change the instruction count and the CPI: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 2.13, Figure 2.29. There the fastest setting does not have the best CPI.
Using only part of the performance equation as a measure misleads: the same book, section 1.10.
Wait cycles of the memory raise the CPI: Harris and Harris, Digital Design and Computer Architecture, ARM edition, section 7.2.
Fixed source, fixed data and one proper answer: Patterson and Hennessy, Computer Organization and Design, ARM edition, section 6.10.
The four versions, the wait cycles and the instruction waits are choices of our lab. The rule for data waits is measured on the real core for 0 to 5 waits. For instruction waits there is no lab rule: those numbers are recordings only.
What a vendor must publish with a CoreMark or Dhrystone score is set by their run rules. Those rules are not in our library and are not checked here.
Now Run It
The lab at the end of this page runs 10 iterations of each version on the five-stage cost rules. A probe ran each version on the real core, and read the same windows from mcycle.
in memory | instructions 1233 | cycles 1637 | CPI 1.33 | per iteration 163.7 | taken 79 | load-use 240 | waits 0
plain | instructions 602 | cycles 846 | CPI 1.41 | per iteration 84.6 | taken 79 | load-use 80 | waits 0
scheduled | instructions 602 | cycles 766 | CPI 1.27 | per iteration 76.6 | taken 79 | load-use 0 | waits 0
unrolled | instructions 422 | cycles 446 | CPI 1.06 | per iteration 44.6 | taken 9 | load-use 0 | waits 0Rule 1: 1233, 602 and 422 instructions for the same work. Unrolling also cut the taken branches from 79 to 9.
Rule 2: "in memory" has CPI 1.33 and "plain" CPI 1.41. Yet "in memory" needs 163.7 cycles for each iteration, and "plain" 84.6.
Rule 2 again, the other way: "plain" and "scheduled" run the same 602 instructions. Only their order differs, and 80 load-use cycles separate them.
Now set DATA_WAITS to 2 and run again.
in memory | instructions 1233 | cycles 2619 | CPI 2.12 | per iteration 261.9 | taken 79 | load-use 240 | waits 982
plain | instructions 602 | cycles 1006 | CPI 1.67 | per iteration 100.6 | taken 79 | load-use 80 | waits 160
scheduled | instructions 602 | cycles 926 | CPI 1.54 | per iteration 92.6 | taken 79 | load-use 0 | waits 160
unrolled | instructions 422 | cycles 606 | CPI 1.44 | per iteration 60.6 | taken 9 | load-use 0 | waits 160Rule 3: "in memory" grows from 163.7 to 261.9 cycles, by 60 percent. "unrolled" grows from 44.6 to 60.6, by 36 percent. The waits fall on the version with the most accesses.
Rule 4 has no lab rule. The probe ran the same versions with the code port slowed down, and recorded the result.
| Version | Single-cycle | Five-stage | Data waits 2 | Code waits 1 | Code waits 2 |
|---|---|---|---|---|---|
| in memory | 123 | 163 | 261 | 278 | 417 |
| plain | 60 | 84 | 100 | 152 | 228 |
| scheduled | 60 | 76 | 92 | 152 | 228 |
| unrolled | 42 | 44 | 60 | 88 | 132 |
Long runs: the window of 20 iterations minus the window of 10, divided by 10. The last four columns are the five-stage core. Waits are cycles added to each access.
With one wait on code, "plain" and "scheduled" both need 152 cycles. The load-use cycle is hidden behind the slow fetch. The careful order of the compiler buys nothing here.
Reading a Vendor's Number
Now the hook. For the five-stage core, the best line of the table is "unrolled" with no waits: 44 cycles, or 22,727 per MHz. The worst is "in memory" with two code waits: 417 cycles, or 2,398 per MHz.
Same C loop, same core, more than nine times apart. Each line of the fine print moved the number. So read a quoted score with this list.
| Ask | What it moved here |
|---|---|
| Which compiler, and which settings? | 44 to 163 cycles for each iteration on the five-stage core |
| Where did code and data sit, with how many waits? | Code waits 2: 44 became 132 for the unrolled version |
| Which clock, and was it per MHz? | Per MHz the single-cycle core led; per second it trailed (S8.14) |
| How long was the run? | One iteration read 82 cycles, a long run 76 for each |
| Was the answer checked? | Without a check, a broken or trimmed run can look fast |
A vendor picks the best setup, and that is fair. Your job is to find the line where your product differs, and to measure that case.
Why It Is Built This Way
| Design choice | The alternative | What the choice buys, and costs |
|---|---|---|
| Fix the source, let the compiler work | Fix the machine code | Fair to every instruction set and every compiler. The score measures the compiler too. |
| Run code from fast memory for the benchmark | Run it from flash, as the product may | The core is shown at its best. Your product may never reach that number. |
| Quote one number | Quote a table of setups | Easy to compare at a glance. The setup hides in the fine print. |
The Trap: The Careful Compiler That Bought Nothing
A team spent a week tuning the order of instructions in a hot loop. On the simulator the loop ran 10 percent faster: 76 cycles in place of 84.
On the board it ran no faster at all. The code sat in flash with wait cycles, and every fetch waited. The saved load-use cycle hid behind the fetch, as our table shows: 152 and 152.
The second trap is to praise a low CPI. "in memory" has the lower CPI of the two versions above, and it runs at half the speed.
The third is to compare a score per MHz from fast SRAM with one from your flash. Both are true, for different setups.
At Scale
This section looks ahead. On a large chip, caches stand between the core and slow memory.
Caches hide the slow memory
A small benchmark fits in a cache, so it never sees the slow memory. A large program may. See Cache Performance.
Unrolling has a cost
Unrolled code is larger. In a small cache it can miss more and lose what it gained.
Instructions that do more
A core with a multiply or a turn-left instruction needs fewer instructions for the same C loop. The instruction set is part of the setup too.
Run rules
Public benchmarks come with rules on what must be reported. Read them before you quote a number.
Try It
Run the lab. Which version has the lowest CPI? Which has the fewest cycles for each iteration?
Set DATA_WAITS to 2. Which version loses the most, and why?
In the rows of "plain", find the load and the instruction that uses it at once.
Names Printed by the Lab
| Name | Meaning |
|---|---|
| in memory, plain, scheduled, unrolled | The four versions of the same C loop. The table below the lab lists them. |
| instructions, cycles | The window of 10 iterations, between the two reads of mcycle, on the five-stage core. |
| per iteration | Cycles divided by the iterations. |
| taken | Taken branches. Each costs 2 cycles. |
| load-use | Instructions that need the load just before them. Each costs 1 cycle. |
| waits | Wait cycles of all loads and stores together. |
in memory | instructions 1233 | cycles 1637 | CPI 1.33 | per iteration 163.7 | taken 79 | load-use 240 | waits 0 plain | instructions 602 | cycles 846 | CPI 1.41 | per iteration 84.6 | taken 79 | load-use 80 | waits 0 scheduled | instructions 602 | cycles 766 | CPI 1.27 | per iteration 76.6 | taken 79 | load-use 0 | waits 0 unrolled | instructions 422 | cycles 446 | CPI 1.06 | per iteration 44.6 | taken 9 | load-use 0 | waits 0Check Yourself
Common Mistakes
Wrong Rank two builds of a program by their CPI.
The one with more instructions can have the lower CPI and be slower.
Fix Compare the cycles of the same work.
Wrong Expect a vendor score on your board.
Your compiler, memories and waits may differ from theirs.
Fix Find the line of the fine print where you differ, and measure that case.
Wrong Tune the order of instructions without looking at the code memory.
With slow fetch, a load-use stall hides behind it.
Fix Measure on the memory that the product runs from.
Remember
- The compiler sets the instructions of an iteration and their order. The source stays the same.
- Compare cycles for the same work. A lower CPI can still be slower.
- Data waits hit code with many accesses. Code waits slow every instruction.
- A score belongs to its setup. Read the fine print before you compare.