public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: David Brown <david@westcontrol.com>
To: gcc@gcc.gnu.org
Subject: Re: issue: unexpected results in optimizations
Date: Tue, 12 Dec 2023 09:39:58 +0100	[thread overview]
Message-ID: <ul968v$v7$1@ciao.gmane.io> (raw)
In-Reply-To: <CAJAwgAxMAAY6AvVwSjUvJ2cPkFi9fqj8Nii1qaaHxER5ChEjzw@mail.gmail.com>

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



  reply	other threads:[~2023-12-12  8:40 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-11 17:14 Jingwen Wu
2023-12-12  8:39 ` David Brown [this message]
2023-12-13 18:49   ` James K. Lowden
  -- strict thread matches above, loose matches on Subject: below --
2023-12-11 17:07 Jingwen Wu
2023-12-11 17:31 ` Dave Blanchard
2023-12-12  8:29   ` Jonathan Wakely
2023-12-12  9:02 ` Jonathan Wakely
2023-12-12 11:08   ` Alexander Monakov
2023-12-11 17:05 Jingwen Wu
2023-12-11 16:51 Jingwen Wu

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to='ul968v$v7$1@ciao.gmane.io' \
    --to=david@westcontrol.com \
    --cc=gcc@gcc.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).