Skip to content

[REQ] Raise Clang optimization level to -O3 for CPU kernels #1310

Description

@c0d1f1ed

Description

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

  1. Raise default to -O3: Change the hardcoded -O2 flag in warp/native/clang/clang.cpp to -O3. Debug mode continues to use -O0.

  2. 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.

  3. 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 psutil REALTIME_PRIORITY_CLASS for scheduling stability. Run-to-run variability was measured at < 2% stddev for most examples under these conditions.

Testing

uv run python warp/tests/test_options.py

Activity

  1. added this to the 1.13.0 milestone on Mar 20, 2026
  2. self-assigned this
    on Mar 20, 2026
  3. 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
  4. c0d1f1ed commented on Mar 27, 2026

    @c0d1f1ed
    ContributorAuthor

    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.

  5. c0d1f1ed commented on Apr 3, 2026

    @c0d1f1ed
    ContributorAuthor

    Implemented in 0f69713

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

feature requestRequest for something to be added

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions