﻿# Compiler-eliminated work: a benchmark expression may not execute as written

A practical investigation of compiler-eliminated work: a benchmark expression may not execute as written, with explicit observations, control cases and evidence limits.

Author: Retrivium team
Updated: 2026-11-06
Canonical URL: https://retrivium.ai/journal/field-bench-compiler-eliminated-microbenchmark-work/
Language: en

## The comparison being tested

A CPU preprocessing microbenchmark reports an enormous speedup after simplifying a transform. The result is unused and the inputs are compile-time constants. The optimized binary can remove or reuse the computation. The source still contains a loop that looks substantial, but the timer may measure little of the work the benchmark author intended to compare.

## The confounding mechanism

GCC documents dead-code and dead-store elimination, while Google Benchmark warns that DoNotOptimize does not prevent every optimization of its expression. A known result can still be folded or reused. Preventing unwanted elimination therefore requires understanding the actual operation and output dependency; merely adding a helper name is not proof that every iteration performs a fresh transform.

## Fix the experimental contract

Define the required observable result, input variation and timing boundary. Preserve optimized compiler flags because disabling optimization entirely creates a different performance question. Keep setup costs separate only where the intended deployment does the same. Do not introduce arbitrary volatile operations or barriers that fundamentally change the kernel and then claim to have measured the original production implementation unchanged.

## A step-by-step comparison

Construct a bounded benchmark with an independent output check and controlled varying inputs. Compare the intended optimized build with an intentionally removable constant-result control. Inspect the generated code or relevant execution evidence to verify retained operations. Measure any observation mechanism’s overhead separately. A successful correctness check outside the timed loop does not by itself prove that every timed iteration recomputed the checked result.

## Evidence required for the result

Acceptance evidence includes source, compiler identity, flags, input generation, result consumption and retained-work inspection. Show which transformations remain possible and why the timed path still represents the declared task. Keep raw timing and correctness outcomes. An attractive result from a removed expression should be classified as an invalid measurement, not as an optimization benefit or proof of exceptional hardware throughput.

## Interpretation and limits

Compiler behavior varies by target, version and whole-program context. Even retained instructions do not establish identical memory behavior or service performance. This protocol validates a microbenchmark’s execution claim; it does not prohibit legitimate production optimizations or guarantee one universal barrier pattern. Deployment-relevant end-to-end checks remain necessary after the isolated operation has been measured correctly.

## Primary references and related reading

Validate retained work and outputs in the optimized build; a benchmark barrier does not forbid every optimization of an expression. [Google Benchmark user guide](https://google.github.io/benchmark/user_guide.html); [GCC optimization options](https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html).

The proposed investigation of compiler eliminated microbenchmark work is not an executed deployment benchmark. Related reading: [GPU profiling: sampling and instrumentation observe different events](/journal/learn-gpu-profiler-sampling-instrumentation/); [A GPU inference benchmark you can trust](/journal/inference-benchmark-method/).
