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.

Five questions this page answers
Question
1What does the compiler decide, if the C source is fixed?
2Can a version with a lower CPI be slower? Why?
3What do wait cycles on data and on code do to a score?
4Which facts must come with a vendor's number?
5With 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

New words on this page
WordMeaning
Compiler settingA 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 schedulingThe compiler puts instructions in an order that avoids waits, for example a load well before its use.
Loop unrollingThe compiler copies the body of a loop several times. Fewer branches run, and the code grows.
Wait cycle of an accessA cycle that a load, a store or a fetch spends waiting for a slow memory.
Code memoryThe memory that the instructions are fetched from: SRAM, ROM or flash.
Fine print of a scoreThe facts that a score needs to mean something: compiler, settings, memories, waits, clock and run length.

One Picture: Everything Behind One Number

From One C Loop to One ScoreStep through the picture. Each box can change the score, while the C loop stays the same.
  • Control / FSM
  • Memory
  • Processor
  • Clock / reset
  • Peripheral
From One C Loop to One ScoreStep through the picture. Each box can change the score, while the C loop stays the same. component The C loop the same source forevery run component The compiler its version and itssettings component The instructions how many in eachiteration, and in whichorder component The core its CPI on thoseinstructions component The memories wait cycles for codeand for data component The clock the MHz that thechip runs at component The score one number, with all ofthis behind it sourcemachine coderuns onwaitscyclesMHz
  1. component The C loopthe same source for every run
    • source → The compiler
  2. component The compilerits version and its settings
    • machine code → The instructions
  3. component The instructionshow many in each iteration, and in which order
    • runs on → The core
  4. component The memorieswait cycles for code and for data
    • waits → The core
  5. component The coreits CPI on those instructions
    • cycles → The score
  6. component The clockthe MHz that the chip runs at
    • MHz → The score
  7. component The scoreone number, with all of this behind it
  1. One source

    The C loop is fixed. A benchmark forbids changes to it.

  2. The compiler

    The compiler decides how many instructions an iteration needs, and in which order they come.

  3. The memories

    Each load and store may wait. If the code sits in a slow memory, every fetch waits.

  4. 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
Four versions of that loop
VersionWhat it doesInstructions for each iteration
in memoryKeeps sum and i in memory: a load before each use, a store after each change. Like code with no optimisation.123
plainKeeps everything in registers. The load of a word comes just before the instruction that needs it.60
scheduledThe same 60 instructions. The load comes first, so the checksum steps run while it finishes.60
unrolledEight 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.

One iteration, three versions
VersionInstructionsTaken branchesLoad-useCyclesCPI
scheduled608, so 16 cyclesnone60 + 16 = 761.27
plain608, so 16 cycles8, one for each word60 + 16 + 8 = 841.40
in memory1238, so 16 cycles24, three for each word123 + 16 + 24 = 1631.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

  1. 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.

  2. Rule 2: Compare cycles, not CPI.

    A version with more, cheaper instructions can have a lower CPI and still be slower.

  3. Rule 3: Each wait cycle is paid by every load and store.

    Code that keeps its variables in memory pays most.

  4. 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.

  5. Rule 5: A score belongs to its whole setup.

    Compiler and settings, code and data memory with their waits, clock, and run length.

  6. 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.

Printed by the lab on Icarus. Each line is one version: 10 iterations, no wait cycles.
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 0

Rule 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.

The same lab with two wait cycles on each load and store.
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 160

Rule 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.

Cycles for each iteration, measured on the real ap_rv32
VersionSingle-cycleFive-stageData waits 2Code waits 1Code waits 2
in memory123163261278417
plain6084100152228
scheduled607692152228
unrolled42446088132

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.

The fine print, and what each line moved in our runs
AskWhat 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 choiceThe alternativeWhat the choice buys, and costs
Fix the source, let the compiler workFix the machine codeFair to every instruction set and every compiler. The score measures the compiler too.
Run code from fast memory for the benchmarkRun it from flash, as the product mayThe core is shown at its best. Your product may never reach that number.
Quote one numberQuote a table of setupsEasy 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 page4 versions, 1 core
A phone chipseveral compilers, cache sizes and clocks

This section looks ahead. On a large chip, caches stand between the core and slow memory.

  1. 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.

  2. Unrolling has a cost

    Unrolled code is larger. In a small cache it can miss more and lose what it gained.

  3. 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.

  4. 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
NameMeaning
in memory, plain, scheduled, unrolledThe four versions of the same C loop. The table below the lab lists them.
instructions, cyclesThe window of 10 iterations, between the two reads of mcycle, on the five-stage core.
per iterationCycles divided by the iterations.
takenTaken branches. Each costs 2 cycles.
load-useInstructions that need the load just before them. Each costs 1 cycle.
waitsWait cycles of all loads and stores together.
RUNIcarus Verilog · in your browser
Edit the code, then run it
Expected: 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 0

Check Yourself

Predict the outputThis run has DATA_WAITS = 1 and two versions. What is the last line? Its form is: unrolled | instructions 422 | cycles ... | CPI .... | per iteration .... | taken 9 | load-use 0 | waits ..
// One C loop, four instruction streams, on the five-stage ap_rv32
// Test code, not the core. It walks the instructions of the measured window and adds up cycles with the
// cost rules of the two builds of ap_rv32. A probe ran the same program on the real cores and read the
// same counts from mcycle. Each version computes the same checksum.
module tb;
  localparam int ROUNDS = 10;           // iterations of the benchmark
  localparam int DATA_WAITS = 1;       // wait cycles of each load and store
  localparam int ALU = 0, LOAD = 1, STORE = 2, BRANCH = 3, CSR = 4;
  localparam int ROWS = 57;
  // One row for each instruction of the window: its kind, the register it writes, the registers it reads,
  // and for a branch the row it goes back to and the trips of its loop.
  int kind [0:ROWS-1], rd [0:ROWS-1], rs1 [0:ROWS-1], rs2 [0:ROWS-1], target [0:ROWS-1], trips [0:ROWS-1];
  task automatic row(input int i, input int k, input int d, input int s1, input int s2, input int t, input int p);
    kind[i] = k;  rd[i] = d;  rs1[i] = s1;  rs2[i] = s2;  target[i] = t;  trips[i] = p;
  endtask
  // Walk the window from row "from" to row "to" - 1 ("at" is the row now running), and add up the cycles.
  // left[r] counts down the trips of the loop that the branch in row r closes.
  int left [0:ROWS-1];
  int n [0:4], five [0:4];
  int instructions, cycles, taken, stalls, waits, reads, at, c, prev_rd;
  bit prev_load;
  task automatic walk(input int from, input int to);
    for (int r = 0; r < ROWS; r++) left[r] = trips[r];
    for (int k = 0; k < 5; k++) begin n[k] = 0; five[k] = 0; end
    taken = 0;  stalls = 0;  waits = 0;  reads = 0;  prev_load = 0;  prev_rd = 0;
    at = from;
    while (at < to) begin
      c = 1;                                                           // every instruction: one cycle
      if (prev_load && prev_rd != 0 && (rs1[at] == prev_rd || rs2[at] == prev_rd)) begin
        c += 1;  stalls++;                                             // it needs the load just before it
      end
      if (kind[at] == LOAD || kind[at] == STORE) begin c += DATA_WAITS;  waits += DATA_WAITS; end
      if (kind[at] == CSR) begin c += 3;  reads += 3; end              // a CSR instruction leaves from MEM
      n[kind[at]]++;
      prev_load = (kind[at] == LOAD);  prev_rd = rd[at];
      if (kind[at] == BRANCH) left[at] = left[at] - 1;
      if (kind[at] == BRANCH && left[at] > 0) begin
        c += 2;  taken++;                                               // taken: two fetched words are thrown away
        five[kind[at]] += c;
        at = target[at];
      end else begin
        if (kind[at] == BRANCH) left[at] = trips[at];               // the loop is done: ready for its next pass
        five[kind[at]] += c;
        at++;
      end
    end
    instructions = n[0] + n[1] + n[2] + n[3] + n[4];
    cycles = five[0] + five[1] + five[2] + five[3] + five[4];
  endtask
  // a / b with two decimals, and with one decimal, rounded half up
  function automatic string dec2(input longint a, input longint b);
    longint v = (a * 200 + b) / (2 * b);
    return $sformatf("%0d.%02d", v / 100, v % 100);
  endfunction
  function automatic string dec1(input longint a, input longint b);
    longint v = (a * 20 + b) / (2 * b);
    return $sformatf("%0d.%0d", v / 10, v % 10);
  endfunction
  localparam int VERSIONS = 2;
  string names [0:VERSIONS-1];
  int from [0:VERSIONS-1], to [0:VERSIONS-1];
  initial begin
    names[0] = "scheduled";  from[0] = 0;  to[0] = 13;
    names[1] = "unrolled";  from[1] = 13;  to[1] = 57;
    //  row  kind   rd  rs1 rs2 to  trips
    // scheduled: in registers, the load placed early
    row(0,  CSR,   18, 0,  0,  0,  0);      // csrr  s2, mcycle
    row(1,  CSR,   19, 0,  0,  0,  0);      // csrr  s3, minstret
    row(2,  ALU,   5,  8,  0,  0,  0);      // round: addi  t0, s0, 0
    row(3,  ALU,   6,  8,  0,  0,  0);      // addi  t1, s0, 32
    row(4,  LOAD,  7,  5,  0,  0,  0);      // word: lw    t2, 0(t0)
    row(5,  ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(6,  ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(7,  ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(8,  ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(9,  ALU,   5,  5,  0,  0,  0);      // addi  t0, t0, 4
    row(10, BRANCH,0,  5,  6,  4,  8);      // bne   t0, t1, word
    row(11, ALU,   9,  9,  0,  0,  0);      // addi  s1, s1, -1
    row(12, BRANCH,0,  9,  0,  2,  ROUNDS); // bne   s1, x0, round
    // unrolled: the inner loop unrolled
    row(13, CSR,   18, 0,  0,  0,  0);      // csrr  s2, mcycle
    row(14, CSR,   19, 0,  0,  0,  0);      // csrr  s3, minstret
    row(15, LOAD,  7,  8,  0,  0,  0);      // round: lw    t2, 0(s0)
    row(16, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(17, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(18, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(19, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(20, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 4(s0)
    row(21, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(22, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(23, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(24, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(25, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 8(s0)
    row(26, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(27, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(28, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(29, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(30, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 12(s0)
    row(31, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(32, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(33, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(34, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(35, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 16(s0)
    row(36, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(37, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(38, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(39, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(40, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 20(s0)
    row(41, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(42, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(43, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(44, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(45, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 24(s0)
    row(46, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(47, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(48, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(49, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(50, LOAD,  7,  8,  0,  0,  0);      // lw    t2, 28(s0)
    row(51, ALU,   28, 10, 0,  0,  0);      // slli  t3, a0, 1
    row(52, ALU,   29, 10, 0,  0,  0);      // srli  t4, a0, 31
    row(53, ALU,   10, 28, 29, 0,  0);      // or    a0, t3, t4
    row(54, ALU,   10, 10, 7,  0,  0);      // xor   a0, a0, t2
    row(55, ALU,   9,  9,  0,  0,  0);      // addi  s1, s1, -1
    row(56, BRANCH,0,  9,  0,  15, ROUNDS); // bne   s1, x0, round
    for (int v = 0; v < VERSIONS; v++) begin
      walk(from[v], to[v]);
      $display("%s | instructions %0d | cycles %0d | CPI %s | per iteration %s | taken %0d | load-use %0d | waits %0d",
        names[v], instructions, cycles, dec2(cycles, instructions), dec1(cycles, ROUNDS), taken, stalls, waits);
    end
  end
endmodule
ConceptWhat does the compiler decide, if the C source is fixed?
Concept"in memory" has CPI 1.33 and "plain" CPI 1.41. Which is faster?
ConceptThe code moves into a memory with one wait cycle. What happens to the load-use cycles of "plain"?
ConceptA vendor quotes a score per MHz. Which fact must you ask for first?

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.

Practice

Report a problem with this page