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

commit d283eba8b886921424ef86ce1804ecfcbc49b8fc
Author: Thomas Schwinge <tschwi...@baylibre.com>
Date:   Mon May 26 13:31:54 2025 +0200

    Avoid SIGSEGV in nvptx 'mkoffload' for voluminous PTX code
    
    In commit 50be486dff4ea2676ed022e9524ef190b92ae2b1
    "nvptx: libgomp+mkoffload.cc: Prepare for reverse offload fn lookup", some
    additional tracking of the PTX code was added, and this assumes that
    potentially every single character of PTX code needs to be tracked as a new
    chunk of PTX code.  That's problematic if we're dealing with voluminous PTX
    code (for example, non-trivial C++ code), and the 'file_idx' 'alloca'tion 
then
    causes stack overflow.  For example:
    
        FAIL: libgomp.c++/target-std__valarray-1.C (test for excess errors)
        UNRESOLVED: libgomp.c++/target-std__valarray-1.C compilation failed to 
produce executable
    
        lto-wrapper: fatal error: 
[...]/build-gcc/gcc//accel/nvptx-none/mkoffload terminated with signal 11 
[Segmentation fault], core dumped
    
            gcc/
            * config/nvptx/mkoffload.cc (process): Use an 'auto_vec' for
            'file_idx'.
    
    (cherry picked from commit 01044e0ee27093a3990996578b15f6ab69ed3395)

Diff:
---
 gcc/config/nvptx/mkoffload.cc | 12 +++++++++---
 1 file changed, 9 insertions(+), 3 deletions(-)

diff --git a/gcc/config/nvptx/mkoffload.cc b/gcc/config/nvptx/mkoffload.cc
index 205bc2df14b1..bee18e75bb3c 100644
--- a/gcc/config/nvptx/mkoffload.cc
+++ b/gcc/config/nvptx/mkoffload.cc
@@ -260,8 +260,10 @@ process (FILE *in, FILE *out, uint32_t omp_requires)
   unsigned ix;
   const char *sm_ver = NULL, *version = NULL;
   const char *sm_ver2 = NULL, *version2 = NULL;
-  size_t file_cnt = 0;
-  size_t *file_idx = XALLOCAVEC (size_t, len);
+  /* To reduce the number of reallocations for 'file_idx', guess 'file_cnt'
+     (very roughly...), based on 'len'.  */
+  const size_t file_cnt_guessed = 13 + len / 27720;
+  auto_vec<size_t> file_idx (file_cnt_guessed);
 
   fprintf (out, "#include <stdint.h>\n\n");
 
@@ -269,9 +271,10 @@ process (FILE *in, FILE *out, uint32_t omp_requires)
      terminated by a NUL.  */
   for (size_t i = 0; i != len;)
     {
+      file_idx.safe_push (i);
+
       char c;
       bool output_fn_ptr = false;
-      file_idx[file_cnt++] = i;
 
       fprintf (out, "static const char ptx_code_%u[] =\n\t\"", obj_count++);
       while ((c = input[i++]))
@@ -349,6 +352,9 @@ process (FILE *in, FILE *out, uint32_t omp_requires)
        }
     }
 
+  const size_t file_cnt = file_idx.length ();
+  gcc_checking_assert (file_cnt == obj_count);
+
   /* Create function-pointer array, required for reverse
      offload function-pointer lookup.  */

Reply via email to