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

            Bug ID: 129275
           Summary: rs6000: "PIC register clobbered by 'r30' in 'asm'" on
                    ppc64le ELFv2 with -mcpu=power10 -fPIC (PC-relative)
           Product: gcc
           Version: 17.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: carlos at redhat dot com
  Target Milestone: ---

On ppc64le ELFv2, `gcc -mcpu=power10 -fPIC` rejects an inline-asm statement
that
clobbers `r30` with:

```
error: PIC register clobbered by 'r30' in 'asm'
```

This currently causes golang to fail to compile because of a testsuite failure:
https://github.com/golang/go/blob/c7505270f4c4854768e6801447cfca44d460ae50/src/cmd/go/testdata/script/test_ppc64_linker_funcs.txt#L33-L35

The ELFv2 ABI does not designate `r30` as a PIC/GOT-base register. `r30` is an
ordinary non-volatile (callee-saved) GPR.

At `-mcpu=power10` the default addressing model is PC-relative (`-mpcrel`,
enabled by default), and **GCC never emits `r30` for PIC in this mode** AFAICT.

clang accepts the identical code and generates correct output; it does not even
warn.

```c
/* repro.c */
void f(void) { __asm__ volatile ("" ::: "r30"); }
```

```
$ powerpc64le-linux-gnu-gcc -mcpu=power9  -fPIC            -c repro.c   # exit
0
$ powerpc64le-linux-gnu-gcc -mcpu=power10 -fno-pic         -c repro.c   # exit
0
$ powerpc64le-linux-gnu-gcc -mcpu=power10 -mno-pcrel -fPIC -c repro.c   # exit
0
$ powerpc64le-linux-gnu-gcc -mcpu=power10 -fPIC            -c repro.c   # exit
1
repro.c: In function 'f':
repro.c:1:16: error: PIC register clobbered by 'r30' in 'asm'
    1 | void f(void) { __asm__ volatile ("" ::: "r30"); }
      |                ^~~~~~~
```

The error is specific to the PC-relative model: it appears with `-mpcrel` (the
default at `-mcpu=power10`) and disappears with `-mno-pcrel` (which restores
TOC/`r2` addressing), with `-fno-pic`, and on `-mcpu=power9`.

Compilation should succeed, as it does with clang, with `-mno-pcrel`, with
`-fno-pic`, and on power9. `r30` is a non-volatile GPR; clobbering it in inline
asm is legal and the compiler should simply save/restore it around the asm.

The error is raised by the *generic* inline-asm clobber check in
`gcc/cfgexpand.cc` (`asm_clobber_reg_is_valid`), which fires purely on the
value of the `PIC_OFFSET_TABLE_REGNUM` macro — it does **not** consult whether
the register is actually used:

```c
/* gcc/cfgexpand.cc:3356, asm_clobber_reg_is_valid() */
/* Clobbering the PIC register is an error.  */
if (PIC_OFFSET_TABLE_REGNUM != INVALID_REGNUM
    && overlaps_hard_reg_set_p (regset, Pmode, PIC_OFFSET_TABLE_REGNUM))
  {
    error ("PIC register clobbered by %qs in %<asm%>", regname);
    is_valid = false;
  }
```

The surrounding comment states the intent: *"Some HW registers cannot be
saved/restored, hence they should not be clobbered by asm statements."* That
rationale does not apply to `r30` on ELFv2: it is an ordinary non-volatile GPR
that is trivially saved/restored.

On rs6000 the macro resolves as follows (`gcc/config/rs6000/rs6000.h`):

```c
/* rs6000.h:53 */
#define TARGET_TOC  (TARGET_HAS_TOC && !TARGET_PCREL)   /* pcrel turns TOC off
*/

/* rs6000.h:1736 */
#define RS6000_PIC_OFFSET_TABLE_REGNUM 30
#define PIC_OFFSET_TABLE_REGNUM                 \
  (TARGET_TOC ? TOC_REGISTER                    \
   : flag_pic ? RS6000_PIC_OFFSET_TABLE_REGNUM  \
   : INVALID_REGNUM)

/* rs6000.h:1742 */
#define TOC_REGISTER (TARGET_MINIMAL_TOC ? RS6000_PIC_OFFSET_TABLE_REGNUM : 2)
```

At `-mcpu=power10` PC-relative addressing is the default (`-mpcrel`), so
`TARGET_PCREL` is true and `TARGET_TOC` is therefore false. With `flag_pic`
set, `PIC_OFFSET_TABLE_REGNUM` falls through to
`RS6000_PIC_OFFSET_TABLE_REGNUM`, i.e. the historical 32-bit SysV GOT-base
register `r30`.

It seems that this is just a historical left-over and the generic check then
rejects the clobber?

Crucially, in the ELFv2 PC-relative configuration the backend does **not**
treat `r30` as a reserved PIC register at all:

* `rs6000.cc:10667-10679` (`rs6000_conditional_register_usage`) only marks
  `RS6000_PIC_OFFSET_TABLE_REGNUM` as `fixed`/`call_used` for `ABI_V4`
  (32-bit SysV `-fpic`/`-fPIC`), `ABI_DARWIN`, and `TARGET_MINIMAL_TOC` — none
of
  which apply to ELFv2 power10. So `r30` is a normal, allocatable callee-saved
  register here, not a fixed PIC base.

* The PC-relative model keeps no GOT-base pointer in any register; addresses
are
  formed per-access relative to the PC (e.g. `pld rX, sym@got@pcrel(0)`).
  Scanning real PIC code (global accesses plus external calls) compiled with
  `-mcpu=power10 -fPIC` shows `r30` is not emitted.

In other words: the macro nominally names `r30` as the PIC register via a
TOC-less fallback that was designed for the 32-bit SysV model, while the ELFv2
pcrel backend neither reserves nor uses `r30` for PIC. The generic clobber
check, keying only off the macro, therefore rejects valid code with no
underlying reason. The `r30` "PIC register" here is vestigial.

```c
/* big.c */
extern int g1, g2; extern void ext(void);
int a(void){ ext(); return g1 + g2; }
int b(int x){ for (int i = 0; i < x; i++) ext(); return g1; }
```

```
$ powerpc64le-linux-gnu-gcc -mcpu=power10 -fPIC -O2 -S -o - big.c | grep 30
   (no matches — r30 is not used for PIC or anything else)
$ powerpc64le-linux-gnu-gcc -mcpu=power10 -fPIC -O2 -S -o - big.c | grep pcrel
        pld 9,g1@got@pcrel
        pld 2,g2@got@pcrel
        pld 2,g1@got@pcrel
        pld 2,g1@got@pcrel
```

Globals are addressed with `pld ...@got@pcrel`; no GOT-base register is set up.

One solution is to make `PIC_OFFSET_TABLE_REGNUM` yield `INVALID_REGNUM` when
PC-relative addressing is in effect.

This currently blocks building goland upstream for POWER10 PIC with gcc.

The fix would be needed for gcc 16 too.

Reply via email to