public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/108876] New: return address clobbered in sibcalls resulting in gfortran tests failing on xtensa
@ 2023-02-21 19:29 jcmvbkbc at gcc dot gnu.org
  2023-02-21 21:54 ` [Bug target/108876] " cvs-commit at gcc dot gnu.org
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: jcmvbkbc at gcc dot gnu.org @ 2023-02-21 19:29 UTC (permalink / raw)
  To: gcc-bugs

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

            Bug ID: 108876
           Summary: return address clobbered in sibcalls resulting in
                    gfortran tests failing on xtensa
           Product: gcc
           Version: 13.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
          Assignee: unassigned at gcc dot gnu.org
          Reporter: jcmvbkbc at gcc dot gnu.org
  Target Milestone: ---

A number of gfortran tests built with -O3 fail with segfault on call0 xtensa
config. E.g. gfortran.dg/allocate_with_source_5.f90
It fails because of the following sibcall code in the
__selectors_MOD_selector_init:

.L445:  
        l32i    a0, sp, 300
        l32i    a2, sp, 192
        l32i    a4, sp, 204
        l32i    a12, sp, 296
        l32i    a13, sp, 292
        l32i    a14, sp, 288
        l32i    a15, sp, 284
        movi    a0, 0x130
        add.n   sp, sp, a0
        jx      a4

RTL generated for this fragment initially looks like this:

(note 3983 3749 3975 121 NOTE_INSN_EPILOGUE_BEG)
(insn 3975 3983 3976 121 (set (reg:SI 0 a0)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 300 [0x12c])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3976 3975 3977 121 (set (reg:SI 12 a12)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 296 [0x128])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3977 3976 3978 121 (set (reg:SI 13 a13)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 292 [0x124])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3978 3977 3979 121 (set (reg:SI 14 a14)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 288 [0x120])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3979 3978 3980 121 (set (reg:SI 15 a15)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 284 [0x11c])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3980 3979 3981 121 (use (reg:SI 0 a0))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3981 3980 3982 121 (set (reg:SI 9 a9)
        (const_int 304 [0x130]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(insn 3982 3981 760 121 (set (reg/f:SI 1 sp)
        (plus:SI (reg/f:SI 1 sp)
            (reg:SI 9 a9)))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (nil))
(call_insn/j 760 3982 761 121 (call (mem:SI (reg:SI 6 a6) [0 __builtin_free S4
A32])
        (const_int 0 [0]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 discrim 1 106
{sibcall_internal}
     (expr_list:REG_CALL_DECL (symbol_ref:SI ("free") [flags 0x41] 
<function_decl 0x7f98b2fa3000 __builtin_free>)
        (expr_list:REG_EH_REGION (const_int 0 [0])
            (nil)))
    (expr_list:SI (use (reg:SI 2 a2))
        (nil)))

but then in the rnreg pass it is changed to the following:

(note 3983 3749 3975 121 NOTE_INSN_EPILOGUE_BEG)
(insn 3975 3983 3976 121 (set (reg:SI 0 a0)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 300 [0x12c])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
     (nil))
(insn 3976 3975 3977 121 (set (reg:SI 12 a12)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 296 [0x128])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
     (nil))
(insn 3977 3976 3978 121 (set (reg:SI 13 a13)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 292 [0x124])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
     (nil))
(insn 3978 3977 3979 121 (set (reg:SI 14 a14)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 288 [0x120])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
     (nil))
(insn 3979 3978 3980 121 (set (reg:SI 15 a15)
        (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                (const_int 284 [0x11c])) [31  S4 A32]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
     (nil))
(insn 3980 3979 3981 121 (use (reg:SI 0 a0))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
     (expr_list:REG_DEAD (reg:SI 0 a0)
        (nil)))
(insn 3981 3980 3982 121 (set (reg:SI 0 a0)
        (const_int 304 [0x130]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
     (nil))
(insn 3982 3981 760 121 (set (reg/f:SI 1 sp)
        (plus:SI (reg/f:SI 1 sp)
            (reg:SI 0 a0)))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 1 {addsi3}
     (expr_list:REG_DEAD (reg:SI 9 a9)
        (nil)))
(call_insn/j 760 3982 761 121 (call (mem:SI (reg:SI 4 a4) [0 __builtin_free S4
A32])
        (const_int 0 [0]))
"gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 discrim 1 106
{sibcall_internal}
     (expr_list:REG_DEAD (reg:SI 6 a6)
        (expr_list:REG_DEAD (reg:SI 2 a2)
            (expr_list:REG_CALL_DECL (symbol_ref:SI ("free") [flags 0x41] 
<function_decl 0x7f98b2fa3000 __builtin_free>)
                (expr_list:REG_EH_REGION (const_int 0 [0])
                    (nil)))))
    (expr_list:SI (use (reg:SI 2 a2))
        (nil)))

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug target/108876] return address clobbered in sibcalls resulting in gfortran tests failing on xtensa
  2023-02-21 19:29 [Bug target/108876] New: return address clobbered in sibcalls resulting in gfortran tests failing on xtensa jcmvbkbc at gcc dot gnu.org
@ 2023-02-21 21:54 ` cvs-commit at gcc dot gnu.org
  2023-02-21 21:59 ` jcmvbkbc at gcc dot gnu.org
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-02-21 21:54 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #1 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Max Filippov <jcmvbkbc@gcc.gnu.org>:

https://gcc.gnu.org/g:b2ef02e8cbbaf95fee98be255f697f47193960ec

commit r13-6266-gb2ef02e8cbbaf95fee98be255f697f47193960ec
Author: Max Filippov <jcmvbkbc@gmail.com>
Date:   Tue Feb 21 11:49:35 2023 -0800

    gcc: xtensa: fix PR target/108876

    gcc/
            PR target/108876
            * config/xtensa/xtensa.cc (xtensa_expand_epilogue): Drop emit_use
            for A0_REG.
            * config/xtensa/xtensa.md (sibcall, sibcall_internal)
            (sibcall_value, sibcall_value_internal): Add 'use' expression
            for A0_REG.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug target/108876] return address clobbered in sibcalls resulting in gfortran tests failing on xtensa
  2023-02-21 19:29 [Bug target/108876] New: return address clobbered in sibcalls resulting in gfortran tests failing on xtensa jcmvbkbc at gcc dot gnu.org
  2023-02-21 21:54 ` [Bug target/108876] " cvs-commit at gcc dot gnu.org
@ 2023-02-21 21:59 ` jcmvbkbc at gcc dot gnu.org
  2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
  2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: jcmvbkbc at gcc dot gnu.org @ 2023-02-21 21:59 UTC (permalink / raw)
  To: gcc-bugs

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

jcmvbkbc at gcc dot gnu.org changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
         Resolution|---                         |FIXED
             Status|UNCONFIRMED                 |RESOLVED

--- Comment #2 from jcmvbkbc at gcc dot gnu.org ---
Fix is committed to the master.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug target/108876] return address clobbered in sibcalls resulting in gfortran tests failing on xtensa
  2023-02-21 19:29 [Bug target/108876] New: return address clobbered in sibcalls resulting in gfortran tests failing on xtensa jcmvbkbc at gcc dot gnu.org
  2023-02-21 21:54 ` [Bug target/108876] " cvs-commit at gcc dot gnu.org
  2023-02-21 21:59 ` jcmvbkbc at gcc dot gnu.org
@ 2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
  2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-02-23  9:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #3 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Max Filippov <jcmvbkbc@gcc.gnu.org>:

https://gcc.gnu.org/g:4c3191de2a82a406200dab364c49a571a80db0af

commit r13-6293-g4c3191de2a82a406200dab364c49a571a80db0af
Author: Max Filippov <jcmvbkbc@gmail.com>
Date:   Wed Feb 22 22:07:58 2023 -0800

    Revert "gcc: xtensa: fix PR target/108876"

    This reverts commit b2ef02e8cbbaf95fee98be255f697f47193960ec.

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [Bug target/108876] return address clobbered in sibcalls resulting in gfortran tests failing on xtensa
  2023-02-21 19:29 [Bug target/108876] New: return address clobbered in sibcalls resulting in gfortran tests failing on xtensa jcmvbkbc at gcc dot gnu.org
                   ` (2 preceding siblings ...)
  2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
@ 2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
  3 siblings, 0 replies; 5+ messages in thread
From: cvs-commit at gcc dot gnu.org @ 2023-02-23  9:32 UTC (permalink / raw)
  To: gcc-bugs

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

--- Comment #4 from CVS Commits <cvs-commit at gcc dot gnu.org> ---
The master branch has been updated by Max Filippov <jcmvbkbc@gcc.gnu.org>:

https://gcc.gnu.org/g:782e442e4f3b88937a725a01245dd749f3f893b9

commit r13-6294-g782e442e4f3b88937a725a01245dd749f3f893b9
Author: Max Filippov <jcmvbkbc@gmail.com>
Date:   Wed Feb 22 22:08:21 2023 -0800

    xtensa: fix PR target/108876

    In commit b2ef02e8cbbaf95fee98be255f697f47193960ec, the sibling call
    insn included (use (reg:SI A0_REG)) to fix the problem, which added
    a USE chain unconditionally to the data flow of register A0 during
    the sibling call.

    As a result, df_regs_ever_live_p (A0_REG) returns true, so even if
    register A0 is not used outside of the sibling call insn, saves and
    restores to stack slots are emitted in pro/epilogue, and finally
    code size increases.
    (This is why I never included (use A0) in sibling calls)

        /* example */
        extern int foo(int);
        int test(int a) {
          return foo(a * 3 + 1);
        }

    ;; before
        test:
            addi    sp, sp, -16     ;; unneeded stack frame allocation
(induced)
            s32i.n  a0, sp, 12      ;; unneeded saving of register A0
            l32i.n  a0, sp, 12      ;; unneeded restoration of register A0
            addx2   a2, a2, a2
            addi.n  a2, a2, 1
            addi    sp, sp, 16      ;; unneeded stack frame freeing (induced)
            j.l     foo, a9         ;; sibling call (truly needs register A0)

    The essential cause is that we emit (use A0) *before* the insns that
    does the stack pointer adjustment during epilogue expansion, so the
    liveness of register A0 ends early, so register A0 is reused afterwards.

    This patch fixes the problem and avoids such regression by doing the
    emit of (use A0) in the sibling call epilogue expansion at the end.

    ;; after
    test:
            addx2   a2, a2, a2
            addi.n  a2, a2, 1
            j.l     foo, a9

    >From RTL-pass "315r.rnreg" by
    "gfortran -O3 -funroll-loops -mabi=call0 -S -da
gcc-gnu/gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":

        ;; Function selector_init (__selectors_MOD_selector_init, funcdef_no=2,
decl_uid=987, cgraph_uid=3, symbol_order=4)
        ...
        (insn 3807 3806 3808 121 (set (reg:SI 15 a15)
                (mem/c:SI (plus:SI (reg/f:SI 1 sp)
                        (const_int 268 [0x10c])) [31  S4 A32]))
"gcc-gnu/gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
             (nil))
        (insn 3808 3807 3809 121 (set (reg:SI 7 a7)
                (const_int 288 [0x120]))
"gcc-gnu/gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 53
{movsi_internal}
             (nil))
        (insn 3809 3808 3810 121 (set (reg/f:SI 1 sp)
                (plus:SI (reg/f:SI 1 sp)
                    (reg:SI 7 a7)))
"gcc-gnu/gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 1 {addsi3}
             (expr_list:REG_DEAD (reg:SI 9 a9)
                (nil)))
        (insn 3810 3809 721 121 (use (reg:SI 0 a0))
"gcc-gnu/gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 -1
             (expr_list:REG_DEAD (reg:SI 0 a0)
                (nil)))
        (call_insn/j 721 3810 722 121 (call (mem:SI (symbol_ref:SI ("free")
[flags 0x41]  <function_decl 0x7f7c885f6000 __builtin_free>) [0 __builtin_free
S4 A32])
                (const_int 0 [0]))
"gcc-gnu/gcc/testsuite/gfortran.dg/allocate_with_source_5.f90":35:30 discrim 1
106 {sibcall_internal}
             (expr_list:REG_DEAD (reg:SI 2 a2)
                (expr_list:REG_CALL_DECL (symbol_ref:SI ("free") [flags 0x41] 
<function_decl 0x7f7c885f6000 __builtin_free>)
                    (expr_list:REG_EH_REGION (const_int 0 [0])
                        (nil))))
            (expr_list:SI (use (reg:SI 2 a2))
                (nil)))

    (IMHO the "rnreg" pass doesn't take REG_ALLOC_ORDER into account;
    it just seems to allocate registers in fixed_regs index order,
    which may have hurt register A0 that became allocatable in the recent
    patch)

    gcc/ChangeLog:
            PR target/108876

            * config/xtensa/xtensa.cc (xtensa_expand_epilogue):
            Emit (use (reg:SI A0_REG)) at the end in the sibling call
            (i.e. the same place as (return) in the normal call).

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-02-23  9:32 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-02-21 19:29 [Bug target/108876] New: return address clobbered in sibcalls resulting in gfortran tests failing on xtensa jcmvbkbc at gcc dot gnu.org
2023-02-21 21:54 ` [Bug target/108876] " cvs-commit at gcc dot gnu.org
2023-02-21 21:59 ` jcmvbkbc at gcc dot gnu.org
2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org
2023-02-23  9:32 ` cvs-commit at gcc dot gnu.org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).