mcpField
C
cpu-pipelines-and-hazards

cpu-pipelines-and-hazards

github.com/mohitmishra786Development269322 installsUpdated 18w ago
Repository

About cpu-pipelines-and-hazards

A computer architecture skill for developers and learners covering pipeline stages, hazards, forwarding, stalls, and branch handling, with practical links to instruction scheduling and performance counters.

Key features

  1. 01Classic pipeline stages
  2. 02Data and control hazards
  3. 03Forwarding and register renaming
  4. 04Branch prediction and flushes
  5. 05Instruction-level parallelism tips
  6. 06Performance counter examples

Use cases

  1. 01Interpreting pipeline stall metrics
  2. 02Explaining instruction-order effects
  3. 03Analyzing assembly scheduling
  4. 04Investigating performance drops in hot loops

Install

Copy the command and run it in a terminal.

$npx skills add https://github.com/mohitmishra786/low-level-dev-skills --skill cpu-pipelines-and-hazards

After installing, type/cpu-pipelines-and-hazardsin Claude Code to confirm it loaded.

Details

From the project README

CPU Pipelines and Hazards

Purpose

Explain classic and modern CPU pipeline concepts: stages, data and control hazards, forwarding/bypassing, stalls, and branch handling — foundational for optimization and understanding microarchitecture counters.

When to Use

  • Interpreting pipeline stall metrics from skills/profilers/intel-vtune-amd-uprof
  • Teaching why instruction order affects throughput
  • Relating assembly scheduling to hardware behavior
  • Debugging unexpected performance cliffs in hot loops

Workflow

1. Five-stage classic pipeline (MIPS-style mental model)
IF → ID → EX → MEM → WB

Overlapped execution: instruction N in EX while N+1 in ID.

2. Data hazards
TypeExampleMitigation
RAW (true)add r1,r2,r3 then sub r4,r1,r5Forwarding from EX/MEM/WB
WAR / WAWRare in in-order; relevant in OoO renameRegister renaming

Without forwarding:

stall until writeback completes
3. Control hazards (branches)
Branch in ID → target unknown until EX
├── Predict taken/not-taken (static or dynamic)
├── Flush wrong-path instructions on mispredict
└── Penalty = pipeline depth (varies by CPU)

See skills/computer-architecture/branch-prediction-and-speculation.

4. Structural hazards

Limited functional units (single memory port) cause stalls even without dependencies.

5. Practical optimization hints
/* Bad — tight dependency chain */
for (int i = 0; i < n; i++)
    acc = acc + data[i];   /* each iter waits on acc */

/* Better — multiple accumulators (ILP) */
acc0 = acc1 = 0;
for (int i = 0; i < n; i += 2) {
    acc0 += data[i];
    acc1 += data[i+1];
}
acc = acc0 + acc1;

Pair with skills/low-level-programming/cpu-cache-opt — memory often dominates.

6. Reading uops / ports (x86)
perf stat -e instructions,cycles,stalls-frontend,stalls-backend ./app

VTune "Microarchitecture Exploration" maps to pipeline slots.

7. Agent usage
/cpu-pipelines-and-hazards Explain RAW hazard in this ARM assembly loop and how to break it

Common Problems

SymptomCauseFix
High stalls-frontendI-cache misses / branch mispredictAlign hot loop; see branch skill
No speedup from unrollMemory boundProfile loads; prefetch
Wrong cycle modelIgnored OoO executionUse perf hardware counters
"NOP fixes it"Timing-sensitive MMIONever tune device delays by NOP

Related Skills

  • skills/computer-architecture/branch-prediction-and-speculation — mispredict cost
  • skills/computer-architecture/memory-hierarchy-and-caches — load latency
  • skills/low-level-programming/cpu-cache-opt — cache-line effects
  • skills/profilers/intel-vtune-amd-uprof — pipeline analysis
  • skills/low-level-programming/assembly-arm — instruction scheduling

Comments

Markdown supported