Stream: bugs

Topic: Weird behaviour with dict and U128


view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 17:37):

So I am using a dictionary and using a type {U64, U8} as key, then it works, however if I just changed type to {U128,U8} I start to get some really weird issues... It says keys exist that shouldnt exist. However if I change the code to all using U64 it runs flawlessly. Code used is here, Warning for spoiler for AoC day 11: https://gist.github.com/jonwarghed/d5606997fb705f5ce2a3c09a3ab98705

view this post on Zulip Anton (Dec 11 2024 at 18:28):

Thanks for reporting this @Jon Erik Kemi Warghed
There is a lot of output if I execute roc test, both when using U128 and U64, can you narrow this down a bit?

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 18:44):

We may just hash u128 wrong.

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 18:47):

Sure let me see if I can make it more easily reproducible as well.

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 18:57):

Code is not much easier, but the output should be clearer, please let me know, I am kinda new in Roc. So not easy to force types correctly yet for a minimal example :)

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 18:58):

image.png

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 19:00):

This is the input? 0 1 10 99 999

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 19:01):

also, what type of machine are you on?

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 19:01):

Ah sorry example input is 125 17

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 19:02):

❯ uname -a
Linux jotunheim 6.6.63 #1-NixOS SMP PREEMPT_DYNAMIC Fri Nov 22 14:38:37 UTC 2024 x86_64 GNU/Linux

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 19:03):

thanks. Seems target specific. Works on mac. So probably is alignment or some silly call conv thing.

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 19:03):

on m1 mac see:

────────────────────────────────────────────────────────────────────────────────
[/tmp/bug.roc:80] ("Inserting", curr, result) = ("Inserting", {pebble: 125, target: 1}, 1)
[/tmp/bug.roc:87] ("Inserting", curr, r) = ("Inserting", {pebble: 17, target: 1}, 2)
[/tmp/bug.roc:103] end_memory.brain = {{pebble: 125, target: 1}: 1, {pebble: 17, target: 1}: 2}
── EXPECT FAILED in /tmp/bug.roc ───────────────────────────────────────────────

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 19:06):

possible, let me know if I should do anything else.

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 19:07):

I'll test on my linux box. See if I can repro

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 19:07):

yeah, I can repro

view this post on Zulip Jon Erik Kemi Warghed (Dec 11 2024 at 19:08):

Cool, I have a devenv.lock if needed.

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 20:12):

Woah, this looks to be a List.append bug.

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 20:12):

That or an inspect printout bug

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 20:14):

This is logging from the list insertion in the dictionary:

[/tmp/MyDict.roc:466] data0 = [({pebble: 125, target: 1}, 1)]
[/tmp/MyDict.roc:467] ("Inserting", key, value) = ("Inserting", {pebble: 17, target: 1}, 2)
[/tmp/MyDict.roc:469] data1 = [({pebble: 125, target: 1}, 1), ({pebble: 322962136477993009152, target: 0}, 36893488147419103233)]
[/tmp/MyDict.roc:471] List.get data1 dataIndex = (Ok ({pebble: 322962136477993009152, target: 0}, 36893488147419103233))

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 20:14):

So somehow these types break List.append.

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 20:52):

Seems like we might be writting at the wrong offset. 322962136477993009152 constains 17 along with garbage. 36893488147419103233 contains both 1 and 2

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 21:36):

Minimal repro is literally just this:

app [main] { pf: platform "https://github.com/roc-lang/basic-cli/releases/download/0.17.0/lZFLstMUCUvd5bjnnpYromZJXkQUrdhbva4xdBInicE.tar.br" }

import pf.Stdout

main =
    x = []
    Stdout.line! (Inspect.toStr x)
    a = ({pebble: 125u128, target: 1u8}, 1u128)
    Stdout.line! "Inserting: $(Inspect.toStr a)"
    y = List.append x a
    Stdout.line! (Inspect.toStr y)
    b = ({pebble: 17, target: 1}, 2)
    Stdout.line! "Inserting: $(Inspect.toStr b)"
    z = List.append y b
    Stdout.line! (Inspect.toStr z)

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 22:14):

Oh, this is just llvm thinking that i128 should be aligned to 8 bytes instead of 16

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 22:15):

So it ends up calculating the gep instruction wrong

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 22:15):

If you assume alignment of 8, { { i128, i8 }, i128 } has a size of 40. If the alignment is 16, the size is 48

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 22:16):

This honestly might be a pain to fix. Is a version of llvm with i128 alignment changed to 16 released yet? If so, updating to that version of llvm may be the best fix (work we already have to do anyway).

view this post on Zulip Sam Mohr (Dec 11 2024 at 22:18):

Looks like LLVM 20 should have it?

view this post on Zulip Sam Mohr (Dec 11 2024 at 22:20):

The v18 update has been a good amount of work, we should try to separately upgrade from LLVM 18 to 20

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 22:23):

Really, not until 20? The rust issue related to this thought it should get release in 18.

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 22:26):

Also, saw some micro benchmarks showing the alignment leading to a 12% perf gain. Clearly, these targeted alignment related issues, but just surprising a bit. Looking at rust projects updating to 1.78.0. some got perf gains of up to 20%. but most got 0% to 5% gains.

view this post on Zulip Sam Mohr (Dec 11 2024 at 22:32):

Let me get a specific answer, this was from loose GH issue investigating

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 23:56):

https://godbolt.org/z/c5vaPd9or

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 23:56):

It is fixed in 18

view this post on Zulip Brendan Hansknecht (Dec 11 2024 at 23:58):

we should try to separately upgrade from LLVM 18 to 20

Also, I think 20 is trunk. 19 is the newest release.

view this post on Zulip Brendan Hansknecht (Dec 12 2024 at 02:02):

Also, can confirm that this bug is fixed with https://github.com/roc-lang/roc/pull/6921

view this post on Zulip Brendan Hansknecht (Dec 13 2024 at 04:12):

This should be fixed on main now


Last updated: Jul 06 2025 at 12:14 UTC