Stream: cranelift

Topic: Why use `i8` for booleans?


view this post on Zulip kmeakin (Mar 29 2023 at 23:21):

Why does cranelift use i8 for booleans instead of i1 like llvm does?

view this post on Zulip Chris Fallin (Mar 29 2023 at 23:49):

Good question! We had a dedicated bool type before, but we had persistent confusion about what the register-level value for true should be (1 or -1) and bugs related to that, and bools were always special, so we shifted to integers instead for any value that needs to be truthy. i1 would have some of the same weirdness -- can't store to memory, etc -- so we stuck with existing integer types. The ops that consume a truthy value, like select or brif, actually should accept any integer width (and test val != 0); icmp produces i8 because on x86 SETcc only sets the lower 8 bits of a register (ugh).

view this post on Zulip Chris Fallin (Mar 29 2023 at 23:51):

(we've talked about making icmp produce an i32 instead -- one could always narrow it back down if a one-byte value were needed -- as it aligns better with what Wasm expects hence reduces IR size out of the Wasm frontend -- but no recent progress on that)

view this post on Zulip kmeakin (Mar 29 2023 at 23:53):

If the boolean type was i1, then the truthy value would be both 1 and -1 ;)

view this post on Zulip Chris Fallin (Mar 29 2023 at 23:56):

That's certainly true!


Last updated: Dec 23 2024 at 12:05 UTC