llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Wrong code at -O1/2 on x86_64-linux-gnu

Open XChy opened this issue 5 months ago • 3 comments

Reproducer: https://godbolt.org/z/cE5nx14bq Testcase:

#include <stdint.h>
#include <stdio.h>
typedef int8_t v8i8 __attribute__((vector_size(8)));
v8i8 g6;
void *g18;
uint64_t __chk;
int main()
{
    g6 = 0 >= g6;
    int8_t v5 = 0;
lbl_cont3:
    g6 = __builtin_shufflevector(g6, g6, 9, 4, 2, 6, 3, 0, 9, 7);
    if (g18) g6 = g6 * g6;
    v5 = v5 + 4;
    switch (v5)
    {
        case 16: goto lbl_sw17;
        default: goto lbl_cont3;
    }
lbl_sw17:
    __chk = g6[5];
    printf("checksum=0x%016llx\n", (unsigned long long)__chk);
}

This case outputs checksum=0xffffffffffffffff at -O0, and outputs checksum=0x0000000000000000 at -O1/2.

XChy avatar Jun 18 '26 10:06 XChy

At a glance, the IR looks fine both in O1 and O2, so possibly some issue occurring later in the backend (doesn't seem to show up when targeting arm64).

antoniofrighetto avatar Jun 18 '26 14:06 antoniofrighetto

Started with Clang 11

BreadTom avatar Jun 18 '26 22:06 BreadTom

Missed leaving some context here. MIR before Control Flow Optimizer (branch-folder):

bb.1.lbl_cont3:
; predecessors: %bb.0, %bb.4
  successors: %bb.2(0x30000000), %bb.3(0x50000000); %bb.2(37.50%), %bb.3(62.50%)
  liveins: $eax, $rcx, $xmm0, $xmm1, $xmm2
  TEST64rr renamable $rcx, renamable $rcx, implicit-def $eflags
  JCC_1 %bb.3, 5, implicit killed $eflags

bb.2:
; predecessors: %bb.1
  successors: %bb.4(0x80000000); %bb.4(100.00%)
  liveins: $eax, $rcx, $xmm0, $xmm1, $xmm2
  renamable $xmm2 = PUNPCKLBWrr killed renamable $xmm2(tied-def 0), renamable $xmm0
  renamable $xmm2 = PSHUFDri killed renamable $xmm2, -40
  ; ...

bb.3.if.then:
; predecessors: %bb.1
  successors: %bb.4(0x80000000); %bb.4(100.00%)
  liveins: $eax, $rcx, $xmm0, $xmm1, $xmm2
  renamable $xmm2 = PUNPCKLBWrr killed renamable $xmm2(tied-def 0), undef renamable $xmm0
  renamable $xmm2 = PSHUFDri killed renamable $xmm2, -40
  ; ...

MIR after branch-folder:

bb.1.lbl_cont3:
; predecessors: %bb.0, %bb.4
  successors: %bb.2(0x30000000), %bb.3(0x50000000); %bb.2(37.50%), %bb.3(62.50%)
  liveins: $eax, $rcx, $xmm0, $xmm1, $xmm2
  renamable $xmm2 = PUNPCKLBWrr killed renamable $xmm2(tied-def 0), undef renamable $xmm0
  renamable $xmm2 = PSHUFDri killed renamable $xmm2, -40
  renamable $xmm2 = PSHUFLWri killed renamable $xmm2, 70
  TEST64rr renamable $rcx, renamable $rcx, implicit-def $eflags
  JCC_1 %bb.3, 5, implicit killed $eflags

bb.2:
; predecessors: %bb.1
  successors: %bb.4(0x80000000); %bb.4(100.00%)
  liveins: $eax, $rcx, $xmm0, $xmm1, $xmm2
  renamable $xmm2 = PSHUFHWri killed renamable $xmm2, 39
  JMP_1 %bb.4

bb.3.if.then:
; predecessors: %bb.1
  successors: %bb.4(0x80000000); %bb.4(100.00%)
  liveins: $eax, $rcx, $xmm0, $xmm1, $xmm2
  renamable $xmm2 = PMULLWrr killed renamable $xmm2(tied-def 0), renamable $xmm2
  ; ...

The pass has hoisted the common PUNPCKLBW, PSHUFD and PSHUFLW out into the shared predecessor, preserving undef on the former, except that the flag was only valid for one instruction of the merged predecessors, as the other would require the actual zero value of $xmm0.

antoniofrighetto avatar Jun 25 '26 12:06 antoniofrighetto