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 worksC99 restrict tells the compiler two pointer parameters do not alias the same memory, enabling aggressive load/store reordering and vectorization. Violating the promise is undefined behavior. BLAS kernels and memcpy implementations use restrict heavily.
Separate read and write buffers with restrict so the compiler can vectorize the loop without runtime alias checks.
cLoading…
-fno-strict-aliasing disables type-based alias analysis; avoid in hot code__restrict__ is the GCC spelling accepted in C++restrict on struct members is C99 only in limited casesKeep 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 mark a hot loop with restrict and show improved vectorization or fewer memory barriers in assembly. This exercise asks you to document the aliasing assumption.
For void add(int *dst, int *a, int *b, int n) with dst[i] = a[i] + b[i], the compiler cannot assume the arrays are separate. If dst overlaps a, a store can change a later load. So it either stays scalar, or emits a runtime overlap check that picks a vector loop only when that is safe. __restrict__ promises "no overlap", so the vector loop runs unconditionally, and if the promise is false the results are silently wrong. Simulate all three on real memory.
cLoading…
for i: m[DST+i] = m[A+i] + m[B+i].[DST, DST+N) or identical to it (DST == A, in-place). If the check passes, run the vector loop, otherwise the scalar one.no overlap, same array (in place), overlap, dst is K elements after X or overlap, dst is K elements before X.restrict was a lie: K of N results wrong if the restrict results differ from C semantics.overlap happens to be harmless for this width.results match, but passing the same array to restrict pointers is still undefined.restrict is correct here.width: 1..16 and call: out of bounds (memory has 64 ints) (every range must fit, and N >= 1).cLoading…
Input:
cLoading…
Output:
cLoading…
Hidden tests cover overlap distances smaller and larger than the vector width, a width of 1 (vectorising changes nothing), overlap with b instead of a, remainders that do not fill a chunk, in-place calls, and out-of-bounds calls.