GMP test fails with -flto
Vincent Lefevre
vincent at vinc17.net
Wed Jul 3 09:46:34 UTC 2019
On 2019-07-03 10:45:24 +0200, Richard Biener wrote:
> On Wed, Jul 3, 2019 at 9:49 AM Vincent Lefevre <vincent at vinc17.net> wrote:
> > Here, after loop enrolling, the compiler could see that the arguments
> > are known and generate a fixed puts(). This kind of optimization must
> > not be done with a structure declared as volatile. But I think that
> > data in the structure could be rearranged (what matters is that when
> > run, the program will still do memory accesses and the behavior will
> > be the same); that's rather unlikely, though.
>
> Could happen though but making the structure object volatile might help
> (the compiler could still see you don't use threads and it's regular data
> and nothing can happen and thus ignore even that...).
C99 says: "An object that has volatile-qualified type may be modified
in ways unknown to the implementation or have other unknown side
effects. Therefore any expression referring to such an object shall
be evaluated strictly according to the rules of the abstract machine,
as described in 5.1.2.3."
No need to use threads. A debugger is sufficient to modify a volatile
object (this is a way "unknown to the implementation").
Note that since there are no other side effects when reading volatile
data here (standard, readable memory), a read is required to be
generated only if the value (which is unknown, as it could have been
modified in a way unknown to the implementation) can affect the output
(which the printf does here).
> The only way to avoid it would be to do sth like
>
> for (i = 0; i < 8; i++)
> if (argv[1][2*i])
> foo.before[i] = foo.after[i] = ((char *)&foo.x)[i] = argv[1][2*i+1];
>
> thus conditionally(! otherwise the compiler might optimize away the
> initializer...) overwrite it from program input.
I don't understand what you want to do here.
> There's also the possibility of the compiler altering the structure
> layout...
Yes. But I think that a copy of the structure and reading the copy
should be sufficient. And all types (at least those related to the
structure) should be volatile-qualified.
--
Vincent Lefèvre <vincent at vinc17.net> - Web: <https://www.vinc17.net/>
100% accessible validated (X)HTML - Blog: <https://www.vinc17.net/blog/>
Work: CR INRIA - computer arithmetic / AriC project (LIP, ENS-Lyon)
More information about the gmp-bugs
mailing list