Skip to content

JIT: compile kernels with -fno-math-errno, so gcc can merge repeated sqrt/pow/exp calls #834

Open

Description

@lmoresi

Problem

The JIT compiles its pointwise kernels at -O3 -g0 with the platform's compiler. gcc defaults to -fmath-errno: sqrt, pow and exp may set errno, so gcc treats each call as having a side effect. It cannot merge repeated calls with the same argument, and it cannot inline sqrt without a fallback call. An expanded constitutive law repeats the same call many times, once in each place a named quantity appears. Apple clang never sets errno (the Darwin maths library does not), so kernels built on macOS do not pay this cost and kernels built on Linux do. Our kernels never read errno.

Measurement

Linux, the environment's conda-forge gcc 14.3, 2× Xeon Gold 6240R, on a machine loaded by other jobs (the times are noisy; the ratios repeat). The -fmath-errno effect is the cost of the law's pointwise callbacks per quadrature point in one Jacobian assembly, measured by swapping in a constant viscosity on the same mesh and state (scripts/sessions/jit_graph/assembly_split.py on feature/jit-graph-codegen):

fixture default flags -fno-math-errno
viscoplastic box (Drucker-Prager yield, temperature-dependent viscosity, floors) 8.2 µs 2.8 µs (a repeat in reverse order: 8.3 / 2.9)
visco-elasto-plastic box 2.7 µs 3.1 µs (no effect within the noise)

With and without the flag, the assembled residual and Jacobian at a fixed state, the solutions, the residual histories and the nonlinear and linear iteration counts are bit-identical, on both fixtures. The generated C does not change.

Proposal

Always pass -fno-math-errno, alongside -std=c99, so that it is kept when UW3_JIT_CFLAGS replaces the optimisation flags. The flags are written into the generated setup.py, which is part of the JIT cache key, so every cached module recompiles once after the change.

Found while measuring the JIT graph route (#823, tier 2).

Activity

  1. added a commit that references this issue on Oct 10, 2026
  2. lmoresi commented on Oct 10, 2026

    @lmoresi
    MemberAuthor

    Fixed by #835 (merged to development, 76bafe4): the JIT's default compile flags are now -O3 -g0 -fno-math-errno; UW3_JIT_CFLAGS replaces them (-std=c99 stays).

  3. lmoresi commented on Oct 10, 2026

    @lmoresi
    MemberAuthor

    Reopened under the issue rule of 2026-10-11 (PR #839): an issue whose fix has merged to development stays open, labelled fixed-in-development, and closes when the fix is released to main. Fixed by #835 (76bafe4).

  4. added
    fixed-in-developmentFix merged to development; the issue closes when it is released to main
    on Oct 10, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or requestfixed-in-developmentFix merged to development; the issue closes when it is released to maintype:codeCode implementation review

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions