Bug 2544649

Summary: GCC crash on s390x when building a C function which uses _Float16/uint16_t, memcpy() and __builtin_bswap16()
Product: [Fedora] Fedora Reporter: Victor Stinner <vstinner>
Component: gccAssignee: Jakub Jelinek <jakub>
Status: NEW --- QA Contact: Fedora Extras Quality Assurance <extras-qa>
Severity: medium Docs Contact:
Priority: unspecified    
Version: 44CC: dmalcolm, fweimer, jakub, jlaw, josmyers, jwakely, mcermak, mpolacek, msebor, nickc, nixuser, sipoyare
Target Milestone: ---   
Target Release: ---   
Hardware: s390x   
OS: Linux   
Whiteboard:
Fixed In Version: Doc Type: ---
Doc Text:
Story Points: ---
Clone Of: Environment:
Last Closed: Type: ---
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Attachments:
Description Flags
reproducer.c: C program to reproduce the GCC crash none

Description Victor Stinner 2026-10-01 15:04:45 UTC
bug.c file to reproduce the crash:
----------
#include <string.h>
#include <stdint.h>

void
PyFloat_Pack2(double x, char *data, int le)
{
    _Float16 y = (_Float16)x;
    if (!le) {
        uint16_t word;
        memcpy(&word, &y, 2);
        word = __builtin_bswap16(word);
        memcpy(data, &word, 2);
    }
    else {
        memcpy(data, &y, sizeof(_Float16));
    }
}
----------

GCC crash with -O1 or higher, but it doesn't crash with -O0 or -Og.

Reproducible: Always

Steps to Reproduce:
1. Create bug.c
2. gcc -O1 -o bug.o -c bug.c
Actual Results:
Build with gcc 16.2.1 (Fedora package gcc-16.2.1-2.fc44.s390x):
----------
$ gcc -O1 -o bug.o -c bug.c
during RTL pass: split1
bug.c: In function ‘PyFloat_Pack2’:
bug.c:17:1: internal compiler error: Segmentation fault
   17 | }
      | ^
/usr/libexec/gcc/s390x-redhat-linux/16/cc1 -quiet bug.c -quiet -dumpbase bug.c -dumpbase-ext .c -mtune=z14 -march=z13 -m64 -mzarch -O1 -o /tmp/cc0XMx28.s
Please submit a full bug report, with preprocessed source.
See <https://bugzilla.redhat.com/> for instructions.
Preprocessed source stored into /tmp/cceEi1QY.out file, please attach this to your bugreport.
----------

Expected Results:
No crash.

Additional Information:
$ uname -a
Linux fedora-stable 7.2.5-200.fc44.s390x #1 SMP PREEMPT Fri Sep 11 15:37:18 UTC 2026 s390x GNU/Linux

$ sudo virt-what
ibm_systemz
ibm_systemz-zvm

Comment 1 Victor Stinner 2026-10-01 15:07:12 UTC
Created attachment 2159407 [details]
reproducer.c: C program to reproduce the GCC crash

Comment 2 Victor Stinner 2026-10-01 15:09:00 UTC
Sorry, I forgot to mention that, so far, I only reproduced the crash on s390x architecture.

Comment 3 Victor Stinner 2026-10-01 17:48:56 UTC
Oh, clang also miscompiles this code (but don't crash): https://github.com/llvm/llvm-project/issues/228151

In Python, I just merged a workaround. Replace:
---
            _Float16 y = (...);
            uint16_t word;
            memcpy(&word, &y, 2);
            word = __builtin_bswap16(word);
            memcpy(data, &word, 2);
---

with:
---
            _Float16 y = (...);
            char buffer[2];
            memcpy(buffer, &y, 2);
            data[0] = buffer[1];
            data[1] = buffer[0];
---

See the Python change: https://github.com/python/cpython/commit/c7a79bd2f4d3dd3bcaebd77c47f07d1feabe320a