Why does gcc not make use of special OP codes

jdoubleu
Posts: 8
Joined: Wed Sep 29, 2021 3:58 pm

Why does gcc not make use of special OP codes

Postby jdoubleu » Wed Sep 29, 2021 4:46 pm

I was wondering why gcc wouldn't use specialized OP codes from the xtensa instruction set architecture (ISA).

I'm using the ESP32-WROVER-B board, which itself uses the Xtensa LX6 microprocessor. From their Overview Handbook, I learned that the Xtensa LX processor family supports some specialized extensions to their instruction set. They include "zero-overhead" loops and min/max hardware-level implementations (compare section 2.12.1 http://loboris.eu/ESP32/Xtensa_lx%20Ove ... G8.1098890). Unfortunately, I couldn't directly find out whether the ESP32 boards include these features. Neither from the comparison table in the documentation (https://docs.espressif.com/projects/esp ... rison.html) nor from the data sheet (https://www.espressif.com/sites/default ... eet_en.pdf).

However what I found were the configurations for the binutils overlays, where the loop and minmax features are enabled (compare https://github.com/espressif/xtensa-ove ... nfig.h#L71). This is true for the ESP-IDF toolchain I downloaded, as well (check

Code: Select all

$IDF_TOOLS_PATH/tools/xtensa-esp32-elf/esp-2021r1-8.4.0/xtensa-esp32-elf/sys-include/xtensa/config/core-isa.h
). I'm not sure what

Code: Select all

xtensa_lx106
is, because it's disabled for that platform.

In theory these features should be available, but when I check the code generated from gcc, I don't see any of those op codes in the assembly listing. Even when I use the

Code: Select all

-O2
flag.

I get the following assembly for a

Code: Select all

int x = (a < b) ? a : b;
:
  1. # a and b are set before
  2. #      ...
  3.     blt a11, a2, .L49
  4.     bne a2, a11, .L48
  5.     bgeu    a10, a3, .L48
  6. #      ...
Furthermore I only see "normal" loops with branches:
  1. #   ...
  2.     j   .L4
  3. .L5:
  4. #      loop body
  5. #      ...
  6. .L4:
  7.     blt a7, a4, .L5
  8. #      ...

jdoubleu
Posts: 8
Joined: Wed Sep 29, 2021 3:58 pm

Re: Why does gcc not make use of special OP codes

Postby jdoubleu » Mon Oct 11, 2021 4:15 pm

As it turns out, the compiler indeed uses these special OP codes and outputs them in at least -O2 mode. It just so appears that they're not being used in most of my functions, probably because the compiler found a better way?

The following C code:
  1. static int my_min(int a, int b)
  2. {
  3.     return a < b ? a : b;
  4. }
  5.  
  6. static void my_loop(int* p)
  7. {
  8.     for (int i = 0; i < 64; ++i)
  9.         p[i] += 1;
  10. }
  11.  
  12. int main()
  13. {
  14.     int a, b;
  15.     std::scanf("%d\n%d", &a, &b);
  16.  
  17.     int x = my_min(a, b);
  18.  
  19.     // please don't do this ever
  20.     my_loop(&x);
  21.  
  22.     std::printf("%d", x);
  23. }
compiled with

Code: Select all

xtensa-esp32-elf-gcc -S -o main.s -c main.cpp -O3
yields:
  1. #      ...
  2.     l32r    a10, .LC1
  3.     addi.n  a12, sp, 4
  4.     addi.n  a11, sp, 8
  5.     call8   scanf
  6.     l32i.n  a10, sp, 4
  7.     l32i.n  a9, sp, 8
  8.     mov.n   a8, sp
  9.     min a9, a9, a10
  10.     s32i.n  a9, sp, 0
  11.     movi.n  a10, 0x40
  12.     loop    a10, .L2_LEND
  13. .L2:
  14.     l32i.n  a9, a8, 0
  15.     addi.n  a9, a9, 1
  16.     s32i.n  a9, a8, 0
  17.     addi.n  a8, a8, 4
  18.     .L2_LEND:
  19.     l32i.n  a11, sp, 0
  20.     l32r    a10, .LC3
  21.     movi.n  a2, 0
  22.     call8   printf
  23.     retw.n
  24. #      ...
As you can see, it includes the min and loop instructions. 🎉
Last edited by jdoubleu on Mon Oct 11, 2021 4:37 pm, edited 1 time in total.


jdoubleu
Posts: 8
Joined: Wed Sep 29, 2021 3:58 pm

Re: Why does gcc not make use of special OP codes

Postby jdoubleu » Mon Oct 11, 2021 4:38 pm

Interesting. The zero-overhead loop is already very constrained in terms of application. There are only few cases where this loop can be used anyways.

Who is online

Users browsing this forum: gamename and 86 guests