From 7ffc7de55b058e78cbc46803b89b712849ea35b3 Mon Sep 17 00:00:00 2001 From: Jason Merrill Date: Thu, 27 Jun 2019 17:29:19 -0400 Subject: [PATCH] PR c++/55442 - memory-hog with highly recursive constexpr. This testcase in the PR is extremely recursive, and therefore uses a huge amount of memory on caching the results of individual calls. We no longer need to track all calls to catch infinite recursion, as we have other limits on maximum depth and operations count. So let's only cache a few calls at the top level: 8 seems to be a reasonable compromise. gcc/c-family/ * c.opt (fconstexpr-loop-limit): New. gcc/cp/ * constexpr.c (push_cx_call_context): Return depth. (cxx_eval_call_expression): Don't cache past constexpr_cache_depth. From-SVN: r272765 --- gcc/c-family/ChangeLog | 5 +++++ gcc/c-family/c.opt | 4 ++++ gcc/cp/ChangeLog | 6 ++++++ gcc/cp/constexpr.c | 29 ++++++++++++++++++----------- gcc/doc/invoke.texi | 16 ++++++++++++++-- 5 files changed, 47 insertions(+), 13 deletions(-) diff --git a/gcc/c-family/ChangeLog b/gcc/c-family/ChangeLog index d40073660b8..22d7c6ecf7c 100644 --- a/gcc/c-family/ChangeLog +++ b/gcc/c-family/ChangeLog @@ -1,3 +1,8 @@ +2019-06-26 Jason Merrill + + PR c++/55442 - memory-hog with highly recursive constexpr. + * c.opt (fconstexpr-loop-limit): New. + 2019-06-25 Jakub Jelinek PR sanitizer/90954 diff --git a/gcc/c-family/c.opt b/gcc/c-family/c.opt index a4cf3bd623d..080066fa608 100644 --- a/gcc/c-family/c.opt +++ b/gcc/c-family/c.opt @@ -1424,6 +1424,10 @@ fconstexpr-depth= C++ ObjC++ Joined RejectNegative UInteger Var(max_constexpr_depth) Init(512) -fconstexpr-depth= Specify maximum constexpr recursion depth. +fconstexpr-cache-depth= +C++ ObjC++ Joined RejectNegative UInteger Var(constexpr_cache_depth) Init(8) +-fconstexpr-cache-depth= Specify maximum constexpr recursion cache depth. + fconstexpr-loop-limit= C++ ObjC++ Joined RejectNegative UInteger Var(constexpr_loop_limit) Init(262144) -fconstexpr-loop-limit= Specify maximum constexpr loop iteration count. diff --git a/gcc/cp/ChangeLog b/gcc/cp/ChangeLog index e6167651431..db7ddf50757 100644 --- a/gcc/cp/ChangeLog +++ b/gcc/cp/ChangeLog @@ -1,3 +1,9 @@ +2019-06-27 Jason Merrill + + PR c++/55442 - memory-hog with highly recursive constexpr. + * constexpr.c (push_cx_call_context): Return depth. + (cxx_eval_call_expression): Don't cache past constexpr_cache_depth. + 2019-06-27 Jan Hubicka * class.c (layout_class_type): Set TYPE_CXX_ODR_P for as-base diff --git a/gcc/cp/constexpr.c b/gcc/cp/constexpr.c index a3a36d09d5e..d11e7af3eb1 100644 --- a/gcc/cp/constexpr.c +++ b/gcc/cp/constexpr.c @@ -1447,16 +1447,17 @@ static vec call_stack; static int call_stack_tick; static int last_cx_error_tick; -static bool +static int push_cx_call_context (tree call) { ++call_stack_tick; if (!EXPR_HAS_LOCATION (call)) SET_EXPR_LOCATION (call, input_location); call_stack.safe_push (call); - if (call_stack.length () > (unsigned) max_constexpr_depth) + int len = call_stack.length (); + if (len > max_constexpr_depth) return false; - return true; + return len; } static void @@ -1587,7 +1588,7 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, tree fun = get_function_named_in_call (t); constexpr_call new_call = { NULL, NULL, NULL, 0, ctx->manifestly_const_eval }; - bool depth_ok; + int depth_ok; if (fun == NULL_TREE) return cxx_eval_internal_function (ctx, t, lval, @@ -1791,14 +1792,20 @@ cxx_eval_call_expression (const constexpr_ctx *ctx, tree t, entry = *slot; if (entry == NULL) { - /* We need to keep a pointer to the entry, not just the slot, as the - slot can move in the call to cxx_eval_builtin_function_call. */ - *slot = entry = ggc_alloc (); - *entry = new_call; - fb.preserve (); + /* Only cache up to constexpr_cache_depth to limit memory use. */ + if (depth_ok < constexpr_cache_depth) + { + /* We need to keep a pointer to the entry, not just the slot, as + the slot can move during evaluation of the body. */ + *slot = entry = ggc_alloc (); + *entry = new_call; + fb.preserve (); + } } - /* Calls that are in progress have their result set to NULL, - so that we can detect circular dependencies. */ + /* Calls that are in progress have their result set to NULL, so that we + can detect circular dependencies. Now that we only cache up to + constexpr_cache_depth this won't catch circular dependencies that + start deeper, but they'll hit the recursion or ops limit. */ else if (entry->result == NULL) { if (!ctx->quiet) diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi index 04fd504496e..6382a840281 100644 --- a/gcc/doc/invoke.texi +++ b/gcc/doc/invoke.texi @@ -209,8 +209,9 @@ in the following sections. @xref{C++ Dialect Options,,Options Controlling C++ Dialect}. @gccoptlist{-fabi-version=@var{n} -fno-access-control @gol -faligned-new=@var{n} -fargs-in-order=@var{n} -fchar8_t -fcheck-new @gol --fconstexpr-depth=@var{n} -fconstexpr-loop-limit=@var{n} @gol --fconstexpr-ops-limit=@var{n} -fno-elide-constructors @gol +-fconstexpr-depth=@var{n} -fconstexpr-cache-depth=@var{n} @gol +-fconstexpr-loop-limit=@var{n} -fconstexpr-ops-limit=@var{n} @gol +-fno-elide-constructors @gol -fno-enforce-eh-specs @gol -fno-gnu-keywords @gol -fno-implicit-templates @gol @@ -2527,6 +2528,17 @@ to @var{n}. A limit is needed to detect endless recursion during constant expression evaluation. The minimum specified by the standard is 512. +@item -fconstexpr-cache-depth=@var{n} +@opindex fconstexpr-cache-depth +Set the maximum level of nested evaluation depth for C++11 constexpr +functions that will be cached to @var{n}. This is a heuristic that +trades off compilation speed (when the cache avoids repeated +calculations) against memory consumption (when the cache grows very +large from highly recursive evaluations). The default is 8. Very few +users are likely to want to adjust it, but if your code does heavy +constexpr calculations you might want to experiment to find which +value works best for you. + @item -fconstexpr-loop-limit=@var{n} @opindex fconstexpr-loop-limit Set the maximum number of iterations for a loop in C++14 constexpr functions -- 2.30.2