Hi,
First, please ignore everything Dave Blanchard writes. I don't know
why, but he likes to post angry, rude and unhelpful messages to this list.
Secondly, this is the wrong list. gcc-help would be the correct list,
as you are asking for help with gcc. This list is for discussions on
the development of gcc.
Thirdly, if you want help, you need to provide something that other
people can comprehend. There is very little that anyone can do to
understand lumps of randomly generated code, especially when it cannot
compile without headers and additional files or libraries that we do not
have.
So your task is to write a /minimal/ piece of stand-alone code that
demonstrates the effect that concerns you. It is fine to use standard
headers like <stdint.h>, but no external headers like this "csmith"
stuff. Aim to make it small enough to be included directly in the text
of the post, not as an attachment. Include the compiler version(s) you
tried, the command line flags, what you expect the results to give, and
what wrong results you got.
Always do development compiles with comprehensive sets of warnings. I
managed to take a section of your code (part that was different between
the "initial.c" and "transformed.c") and compile it - there were lots of
warnings. There are a lot of overflows in initialisations, pointless
calculations on the left of commas, and other indications of badly
written code. There were also static warnings about undefined behaviour
in some calculations - and that, most likely, is key.
When code has undefined behaviour, you cannot expect the compiler to
give any particular results. It's all down to luck. And luck varies
with the details, such as optimisation levels. It's "garbage in,
garbage out", and that is the explanation for differing results.
So compile with "-Wall -Wextra -std=c99 -Wpedantic -O2" and check all
the warnings. (Static warnings work better when optimising the code.)
If you have fixed the immediate problems in the code, add the
"-fsanitize=undefined" flag before running it. That will do run-time
undefined behaviour checks.
If you have a code block that is small enough to comprehend, and that
you are now confident has no undefined behaviour, and you get different
results with different optimisations, post it to the gcc-help list.
Then people can try it and give opinions - maybe there is a gcc bug.
I hope that all helps.
David
On 11/12/2023 18:14, Jingwen Wu via Gcc wrote:
Hello, I'm sorry to bother you. And I have some gcc compiler optimization
questions to ask you.
First of all, I used csmith tools to generate c files randomly. Meanwhile,
the final running result was the checksum for global variables in a c file.
For the two c files in the attachment, I performed the equivalent
transformation of loop from *initial.**c* to *transformed.c*. And the two
files produced different results (i.e. different checksum values) when
using *-Os* optimization level, while the results of both were the same
when using other levels of optimization such as *-O0*, -O1, -O2, -O3,
*-Ofast*.
Please help me to explain why this is, thank you.
command line: *gcc file.c -Os -lm -I $CSMITH_HOME/include && ./a.out*
version: gcc 12.2.0
os: ubuntu 22.04