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

Added Cmm_helpers.Scalar_type #3423

Draft
wants to merge 16 commits into
base: main
Choose a base branch
from
Draft

Added Cmm_helpers.Scalar_type #3423

wants to merge 16 commits into from

Conversation

jvanburen
Copy link
Contributor

Cmm_helpers.Scalar_type provides utilities for converting between integers types of different widths and signedness. This is in preparation for adding unboxed small integer types.

@jvanburen jvanburen requested review from mshinwell and Gbury and removed request for mshinwell January 3, 2025 16:04
@mshinwell mshinwell added the cmm Cmm language / helpers changes label Jan 15, 2025
Copy link

Selection Change Check

This PR modifies the original selection pass (targeting Mach).
Please check whether the changes should also be applied to the
CFG variant of the pass.

@jvanburen jvanburen force-pushed the cmm-refactor-unboxed-fields branch from 16f1b23 to 443a81c Compare January 29, 2025 17:14
@jvanburen jvanburen force-pushed the cmm-refactor-unboxed-fields branch from 443a81c to 2f5d60b Compare February 13, 2025 22:45
Base automatically changed from cmm-refactor-unboxed-fields to main February 18, 2025 16:42
@jvanburen jvanburen force-pushed the cmm-scalar-type branch 2 times, most recently from 1d28ccf to e856bc9 Compare February 18, 2025 17:47
@jvanburen jvanburen requested a review from TheNumbat February 21, 2025 20:53
@TheNumbat TheNumbat changed the base branch from main to refactor-cmm-for-more-integer-widths February 25, 2025 19:02
Copy link
Contributor

@TheNumbat TheNumbat 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, I'm confident the conversions are correct. Have you checked that this doesn't hurt codegen for static casts? We don't have a good way to test that automatically, but spot-checking a few things seems sufficient.

@jvanburen
Copy link
Contributor Author

Looks good, I'm confident the conversions are correct. Have you checked that this doesn't hurt codegen for static casts? We don't have a good way to test that automatically, but spot-checking a few things seems sufficient.

I noticed that the generated Cors could mess up the generation of lea for tagging, so I fixed that case in instruction selection.
Otherwise I didn't see anything meaningful.

@mshinwell
Copy link
Collaborator

Should we e.g. examine the actual generated assembly for a few executables before and after this change? I think probably yes.

Base automatically changed from refactor-cmm-for-more-integer-widths to main March 5, 2025 16:57
@jvanburen jvanburen marked this pull request as draft March 5, 2025 20:57
let nativeint = Untagged Integer.nativeint

let[@inline] untagged = function
| Untagged t -> t
Copy link
Contributor

Choose a reason for hiding this comment

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

Are there legitimate uses where we try and untag an already untagged integral type ? That seems like a bug to me at first glance, so unless we have some actual uses cases (and they are correct), we should probably raise a fatal_error here since this may indicate a bug elsewhere ?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes, this is so that you can cast to the untagged representation of a type

Copy link
Contributor Author

Choose a reason for hiding this comment

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

it's not for asserting that we know a type is untagged

Copy link
Contributor

Choose a reason for hiding this comment

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

Yes, this is so that you can cast to the untagged representation of a type

When do we legitimately need to "cast to the untagged representation of a type" when that type is already untagged ?

Put another way: I'm arguing that this line should read Untagged _ -> assert false and that this assert should not trigger since untagging something that is already untagged has no clearly defined semantics and actually indicates a bug/error. Can you give a detailed explanation of at least one case where that line of code is effectively used, and why do we need it / why the code is better (or simpler) that way ?

Copy link
Contributor

Choose a reason for hiding this comment

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

From what I read, the only use of that function is in cast_cast_without_losing_information , where the actual information that is of interest is the number of bits actually used to store information, so I think it should be better to rearrange the code a bit to not need this function (and instead use a function that returns the actually use-for-information bit-width for instance).

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(C.Scalar_type.Integral.untagged kind)

it's useful b/c you can static_cast or conjugate to untagged kind , and it's just a no-op if the integer is already untagged.

Misc.fatal_errorf
"static_cast: casting floats to unsigned values is undefined"
| Signed ->
(* we can truncate, but we don't want to promote *)
Copy link
Contributor

Choose a reason for hiding this comment

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

why ?

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 clarified the comments

@Gbury
Copy link
Contributor

Gbury commented Mar 6, 2025

Also: there's a CI failure that seems related to this PR: Fatal error: exception Stdlib__Domain.Encapsulated("Invalid_argument(\"Random.int32_in_range\")") on chi2.ml in the upstream testsuite

Copy link
Contributor

@Gbury Gbury left a comment

Choose a reason for hiding this comment

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

Should I wait until you fix the CI failures to review again ?

@jvanburen jvanburen force-pushed the cmm-scalar-type branch 3 times, most recently from 8a57568 to 5a2037e Compare March 10, 2025 14:18
This will be used by a future pr to simplify casting without impacting
performance as much
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cmm Cmm language / helpers changes needs testing
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants