Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(hugr-llvm): Emit divmod and mod operations #2025

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open

Conversation

croyzor
Copy link
Contributor

@croyzor croyzor commented Mar 25, 2025

Relates to #1702 .

Copy link

codecov bot commented Mar 25, 2025

Codecov Report

Attention: Patch coverage is 94.40559% with 16 lines in your changes missing coverage. Please review.

Project coverage is 83.40%. Comparing base (41905f9) to head (57823a8).
Report is 3 commits behind head on main.

Files with missing lines Patch % Lines
hugr-llvm/src/extension/int.rs 94.40% 2 Missing and 14 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2025      +/-   ##
==========================================
- Coverage   83.79%   83.40%   -0.39%     
==========================================
  Files         211      213       +2     
  Lines       39757    40482     +725     
  Branches    36435    36696     +261     
==========================================
+ Hits        33314    33764     +450     
- Misses       4567     4829     +262     
- Partials     1876     1889      +13     
Flag Coverage Δ
python 85.84% <ø> (-6.40%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@croyzor croyzor marked this pull request as ready for review March 25, 2025 15:27
@croyzor croyzor requested a review from a team as a code owner March 25, 2025 15:27
@croyzor croyzor requested a review from aborgna-q March 25, 2025 15:27
Comment on lines +741 to +754
// Helper to get the log width arg to an int op when it's the only argument
// panic if there's not exactly one nat arg
fn get_width_arg<H: HugrView<Node = Node>>(args: &EmitOpArgs<'_, '_, ExtensionOp, H>) -> u64 {
let [arg] = args
.node
.args()
.to_owned()
.try_into()
.expect("Exactly one type argument to int op");
let TypeArg::BoundedNat { n: log_width } = arg else {
panic!("Unexpected non-nat arg to int op");
};
log_width
}
Copy link
Collaborator

Choose a reason for hiding this comment

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

This is used in various other places (conversions.rs:156 and int.rs:359,377,396,549 on main). Should we rename this to get_int_width and call it on those places to?

Also, no need to clone the array,

let [TypeArg::BoundedNat { n }] = args.node.args() else { bail!(...) }
*n

should do the trick.

Comment on lines 767 to 769
let lower_bounds_check = if signed {
ctx.builder()
.build_int_compare(IntPredicate::SGT, denominator, zero, "valid_div")
Copy link
Collaborator

Choose a reason for hiding this comment

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

This caught me by surprise. Why are we doing "signed dividend, unsigned divisor" in div/mod ???

The docs say that the operand may be any non-zero unsigned value,

idivmod_s => "given signed integer -2^{N-1} <= n < 2^{N-1} and unsigned 0 <= m < 2^N, generates \
signed q and unsigned r where q*m+r=n, 0<=r<m (m=0 will call panic)",

so to be correct in the lowering we need to allow any non-zero value here and return.

Even more, LLVM's signed remainder is not modulo; its result has the same sign as the dividend.
To lower things correctly here we need to do a lot of extra logic

$$ \texttt{hugr-divmod-s}(n: signed, m: unsigned) = $$ $$ \begin{cases} (0, n) \qquad\qquad\qquad\qquad\qquad\qquad & \text{if } m \geq 2^{N-1} \text{ and } n>0\\ (-1, r-n) & \text{if } m \geq 2^{N-1} \text{ and } n<0\\ (\texttt{llvm-signed-div}(n,m), &\\ \quad \texttt{llvm-signed-rem}(n,m)) & \text{if } m < 2^{N-1} \text{ and } n>0\\ (\texttt{llvm-signed-div}(n,m), 0) & \text{if } m < 2^{N-1} \text{ and } n<0 \text{ and } \texttt{llvm-signed-rem}(n,m)= 0 \\ (\texttt{llvm-signed-div}(n,m)-1, & \\ \quad n-\texttt{llvm-signed-rem}(n,m)) & \text{if } m < 2^{N-1} \text{ and } n<0 \text{ and } \texttt{llvm-signed-rem}(n,m) \neq 0 \end{cases} $$

💀

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think we can write the second case ($m \geq 2^{N -1} \text{ and } n &lt; 0$) as (-1, n + m).
qm + r = qm + n + m where q = -1 gets us back n more simply

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Plus the last case should be (llvm-signed-div(n, m) - 1, m - llvm-signed-rem(n,m)) (m instead of n in the snd element)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Actually the last case should be llvm-signed-div(n, m) - 1, m + llvm-signed-rem(n, m)) because the result of llvm-signed-rem will be negative to match the dividend

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