ldc icon indicating copy to clipboard operation
ldc copied to clipboard

Defining what happens when a bool isn’t 0 or 1

Open JohanEngelen opened this issue 6 months ago • 2 comments

I just learned that there can be subtle bugs when assuming that a bool value is either 1 or 0. Read about it here: https://discourse.llvm.org/t/defining-what-happens-when-a-bool-isn-t-0-or-1/86778

We do this in (at least) two locations: https://github.com/ldc-developers/ldc/blob/3acd7617fcfee90470adfeb99da8b9f06a67ca01/gen/dvalue.cpp#L139-L145

https://github.com/ldc-developers/ldc/blob/3acd7617fcfee90470adfeb99da8b9f06a67ca01/gen/dvalue.cpp#L242-L248

I recreated the example from the LLVM post in D: https://d.godbolt.org/z/hsrErrGe3 (C code: https://godbolt.org/z/cofqzMooz) The assembly is the same but somehow the bug does not show up for LDC... huh? (3rd executor should output "0 0 0 100" when the bug happens) I don't understand why it is not showing up while the assembly code is nearly identical...

JohanEngelen avatar Jun 16 '25 22:06 JohanEngelen

The assembly is the same but somehow the bug does not show up for LDC... huh? (

You have selected a different compiler version in the executors (and missing option -O3). These are not set to use the latest CI build, but 1.40.1. If switched to latest CI, they show the same result as the C code. LDC 1.40.1 only uses the lowest bit as index.

rainers avatar Jun 17 '25 05:06 rainers

Thanks Rainer! It seems to require LLVM 20. https://d.godbolt.org/z/K168Y3T46

JohanEngelen avatar Jun 17 '25 17:06 JohanEngelen