Skip to content

Conversation

@mratsim
Copy link
Owner

@mratsim mratsim commented Oct 21, 2025

See #582, downstream status-im/nimbus-eth1#3587 and #581.

GCC hardstops on current ARM64 inline ASM and cannot take full advantage of ARM hardware, which has 31 registers (+ the zero register) as it doesn't accept more than 30 register operands, with input/output registers being counted twice.

This reduces register usage count by 2 at the price of less instruction-level parallelism exposed. Hopefully impact is negligeable. Benchmarks pending.

cc @advaita-saha, etan-status

@gemini-code-assist
Copy link

Summary of Changes

Hello @mratsim, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a known GCC limitation concerning the number of register operands allowed in ARM64 inline assembly. The changes involve refactoring the assembly macros for Montgomery multiplication and reduction to reduce the overall register count. This ensures compatibility with GCC compilers while carefully considering and aiming to minimize any potential performance trade-offs related to instruction-level parallelism.

Highlights

  • GCC Compatibility: Modified ARM64 inline assembly to resolve a GCC limitation regarding the maximum number of register operands, which previously caused compilation issues.
  • Register Usage Optimization: Reduced the number of temporary registers used in Montgomery multiplication and reduction macros (mulMont_CIOS_sparebit_gen, sumprodMont_CIOS_spare2bits_gen, and redc2xMont_gen) by removing redundant variables and aliasing where possible.
  • Instruction-Level Parallelism (ILP) Consideration: Acknowledged that the reduction in register usage might slightly decrease instruction-level parallelism, but the impact is expected to be negligible, with benchmarks pending to confirm.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request correctly addresses a GCC compiler limitation on ARM64 that restricts the number of register operands in inline assembly. The changes reduce the register count by removing temporary registers used for breaking dependency chains and by aliasing registers where possible. This is a necessary fix for GCC compatibility. You've correctly identified the potential performance trade-off (reduced instruction-level parallelism) and have documented it well in the code. The changes are consistent and look solid.

Comment on lines 298 to +299
bi = scratch[4] # Stores b[i] during mul and u during reduction
m = scratch[5] # Red step: (t[0] * m0ninv) mod 2ʷ
m = scratch[4] # Red step: (t[0] * m0ninv) mod 2ʷ

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The comment for bi on line 298 appears to be outdated. It mentions that scratch[4] stores u during reduction, but u is now assigned to scratch[5]. Since scratch[4] is now reused for m, I suggest updating the comment for bi to reflect its actual usage and avoid potential confusion.

    bi = scratch[4]                                  # Stores b[i] during mul
    m = scratch[4]                                   # Red step: (t[0] * m0ninv) mod 2ʷ

@mratsim
Copy link
Owner Author

mratsim commented Oct 21, 2025

New assembly nimble bench_summary_bls12_381

Untitled 4 Untitled 5

It seems like there is a 4% performance impact on scalar multiplication (BLS signature) and 5.5% performance impact on pairing (BLS verification)

@mratsim
Copy link
Owner Author

mratsim commented Oct 21, 2025

Changing #581 ARM ASM CI to run this PR to confirm if #582 is fixed. Afterwards, we can test whether we can get away with saving only 1 register in multiplication instead of 2.

@mratsim
Copy link
Owner Author

mratsim commented Oct 21, 2025

It seems like we saved enough registers in the Montgomery multiplication but not in the fused sumproduct or the Montgomery reduction :/

image

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants