On 24/02/2020 20:08, Andres Freund wrote: > Hi, > > On 2020-02-24 19:18:26 +0300, Pavel Begunkov wrote: >> On 24/02/2020 19:02, Jens Axboe wrote: >>>> Usually doesn't work because of such possible "hackier assignments". >>>> Ok, I have to go and experiment a bit. Anyway, it probably generates a lot of >>>> useless stuff, e.g. for req->ctx >>> >>> Tried this, and it generates the same code... >> >> Maybe it wasn't able to optimise in the first place >> >> E.g. for the following code any compiler generates 2 reads (thanks godbolt). >> >> extern void foo(int); >> int bar(const int *v) >> { >> foo(*v); >> return *v; >> } > > Yea, the compiler really can't assume anything for this kind of > thing. > a) It's valid C to cast away the const here, as long as it's guaranteed > that v isn't pointing to to actually const memory. > b) foo() could actually have access to *v without the argument, > e.g. through a global. > and even in the case of a const member of a struct, as far as I know > it's legal to change the values, as long as the allocation isn't const. Yep, regular stuff. And that's why I want to find a way to force compilers to think otherwise. e.g. kind of __attribute__ -- Pavel Begunkov