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: Add emitters for int <-> float/usize conversions #94

Merged
merged 32 commits into from
Sep 12, 2024

Conversation

croyzor
Copy link
Contributor

@croyzor croyzor commented Sep 3, 2024

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.

@croyzor croyzor requested a review from a team as a code owner September 3, 2024 13:58
@croyzor croyzor requested a review from mark-koch September 3, 2024 13:58
src/emit/ops.rs Show resolved Hide resolved
@codecov-commenter
Copy link

codecov-commenter commented Sep 3, 2024

Codecov Report

Attention: Patch coverage is 84.43709% with 47 lines in your changes missing coverage. Please review.

Please upload report for BASE (main@aba5bd0). Learn more about missing BASE report.

Files with missing lines Patch % Lines
src/custom/conversions.rs 88.88% 18 Missing and 9 partials ⚠️
src/custom/int.rs 56.25% 12 Missing and 2 partials ⚠️
src/emit/func.rs 0.00% 3 Missing ⚠️
src/emit/ops.rs 50.00% 0 Missing and 2 partials ⚠️
src/custom/float.rs 95.00% 0 Missing and 1 partial ⚠️
Additional details and impacted files
@@           Coverage Diff           @@
##             main      #94   +/-   ##
=======================================
  Coverage        ?   82.26%           
=======================================
  Files           ?       20           
  Lines           ?     2972           
  Branches        ?     2972           
=======================================
  Hits            ?     2445           
  Misses          ?      333           
  Partials        ?      194           
Flag Coverage Δ
rust 82.26% <84.43%> (?)

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.

src/custom/conversions.rs Outdated Show resolved Hide resolved
src/custom/conversions.rs Outdated Show resolved Hide resolved
src/custom/conversions.rs Outdated Show resolved Hide resolved
src/custom/conversions.rs Outdated Show resolved Hide resolved
src/emit/ops.rs Show resolved Hide resolved
src/custom/conversions.rs Outdated Show resolved Hide resolved
@croyzor croyzor changed the title feat: Add emitters for int <-> float conversions feat: Add emitters for int <-> float/usize conversions Sep 9, 2024
@croyzor croyzor requested a review from mark-koch September 9, 2024 14:30

match conversion_op.def() {
ConvertOpDef::trunc_u => {
// This op should have one type arg only: the log-width of the
Copy link
Contributor

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?

self.build_trunc_op(false, log_width, args)
}

ConvertOpDef::trunc_s => {
Copy link
Contributor

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)
}

Copy link
Contributor Author

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.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// `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`.
Copy link
Contributor

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...

Copy link
Contributor Author

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... 🙃

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
/// 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`).

Copy link
Contributor

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?

Copy link
Contributor

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...

@croyzor croyzor requested a review from mark-koch September 11, 2024 08:59
#[rstest]
#[case("convert_u", 4)]
#[case("convert_s", 5)]
fn test_convert(mut llvm_ctx: TestContext, #[case] op_name: &str, #[case] width: u8) -> () {
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: width -> log_width

Copy link
Contributor

@mark-koch mark-koch left a 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

hugr_type: &CustomType,
) -> Result<BasicTypeEnum<'c>> {
Err(anyhow!(
"IntOpsCodegenExtension: unsupported type: {}",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"IntOpsCodegenExtension: unsupported type: {}",
"ConversionsCodegenExtension: unsupported type: {}",

6 => (64, (i64::MIN, i64::MAX), u64::MAX),
m => {
return Err(anyhow!(
"IntTypesCodegenExtension: unsupported log_width: {}",
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
"IntTypesCodegenExtension: unsupported log_width: {}",
"ConversionEmitter: unsupported log_width: {}",

@croyzor croyzor enabled auto-merge September 12, 2024 13:27
@croyzor croyzor disabled auto-merge September 12, 2024 13:28
@croyzor croyzor enabled auto-merge September 12, 2024 13:28
@croyzor croyzor added this pull request to the merge queue Sep 12, 2024
Merged via the queue into main with commit 1286f60 Sep 12, 2024
10 checks passed
@croyzor croyzor deleted the feat/conversions branch September 12, 2024 13:30
@hugrbot hugrbot mentioned this pull request Sep 12, 2024
github-merge-queue bot pushed a commit that referenced this pull request Sep 16, 2024
## 🤖 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/).
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.

4 participants