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

case of integer matching does not work correctly for larger numbers #8493

Closed
radeusgd opened this issue Dec 7, 2023 · 7 comments · Fixed by #8510
Closed

case of integer matching does not work correctly for larger numbers #8493

radeusgd opened this issue Dec 7, 2023 · 7 comments · Fixed by #8510
Assignees
Labels
--bug Type: bug -compiler p-high Should be completed in the next sprint

Comments

@radeusgd
Copy link
Member

radeusgd commented Dec 7, 2023

Repro:

from Standard.Base import all

foo x = case x of
    1 -> "foo: ONE"
    2 -> "foo: TWO"
    200 -> "foo: TWO HUNDRED"
    9999 -> "foo: NINE THOUSAND NINE HUNDRED NINETY NINE"
    _ -> "foo: OTHER - "+x.to_text

main =
    IO.println "foo:"
    IO.println (foo 1)
    IO.println (foo 2)
    IO.println (foo 200)
    IO.println (foo 9999)
    IO.println (foo 3)

Actual behaviour

It gives:

foo:
foo: ONE
foo: TWO
foo: OTHER - 200
foo: OTHER - 9999
foo: OTHER - 3

missing the 200 and 9999 branches.

Expected behaviour

foo:
foo: ONE
foo: TWO
foo: TWO HUNDRED
foo: NINE THOUSAND NINE HUNDRED NINETY NINE
foo: OTHER - 3
@radeusgd
Copy link
Member Author

radeusgd commented Dec 7, 2023

I suspect we are likely performing a == on boxed Long values. For small values, the JVM uses the AutoBoxCache to re-use the boxed values. For larger values, new instances get created and they are no longer ==.

@radeusgd
Copy link
Member Author

radeusgd commented Dec 7, 2023

@radeusgd
Copy link
Member Author

radeusgd commented Dec 7, 2023

So applying the following patch

Index: engine/runtime/src/main/java/org/enso/interpreter/node/controlflow/caseexpr/NumericLiteralBranchNode.java
IDEA additional info:
Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP
<+>UTF-8
===================================================================
diff --git a/engine/runtime/src/main/java/org/enso/interpreter/node/controlflow/caseexpr/NumericLiteralBranchNode.java b/engine/runtime/src/main/java/org/enso/interpreter/node/controlflow/caseexpr/NumericLiteralBranchNode.java
--- a/engine/runtime/src/main/java/org/enso/interpreter/node/controlflow/caseexpr/NumericLiteralBranchNode.java	(revision 2182072c7cad5725e3a5187e7c68e2ff02f03190)
+++ b/engine/runtime/src/main/java/org/enso/interpreter/node/controlflow/caseexpr/NumericLiteralBranchNode.java	(revision 2ff98076e0244074a88ffdfa937d77413dc58442)
@@ -42,7 +42,7 @@
       Object state,
       Object target,
       @CachedLibrary(limit = "1") InteropLibrary interop) {
-    if (numProfile.profile(literal == target)) accept(frame, state, new Object[0]);
+    if (numProfile.profile(literal.equals(target))) accept(frame, state, new Object[0]);
   }
 
   @Fallback

seems to fix the issue.

@JaroslavTulach do you think that fix seems OK?

I'm happy to integrate it as part of my PR for ticket #8352 as I need it there to fix HTTP status code translation.

radeusgd added a commit that referenced this issue Dec 7, 2023
radeusgd added a commit that referenced this issue Dec 7, 2023
@radeusgd radeusgd added p-high Should be completed in the next sprint --bug Type: bug -compiler labels Dec 7, 2023
@radeusgd radeusgd linked a pull request Dec 7, 2023 that will close this issue
5 tasks
radeusgd added a commit that referenced this issue Dec 8, 2023
radeusgd added a commit that referenced this issue Dec 8, 2023
@hubertp
Copy link
Collaborator

hubertp commented Dec 8, 2023

@radeusgd I'd rather we do it in a separate PR (as @JaroslavTulach mentioned, there are problems with native image build in that PR).

@JaroslavTulach
Copy link
Member

Yes, numProfile.profile(literal == target) seems to be wrong. CCing @hubertp - alas I believe that using equals isn't particularly PE friendly and your CI shall fail in engine-runner/buildNativeImage.

For long and double values we should be using PrimitiveValueProfile (or implement something like that manually). For BigInteger we can fast track == and then .equals behind @TruffleBoundary.

do it in a separate PR

Yes, this is not going to be straightforward fix.

@radeusgd
Copy link
Member Author

radeusgd commented Dec 8, 2023

Yes, numProfile.profile(literal == target) seems to be wrong. CCing @hubertp - alas I believe that using equals isn't particularly PE friendly and your CI shall fail in engine-runner/buildNativeImage.

For long and double values we should be using PrimitiveValueProfile (or implement something like that manually). For BigInteger we can fast track == and then .equals behind @TruffleBoundary.

do it in a separate PR

Yes, this is not going to be straightforward fix.

Okay. I guess I will be figuring out a workaround for my PR then, or will be waiting with integration until a fix.

@radeusgd radeusgd removed a link to a pull request Dec 8, 2023
5 tasks
@JaroslavTulach JaroslavTulach moved this from ❓New to 👁️ Code review in Issues Board Dec 11, 2023
@github-project-automation github-project-automation bot moved this from 👁️ Code review to 🟢 Accepted in Issues Board Dec 12, 2023
@enso-bot
Copy link

enso-bot bot commented Dec 12, 2023

Jaroslav Tulach reports a new STANDUP for yesterday (2023-12-11):

Progress: - fix of #8493 by #8510

  • emails
  • advent of code
  • Wojciech: review of my GeeCON presentation It should be finished by 2023-12-11.

Next Day: Meetings, planning

mergify bot pushed a commit that referenced this issue Dec 15, 2023
- Closes #8352
- ~~Proposed fix for #8493~~
- The temporary fix is deemed not viable. I will try to figure out a workaround and leave fixing #8493 to the engine team.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
--bug Type: bug -compiler p-high Should be completed in the next sprint
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

3 participants