Hacktoberfest 2026: the issues maintainers tagged for October, open and beginner-friendly. Browse Hacktoberfest issues

perf: GPT-6.1 Sol says compiler optimization speeds up SHA-256 by 2.36×

Open
#15,641 1 comment 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
5/5
Estimated time
Over a week
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Active
Domain
compilers

Research direction

Start with the prototype in src/Lean/Compiler/LCNF/ScalarArgs.lean, then read its integration in src/Lean/Compiler/LCNF/Passes.lean and the benchmark in tests/bench/sha256/Sha256ProbeBaseline.lean. Review how the pass handles scalar type selection, recursive calls, and wrappers, and run the SHA-256 benchmark and relevant compiler tests. Done means the prototype has received review and testing sufficient to establish correctness and performance.

Written by the indexing model from the issue text.

Description

I don't fully understand the details, but the improvement seems significant, so I am submitting this issue. I hope it would help your development. Feel free to close this issue if it not so helpful.

GPT-6.1 Sol (Medium) + Codex App


Title: Compiler optimization speeds up SHA-256 by 2.36×

I (GPT-6.1 Sol) implemented a prototype compiler optimization that speeds up a SHA-256 benchmark by 2.36×, without changing the original Lean SHA-256 code.

The implementation is on the opt_sha256 branch. The relevant files are:

  • ScalarArgs.lean: the new optimization pass, including scalar-type selection, argument splitting, recursive-call rewriting, and wrapper generation.
  • Passes.lean: integration into the monomorphic optimization pipeline.
  • Sha256ProbeBaseline.lean: the SHA-256 block-processing code, including the loop with eight mutable UInt32 variables.

The SHA-256 block-processing loop maintains eight mutable UInt32 variables representing its working state. Lean carries this state through recursive calls as nested products. Even after specialization and tail-recursion lowering, the generated C retains tuple projections, field updates, and reference-counting/reuse checks on each iteration. Object reuse reduces allocations, but the remaining operations still introduce substantial overhead.

In my local benchmark, runtime decreased from 42.69 ms to 18.05 ms, approximately 58% less time. The difference persists when the generated C is compiled with -O3.

The transformation replaces recursive product arguments with separate scalar arguments.

The following pseudocode illustrates the transformation after simplification; it is not actual Lean code or a literal dump of the generated C.

Before:
  loop(i, state):
    (a, (b, (c, d))) = state
    ...
    return loop(i + 1, (a', (b', (c', d'))))

After:
  worker(i, a, b, c, d):
    ...
    return worker(i + 1, a', b', c', d')

This example shows four variables for readability; the SHA-256 loop carries eight. The worker passes updated scalar values directly to the next iteration, eliminating nested tuple packing and unpacking inside the loop.

A wrapper preserves the original function interface. Tuple construction and projection become local to the worker, allowing the existing simplifier to eliminate them before reference-counting insertion. In the optimized SHA-256 loop, the generated C carries eight uint32_t values between iterations and constructs the result tuple at termination.

The prototype restricts eligible field types to known scalar types supported by existing code generation. It also limits product size and expanded argument count. It does not attempt to solve general structure-layout optimization.

Similar loop-state overhead was discussed in the 2023 Zulip thread “two RBMaps”. This prototype automates an execution shape similar to the suggested handwritten tail-recursion workaround.

This implementation is a prototype and needs further review and testing.

Dominant language
Lean
Stars
9.7k
Forks
1.1k
Avg merge
1d 17m
Merged PRs (30d)
296

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from leanprover/lean4

All issues in leanprover/lean4

Similar issues

More Compilers issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.