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

            Bug ID: 118614
           Summary: [riscv] Naked function attribute on riscv optimizes
                    away C conditional
           Product: gcc
           Version: 14.2.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: c
          Assignee: unassigned at gcc dot gnu.org
          Reporter: charlie at rivosinc dot com
  Target Milestone: ---

riscv GCC assumes that the when the s0 register is used as a function pointer
it will always be non-zero. This causes the body of an if statement that is
predicated on this s0 variable to always execute.

I understand that the wording in the docs for naked functions say:

"While using extended asm or a mixture of basic asm and C code may appear to
work, they cannot be depended upon to work reliably and are not supported."

However, since this issue appears to be constrained to the sp and s0 registers,
I was wondering what was going on here.

Here is the code:

void __attribute__((__naked__)) ret_from_fork()
{
    register int (*fn)(void *) asm("s0");
    register void *fn_arg asm("s1");

    if (fn)
            fn(fn_arg);
}

outputs:
ret_from_fork:
        mv      a0,s1
        jalr    s0

A godbolt link containing this: https://godbolt.org/z/h3cv6e18K.

Reply via email to