Skip to content

Propagate physical comparison operators to lambda - #13588

Merged
OlivierNicole merged 4 commits into
ocaml:trunkfrom
hhugo:phys-comp
Apr 2, 2026
Merged

OlivierNicole merged 4 commits into
ocaml:trunkfrom
hhugo:phys-comp

Conversation

@hhugo

@hhugo hhugo commented Nov 2, 2024

Copy link
Copy Markdown
Contributor

Context

The two snippets below currently compile to the same bytecode
let f (a : int) b = a = b
let g a b = a == b

In js_of_ocaml, I want to compile theses two differently. The reason is that the === javascript operator doesn't match the behavior of the standard ocaml runtime.

For details, -0. === 0. returns true and NaN === NaN returns false. See ocsigen/js_of_ocaml#1372

This PR

This PR prepares the code base for the actual fix by forwarding more information to lambda.
This would allow #13438 to emit more hints or we could decide to change bytegen later to emit different code.

The main change in this PR is the addition of a primitive in lambda Pphyscomp of physical_comparison
and the following change in translprim.ml

-    "%eq", Primitive ((Pintcomp Ceq), 2);
-    "%noteq", Primitive ((Pintcomp Cne), 2);
+    "%eq", Primitive ((Pphyscomp CPeq), 2);
+    "%noteq", Primitive ((Pphyscomp CPneq), 2);

@hhugo

hhugo commented Dec 9, 2024

Copy link
Copy Markdown
Contributor Author

@OlivierNicole, do you still plan to review this PR ?

@OlivierNicole

Copy link
Copy Markdown
Contributor

Sorry for the delay.

As this depends on #13438 to be useful, we should probably decide on the fate of that one first.

@OlivierNicole OlivierNicole left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall this looks good to me modulo a small renaming suggestion in Instruct. This set of changes has the nice side effect that we will stop compiling (==) to Pintcomp, which I’ve always found a bit confusing.

Comment thread bytecomp/instruct.mli Resolved
Comment thread bytecomp/instruct.mli Resolved
Comment thread bytecomp/bytegen.ml Resolved
hhugo and others added 3 commits April 2, 2026 13:59
Separate the physical equality bytecode instruction from integer
comparison by introducing a dedicated Kphyscomp constructor, instead
of encoding it as Kintcomp with a boolean flag.

Co-Authored-By: Claude Opus 4.6 (1M context) <[email protected]>

@OlivierNicole OlivierNicole left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

LGTM. Could you add a changelog entry?

@hhugo

hhugo commented Apr 2, 2026

Copy link
Copy Markdown
Contributor Author

LGTM. Could you add a changelog entry?

done

@OlivierNicole
OlivierNicole merged commit c5b039b into ocaml:trunk Apr 2, 2026
25 checks passed
@OlivierNicole

Copy link
Copy Markdown
Contributor

Thanks!

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