-
Notifications
You must be signed in to change notification settings - Fork 8
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
base: main
Are you sure you want to change the base?
Conversation
Codecov ReportAttention: Patch coverage is
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
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
// 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 | ||
} |
There was a problem hiding this comment.
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.
hugr-llvm/src/extension/int.rs
Outdated
let lower_bounds_check = if signed { | ||
ctx.builder() | ||
.build_int_compare(IntPredicate::SGT, denominator, zero, "valid_div") |
There was a problem hiding this comment.
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,
hugr/hugr-core/src/std_extensions/arithmetic/int_ops.rs
Lines 194 to 195 in 41905f9
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
💀
There was a problem hiding this comment.
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 ((-1, n + m)
.
qm + r = qm + n + m
where q = -1
gets us back n
more simply
There was a problem hiding this comment.
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)
There was a problem hiding this comment.
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
Relates to #1702 .