From 22e76700aea63eb8c298403bb2e717a6a4fc9c1e Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwinge@baylibre.com>
Date: Mon, 24 Feb 2025 10:09:11 +0100
Subject: [PATCH] nvptx: Delay 'sorry, unimplemented: dynamic stack allocation
 not supported' from expansion time to code generation

This gives the back end a chance to clean out a few more unnecessary instances
of dynamic stack allocation.  This progresses:

    PASS: gcc.dg/pr78902.c  (test for warnings, line 7)
    PASS: gcc.dg/pr78902.c  (test for warnings, line 8)
    PASS: gcc.dg/pr78902.c  (test for warnings, line 9)
    PASS: gcc.dg/pr78902.c  (test for warnings, line 10)
    PASS: gcc.dg/pr78902.c  (test for warnings, line 11)
    PASS: gcc.dg/pr78902.c  (test for warnings, line 12)
    PASS: gcc.dg/pr78902.c  (test for warnings, line 13)
    PASS: gcc.dg/pr78902.c strndup excessive bound at line 14 (test for warnings, line 13)
    [-UNSUPPORTED: gcc.dg/pr78902.c: dynamic stack allocation not supported-]
    {+PASS: gcc.dg/pr78902.c (test for excess errors)+}

    UNSUPPORTED: gcc.dg/torture/pr71901.c   -O0 : dynamic stack allocation not supported
    [-UNSUPPORTED:-]{+PASS:+} gcc.dg/torture/pr71901.c   -O1  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}
    UNSUPPORTED: gcc.dg/torture/pr71901.c   -O2 : dynamic stack allocation not supported
    UNSUPPORTED: gcc.dg/torture/pr71901.c   -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions : dynamic stack allocation not supported
    UNSUPPORTED: gcc.dg/torture/pr71901.c   -O3 -g : dynamic stack allocation not supported
    [-UNSUPPORTED:-]{+PASS:+} gcc.dg/torture/pr71901.c   -Os  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}

    UNSUPPORTED: gcc.dg/torture/pr78742.c   -O0 : dynamic stack allocation not supported
    [-UNSUPPORTED:-]{+PASS:+} gcc.dg/torture/pr78742.c   -O1  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}
    [-UNSUPPORTED:-]{+PASS:+} gcc.dg/torture/pr78742.c   -O2  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}
    [-UNSUPPORTED:-]{+PASS:+} gcc.dg/torture/pr78742.c   -O3 -fomit-frame-pointer -funroll-loops -fpeel-loops -ftracer -finline-functions  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}
    [-UNSUPPORTED:-]{+PASS:+} gcc.dg/torture/pr78742.c   -O3 -g  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}
    UNSUPPORTED: gcc.dg/torture/pr78742.c   -Os : dynamic stack allocation not supported

    [-UNSUPPORTED:-]{+PASS:+} gfortran.dg/pr101267.f90   -O  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}

    [-UNSUPPORTED:-]{+PASS:+} gfortran.dg/pr112404.f90   -O  [-: dynamic stack allocation not supported-]{+(test for excess errors)+}

	gcc/
	* config/nvptx/nvptx.md (define_expand "allocate_stack")
	[!TARGET_SOFT_STACK]: Move
	'sorry ("dynamic stack allocation not supported");'...
	(define_insn "@nvptx_alloca_<mode>"): ... here.
	gcc/testsuite/
	* gcc.target/nvptx/alloca-1-unused-O0-sm_30.c: Adjust.
---
 gcc/config/nvptx/nvptx.md                     | 42 ++++++++++---------
 .../nvptx/alloca-1-unused-O0-sm_30.c          | 12 +++++-
 2 files changed, 32 insertions(+), 22 deletions(-)

diff --git a/gcc/config/nvptx/nvptx.md b/gcc/config/nvptx/nvptx.md
index e7d3630d4c4b..d08afd4378bf 100644
--- a/gcc/config/nvptx/nvptx.md
+++ b/gcc/config/nvptx/nvptx.md
@@ -1671,15 +1671,8 @@
    (match_operand 1 "nvptx_register_operand")]
   ""
 {
-  if (!TARGET_SOFT_STACK
-      && TARGET_PTX_7_3
-      && TARGET_SM52)
+  if (!TARGET_SOFT_STACK)
     emit_insn (gen_nvptx_alloca (Pmode, operands[0], operands[1]));
-  else if (!TARGET_SOFT_STACK)
-    {
-      sorry ("dynamic stack allocation not supported");
-      emit_insn (gen_nop ());
-    }
   else if (TARGET_SOFT_STACK)
     {
       emit_move_insn (stack_pointer_rtx,
@@ -1696,19 +1689,28 @@
   [(set (match_operand:P 0 "nvptx_register_operand" "=R")
         (unspec:P [(match_operand:P 1 "nvptx_nonmemory_operand" "Ri")]
 		  UNSPEC_ALLOCA))]
-  "TARGET_PTX_7_3
-   && TARGET_SM52"
+  ""
   {
-    /* Convert the address from '.local' state space to generic.  That way,
-       we don't have to use 'st.local', 'ld.local', and can easily pass the
-       address to other "generic functions".
-       TODO 'gcc.target/nvptx/alloca-5.c' */
-    output_asm_insn ("{", NULL);
-    output_asm_insn ("\\t.reg%t0\\t%0_local;", operands);
-    output_asm_insn ("\\talloca%u0\\t%0_local, %1;", operands);
-    output_asm_insn ("\\tcvta.local%u0\\t%0, %0_local;", operands);
-    output_asm_insn ("}", NULL);
-    return "";
+    if (TARGET_PTX_7_3
+	&& TARGET_SM52)
+      {
+	/* Convert the address from '.local' state space to generic.  That way,
+	   we don't have to use 'st.local', 'ld.local', and can easily pass the
+	   address to other "generic functions".
+	   TODO 'gcc.target/nvptx/alloca-5.c' */
+	output_asm_insn ("{", NULL);
+	output_asm_insn ("\\t.reg%t0\\t%0_local;", operands);
+	output_asm_insn ("\\talloca%u0\\t%0_local, %1;", operands);
+	output_asm_insn ("\\tcvta.local%u0\\t%0, %0_local;", operands);
+	output_asm_insn ("}", NULL);
+	return "";
+      }
+    else
+      {
+	sorry_at (INSN_LOCATION (insn),
+		  "dynamic stack allocation not supported");
+	return "";
+      }
   }
   [(set_attr "predicable" "no")])
 
diff --git a/gcc/testsuite/gcc.target/nvptx/alloca-1-unused-O0-sm_30.c b/gcc/testsuite/gcc.target/nvptx/alloca-1-unused-O0-sm_30.c
index c948ccc1f1b9..32e85e64cc51 100644
--- a/gcc/testsuite/gcc.target/nvptx/alloca-1-unused-O0-sm_30.c
+++ b/gcc/testsuite/gcc.target/nvptx/alloca-1-unused-O0-sm_30.c
@@ -1,9 +1,17 @@
-/* { dg-do compile } */
+/* { dg-do assemble } */
 /* { dg-options {-O0 -mno-soft-stack} } */
 /* { dg-additional-options -march=sm_30 } */
+/* { dg-additional-options -save-temps } */
+/* { dg-final { check-function-bodies {** } {} } } */
 
 void f(void)
 {
   __builtin_alloca(123); /* { dg-warning "ignoring return value of '__builtin_alloca' declared with attribute 'warn_unused_result'" } */
-  /* { dg-message {sorry, unimplemented: dynamic stack allocation not supported} {} { target *-*-* } .-1 } */
+  /* { dg-bogus {sorry, unimplemented: dynamic stack allocation not supported} {} { target *-*-* } .-1 } */
 }
+/*
+** f:
+** \.visible \.func f
+** {
+** 	ret;
+*/
-- 
GitLab