You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[REQ] Raise Clang optimization level to -O3 for CPU kernels #1310
Raise the default embedded Clang/LLVM optimization level for CPU kernel compilation from -O2 to -O3 to enable more aggressive loop unrolling, instruction scheduling, and SLP (superword-level parallelism) vectorization of scalar instruction sequences.
Context
Benchmarking Newton examples on CPU showed that -O3 produces a small but consistent improvement over -O2 when host CPU feature detection is enabled (#1308). The median gain is ~1% with individual examples reaching 4-5%.
The improvement does not come from loop auto-vectorization. An experiment disabling vectorization entirely (-fno-vectorize -fno-slp-vectorize) while keeping native CPU features showed no measurable difference from the vectorization-enabled build. This means there are future gains that could come from using -O3 after the generated code is made more vectorization-friendly.
Changes
Raise default to -O3: Change the hardcoded -O2 flag in warp/native/clang/clang.cpp to -O3. Debug mode continues to use -O0.
Make configurable via warp.config.optimization_level: The existing config setting and per-module option (previously GPU-only) are wired through to the CPU build path, so users can dial back to -O2 if needed. The wp_compile_cpp() C function accepts an optimization_level parameter (0-3) that selects the corresponding -O flag.
Fix module hash: Extract a _resolve_optimization_level() helper so the None -> default resolution is defined in one place, used by both ModuleHasher and Module.build_module(). This ensures the kernel cache
is properly invalidated when the optimization level changes, and that None and the explicit default produce the same hash.
Benchmarking methodology
Newton CPU benchmarks were run using --benchmark 10 with psutilREALTIME_PRIORITY_CLASS for scheduling stability. Run-to-run variability was measured at < 2% stddev for most examples under these conditions.
changed the title [-][REQ] Raise Clang frontend optimization level to -O3 for CPU kernels[/-][+][REQ] Raise Clang optimization level to -O3 for CPU kernels[/+]on Mar 24, 2026
Further analysis of the implementation and the internals of Clang/LLVM revealed that -O3 and -O2 have few differences. -O2 is the standard level of optimization, well-balanced to avoid optimizations that may use compromises that don't result in a net gain. -O3's aggressiveness can backfire.
We still want the user to have control over it through config.optimization_level. But None should mean 2 for CPU kernels, keeping 3 as the default for CUDA kernels.
Activity
[-][REQ] Raise Clang frontend optimization level to -O3 for CPU kernels[/-][+][REQ] Raise Clang optimization level to -O3 for CPU kernels[/+]c0d1f1ed commentedon Mar 27, 2026
Further analysis of the implementation and the internals of Clang/LLVM revealed that -O3 and -O2 have few differences. -O2 is the standard level of optimization, well-balanced to avoid optimizations that may use compromises that don't result in a net gain. -O3's aggressiveness can backfire.
We still want the user to have control over it through
config.optimization_level. ButNoneshould mean2for CPU kernels, keeping3as the default for CUDA kernels.c0d1f1ed commentedon Apr 3, 2026
Implemented in 0f69713
Make CPU kernel optimization level configurable, default to -O2 [NVIDâĶ
Merge branch 'ncapens/clang-o3' into 'main'