[llvm] [LangRef] Specify that dereferenceable implies a spurious read (PR #218413)

via llvm-commits llvm-commits at lists.llvm.org
Fri Aug 28 08:59:34 PDT 2026


ais523 wrote:

> unless we can come up with some magic way to have noalias with poison on load instead of IUB, which appears to be impossible.

I believe that the magic in question is already implemented in LLVM: aren't the rules for `noalias` violations the same as those for race conditions in multithreaded programs? I think it's possible to implement `noalias` as follows: for each `noalias` parameter, imagine you created a thread for that parameter; then all accesses through that parameter (and pointers derived from it) are done as though you communicated to that thread to ask it to perform the access for you (without synchronization, i.e. with relaxed synchronization rules, but you wait for the reply before continuing). 

For example, this solves [RalfJung's example from the forum thread](https://discourse.llvm.org/t/interaction-of-noalias-and-dereferenceable/66979/19):

```c
void test(noalias int *ptr1, dereferenceable(4) int *ptr2) {
  int x = *ptr2;
  int b = freeze(poison);
  if (b) {
    *ptr1 = 0;
  } else {
    printf("%d\n", x);
  }
}
```

Imagine that the `*ptr1 = 0;` line is implemented by asking another thread to set `*ptr1` to 0 via setting a relaxed-atomic variable that both threads can see. Here, if `b`, there is a race condition between the write to `ptr1` and the read from `ptr2`, which retroactively causes `x` to be poison, whereas if not `b`, there is no race condition, and thus `x` is not poison. As such. the speculative read of `x` is legal, even though it requires retroactivity to work correctly, because the relaxed-atomic communication provides the right amount of retroactivity.

https://github.com/llvm/llvm-project/pull/218413


More information about the llvm-commits mailing list