From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from ciao.gmane.io (ciao.gmane.io [116.202.254.214]) by sourceware.org (Postfix) with ESMTPS id 407A53858C78 for ; Tue, 12 Dec 2023 08:40:07 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 407A53858C78 Authentication-Results: sourceware.org; dmarc=fail (p=quarantine dis=none) header.from=westcontrol.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=m.gmane-mx.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 407A53858C78 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=116.202.254.214 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702370409; cv=none; b=O2qzKnbnXd+xgG2rQPCCnod6c3OfzPoqtt1KlawN2CYjmBkLs/3S/NAsStq5IwTLdiebi0FnZ/NCHdoQ8PLm+JMHhLr6iYi9K3jCoIrQ6cE1/CI9c9Yb9/MzmlU/qk4JqQ9IDEE4e4v2++SHUo184gI3uYufPddOrNtuoLJA4+4= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1702370409; c=relaxed/simple; bh=cNaRni1ILGfZjdkkSIq2lMzmu/e7qSpuvukuEVLBSqk=; h=To:From:Subject:Date:Message-ID:Mime-Version; b=EGv9I0RtNfgRi3Af4MIyTF/0vH5ngPebIW27760z+GnlmZeEAwdpV059x8ZxOzWU386D59OL92sO0SS3tau2ScDP36boiqazU2Wy/AqtHSkhixciSL5JC4W3qNV04baE+NsqqbDv+axit+oIi3iCMhj4iJpif++0NbXvZGZPZzQ= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1rCyJA-0000OK-Vt for gcc@gcc.gnu.org; Tue, 12 Dec 2023 09:40:04 +0100 X-Injected-Via-Gmane: http://gmane.org/ To: gcc@gcc.gnu.org From: David Brown Subject: Re: issue: unexpected results in optimizations Date: Tue, 12 Dec 2023 09:39:58 +0100 Message-ID: References: Mime-Version: 1.0 Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0 Content-Language: en-GB In-Reply-To: X-Spam-Status: No, score=-1.7 required=5.0 tests=BAYES_00,FORGED_MUA_MOZILLA,HEADER_FROM_DIFFERENT_DOMAINS,KAM_DMARC_STATUS,NICE_REPLY_A,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: 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 , 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