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

via llvm-commits llvm-commits at lists.llvm.org
Sat Aug 29 07:03:39 PDT 2026


ais523 wrote:

Re: retroactive reads of `poison`, my current mental model for this sort of retroactive change in multithreaded programs is that accesses that might potentially race can nondeterministically return `poison`, but any such execution that subsequently turns out not to lead to another thread racing on the address is "non-behavior" (i.e. the program has to be compiled in such a way that such executions cannot happen). As such, there isn't an actual retroactive change, just an event that cuts off possible branches of the space of possible executions.

I agree that there are potential out-of-thin-air problems in doing this sort of thing (and have not yet worked out whether they are serious or not). My hope is that whatever solution LLVM chooses for out-of-thin-air problems in multithreading, it will also solve any out-of-thin-air problems that are created by noalias (and that because the out-of-thin-air problem needs to be solved *anyway*, it is therefore not a problem to introduce new things that would depend on a solution). Additionally, I suspect that LLVM will, even if it introduces such out-of-thin-air cycles, do so only in situations where the original program had a `noalias` violation (although this is hard to prove and might be wrong).

An example of the sort of situation where this could be relevant (C-like pseudocode for LLVM IR, with `restrict` representing `noalias` and `freeze` being LLVM's freeze instruction):

```c
int foo(int *restrict a, int *restrict b) {
    if (freeze(*a) == 1) {
        *b = 2;
    }
}
```

If this is called with `a` and `b` both pointing to the same block of memory that contains 0, the desired outcome is that no memory changes (and because `b` was not actually accessed through, there is no `noalias` violation). In a "`noalias` violations are `poison`" world, there is a second consistent execution (in which the read of `a` reads `poison` that freezes as 1, then the assignment to `b` violates the `noalias`, justifying the `poison` read from `a`). This execution contains a causality cycle and thus creates `poison` from thin air, and would therefore need to be disallowed via whatever means is used to disallow executions of multithreaded programs that contain a similar cycle. (I'm not aware of any programs that could use `noalias` violations to create values other than `poison` out of thin air, although I haven't been able to rule out the possibility that they exist.)

----

On a separate note, an observation on the consequences of the change proposed in this PR (`dereferenceable` permitting a spurious read that conflicts with `noalias`) is that it becomes extremely hard to infer `dereferenceable` later in a function based on a `dereferenceable` at entry. The issue is that in code like this (with `restrict` representing `noalias` and `static` representing `dereferenceable`):

```c
extern void bar(int *restrict a);
int foo(int c[static 1]) {
    bar(*c);
    /* ... */
}
```

this change would make it undefined behavior for `foo` to spuriously read from `c` while `bar` was running (which can happen even in a single-threaded program, if `bar` gets inlined and a spurious read of `c` gets moved inside the inlined code). This means that no amount of `nofree`, `nosync`, `willreturn` etc. on `bar` is enough for the `dereferenceable` to exist inside `bar`, which in turn means that it's very hard to confine it to the portion of `foo` which can be proven to be outside `bar`.

I don't think this is necessarily fatal: either `bar` provably never writes to its argument, in which case there is no `noalias` conflict, or it cannot be proven to not write to its argument, in which case spuriously reading before `bar` returns would be pointless as the spuriously read value would be out of date anyway, so it may be possible to get some level of useful optimizations by temporarily disabling the `dereferenceable` but somehow "reviving" it after `bar` returns. This does seem pretty subtle and hard to implement correctly, though.

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


More information about the llvm-commits mailing list