https://gcc.gnu.org/g:d8403076bc4a5bdeb36fe0c5a3b6a6c71f3cfa84
commit d8403076bc4a5bdeb36fe0c5a3b6a6c71f3cfa84 Author: Thomas Schwinge <tschwi...@baylibre.com> Date: Fri Dec 13 15:09:49 2024 +0100 nvptx: Handle '__builtin_stack_save()' in a well-behaved way for PTX "native" stacks [PR65181] PR target/65181 gcc/ * config/nvptx/nvptx.md [!TARGET_SOFT_STACK] (save_stack_block): 'define_expand'. gcc/testsuite/ * gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c: Adjust. Diff: --- gcc/ChangeLog.omp | 4 ++++ gcc/config/nvptx/nvptx.md | 17 ++++++++++++++++- gcc/testsuite/ChangeLog.omp | 4 ++++ .../__builtin_stack_save___builtin_stack_restore-1.c | 17 ++++++----------- 4 files changed, 30 insertions(+), 12 deletions(-) diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp index ce600d235f66..95c0ca220ec8 100644 --- a/gcc/ChangeLog.omp +++ b/gcc/ChangeLog.omp @@ -1,5 +1,9 @@ 2024-12-18 Thomas Schwinge <tschwi...@baylibre.com> + PR target/65181 + * config/nvptx/nvptx.md [!TARGET_SOFT_STACK] (save_stack_block): + 'define_expand'. + Backported from trunk: 2024-08-09 Andrew Stubbs <a...@baylibre.com> diff --git a/gcc/config/nvptx/nvptx.md b/gcc/config/nvptx/nvptx.md index 18033bef699d..816d09f35102 100644 --- a/gcc/config/nvptx/nvptx.md +++ b/gcc/config/nvptx/nvptx.md @@ -1708,12 +1708,27 @@ return nvptx_output_set_softstack (REGNO (operands[0])); }) +(define_expand "save_stack_block" + [(match_operand 0 "register_operand" "") + (match_operand 1 "register_operand" "")] + "!TARGET_SOFT_STACK" +{ + /* The concept of a '%stack' pointer doesn't apply to PTX like this. + However, GCC occasionally synthesize '__builtin_stack_save ()', + '__builtin_stack_restore ()', and isn't able to optimize them all away. + Just submit a dummy -- user code shouldn't be able to observe this. */ + emit_move_insn (operands[0], GEN_INT (0xdeadbeef)); + DONE; +}) + (define_expand "restore_stack_block" [(match_operand 0 "register_operand" "") (match_operand 1 "register_operand" "")] "" { - if (TARGET_SOFT_STACK) + if (!TARGET_SOFT_STACK) + ; /* See 'save_stack_block'. */ + else { emit_move_insn (operands[0], operands[1]); emit_insn (gen_set_softstack (Pmode, operands[0])); diff --git a/gcc/testsuite/ChangeLog.omp b/gcc/testsuite/ChangeLog.omp index 76c302c02430..0f933749b622 100644 --- a/gcc/testsuite/ChangeLog.omp +++ b/gcc/testsuite/ChangeLog.omp @@ -1,5 +1,9 @@ 2024-12-18 Thomas Schwinge <tschwi...@baylibre.com> + PR target/65181 + * gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c: + Adjust. + PR target/65181 * gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c: Add. diff --git a/gcc/testsuite/gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c b/gcc/testsuite/gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c index 294014dae1db..35a879fd5973 100644 --- a/gcc/testsuite/gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c +++ b/gcc/testsuite/gcc.target/nvptx/__builtin_stack_save___builtin_stack_restore-1.c @@ -1,7 +1,6 @@ /* Document what we do for '__builtin_stack_save()', '__builtin_stack_restore()'. */ -/* { dg-do compile } - TODO We can't 'assemble' this -- it's invalid PTX code. */ +/* { dg-do assemble } */ /* { dg-options {-O3 -mno-soft-stack} } */ /* { dg-additional-options -save-temps } */ /* { dg-final { check-function-bodies {** } {} } } */ @@ -10,8 +9,10 @@ void *p; void f(void) { + // 0xdeadbeef p = __builtin_stack_save(); asm volatile ("" : : : "memory"); + // no-op __builtin_stack_restore(p); asm volatile ("" : : : "memory"); } @@ -19,14 +20,8 @@ void f(void) ** f: ** \.visible \.func f ** { -** st\.global\.u64 \[p\], %stack; +** \.reg\.u64 (%r[0-9]+); +** mov\.u64 \1, 3735928559; +** st\.global\.u64 \[p\], \1; ** ret; */ - -/* The concept of a '%stack' pointer doesn't apply like this for - '-mno-soft-stack': PTX "native" stacks (TODO). - - { dg-final { scan-assembler-not {%stack} { xfail *-*-* } } } */ - -/* As these are an internal-use built-in function, we don't bother with - emitting proper error diagnostics. */