Quuxplusone / LLVMBugzillaTest

0 stars 0 forks source link

LLVM generates illegal instruction with -O3 on powerpc (32bit) #45653

Open Quuxplusone opened 4 years ago

Quuxplusone commented 4 years ago
Bugzilla Link PR46683
Status NEW
Importance P normal
Reported by Jörn Heusipp (osmanx@problemloesungsmaschine.de)
Reported on 2020-07-11 00:29:11 -0700
Last modified on 2021-04-12 01:49:12 -0700
Version 10.0
Hardware Macintosh Linux
CC efriedma@quicinc.com, glaubitz@physik.fu-berlin.de, llvm-bugs@lists.llvm.org, neeilans@live.com, nemanja.i.ibm@gmail.com, richard-llvm@metafoo.co.uk
Fixed by commit(s)
Attachments test.c (487 bytes, text/plain)
Blocks
Blocked by
See also
Created attachment 23720
minimal test case

Clang 10 (and 9, and 8, did not check earlier versions) generates illegal
instruction (clrldi) with -O3 on powerpc (32bit). Even setting -mcpu=generic
(or any other -mcpu for that matter) does not avoid generating the problematic
instruction. -O2 works.

The bug affects building libopenmpt (https://lib.openmpt.org/) for powerpc with
clang.

=====
manx@bleifus:~/tmp$ cat test.c
/*
clang -std=c11 -O3 -Wall -Wextra -o test test.c
*/
static unsigned int reverse(unsigned int value) {
        const unsigned long bits = sizeof(unsigned int) * 8;
        unsigned int result = 0;
        for (unsigned long i = 0; i < bits; ++i) {
                result <<= 1;
                result |= (unsigned int)(value & 0x1);
                value >>= 1;
        }
        return result;
}
int main(int argc, char * argv []) {
        (void)argv;
        return reverse(argc);
}
manx@bleifus:~/tmp$ clang-10 --version
clang version 10.0.0-3
Target: powerpc-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin
manx@bleifus:~/tmp$ clang-10 -std=c11 -O3 -Wall -Wextra -o test test.c
manx@bleifus:~/tmp$ ./test
Illegal instruction
manx@bleifus:~/tmp$ cat /proc/cpuinfo
processor       : 0
cpu             : 7447A, altivec supported
clock           : 1416.666661MHz
revision        : 1.1 (pvr 8003 0101)
bogomips        : 83.24

timebase        : 41620907
platform        : PowerMac
model           : PowerMac10,1
machine         : PowerMac10,1
motherboard     : PowerMac10,1 MacRISC3 Power Macintosh
detected as     : 287 (Mac mini)
pmac flags      : 00000010
L2 cache        : 512K unified
pmac-generation : NewWorld
Memory          : 512 MB
manx@bleifus:~/tmp$ clang-10 -c -std=c11 -O3 -Wall -Wextra -o test.o test.c
manx@bleifus:~/tmp$ objdump -d test.o

test.o:     file format elf32-powerpc

Disassembly of section .text:

00000000 <main>:
   0:   3c 80 aa aa     lis     r4,-21846
   4:   60 84 aa aa     ori     r4,r4,43690
   8:   54 65 08 3c     rlwinm  r5,r3,1,0,30
   c:   7c a4 20 38     and     r4,r5,r4
  10:   3c a0 55 55     lis     r5,21845
  14:   60 a5 55 55     ori     r5,r5,21845
  18:   54 63 f8 7e     rlwinm  r3,r3,31,1,31
  1c:   7c 63 28 38     and     r3,r3,r5
  20:   3c a0 cc cc     lis     r5,-13108
  24:   7c 63 23 78     or      r3,r3,r4
  28:   60 a5 cc cc     ori     r5,r5,52428
  2c:   54 64 10 3a     rlwinm  r4,r3,2,0,29
  30:   7c 84 28 38     and     r4,r4,r5
  34:   3c a0 33 33     lis     r5,13107
  38:   60 a5 33 33     ori     r5,r5,13107
  3c:   54 63 f0 be     rlwinm  r3,r3,30,2,31
  40:   7c 63 28 38     and     r3,r3,r5
  44:   3c a0 f0 f0     lis     r5,-3856
  48:   7c 63 23 78     or      r3,r3,r4
  4c:   60 a5 f0 f0     ori     r5,r5,61680
  50:   54 64 20 36     rlwinm  r4,r3,4,0,27
  54:   7c 84 28 38     and     r4,r4,r5
  58:   3c a0 0f 0f     lis     r5,3855
  5c:   60 a5 0f 0f     ori     r5,r5,3855
  60:   54 63 e1 3e     rlwinm  r3,r3,28,4,31
  64:   7c 63 28 38     and     r3,r3,r5
  68:   7c 63 23 78     or      r3,r3,r4
  6c:   54 64 c0 3e     rotlwi  r4,r3,24
  70:   50 64 42 1e     rlwimi  r4,r3,8,8,15
  74:   50 64 46 3e     rlwimi  r4,r3,8,24,31
  78:   78 83 00 20     clrldi  r3,r4,32
  7c:   4e 80 00 20     blr
manx@bleifus:~/tmp$ gdb ./test
GNU gdb (Debian 9.2-1) 9.2
Copyright (C) 2020 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.
Type "show copying" and "show warranty" for details.
This GDB was configured as "powerpc-linux-gnu".
Type "show configuration" for configuration details.
For bug reporting instructions, please see:
<http://www.gnu.org/software/gdb/bugs/>.
Find the GDB manual and other documentation resources online at:
    <http://www.gnu.org/software/gdb/documentation/>.

For help, type "help".
Type "apropos word" to search for commands related to "word"...
Reading symbols from ./test...
(No debugging symbols found in ./test)
(gdb) run
Starting program: /home/manx/tmp/test

Program received signal SIGILL, Illegal instruction.
0x1000047c in main ()
(gdb) display/i $pc
1: x/i $pc
=> 0x1000047c <main+120>:       clrldi  r3,r4,32
(gdb) quit
A debugging session is active.

        Inferior 1 [process 3728] will be killed.

Quit anyway? (y or n) y
manx@bleifus:~/tmp$ clang-10 -std=c11 -O2 -Wall -Wextra -o test test.c
manx@bleifus:~/tmp$ ./test
manx@bleifus:~/tmp$ clang-10 -c -std=c11 -O2 -Wall -Wextra -o test.o test.c
manx@bleifus:~/tmp$ objdump -d test.o

test.o:     file format elf32-powerpc

Disassembly of section .text:

00000000 <main>:
   0:   38 80 00 20     li      r4,32
   4:   7c 89 03 a6     mtctr   r4
   8:   38 80 00 00     li      r4,0
   c:   7c 85 23 78     mr      r5,r4
  10:   7c 64 1b 78     mr      r4,r3
  14:   54 63 f8 7e     rlwinm  r3,r3,31,1,31
  18:   50 a4 08 3c     rlwimi  r4,r5,1,0,30
  1c:   42 00 ff f0     bdnz    c <main+0xc>
  20:   7c 83 23 78     mr      r3,r4
  24:   4e 80 00 20     blr
manx@bleifus:~/tmp$ clang-10 -std=c11 -mcpu=generic -O3 -Wall -Wextra -o test
test.c
manx@bleifus:~/tmp$ ./test
Illegal instruction
manx@bleifus:~/tmp$
=====

Tested on Debian-ports powerpc on an Apple MacMini G4.
Quuxplusone commented 4 years ago

Attached test.c (487 bytes, text/plain): minimal test case

Quuxplusone commented 4 years ago

This should be fixed by a5d161c119d5a03c1ce834c6f4ce2576d6a064e4. I think we missed the opportunity to get it into 10.01, but this should work with 11.0.

Can you please confirm if this is the case with the original code?

Quuxplusone commented 3 years ago
(In reply to Nemanja Ivanovic from comment #1)
> This should be fixed by a5d161c119d5a03c1ce834c6f4ce2576d6a064e4. I think we
> missed the opportunity to get it into 10.01, but this should work with 11.0.
>
> Can you please confirm if this is the case with the original code?

Sorry for the late reply, I did not have the affected machine up for some time.

I can confirm that libopenmpt now builds with -O3 successfully with Debian-
ports powerpc on an Apple MacMini G4.

manx@bleifus:~$ clang --version
Debian clang version 11.0.1-2
Target: powerpc-unknown-linux-gnu
Thread model: posix
InstalledDir: /usr/bin