Skip to content

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

@JohanEngelen

Description

@JohanEngelen

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:

ldc/gen/dvalue.cpp

Lines 139 to 145 in 3acd761

if (isOptimizationEnabled()) {
// attach range metadata for i8 being loaded: [0, 2)
llvm::MDBuilder mdBuilder(gIR->context());
llvm::cast<llvm::LoadInst>(rval)->setMetadata(
llvm::LLVMContext::MD_range,
mdBuilder.createRange(llvm::APInt(8, 0), llvm::APInt(8, 2)));
}

ldc/gen/dvalue.cpp

Lines 242 to 248 in 3acd761

if (isOptimizationEnabled()) {
// attach range metadata for i8 being loaded: [0, 2)
llvm::MDBuilder mdBuilder(gIR->context());
llvm::cast<llvm::LoadInst>(rval)->setMetadata(
llvm::LLVMContext::MD_range,
mdBuilder.createRange(llvm::APInt(8, 0), llvm::APInt(8, 2)));
}

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...

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions