On Fri, Dec 13, 2024 at 04:52:58PM +1300, Campbell Suter wrote:
> Each lambda that can be converted to a plain function pointer has a
> thunk generated for it, which invokes the body of the lambda function.
> 
> When a section attribute is added to a lambda function, it only applies
> to the body of the lambda function, and not the thunk. When a lambda is
> only ever used by converting it to a function pointer, the body of the
> lambda is inlined into this thunk. As a result, the section attribute
> is effectively ignored: the function it applied to is gone, and the thunk
> does not have the section attribute applied to it either.
> 
> This patch checks if a section attribute is present on a lambda, and
> applies it to the thunk.
> 
> The motivation for this change is embedded devices where most code is
> executed from flash, but code which must execute while the device is
> being reprogrammed can be moved to RAM by placing it in a different
> section.
> 
> This patch was tested with bootstrapping on x86-64 under WSL, and
> the newly added test was also run on 32-bit ARM.

Hi, thanks for the patch.
 
> gcc/cp/ChangeLog:
> 
>       * lambda.cc (maybe_add_lambda_conv_op): Don't ignore section
>       attributes on lambda functions which are converted to plain
>       function pointers.
> 
> gcc/testsuite/ChangeLog:
> 
>       * g++.dg/ext/attr-section-lambda.C: New test.
> 
> Signed-off-by: Campbell Suter <z...@znix.xyz>
> ---
>  gcc/cp/lambda.cc                              |  8 ++++
>  .../g++.dg/ext/attr-section-lambda.C          | 42 +++++++++++++++++++
>  2 files changed, 50 insertions(+)
>  create mode 100644 gcc/testsuite/g++.dg/ext/attr-section-lambda.C
> 
> diff --git a/gcc/cp/lambda.cc b/gcc/cp/lambda.cc
> index d8a15d97d..e8937cc0d 100644
> --- a/gcc/cp/lambda.cc
> +++ b/gcc/cp/lambda.cc
> @@ -32,6 +32,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "gimplify.h"
>  #include "target.h"
>  #include "decl.h"
> +#include "attribs.h"
>  
>  /* Constructor for a lambda expression.  */
>  
> @@ -1376,6 +1377,13 @@ maybe_add_lambda_conv_op (tree type)
>    if (generic_lambda_p)
>      fn = add_inherited_template_parms (fn, DECL_TI_TEMPLATE (callop));
>  
> +  if (lookup_attribute ("section", DECL_ATTRIBUTES (callop)))
> +    {
> +      duplicate_one_attribute(&DECL_ATTRIBUTES (fn),
> +     DECL_ATTRIBUTES (callop), "section");
> +      set_decl_section_name (statfn, callop);
> +    }

duplicate_one_attribute does two lookups, but we just looked up the
callop attr, and we just built up fn so it shouldn't have any attrs.
Thus I wonder if writing it like this would be a little neater:

  if (tree a = lookup_attribute ("section", DECL_ATTRIBUTES (callop)))
    {
      DECL_ATTRIBUTES (fn) = attr_chainon (DECL_ATTRIBUTES (fn), a);
      set_decl_section_name (fn, callop);
    }

Sadly I see that just the set_decl_section_name call wouldn't be enough...

Marek

Reply via email to