Issue 75604
Summary unexpected output results in fusion using -Os
Labels
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      8426F 85545B29        85545B29        85545B29        7FD9E5FE        85545B29 
file2.c      8426F           85545B29        85545B29        85545B29 85545B29        85545B29     
```
 
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_a120[2];
static int64_t g_b120[2];

struct S0 {
  const volatile uint32_t f0;
  uint64_t f1;
  int64_t f2;
};

union U1 {
  int16_t f0;
  uint16_t f1;
  uint16_t f2;
  const struct S0 f3;
};

static int32_t g_17[3] = {0x556123D4L, 0x556123D4L, 0x556123D4L};
static int32_t *g_29 = (void *)0;
static union U1 g_105 = {0xB5B7L};
static union U1 g_272[1] = {{0xB220L}};
static struct S0 g_350 = {2UL, 18446744073709551615UL, 0x484C2F172B251D2FLL};
static uint16_t g_352 = 5UL;
static struct S0 g_388 = {0UL, 0xB2916F400852E206LL, 0L};

static const void func_1(void);
static int32_t *func_18(const int16_t p_19, int32_t **p_20, int32_t **p_23);

static const void func_1(void) {
  int i, j;
  int ii_3;
  // fusion
  for (i = 0, ii_3 = 0; i < 2; i++, ii_3++) {
    for (j = 0; j < 1; j++) {
 g_a120[ii_3] = g_17[0] * g_352 - g_272[0].f1;
    }
  }
  int jj_3;
  for (jj_3 = 0; jj_3 < 2; jj_3++) {
    g_b120[jj_3] = g_105.f1 * g_a120[jj_3] - g_388.f2;
  }
  for (g_17[1] = 0; (g_17[1] <= 1); g_17[1] += 1) {
  }
  g_29 = func_18((g_272[0].f0 = g_350.f0), &g_29, &g_29);
}

static int32_t *func_18(const int16_t p_19, int32_t **p_20, int32_t **p_23) {
  return (*p_20);
}

int main(void) {
  int i, j, k;
  int print_hash_value = 0;
 platform_main_begin();
  crc32_gentab();
  func_1();
  for (i = 0; i < 3; i++) {
    transparent_crc(g_17[i], "g_17[i]", print_hash_value);
  }
  for (i = 0; i < 1; i++) {
 transparent_crc(g_272[i].f0, "g_272[i].f0", print_hash_value);
  }
 transparent_crc(g_350.f0, "g_350.f0", print_hash_value);
 transparent_crc(g_350.f1, "g_350.f1", print_hash_value);
 transparent_crc(g_350.f2, "g_350.f2", print_hash_value);
 transparent_crc(g_352, "g_352", print_hash_value);
 transparent_crc(g_388.f0, "g_388.f0", print_hash_value);
 transparent_crc(g_388.f1, "g_388.f1", print_hash_value);
 transparent_crc(g_388.f2, "g_388.f2", print_hash_value);
  for (i = 0; i < 2; i++) {
    transparent_crc(g_a120[i], "g_a120[i]", print_hash_value);
  }
  for (i = 0; i < 2; i++) {
 transparent_crc(g_b120[i], "g_b120[i]", print_hash_value);
  }
 platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
 return 0;
}

```

**file2.c**
```

#include "csmith.h"

static int32_t g_a120[2];
static int64_t g_b120[2];

struct S0 {
  const volatile uint32_t f0;
  uint64_t f1;
  int64_t f2;
};

union U1 {
  int16_t f0;
  uint16_t f1;
  uint16_t f2;
  const struct S0 f3;
};

static int32_t g_17[3] = {0x556123D4L, 0x556123D4L, 0x556123D4L};
static int32_t *g_29 = (void *)0;
static union U1 g_105 = {0xB5B7L};
static union U1 g_272[1] = {{0xB220L}};
static struct S0 g_350 = {2UL, 18446744073709551615UL, 0x484C2F172B251D2FLL};
static uint16_t g_352 = 5UL;
static struct S0 g_388 = {0UL, 0xB2916F400852E206LL, 0L};

static const void func_1(void);
static int32_t *func_18(const int16_t p_19, int32_t **p_20, int32_t **p_23);

static const void func_1(void) {
  int i, j;
 int ii_3;
  int jj_3;
  int ij_3;
  // fusion
  for (i = 0, ii_3 = 0, jj_3 = 0, ij_3 = 0; ij_3 <= 2; ij_3++) {
    if (ij_3 <= 2 && i < 2) {
      for (j = 0; j < 1; j++) {
        g_a120[ii_3] = g_17[0] * g_352 - g_272[0].f1;
      }
      i++;
      ii_3++;
    }
 if (ij_3 <= 2 && jj_3 < 2) {
      g_b120[jj_3] = g_105.f1 * g_a120[jj_3] - g_388.f2;
      jj_3++;
    }
  }
  for (g_17[1] = 0; (g_17[1] <= 1); g_17[1] += 1) {
  }
  g_29 = func_18((g_272[0].f0 = g_350.f0), &g_29, &g_29);
}

static int32_t *func_18(const int16_t p_19, int32_t **p_20, int32_t **p_23) {
  return (*p_20);
}

int main(void) {
  int i, j, k;
  int print_hash_value = 0;
 platform_main_begin();
  crc32_gentab();
  func_1();
  for (i = 0; i < 3; i++) {
    transparent_crc(g_17[i], "g_17[i]", print_hash_value);
  }
  for (i = 0; i < 1; i++) {
 transparent_crc(g_272[i].f0, "g_272[i].f0", print_hash_value);
  }
 transparent_crc(g_350.f0, "g_350.f0", print_hash_value);
 transparent_crc(g_350.f1, "g_350.f1", print_hash_value);
 transparent_crc(g_350.f2, "g_350.f2", print_hash_value);
 transparent_crc(g_352, "g_352", print_hash_value);
 transparent_crc(g_388.f0, "g_388.f0", print_hash_value);
 transparent_crc(g_388.f1, "g_388.f1", print_hash_value);
 transparent_crc(g_388.f2, "g_388.f2", print_hash_value);
  for (i = 0; i < 2; i++) {
    transparent_crc(g_a120[i], "g_a120[i]", print_hash_value);
  }
  for (i = 0; i < 2; i++) {
 transparent_crc(g_b120[i], "g_b120[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