Master the fundamental concepts of compiler optimization techniques through this focused micro-challenge.
You have read the whole brief, and the concepts above stay free on every task. Writing and running the code needs a plan.
Three hints are available for this task, revealed one at a time inside the code workspace so you can struggle productively before seeing them.
Every task includes starter code, theory, and hidden tests so you can implement and verify locally in the browser.
How it worksAuto-vectorization needs provably independent iterations, aligned pointers, and no function calls in the inner loop. #pragma GCC ivdep, clang loop vectorize(enable), and OpenMP simd directives remove assumed dependencies the compiler cannot prove.
Simple counting loop, no backward dependencies, stride-one access on arrays.
cLoading…
-fopt-info-vec prints why vectorization failed__attribute__((aligned(32))) helps AVX loadsrestrict plus ivdep often unlocks SIMD togetherobjdump -d -MintelKeep the relevant documentation open while you implement. When your output disagrees with the reference, trace one failing case by hand before changing random lines.
You will add pragma or attribute hints to vectorize a loop and verify SIMD instructions in assembly. This exercise asks you to report speedup and name the SIMD width used.
Document one invariant you will assert in tests and how you would detect its violation from observable symptoms.
Why did the compiler refuse to vectorise your loop? Write a small vectoriser legality checker, like the reasoning behind -fopt-info-vec-missed. It reads simple loops over index i, finds the barriers (calls, scatters, float reductions, loop-carried dependences, possible aliasing), and reports either why the loop cannot be vectorised or what vector code would result. Hints such as restrict, fast-math and omp-simd remove specific barriers, exactly as they do in GCC and Clang.
cLoading…
An index is i, i+c, i-c, k*i, k*i+c or c (affine), or ARRAY[...] (indirect). name(...) in an expression is a function call. Data is float unless the int hint is given.
=, a bad index, or a plain SCALAR = ...) gives cannot analyse: unsupported statement.not vectorized: call to NAME() (declare it simd, or use omp-simd), for the first call seen.not vectorized: indirect store may write one element twice in a vector.SCALAR += ... on float data gives not vectorized: floating-point reduction into S needs fast-math or omp-simd (reassociation).not vectorized: unknown dependence on A (non-unit stride or indirect index). Otherwise let d = write offset - other offset, and keep the smallest positive d. Accesses to different arrays need a runtime alias check unless the restrict hint is given.0 < d < W:
omp-simd, warn and keep W;The hints lift specific rules. omp-simd asserts safety: it skips rules 2, 3 and 4 and the "unknown dependence" check, and it removes the runtime alias check. fast-math or int lift rule 4. restrict removes the runtime alias check.
cLoading…
The notes come in this order: alias check, masking (the loop has an if), the first strided access, the first indirect load, and the reduction. loop NAME: trip count must be positive and width: power of two, 2..64 are the input errors.
Input:
cLoading…
Output:
cLoading…
Hidden tests cover integer reductions, masked conditional loops, strided and gather loads, distance-2 and distance-8 dependences, anti-dependences (a[i] = a[i+1]), omp-simd overriding calls and dependences, several statements in one loop, and malformed statements.