<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/56017>56017</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            runtime error: load of value 0, which is not a valid value for type 'bool'
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
            PiJoules
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          PiJoules
      </td>
    </tr>
</table>

<pre>
    ```
union Flag {
    constexpr Flag() : empty{} {}

    struct {} empty;
    bool value;
};

int main() {
    static Flag flag;
    return flag.value;
}
```

When this is compiled with `-std=c++17 -O3 -fsanitize=undefined`, UBSan reports:

```
/tmp/test.cc:10:17: runtime error: load of value 0, which is not a valid value for type 'bool'
```

which doesn't make sense since 0 is one of the only two values a `bool` can be.

In the IR, `0` is passed directly to the function that generates this message:

```
tail call void @__ubsan_handle_load_invalid_value(ptr nonnull @2, i64 0) #2, !nosanitize !5
```

with `-O0`, the IR is:

```
define dso_local noundef i32 @main() #0 prologue <{ i32, i32 }> <{ i32 846595819, i32 trunc (i64 sub (i64 ptrtoint (ptr @0 to i64), i64 ptrtoint (ptr @main to i64)) to i32) }> {
  %1 = alloca i32, align 4
  store i32 0, ptr %1, align 4
  %2 = load i8, ptr @_ZZ4mainE4flag, align 1
  %3 = icmp ule i8 %2, 1, !nosanitize !6
  %4 = zext i8 %2 to i64, !nosanitize !6
  br i1 %3, label %6, label %5, !prof !7, !nosanitize !6

5:                                                ; preds = %0
  call void @__ubsan_handle_load_invalid_value(ptr @2, i64 %4) #2, !nosanitize !6
  br label %6, !nosanitize !6

6:                                                ; preds = %5, %0
  %7 = trunc i8 %2 to i1
  %8 = zext i1 %7 to i32
  ret i32 %8
}
```

which passes the actual byte value of the bool (`%4`) to `__ubsan_handle_load_invalid_value` if it fails the check. So somewhere in the optimization pass pipeline, this value is changed to zero which produces this weird message.
</pre>
<img width="1px" height="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