KatInterview

Taming a Compiler on a Wide-Issue VLIW Target

System DesignModel answer
HardModel answerSystem Designcompiler-optimizationvliwperformance-engineering

Last updated

Explain how to steer or override a compiler's scheduling on a VLIW-style core, and how to verify the result is both correct and faster without relying on undefined behavior.

You are porting a hot numeric kernel (for example a dot product, a stencil, or one round of a hash) to a processor that issues several operations per cycle in fixed bundles, as VLIW designs do. The kernel accounts for roughly 70% of runtime, yet the compiler's output fills only about a third of each bundle's issue slots. In some places the compiler also seems to have been too aggressive, for instance by unrolling so far that it spills registers.

Describe how you would handle this. Your answer should cover:

  1. How you would establish that the compiler really is the problem, and what the theoretical best schedule looks like.
  2. The tools you would use to steer the compiler, from least to most invasive: qualifiers and attributes, pragmas, flags, source rewrites (manual unrolling, software pipelining), intrinsics and finally inline assembly. Say when each one is justified.
  3. How you would measure the effect of each change so that both speed and correctness are trustworthy.
  4. The risks involved, in particular undefined behavior, aliasing assumptions and loss of portability.

State any assumptions you make about the target and the toolchain.

Hints

Hint 1

Before changing code, work out a lower bound on cycles per loop iteration from the number of operations per resource type and from the longest loop-carried dependence chain. Compare it with what the compiler achieved.

Hint 2

Order your interventions so each one gives the compiler more information or control only as needed: facts it cannot prove, then hints, then restructuring, then explicit instructions, then raw assembly.

Hint 3

Many hints (restrict, ivdep, assume-aligned) are promises, not suggestions. Think about what happens when a caller breaks the promise, and how you would test for it.

Hint 4

For measurement, think about a trusted scalar reference, odd sizes such as 0, 1 and non-multiples of the unroll factor, controlled benchmarking conditions, and a guard against future compiler upgrades undoing your gains.