https://gcc.gnu.org/bugzilla/show_bug.cgi?id=119028

            Bug ID: 119028
           Summary: Inconsistent behavior across optimization levels in
                    GCC 14.2.0
           Product: gcc
           Version: 14.2.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: middle-end
          Assignee: unassigned at gcc dot gnu.org
          Reporter: xieym3 at zohomail dot com
  Target Milestone: ---

I have encountered an issue where a specific C source file exhibits different
behaviors when compiled with different optimization levels in GCC 14.2.0.

When compiling with -O0 or -Os, I get the following linker error:
undefined reference to `sqrt'

However, when compiling with -O1, -O2, or -Ofast, the compilation and linking
complete successfully without errors.

I am uncertain whether this is a bug in GCC or a misconfiguration on my part.
It can be reproduced on https://godbolt.org/z/599neq67z.

$ gcc --verbose
Using built-in specs.
COLLECT_GCC=/data/xieym/llm/install/gcc-14.2.0/bin/gcc
COLLECT_LTO_WRAPPER=/data/xieym/llm/install/gcc-14.2.0/libexec/gcc/x86_64-pc-linux-gnu/14.2.0/lto-wrapper
Target: x86_64-pc-linux-gnu
Configured with: /data/xieym/llm/src/gcc-14.2.0/configure --enable-coverage
--enable-checking --disable-multilib --disable-shared --disable-bootstrap
--enable-languages=c,c++ --prefix=/data/xieym/llm/install/gcc-14.2.0
Thread model: posix
Supported LTO compression algorithms: zlib zstd
gcc version 14.2.0 (GCC)

$ cat file.c

#include <stdio.h>
#include <math.h>

// Function to be always inlined
static inline __attribute__((always_inline)) double square(double x) {
    return x * x;
}

// Function to perform vectorizable operations
void process_data(double* data, size_t size) {
    for (size_t i = 0; i < size; i++) {
        data[i] = sqrt(square(data[i]) + 1.0); // Vectorizable operation
    }
}

int compute_sum(int* array, size_t size) {
    int sum = 0;
    for (size_t i = 0; i < size; i++) {
        for (size_t j = 0; j < size; j++) {
            if (array[i] > array[j]) {
                sum += array[i] - array[j]; // Nested loops creating cycles
            }
        }
    }
    return sum;
}

int main() {
    const size_t array_size = 100;
    double data[array_size];
    int array[array_size];

    // Initialize data for processing
    for (size_t i = 0; i < array_size; i++) {
        data[i] = (double)i / 10.0;
        array[i] = i % 10;
    }

    // Process data with vectorizable operations
    process_data(data, array_size);

    // Compute sum with nested loops
    int result = compute_sum(array, array_size);
    printf("Computed sum: %d\n", result);

#ifdef _WIN32
    __try {
        // Windows-specific exception handling with catchswitch
        printf("Try block\n");
        if (result < 5000) {
            *(int*)0 = 0; // Force an exception
        }
    } __except(1) {
        printf("Exception caught\n");
    }
#endif

    return 0;
}

Reply via email to