Valgrind shows this fixes ~1 KB of leak per iteration (on x86_64) by
plugging these leaks allocated at opts.c lines 286 and 289:

==57820== 2,752 bytes in 4 blocks are definitely lost in loss record 875 of 917
==57820==    at 0x4A0645D: malloc (in 
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==57820==    by 0x59A6747: xmalloc (xmalloc.c:147)
==57820==    by 0x595542A: init_options_struct(gcc_options*, gcc_options*) 
(opts.c:286)
==57820==    by 0x4E2ED61: toplev::main(int, char**) (toplev.c:2081)
==57820==    by 0x4E43186: gcc::jit::playback::context::compile() 
(jit-playback.c:1615)
==57820==    by 0x4E4018D: gcc::jit::recording::context::compile() 
(jit-recording.c:861)
==57820==    by 0x401CA4: test_jit (harness.h:190)
==57820==    by 0x401D88: main (harness.h:232)
==57820==
==57820== 2,752 bytes in 4 blocks are definitely lost in loss record 876 of 917
==57820==    at 0x4A081D4: calloc (in 
/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so)
==57820==    by 0x59A6780: xcalloc (xmalloc.c:162)
==57820==    by 0x595543E: init_options_struct(gcc_options*, gcc_options*) 
(opts.c:289)
==57820==    by 0x4E2ED61: toplev::main(int, char**) (toplev.c:2081)
==57820==    by 0x4E43186: gcc::jit::playback::context::compile() 
(jit-playback.c:1615)
==57820==    by 0x4E4018D: gcc::jit::recording::context::compile() 
(jit-recording.c:861)
==57820==    by 0x401CA4: test_jit (harness.h:190)
==57820==    by 0x401D88: main (harness.h:232)

gcc/ChangeLog:
        PR jit/63854
        * opts.c (finalize_options_struct): New.
        * opts.h (finalize_options_struct): New.
        * toplev.c (toplev::finalize): Call finalize_options_struct
        on global_options and global_options_set.
---
 gcc/opts.c   | 8 ++++++++
 gcc/opts.h   | 1 +
 gcc/toplev.c | 3 +++
 3 files changed, 12 insertions(+)

diff --git a/gcc/opts.c b/gcc/opts.c
index d22882b..dabd3c6 100644
--- a/gcc/opts.c
+++ b/gcc/opts.c
@@ -307,6 +307,14 @@ init_options_struct (struct gcc_options *opts, struct 
gcc_options *opts_set)
   targetm_common.option_init_struct (opts);
 }
 
+/* Release any allocations owned by OPTS.  */
+
+void
+finalize_options_struct (struct gcc_options *opts)
+{
+  XDELETEVEC (opts->x_param_values);
+}
+
 /* If indicated by the optimization level LEVEL (-Os if SIZE is set,
    -Ofast if FAST is set, -Og if DEBUG is set), apply the option DEFAULT_OPT
    to OPTS and OPTS_SET, diagnostic context DC, location LOC, with language
diff --git a/gcc/opts.h b/gcc/opts.h
index f694082..c3ec942 100644
--- a/gcc/opts.h
+++ b/gcc/opts.h
@@ -325,6 +325,7 @@ extern void decode_cmdline_options_to_array (unsigned int 
argc,
 extern void init_options_once (void);
 extern void init_options_struct (struct gcc_options *opts,
                                 struct gcc_options *opts_set);
+extern void finalize_options_struct (struct gcc_options *opts);
 extern void decode_cmdline_options_to_array_default_mask (unsigned int argc,
                                                          const char **argv, 
                                                          struct 
cl_decoded_option **decoded_options,
diff --git a/gcc/toplev.c b/gcc/toplev.c
index 2e48047..4b4e568 100644
--- a/gcc/toplev.c
+++ b/gcc/toplev.c
@@ -2169,4 +2169,7 @@ toplev::finalize (void)
   ipa_cp_c_finalize ();
   ipa_reference_c_finalize ();
   params_c_finalize ();
+
+  finalize_options_struct (&global_options);
+  finalize_options_struct (&global_options_set);
 }
-- 
1.8.5.3

Reply via email to