Skip to content

Conversation

@Vindaar
Copy link
Collaborator

@Vindaar Vindaar commented Oct 14, 2025

Decided to put a small typo fix of in-place pow for finite fields into this PR, because I noticed it in the same context and it's one character change.

On the GPU compiler side this:

  • adds support for all uint literal types
  • simplifies how we register types in the Nim -> GPU Ast transformation. We now do this in one place only, namely in nimToGpuType. We needed to pass the GPU context into this proc, which initially I didn't intend to do. But the code was getting a bit ugly with maybeAddType calls all over the place.

Instead of having calls to `maybeAddType` all over the place, we now
pass the GPU context into the `nimToGpuType` function and call the
helper to register types in there. That makes the most sense and is
the cleanest.

Initially I was against this, because it was nice that ~nimToGpuType~
was a pure function without dependence on any kind of context
state. But the upside is worth it and we don't really need the
function in another context anyway.
@gemini-code-assist
Copy link

Summary of Changes

Hello @Vindaar, 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 primarily enhances the GPU compiler's type handling by centralizing the registration of types during the Nim to GPU AST transformation, making the process more robust and easier to maintain. Additionally, it expands the compiler's capability to correctly interpret various unsigned integer literals. A minor, yet important, typo in a finite field exponentiation function has also been addressed, improving the correctness of mathematical operations.

Highlights

  • Finite Field pow Fix: Corrected a one-character typo in the pow function for finite fields, ensuring the result is assigned to the correct variable r instead of a.
  • Centralized GPU Type Registration: Streamlined the GPU Abstract Syntax Tree (AST) transformation process by modifying nimToGpuType and related functions to consistently accept a GpuContext parameter, centralizing all type registration logic within the nimToGpuType procedure itself.
  • Expanded uint Literal Support: The GPU compiler now supports a wider range of unsigned integer literal types, including nnkUIntLit, nnkUint64Lit, nnkUInt16Lit, and nnkUInt32Lit, improving the compiler's ability to handle various integer literals.
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 introduces a few valuable changes. It fixes a critical bug in the pow function for finite fields, where the exponentiation was performed on the input argument instead of the result variable. It also extends support for more unsigned integer literal types in the GPU compiler. Finally, it refactors the type registration logic in the Nim to GPU AST transformation, centralizing it into nimToGpuType for better maintainability. The refactoring is well-executed. I have one suggestion to reduce code duplication in the handling of new unsigned integer literals.

Comment on lines +1133 to +1144
of nnkUIntLit, nnkUint64Lit:
result = GpuAst(kind: gpuLit)
result.lValue = $node.intVal
result.lType = initGpuType(gtUInt32)
of nnkUIntLit:
result.lType = initGpuType(gtUInt64) ## XXX: base on target platform!
of nnkUInt16Lit:
result = GpuAst(kind: gpuLit)
result.lValue = $node.intVal
result.lType = initGpuType(gtUInt64) ## XXX: base on target platform!
result.lType = initGpuType(gtUInt16) ## XXX: base on target platform!
of nnkUInt32Lit:
result = GpuAst(kind: gpuLit)
result.lValue = $node.intVal
result.lType = initGpuType(gtUInt32) ## XXX: base on target platform!

Choose a reason for hiding this comment

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

medium

These blocks for handling unsigned integer literals are very similar. You can reduce code duplication by using a template or a helper procedure. For example, you could define a template inside toGpuAst:

template uintLit(typ: GpuTypeKind) =
  result = GpuAst(kind: gpuLit)
  result.lValue = $node.intVal
  result.lType = initGpuType(typ) ## XXX: base on target platform!

And then use it like this:

of nnkUIntLit, nnkUint64Lit:
  uintLit(gtUInt64)
of nnkUInt16Lit:
  uintLit(gtUInt16)
of nnkUInt32Lit:
  uintLit(gtUInt32)

This would make the code more concise and easier to maintain.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah, I know I could bot. :)

Copy link
Owner

@mratsim mratsim left a comment

Choose a reason for hiding this comment

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

LGTM apart from the default value which should be 32-bit for int, uint float I think.

## ``exponent``: a finite field element or big integer
r = a
a.pow(exponent)
r.pow(exponent)
Copy link
Owner

Choose a reason for hiding this comment

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

Crazy that this wasn't tested

result.lValue = $node.intVal
result.lType = initGpuType(gtUInt32)
of nnkUIntLit:
result.lType = initGpuType(gtUInt64) ## XXX: base on target platform!
Copy link
Owner

Choose a reason for hiding this comment

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

uint should probably be mapped to uint32 by default as well on GPU.

result.lType = initGpuType(gtUInt32) ## XXX: base on target platform!
of nnkFloat64Lit, nnkFloatLit:
result = GpuAst(kind: gpuLit)
result.lValue = $node.floatVal & "f"
Copy link
Owner

Choose a reason for hiding this comment

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

iirc, f is float32, and default float in C/Metal/Cuda is float32 not float64 iirc

@Vindaar
Copy link
Collaborator Author

Vindaar commented Oct 18, 2025

While I agree in principle about the type mappings, in practice I'm starting to consider the opposite approach. Because the code is part of a typed macro and we want the Nim compile time guarantees, if we map int, uint, float to different types than in regular Nim, we deal with many very annoying type mismatch bugs on the Nim side. In the end one anyways has to to all sorts of manual type conversions. Say if int is int32 then it should be fine to add an int to an int32 variable, or assign int to int32. but of course that's not valid Nim.

So I'm considering to keep Nim semantics (which this PR of course doesn't do yet) and document that.

@mratsim
Copy link
Owner

mratsim commented Oct 21, 2025

Let's merge it in like that for now then.

I do think it would be quite verbose to always read u32 / f32 in GPU code but:

  • we probably would write generic algorithms most of the time and leave instantiation type to the caller
  • we can create converters template pragma, for example {.conv32.} that signals that all types are 32-bit within the proc scope.

@mratsim mratsim merged commit 8e117d7 into master Oct 21, 2025
16 checks passed
@mratsim mratsim deleted the minorGpuCompilerImprovements branch October 21, 2025 07:14
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.

3 participants