Add fast_divmod magic-number division helper - #906
Open
kashif wants to merge 3 commits into
Open
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
Adds a host/device “magic-number” division utility to avoid expensive integer division sequences on GPUs, exposing both a low-level divmod function and a convenience wrapper for constant divisors. This fits into FlyDSL’s expr numeric utilities and is intended to help kernel scheduling/indexing code where divisors are only known at launch.
Changes:
- Added
fastdivmod_magic(divisor)host helper to compute(magic, shift)for0 <= n < 2**31. - Added
fast_divmod(dividend, divisor, magic, shift)device-side divmod implementation plusFastDivmodwrapper for compile-time constant divisors. - Added unit + device tests validating the host math and GPU behavior against native
//and%.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
python/flydsl/expr/numeric.py |
Implements fastdivmod_magic, fast_divmod, and FastDivmod in the numeric DSL layer. |
python/flydsl/expr/typing.py |
Re-exports the new helpers/types through flydsl.expr’s public surface (__all__). |
tests/unit/test_fast_divmod.py |
Adds L0 host verification and L2 GPU kernels comparing results to native divmod. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
coderfeli
reviewed
Jul 27, 2026
Collaborator
|
This is not enough. |
Author
|
thanks for the pointer! checking |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Adds a magic-number division helper so a runtime divisor becomes a multiply and two shifts instead of a hardware integer divide, which GPUs do not have.
fastdivmod_magic(divisor)precomputes the(magic, shift)pair on the host.fast_divmod(dividend, divisor, magic, shift)does the device-side divmod, andFastDivmodwraps both for a compile-time constant divisor. It is valid for non-negative dividends below 2^31, the same contract ascutlass::FastDivmod.The intended use is tile schedulers that do one divmod per work tile from a grid dimension known only at launch, where the compiler cannot fold the divisor. For a single divmod the generated code on gfx1150 drops from 57 to 25 instructions.
Tests check the host magic against Python
//and%over a range of divisors, plus device tests comparingfast_divmodand theFastDivmodclass to native divmod on GPU.