Issue 75592
Summary unexpected output results in fusion using -Os
Labels new issue
Assignees
Reporter Elowen-jjw
    For the following two c files, I performed the equivalent transformation of loop from **file1.c** to **file2.c**. I ran these two files respectively using different optimization levels including **-O0**, **-O1**, **-O2**, **-O3**, **-Os**, **-Ofast**. The exact output results(i.e. checksum values) are:
```
                -O0 -O1             -O2             -O3             -Os             -Ofast 
file1.c        6BB53573        64EEDCA5        78F90205 78F90205        6BB53573        78F90205     
file2.c        6BB53573 64EEDCA5        78F90205        78F90205        64EEDCA5        78F90205 
```
 
Please help me to explain why these two files produced different output results when using the same optimization level(i.e. -Os), thank you.

command line:
```
clang <filename.c> <optimization level> -lm -I $CSMITH_HOME/include && ./a.out
```
version: clang+llvm 14.0.0
os: ubuntu 22.04

**file1.c**
```

#include "csmith.h"

static int32_t g_a96[4];
static uint64_t g_b96[4];

struct S0 {
  volatile uint16_t f0;
  int32_t f1;
 unsigned f2 : 24;
  int64_t f3;
};

struct S1 {
  unsigned f0 : 6;
  unsigned f1 : 5;
  signed f2 : 15;
  volatile uint8_t f3;
 signed f4 : 12;
};

static uint64_t g_35[1][2][1] = {{{0xE3C489718B2F1036LL}, {0xE3C489718B2F1036LL}}};
static struct S1 g_46[1][2][2] = {{{{6, 2, 127, 0x92L, -50}, {6, 2, 127, 0x92L, -50}}, {{6, 2, 127, 0x92L, -50}, {6, 2, 127, 0x92L, -50}}}};
static volatile struct S0 g_53 = {0x4032L, 0xAA3630DDL, 2867, 0x930CB0470BDE7D1ELL};
static struct S0 g_72 = {1UL, 0xCEF6D7B3L, 1180, 0xCAAAB22D3B6D3E8DLL};
static struct S1 *g_200 = &g_46[0][1][1];
static struct S1 **g_199[3][3][1] = {{{&g_200}, {&g_200}, {&g_200}}, {{&g_200}, {&g_200}, {&g_200}}, {{&g_200}, {&g_200}, {&g_200}}};

static void func_1(void);

static void func_1(void) {
  int32_t l_5 = 0L;
  int i;
  int ii_0;
  // fusion
  for (i = 0, ii_0 = 0; i < 3; i++, ii_0++) {
    g_a96[ii_0] = g_72.f0 * l_5 + g_53.f1;
  }
  int jj_0;
  for (jj_0 = 0; jj_0 < 4; jj_0++) {
 g_b96[jj_0] = (**g_199[2][2][0]).f0 * g_a96[jj_0] + g_35[0][0][0];
 }
}

int main(void) {
  int i, j, k;
  int print_hash_value = 0;
  platform_main_begin();
  crc32_gentab();
  func_1();
  for (i = 0; i < 1; i++) {
    for (j = 0; j < 2; j++) {
      for (k = 0; k < 1; k++) {
        transparent_crc(g_35[i][j][k], "g_35[i][j][k]", print_hash_value);
      }
    }
  }
  for (i = 0; i < 1; i++) {
    for (j = 0; j < 2; j++) {
      for (k = 0; k < 2; k++) {
 transparent_crc(g_46[i][j][k].f0, "g_46[i][j][k].f0", print_hash_value);
        transparent_crc(g_46[i][j][k].f1, "g_46[i][j][k].f1", print_hash_value);
 transparent_crc(g_46[i][j][k].f2, "g_46[i][j][k].f2", print_hash_value);
        transparent_crc(g_46[i][j][k].f3, "g_46[i][j][k].f3", print_hash_value);
 transparent_crc(g_46[i][j][k].f4, "g_46[i][j][k].f4", print_hash_value);
      }
    }
  }
  transparent_crc(g_53.f0, "g_53.f0", print_hash_value);
  transparent_crc(g_53.f1, "g_53.f1", print_hash_value);
  transparent_crc(g_53.f2, "g_53.f2", print_hash_value);
  transparent_crc(g_53.f3, "g_53.f3", print_hash_value);
  transparent_crc(g_72.f0, "g_72.f0", print_hash_value);
  transparent_crc(g_72.f1, "g_72.f1", print_hash_value);
  transparent_crc(g_72.f2, "g_72.f2", print_hash_value);
  transparent_crc(g_72.f3, "g_72.f3", print_hash_value);
  for (i = 0; i < 4; i++) {
 transparent_crc(g_a96[i], "g_a96[i]", print_hash_value);
  }
  for (i = 0; i < 4; i++) {
    transparent_crc(g_b96[i], "g_b96[i]", print_hash_value);
  }
  platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
  return 0;
}

```

**file2.c**
```

#include "csmith.h"

static int32_t g_a96[4];
static uint64_t g_b96[4];

struct S0 {
  volatile uint16_t f0;
  int32_t f1;
 unsigned f2 : 24;
  int64_t f3;
};

struct S1 {
  unsigned f0 : 6;
  unsigned f1 : 5;
  signed f2 : 15;
  volatile uint8_t f3;
 signed f4 : 12;
};

static uint64_t g_35[1][2][1] = {{{0xE3C489718B2F1036LL}, {0xE3C489718B2F1036LL}}};
static struct S1 g_46[1][2][2] = {{{{6, 2, 127, 0x92L, -50}, {6, 2, 127, 0x92L, -50}}, {{6, 2, 127, 0x92L, -50}, {6, 2, 127, 0x92L, -50}}}};
static volatile struct S0 g_53 = {0x4032L, 0xAA3630DDL, 2867, 0x930CB0470BDE7D1ELL};
static struct S0 g_72 = {1UL, 0xCEF6D7B3L, 1180, 0xCAAAB22D3B6D3E8DLL};
static struct S1 *g_200 = &g_46[0][1][1];
static struct S1 **g_199[3][3][1] = {{{&g_200}, {&g_200}, {&g_200}}, {{&g_200}, {&g_200}, {&g_200}}, {{&g_200}, {&g_200}, {&g_200}}};

static void func_1(void);

static void func_1(void) {
  int32_t l_5 = 0L;
  int i;
  int ii_0;
  int jj_0;
  int ij_0;
  // fusion
  for (i = 0, ii_0 = 0, jj_0 = 0, ij_0 = 0; ij_0 <= 4; ij_0++) {
    if (ij_0 <= 3 && i < 3) {
      g_a96[ii_0] = g_72.f0 * l_5 + g_53.f1;
      i++;
      ii_0++;
    }
    if (ij_0 <= 4 && jj_0 < 4) {
      g_b96[jj_0] = (**g_199[2][2][0]).f0 * g_a96[jj_0] + g_35[0][0][0];
      jj_0++;
    }
  }
}

int main(void) {
  int i, j, k;
  int print_hash_value = 0;
 platform_main_begin();
  crc32_gentab();
  func_1();
  for (i = 0; i < 1; i++) {
    for (j = 0; j < 2; j++) {
      for (k = 0; k < 1; k++) {
        transparent_crc(g_35[i][j][k], "g_35[i][j][k]", print_hash_value);
      }
    }
  }
  for (i = 0; i < 1; i++) {
    for (j = 0; j < 2; j++) {
      for (k = 0; k < 2; k++) {
 transparent_crc(g_46[i][j][k].f0, "g_46[i][j][k].f0", print_hash_value);
        transparent_crc(g_46[i][j][k].f1, "g_46[i][j][k].f1", print_hash_value);
 transparent_crc(g_46[i][j][k].f2, "g_46[i][j][k].f2", print_hash_value);
        transparent_crc(g_46[i][j][k].f3, "g_46[i][j][k].f3", print_hash_value);
 transparent_crc(g_46[i][j][k].f4, "g_46[i][j][k].f4", print_hash_value);
      }
    }
  }
  transparent_crc(g_53.f0, "g_53.f0", print_hash_value);
  transparent_crc(g_53.f1, "g_53.f1", print_hash_value);
  transparent_crc(g_53.f2, "g_53.f2", print_hash_value);
  transparent_crc(g_53.f3, "g_53.f3", print_hash_value);
  transparent_crc(g_72.f0, "g_72.f0", print_hash_value);
  transparent_crc(g_72.f1, "g_72.f1", print_hash_value);
  transparent_crc(g_72.f2, "g_72.f2", print_hash_value);
  transparent_crc(g_72.f3, "g_72.f3", print_hash_value);
  for (i = 0; i < 4; i++) {
 transparent_crc(g_a96[i], "g_a96[i]", print_hash_value);
  }
  for (i = 0; i < 4; i++) {
    transparent_crc(g_b96[i], "g_b96[i]", print_hash_value);
  }
  platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
  return 0;
}

```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to