The uses of MPFR in gimple-ssa-sprintf.c apparently cause the library to allocates some internal caches that it then leaks on program exit, causing Valgrind memory leak errors. The MPFR manual "strongly advises to [call mpfr_free_cache] before terminating a thread, or before exiting when using tools like 'valgrind' (to avoid memory leaks being reported)) so the attached patch does just that.
It' seems like an obvious fix that could presumably be committed without a review or approval but I'd like to give others a chance to comment on the placement of the call and whether it should be guarded by ENABLE_VALGRIND_ANNOTATIONS. Joseph, since you commented on the bug, do you have a suggestion for a different site for it or a guard? The only other call to the function is in the Fortran FE and it's neither guarded nor does it appear to ever be called. Thanks Martin
PR tree-optimization/79699 - small memory leak in MPFR gcc/ChangeLog: PR tree-optimization/79699 * gimple-ssa-sprintf.c (pass_sprintf_length::execute): Free MPFR caches to avoid a memory leak on program exit. diff --git a/gcc/gimple-ssa-sprintf.c b/gcc/gimple-ssa-sprintf.c index 7688439..0c00fa0 100644 --- a/gcc/gimple-ssa-sprintf.c +++ b/gcc/gimple-ssa-sprintf.c @@ -3612,6 +3612,8 @@ pass_sprintf_length::execute (function *fun) /* Clean up object size info. */ fini_object_sizes (); + /* Clean up MPFR caches (see bug 79699). */ + mpfr_free_cache (); return 0; }