Issue 75809
Summary Unexpected results in optimizations when using -O1.
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 timeout         FA5BEE67        FA5BEE67        FA5BEE67        FA5BEE67 FA5BEE67      
file2.c        timeout         AC3672BB        FA5BEE67 FA5BEE67        FA5BEE67        FA5BEE67
```
The ```timeout``` means the ```./a.out``` had been running for a long time and still didn't terminate.
using command line:
```
clang <filename.c> <optimization level> -lm -I $CSMITH_HOME/include && ./a.out
```
Besides, I ran the command line ```clang <filename.c> <optimization level> -fsanitize=<option> -I /usr/include/csmith/ && ./a.out```, options include **address**, **undefined** and **memory**, and the terminal showed that my programs have no undefined behavior. Meanwhile, both test cases appear not to be relying on uninitialized memory, at least according to **valgrind**.

Please help me to explain why these two files produced different output results when using the same optimization level(i.e. -O1), while the transformation of loop is equivalent when using other optimization levels,  thank you.

version: clang+llvm 14.0.0
os: ubuntu 22.04

And the content of ```csmith.h``` is as follows.
```
/* -*- mode: C -*-
 *
 * Copyright (c) 2007-2011, 2013, 2014 The University of Utah
 * All rights reserved.
 *
 * This file is part of `csmith', a random generator of C programs.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   * Redistributions of source code must retain the above copyright notice,
 *     this list of conditions and the following disclaimer.
 *
 *   * Redistributions in binary form must reproduce the above copyright
 *     notice, this list of conditions and the following disclaimer in the
 *     documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#ifndef RANDOM_RUNTIME_H
#define RANDOM_RUNTIME_H

#ifdef CSMITH_MINIMAL
#include "csmith_minimal.h"
#else

/*****************************************************************************/

#include <string.h>
#include <float.h>
#include <math.h>

#define __STDC_LIMIT_MACROS
#include "random_inc.h"

static uint32_t crc32_tab[256];
static uint32_t crc32_context = 0xFFFFFFFFUL;

static void 
crc32_gentab (void)
{
	uint32_t crc;
	const uint32_t poly = 0xEDB88320UL;
	int i, j;
	
	for (i = 0; i < 256; i++) {
		crc = i;
		for (j = 8; j > 0; j--) {
			if (crc & 1) {
				crc = (crc >> 1) ^ poly;
			} else {
				crc >>= 1;
			}
		}
		crc32_tab[i] = crc;
	}
}

static void 
crc32_byte (uint8_t b) {
	crc32_context = 
		((crc32_context >> 8) & 0x00FFFFFF) ^ 
		crc32_tab[(crc32_context ^ b) & 0xFF];
}

#if defined(__SPLAT__) || defined(NO_LONGLONG)
static void 
crc32_8bytes (uint32_t val)
{
	crc32_byte ((val>>0) & 0xff);
	crc32_byte ((val>>8) & 0xff);
	crc32_byte ((val>>16) & 0xff);
	crc32_byte ((val>>24) & 0xff);
}

static void 
transparent_crc (uint32_t val, char* vname, int flag)
{
	crc32_8bytes(val);
	if (flag) {
 		printf("...checksum after hashing %s : %X\n", vname, crc32_context ^ 0xFFFFFFFFU);
	}
}
#else
static void 
crc32_8bytes (uint64_t val)
{
	crc32_byte ((val>>0) & 0xff);
	crc32_byte ((val>>8) & 0xff);
	crc32_byte ((val>>16) & 0xff);
	crc32_byte ((val>>24) & 0xff);
	crc32_byte ((val>>32) & 0xff);
	crc32_byte ((val>>40) & 0xff);
	crc32_byte ((val>>48) & 0xff);
	crc32_byte ((val>>56) & 0xff);
}

static void 
transparent_crc (uint64_t val, char* vname, int flag)
{
	crc32_8bytes(val);
	if (flag) {
 		printf("...checksum after hashing %s : %lX\n", vname, crc32_context ^ 0xFFFFFFFFUL);
	}
}

#endif

static void 
transparent_crc_bytes (char *ptr, int nbytes, char* vname, int flag)
{
    int i;
    for (i=0; i<nbytes; i++) {
 crc32_byte(ptr[i]);
    }
	if (flag) {
  		printf("...checksum after hashing %s : %lX\n", vname, crc32_context ^ 0xFFFFFFFFUL);
	}
}

/*****************************************************************************/

#endif

#endif /* RANDOM_RUNTIME_H */

/*
 * Local Variables:
 * c-basic-offset: 4
 * tab-width: 4
 * End:
 */

/* End of file. */
```
**file1.c**
```

#include "csmith.h"

struct S0 {
  unsigned f0 : 28;
  signed f1 : 3;
  signed f2 : 26;
  signed f3 : 29;
};

static int64_t g_31 = 0x9117A540F8278B72LL;
static struct S0 g_41 = {14571, -0, 4770, 6340};
static struct S0 *volatile g_220[1] = {&g_41};

static void func_1(void);

static void func_1() {
  int32_t l_34 = 0xDFE53505L;
  // unswitching
  for (g_31 = (-27); (g_31 < 19); g_31++) {
    uint64_t l_42[2];
    int i;
    for (i = 0; i < 2; i++) {
      l_42[i] = 0xE971521F00BB6E74LL;
    }
    if (l_42[1]) {
      if ((*g_220[0]).f1) {
        break;
      }
      for (l_34 = (-22); (l_34 != (-14)); l_34 = safe_add_func_uint16_t_u_u(l_34, 8)) {
      }
 }
  }
}

int main(void) {
  int print_hash_value = 0;
 platform_main_begin();
  crc32_gentab();
  func_1();
 transparent_crc(g_31, "g_31", print_hash_value);
 transparent_crc(g_41.f0, "g_41.f0", print_hash_value);
 transparent_crc(g_41.f1, "g_41.f1", print_hash_value);
 transparent_crc(g_41.f2, "g_41.f2", print_hash_value);
 transparent_crc(g_41.f3, "g_41.f3", print_hash_value);
 platform_main_end(crc32_context ^ 0xFFFFFFFFUL, print_hash_value);
 return 0;
}

```
**file2.c**
```
#include <math.h>
#include <stdlib.h>

#include "csmith.h"

struct S0 {
  unsigned f0 : 28;
  signed f1 : 3;
  signed f2 : 26;
  signed f3 : 29;
};

static int64_t g_31 = 0x9117A540F8278B72LL;
static struct S0 g_41 = {14571, -0, 4770, 6340};
static struct S0 *volatile g_220[1] = {&g_41};

static void func_1(void);

static void func_1() {
  int32_t l_34 = 0xDFE53505L;
  // unswitching
  if ((*g_220[0]).f1) {
    for (g_31 = (-27); (g_31 < 19); g_31++) {
 uint64_t l_42[2];
      int i;
      for (i = 0; i < 2; i++) {
 l_42[i] = 0xE971521F00BB6E74LL;
      }
      if (l_42[1]) {
 { break; }
        for (l_34 = (-22); (l_34 != (-14)); l_34 = safe_add_func_uint16_t_u_u(l_34, 8)) {
        }
      }
    }
 } else {
    for (g_31 = (-27); (g_31 < 19); g_31++) {
      uint64_t l_42[2];
      int i;
      for (i = 0; i < 2; i++) {
 l_42[i] = 0xE971521F00BB6E74LL;
      }
      if (l_42[1]) {
 for (l_34 = (-22); (l_34 != (-14)); l_34 = safe_add_func_uint16_t_u_u(l_34, 8)) {
        }
      }
    }
  }
}

int main(void) {
  int print_hash_value = 0;
  platform_main_begin();
 crc32_gentab();
  func_1();
  transparent_crc(g_31, "g_31", print_hash_value);
  transparent_crc(g_41.f0, "g_41.f0", print_hash_value);
  transparent_crc(g_41.f1, "g_41.f1", print_hash_value);
  transparent_crc(g_41.f2, "g_41.f2", print_hash_value);
  transparent_crc(g_41.f3, "g_41.f3", 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