-
Notifications
You must be signed in to change notification settings - Fork 2
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: Add emitters for int <-> float/usize conversions #94
Conversation
Codecov ReportAttention: Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #94 +/- ##
=======================================
Coverage ? 82.26%
=======================================
Files ? 20
Lines ? 2972
Branches ? 2972
=======================================
Hits ? 2445
Misses ? 333
Partials ? 194
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. |
Co-authored-by: Mark Koch <[email protected]>
045257b
to
d517c08
Compare
src/custom/conversions.rs
Outdated
|
||
match conversion_op.def() { | ||
ConvertOpDef::trunc_u => { | ||
// This op should have one type arg only: the log-width of the |
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.
How about puttig this comment in the message from the panic
?
src/custom/conversions.rs
Outdated
self.build_trunc_op(false, log_width, args) | ||
} | ||
|
||
ConvertOpDef::trunc_s => { |
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.
You could do
ConvertOpDef::trunc_u | ConvertOpDef::trunc_s => {
let signed = conversion_op.def() == ConvertOpDef::trunc_s;
...
self.build_trunc_op(signed, log_width, args)
}
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.
Done
src/test.rs
Outdated
/// Lower `hugr` to LLVM, then JIT and execute the function named `entry` in | ||
/// the inner module. | ||
/// Lower `hugr` to LLVM, then JIT and execute the function named | ||
/// `entry_point` in the inner module. |
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.
/// `entry_point` in the inner module. | |
/// by `entry_point` in the inner module. |
I'm not quite sure what the "inner" "module" is (if AIUI we have a Hugr, not a Package, isn't that a/the-only module?), but nvm
/// | ||
/// That function must take no arguments and return an `i64`. | ||
/// That function must take no arguments and return an LLVM `i64`. |
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.
Seems odd that the function must return an i64
when this method is called exec_hugr_u64
...
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.
exec_hugr_u64
refers to the hugr u64 that's returned, which is converted to an LLVM i64 (LLVM doesn't distinguish u/i in its type names). I was trying to help the confusion with this change, but we're not there yet... 🙃
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.
/// That function must take no arguments and return an LLVM `i64`. | |
/// That function must take no arguments and return a Hugr `u64` (compiled to an LLVM `i64`). |
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.
No, hang on - Hugr doesn't distinguish these either, does it? So just exec_hugr_i64
?
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.
In fact this works fine for a Hugr returning, say, an int<0>
(i.e. 1-bit int) - presumably anything assignment-convertible to an LLVM i64
- so it should probably be called exec_hugr_int
...
src/custom/conversions.rs
Outdated
#[rstest] | ||
#[case("convert_u", 4)] | ||
#[case("convert_s", 5)] | ||
fn test_convert(mut llvm_ctx: TestContext, #[case] op_name: &str, #[case] width: u8) -> () { |
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.
nit: width
-> 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.
Looks good to me! I'm fine with doing the conversion unconditionally and leaving out branching for now. But maybe create an issue so we can discuss and not forget about it.
The only thing I'm still confused by is the weird behaviour of the float_roundtrip
test. No idea why you're getting 2^32. I'm happy to merge as is, but we should also create an issue to further investigate.
Also, it might be nice to have an execution test that covers the failure case. But only bother with this if it's not a big hassle since the IR looks ok
src/custom/conversions.rs
Outdated
hugr_type: &CustomType, | ||
) -> Result<BasicTypeEnum<'c>> { | ||
Err(anyhow!( | ||
"IntOpsCodegenExtension: unsupported type: {}", |
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.
"IntOpsCodegenExtension: unsupported type: {}", | |
"ConversionsCodegenExtension: unsupported type: {}", |
src/custom/conversions.rs
Outdated
6 => (64, (i64::MIN, i64::MAX), u64::MAX), | ||
m => { | ||
return Err(anyhow!( | ||
"IntTypesCodegenExtension: unsupported 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.
"IntTypesCodegenExtension: unsupported log_width: {}", | |
"ConversionEmitter: unsupported log_width: {}", |
## 🤖 New release * `hugr-llvm`: 0.4.0 -> 0.5.0 <details><summary><i><b>Changelog</b></i></summary><p> <blockquote> ## [0.5.0](v0.4.0...v0.5.0) - 2024-09-16 ### New Features - Add emitters for int <-> float/usize conversions ([#94](#94)) - [**breaking**] array ops ([#96](#96)) - Add conversions itobool, ifrombool ([#101](#101)) - Add `tket2` feature and lowerings for `tket2.rotation` extension ([#100](#100)) ### Testing - Add execution test framework ([#97](#97)) </blockquote> </p></details> --- This PR was generated with [release-plz](https://github.com/MarcoIeni/release-plz/).
Work towards #22, but since we updated to hugr 0.12.0 there are more operations in
arithmetic.conversions
. I'll implement those in a separate PR.