public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] Make mergeable read-only sections per-function, if requested.
@ 2015-05-06 23:00 Segher Boessenkool
  2015-05-07  8:15 ` Marek Polacek
  0 siblings, 1 reply; 6+ messages in thread
From: Segher Boessenkool @ 2015-05-06 23:00 UTC (permalink / raw)
  To: gcc-patches; +Cc: Segher Boessenkool

Currently GCC does not put mergeable read-only data in a per-function
section, so the -gc-sections linker option does not do much for such
data.  Fix that.

Bootstrapped and tested on powerpc64-linux; no regressions.

Is this okay for trunk?


Segher


2015-05-06  Segher Boessenkool  <segher@kernel.crashing.org>

	PR middle-end/192
	PR middle-end/54303
	* varasm.c (function_mergeable_rodata_prefix): New function.
	(mergeable_string_section): Use it.
	(mergeable_constant_section): Use it.

---
 gcc/varasm.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/gcc/varasm.c b/gcc/varasm.c
index 62d5163..d8f077d 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -783,6 +783,16 @@ default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
   return readonly_data_section;
 }
 
+const char *
+function_mergeable_rodata_prefix (void)
+{
+  section *s = targetm.asm_out.function_rodata_section (current_function_decl);
+  if (SECTION_STYLE (s) == SECTION_NAMED)
+    return s->named.name;
+      else
+    return targetm.asm_out.mergeable_rodata_prefix;
+}
+
 /* Return the section to use for string merging.  */
 
 static section *
@@ -804,7 +814,7 @@ mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
       const char *str;
       HOST_WIDE_INT i;
       int j, unit;
-      const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
+      const char *prefix = function_mergeable_rodata_prefix ();
       char *name = (char *) alloca (strlen (prefix) + 30);
 
       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
@@ -857,7 +867,7 @@ mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
       && align <= 256
       && (align & (align - 1)) == 0)
     {
-      const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
+      const char *prefix = function_mergeable_rodata_prefix ();
       char *name = (char *) alloca (strlen (prefix) + 30);
 
       sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
-- 
1.8.1.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make mergeable read-only sections per-function, if requested.
  2015-05-06 23:00 [PATCH] Make mergeable read-only sections per-function, if requested Segher Boessenkool
@ 2015-05-07  8:15 ` Marek Polacek
  2015-05-07 12:31   ` Segher Boessenkool
  2015-05-07 13:29   ` Jeff Law
  0 siblings, 2 replies; 6+ messages in thread
From: Marek Polacek @ 2015-05-07  8:15 UTC (permalink / raw)
  To: Segher Boessenkool; +Cc: gcc-patches

On Wed, May 06, 2015 at 03:59:33PM -0700, Segher Boessenkool wrote:
> Currently GCC does not put mergeable read-only data in a per-function
> section, so the -gc-sections linker option does not do much for such
> data.  Fix that.

Dunno if it is a good idea, but...

> diff --git a/gcc/varasm.c b/gcc/varasm.c
> index 62d5163..d8f077d 100644
> --- a/gcc/varasm.c
> +++ b/gcc/varasm.c
> @@ -783,6 +783,16 @@ default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
>    return readonly_data_section;
>  }
>  
> +const char *
> +function_mergeable_rodata_prefix (void)

This function needs a comment.

> +{
> +  section *s = targetm.asm_out.function_rodata_section (current_function_decl);
> +  if (SECTION_STYLE (s) == SECTION_NAMED)
> +    return s->named.name;
> +      else
> +    return targetm.asm_out.mergeable_rodata_prefix;

else is wrongly formatted.

	Marek

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make mergeable read-only sections per-function, if requested.
  2015-05-07  8:15 ` Marek Polacek
@ 2015-05-07 12:31   ` Segher Boessenkool
  2015-05-07 13:31     ` Jeff Law
  2015-05-07 13:29   ` Jeff Law
  1 sibling, 1 reply; 6+ messages in thread
From: Segher Boessenkool @ 2015-05-07 12:31 UTC (permalink / raw)
  To: Marek Polacek; +Cc: gcc-patches

On Thu, May 07, 2015 at 10:15:08AM +0200, Marek Polacek wrote:
> > +const char *
> > +function_mergeable_rodata_prefix (void)
> 
> This function needs a comment.

And much more importantly, I forgot to make it static.  Fixed.

> > +{
> > +  section *s = targetm.asm_out.function_rodata_section (current_function_decl);
> > +  if (SECTION_STYLE (s) == SECTION_NAMED)
> > +    return s->named.name;
> > +      else
> > +    return targetm.asm_out.mergeable_rodata_prefix;
> 
> else is wrongly formatted.

Huh, how did I do this.  Fixed.

Thanks for the review.  New patch:

- - -

Make mergeable read-only sections per-function, if requested.

Currently GCC does not put mergeable read-only data in a per-function
section, so the --gc-sections linker option does not do much for such
data.  Fix that.

Bootstrapped and tested on powerpc64-linux; no regressions.

Is this okay for trunk?


Segher


2015-05-07  Segher Boessenkool  <segher@kernel.crashing.org>

	PR middle-end/192
	PR middle-end/54303
	* varasm.c (function_mergeable_rodata_prefix): New function.
	(mergeable_string_section): Use it.
	(mergeable_constant_section): Use it.
---
 gcc/varasm.c | 16 ++++++++++++++--
 1 file changed, 14 insertions(+), 2 deletions(-)

diff --git a/gcc/varasm.c b/gcc/varasm.c
index 62d5163..11cb2c5 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -783,6 +783,18 @@ default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
   return readonly_data_section;
 }
 
+/* A subroutine of mergeable_string_section and mergeable_constant_section.  */
+
+static const char *
+function_mergeable_rodata_prefix (void)
+{
+  section *s = targetm.asm_out.function_rodata_section (current_function_decl);
+  if (SECTION_STYLE (s) == SECTION_NAMED)
+    return s->named.name;
+  else
+    return targetm.asm_out.mergeable_rodata_prefix;
+}
+
 /* Return the section to use for string merging.  */
 
 static section *
@@ -804,7 +816,7 @@ mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
       const char *str;
       HOST_WIDE_INT i;
       int j, unit;
-      const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
+      const char *prefix = function_mergeable_rodata_prefix ();
       char *name = (char *) alloca (strlen (prefix) + 30);
 
       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
@@ -857,7 +869,7 @@ mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
       && align <= 256
       && (align & (align - 1)) == 0)
     {
-      const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
+      const char *prefix = function_mergeable_rodata_prefix ();
       char *name = (char *) alloca (strlen (prefix) + 30);
 
       sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
-- 
1.8.1.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make mergeable read-only sections per-function, if requested.
  2015-05-07  8:15 ` Marek Polacek
  2015-05-07 12:31   ` Segher Boessenkool
@ 2015-05-07 13:29   ` Jeff Law
  1 sibling, 0 replies; 6+ messages in thread
From: Jeff Law @ 2015-05-07 13:29 UTC (permalink / raw)
  To: Marek Polacek, Segher Boessenkool; +Cc: gcc-patches

On 05/07/2015 02:15 AM, Marek Polacek wrote:
> On Wed, May 06, 2015 at 03:59:33PM -0700, Segher Boessenkool wrote:
>> Currently GCC does not put mergeable read-only data in a per-function
>> section, so the -gc-sections linker option does not do much for such
>> data.  Fix that.
>
> Dunno if it is a good idea, but...
Why not?  In a static build it gives the linker a chance to remove 
unreferenced data or to reorder the data to be more tlb or dcache 
friendly.  That's the whole purpose behind -f{function,data}-sections.

Jeff


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make mergeable read-only sections per-function, if requested.
  2015-05-07 12:31   ` Segher Boessenkool
@ 2015-05-07 13:31     ` Jeff Law
  2015-05-07 15:51       ` Segher Boessenkool
  0 siblings, 1 reply; 6+ messages in thread
From: Jeff Law @ 2015-05-07 13:31 UTC (permalink / raw)
  To: Segher Boessenkool, Marek Polacek; +Cc: gcc-patches

On 05/07/2015 06:30 AM, Segher Boessenkool wrote:
> On Thu, May 07, 2015 at 10:15:08AM +0200, Marek Polacek wrote:
>>> +const char *
>>> +function_mergeable_rodata_prefix (void)
>>
>> This function needs a comment.
>
> And much more importantly, I forgot to make it static.  Fixed.
>
>>> +{
>>> +  section *s = targetm.asm_out.function_rodata_section (current_function_decl);
>>> +  if (SECTION_STYLE (s) == SECTION_NAMED)
>>> +    return s->named.name;
>>> +      else
>>> +    return targetm.asm_out.mergeable_rodata_prefix;
>>
>> else is wrongly formatted.
>
> Huh, how did I do this.  Fixed.
>
> Thanks for the review.  New patch:
>
> - - -
>
> Make mergeable read-only sections per-function, if requested.
>
> Currently GCC does not put mergeable read-only data in a per-function
> section, so the --gc-sections linker option does not do much for such
> data.  Fix that.
>
> Bootstrapped and tested on powerpc64-linux; no regressions.
>
> Is this okay for trunk?
>
>
> Segher
>
>
> 2015-05-07  Segher Boessenkool  <segher@kernel.crashing.org>
>
> 	PR middle-end/192
> 	PR middle-end/54303
> 	* varasm.c (function_mergeable_rodata_prefix): New function.
> 	(mergeable_string_section): Use it.
> 	(mergeable_constant_section): Use it.
If you could cobble together a little test (ppc specific is fine with 
me) it'd be appreciated.

With that, approved for the trunk.

THanks,
jeff

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] Make mergeable read-only sections per-function, if requested.
  2015-05-07 13:31     ` Jeff Law
@ 2015-05-07 15:51       ` Segher Boessenkool
  0 siblings, 0 replies; 6+ messages in thread
From: Segher Boessenkool @ 2015-05-07 15:51 UTC (permalink / raw)
  To: Jeff Law; +Cc: Marek Polacek, gcc-patches

On Thu, May 07, 2015 at 07:31:33AM -0600, Jeff Law wrote:
> If you could cobble together a little test (ppc specific is fine with 
> me) it'd be appreciated.
> 
> With that, approved for the trunk.

I did one for *-*-linux*.  Tested on powerpc64-linux and x86_64-linux.
Committed.


Segher


2015-05-07  Segher Boessenkool  <segher@kernel.crashing.org>

gcc/
	PR middle-end/192
	PR middle-end/54303
	* varasm.c (function_mergeable_rodata_prefix): New function.
	(mergeable_string_section): Use it.
	(mergeable_constant_section): Use it.

gcc/testsuite/
	PR middle-end/192
	PR middle-end/54303
	* gcc.dg/fdata-sections-2.c: New file.

---
 gcc/testsuite/gcc.dg/fdata-sections-2.c | 18 ++++++++++++++++++
 gcc/varasm.c                            | 16 ++++++++++++++--
 2 files changed, 32 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/fdata-sections-2.c

diff --git a/gcc/testsuite/gcc.dg/fdata-sections-2.c b/gcc/testsuite/gcc.dg/fdata-sections-2.c
new file mode 100644
index 0000000..dda90ba7
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/fdata-sections-2.c
@@ -0,0 +1,18 @@
+/* PR middle-end/192 */
+/* PR middle-end/54303 */
+
+/* This checks that string constants are put in per-function rodata
+   sections, so that they can be garbage collected.  */
+
+/* { dg-do compile { target *-*-linux* } } */
+/* { dg-options "-O -ffunction-sections -fdata-sections" } */
+
+const char *f1(void) { return "falderalde"; }
+const char *f2(void) { return "a"; }
+const char *f3(void) { return "falderalde"; }
+const char *f4(void) { return "eralde"; }
+
+/* { dg-final { scan-assembler {\.rodata\.f1\.str} } } */
+/* { dg-final { scan-assembler {\.rodata\.f2\.str} } } */
+/* { dg-final { scan-assembler-not {\.rodata\.f3\.str} } } */
+/* { dg-final { scan-assembler {\.rodata\.f4\.str} } } */
diff --git a/gcc/varasm.c b/gcc/varasm.c
index 62d5163..11cb2c5 100644
--- a/gcc/varasm.c
+++ b/gcc/varasm.c
@@ -783,6 +783,18 @@ default_no_function_rodata_section (tree decl ATTRIBUTE_UNUSED)
   return readonly_data_section;
 }
 
+/* A subroutine of mergeable_string_section and mergeable_constant_section.  */
+
+static const char *
+function_mergeable_rodata_prefix (void)
+{
+  section *s = targetm.asm_out.function_rodata_section (current_function_decl);
+  if (SECTION_STYLE (s) == SECTION_NAMED)
+    return s->named.name;
+  else
+    return targetm.asm_out.mergeable_rodata_prefix;
+}
+
 /* Return the section to use for string merging.  */
 
 static section *
@@ -804,7 +816,7 @@ mergeable_string_section (tree decl ATTRIBUTE_UNUSED,
       const char *str;
       HOST_WIDE_INT i;
       int j, unit;
-      const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
+      const char *prefix = function_mergeable_rodata_prefix ();
       char *name = (char *) alloca (strlen (prefix) + 30);
 
       mode = TYPE_MODE (TREE_TYPE (TREE_TYPE (decl)));
@@ -857,7 +869,7 @@ mergeable_constant_section (machine_mode mode ATTRIBUTE_UNUSED,
       && align <= 256
       && (align & (align - 1)) == 0)
     {
-      const char *prefix = targetm.asm_out.mergeable_rodata_prefix;
+      const char *prefix = function_mergeable_rodata_prefix ();
       char *name = (char *) alloca (strlen (prefix) + 30);
 
       sprintf (name, "%s.cst%d", prefix, (int) (align / 8));
-- 
1.8.1.4

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2015-05-07 15:51 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-06 23:00 [PATCH] Make mergeable read-only sections per-function, if requested Segher Boessenkool
2015-05-07  8:15 ` Marek Polacek
2015-05-07 12:31   ` Segher Boessenkool
2015-05-07 13:31     ` Jeff Law
2015-05-07 15:51       ` Segher Boessenkool
2015-05-07 13:29   ` Jeff Law

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).