public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
From: Jingwen Wu <elowen.jjw@gmail.com>
To: gcc@gcc.gnu.org
Subject: issue: unexpected results in optimizations
Date: Tue, 12 Dec 2023 01:07:50 +0800	[thread overview]
Message-ID: <CAJAwgAxf9SE2YjE7d_ewiVFcctxKHe-U+ehdUOWTQWSceO+6eg@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 774 bytes --]

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 *-O2* optimization level, while the results of both were the same
when using other levels of optimization such as *-O0*, *-O1*, *-O3*, *-Os*,
*-Ofast*.
Please help me to explain why this is, thank you.

command line: *gcc file.c -O2 -lm -I $CSMITH_HOME/include && ./a.out*
version: gcc 12.2.0
os: ubuntu 22.04

[-- Attachment #2: initial.c --]
[-- Type: text/x-c-code, Size: 1866 bytes --]


#include "csmith.h"

static int32_t g_a321[3];
static int64_t g_b321[3];

union U0 {
  const int32_t f0;
};

union U1 {
  const uint16_t f0;
  const uint64_t f1;
  int32_t f2;
  const signed f3 : 29;
};

union U2 {
  int64_t f0;
  int16_t f1;
};

static uint32_t g_39 = 0x78C76445L;
static union U0 g_87 = {0xA9ACF304L};
static union U0 *g_89[1][3][2] = {{{&g_87, &g_87}, {&g_87, &g_87}, {&g_87, &g_87}}};
static union U0 **volatile g_88[3][1] = {{&g_89[0][2][1]}, {&g_89[0][2][1]}, {&g_89[0][2][1]}};
static int32_t g_165 = 2L;
static union U2 g_261[3] = {{0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}};
static union U1 g_412 = {3UL};

static void func_1(void);

static void func_1() {
  uint8_t l_19 = 1UL;
  for (l_19 = 0; (l_19 <= 2); l_19 += 1) {
    int i, j;
    for (i = 0; i < 1; i++) {
      for (j = 0; j < 3; j++) {
      }
    }
    int ii_7;
    // fusion in add execTimes
    for (g_39 = 0, ii_7 = 0; (g_39 <= 1); g_39 += 1, ii_7++) {
      g_a321[ii_7] = (*g_89[0][2][0]).f0 * l_19 + g_165;
    }
    int jj_7;
    for (jj_7 = 0; jj_7 < 3; jj_7++) {
      g_b321[jj_7] = g_261[1].f0 * g_a321[jj_7] + g_412.f2;
    }
  }
}

int main(void) {
  int i;
  int print_hash_value = 0;
  platform_main_begin();
  crc32_gentab();
  func_1();
  transparent_crc(g_39, "g_39", print_hash_value);
  transparent_crc(g_87.f0, "g_87.f0", print_hash_value);
  transparent_crc(g_165, "g_165", print_hash_value);
  for (i = 0; i < 3; i++) {
    transparent_crc(g_261[i].f0, "g_261[i].f0", print_hash_value);
  }
  transparent_crc(g_412.f0, "g_412.f0", print_hash_value);
  for (i = 0; i < 3; i++) {
    transparent_crc(g_a321[i], "g_a321[i]", print_hash_value);
  }
  for (i = 0; i < 3; i++) {
    transparent_crc(g_b321[i], "g_b321[i]", print_hash_value);
  }
  platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
  return 0;
}

[-- Attachment #3: transformed.c --]
[-- Type: text/x-c-code, Size: 1966 bytes --]


#include "csmith.h"

static int32_t g_a321[3];
static int64_t g_b321[3];

union U0 {
  const int32_t f0;
};

union U1 {
  const uint16_t f0;
  const uint64_t f1;
  int32_t f2;
  const signed f3 : 29;
};

union U2 {
  int64_t f0;
  int16_t f1;
};

static uint32_t g_39 = 0x78C76445L;
static union U0 g_87 = {0xA9ACF304L};
static union U0 *g_89[1][3][2] = {{{&g_87, &g_87}, {&g_87, &g_87}, {&g_87, &g_87}}};
static union U0 **volatile g_88[3][1] = {{&g_89[0][2][1]}, {&g_89[0][2][1]}, {&g_89[0][2][1]}};
static int32_t g_165 = 2L;
static union U2 g_261[3] = {{0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}, {0xDF9C6E8BCC63CCAELL}};
static union U1 g_412 = {3UL};

static void func_1(void);

static void func_1() {
  uint8_t l_19 = 1UL;
  for (l_19 = 0; (l_19 <= 2); l_19 += 1) {
    int i, j;
    for (i = 0; i < 1; i++) {
      for (j = 0; j < 3; j++) {
      }
    }
    int ii_7;
    int jj_7;
    int ij_7;
    // fusion in add execTimes
    for (g_39 = 0, ii_7 = 0, jj_7 = 0, ij_7 = 0; ij_7 < 5; ij_7++) {
      if (ij_7 <= 2 && (g_39 <= 1)) {
        g_a321[ii_7] = (*g_89[0][2][0]).f0 * l_19 + g_165;
        g_39 += 1;
        ii_7++;
      } else {
        jj_7 = ij_7 - 2;
        g_b321[jj_7] = g_261[1].f0 * g_a321[jj_7] + g_412.f2;
      }
    }
  }
}

int main(void) {
  int i;
  int print_hash_value = 0;
  platform_main_begin();
  crc32_gentab();
  func_1();
  transparent_crc(g_39, "g_39", print_hash_value);
  transparent_crc(g_87.f0, "g_87.f0", print_hash_value);
  transparent_crc(g_165, "g_165", print_hash_value);
  for (i = 0; i < 3; i++) {
    transparent_crc(g_261[i].f0, "g_261[i].f0", print_hash_value);
  }
  transparent_crc(g_412.f0, "g_412.f0", print_hash_value);
  for (i = 0; i < 3; i++) {
    transparent_crc(g_a321[i], "g_a321[i]", print_hash_value);
  }
  for (i = 0; i < 3; i++) {
    transparent_crc(g_b321[i], "g_b321[i]", print_hash_value);
  }
  platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
  return 0;
}

             reply	other threads:[~2023-12-11 17:08 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-12-11 17:07 Jingwen Wu [this message]
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
  -- strict thread matches above, loose matches on Subject: below --
2023-12-11 17:14 Jingwen Wu
2023-12-12  8:39 ` David Brown
2023-12-13 18:49   ` James K. Lowden
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=CAJAwgAxf9SE2YjE7d_ewiVFcctxKHe-U+ehdUOWTQWSceO+6eg@mail.gmail.com \
    --to=elowen.jjw@gmail.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).