diff mbox series

tree-sra: Avoid refreshing into const base decls (PR 100453)

Message ID ri6lf8istyn.fsf@suse.cz
State New
Headers show
Series tree-sra: Avoid refreshing into const base decls (PR 100453) | expand

Commit Message

Martin Jambor May 13, 2021, 12:23 p.m. UTC
Hi,

When SRA transforms an assignment where the RHS is an aggregate decl
that it creates replacements for, the (least efficient) fallback
method of dealing with them is to store all the replacements back into
the original decl and then let the original assignment takes its
course.

That of course should not need to be done for TREE_READONLY bases
which cannot change contents.  The SRA code handled this situation in
one of two necessary places but only for DECL_IN_CONSTANT_POOL const
decls, this patch modifies both to check TREE_READONLY.

Bootstrapped and tested on aarch64-linux, OK for trunk?

Thanks,

Martin



gcc/ChangeLog:

2021-05-12  Martin Jambor  <mjambor@suse.cz>

	PR tree-optimization/100453
	* tree-sra.c (sra_modify_assign): All const base accesses do not
	need refreshing, not just those from decl_pool.
	(sra_modify_assign): Do not refresh into a const base decl.

gcc/testsuite/ChangeLog:

2021-05-12  Martin Jambor  <mjambor@suse.cz>

	PR tree-optimization/100453
	* gcc.dg/tree-ssa/pr100453.c: New test.
---
 gcc/testsuite/gcc.dg/tree-ssa/pr100453.c | 18 ++++++++++++++++++
 gcc/tree-sra.c                           |  4 ++--
 2 files changed, 20 insertions(+), 2 deletions(-)
 create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/pr100453.c

Comments

Jeff Law May 13, 2021, 3:17 p.m. UTC | #1
On 5/13/2021 6:23 AM, Martin Jambor wrote:
> Hi,
>
> When SRA transforms an assignment where the RHS is an aggregate decl
> that it creates replacements for, the (least efficient) fallback
> method of dealing with them is to store all the replacements back into
> the original decl and then let the original assignment takes its
> course.
>
> That of course should not need to be done for TREE_READONLY bases
> which cannot change contents.  The SRA code handled this situation in
> one of two necessary places but only for DECL_IN_CONSTANT_POOL const
> decls, this patch modifies both to check TREE_READONLY.
>
> Bootstrapped and tested on aarch64-linux, OK for trunk?
>
> Thanks,
>
> Martin
>
>
>
> gcc/ChangeLog:
>
> 2021-05-12  Martin Jambor  <mjambor@suse.cz>
>
> 	PR tree-optimization/100453
> 	* tree-sra.c (sra_modify_assign): All const base accesses do not
> 	need refreshing, not just those from decl_pool.
> 	(sra_modify_assign): Do not refresh into a const base decl.
>
> gcc/testsuite/ChangeLog:
>
> 2021-05-12  Martin Jambor  <mjambor@suse.cz>
>
> 	PR tree-optimization/100453
> 	* gcc.dg/tree-ssa/pr100453.c: New test.

OK

jeff
Bernd Edlinger May 14, 2021, 4:43 p.m. UTC | #2
On 5/13/21 5:17 PM, Jeff Law via Gcc-patches wrote:
> 
> On 5/13/2021 6:23 AM, Martin Jambor wrote:
>> Hi,
>>
>> When SRA transforms an assignment where the RHS is an aggregate decl
>> that it creates replacements for, the (least efficient) fallback
>> method of dealing with them is to store all the replacements back into
>> the original decl and then let the original assignment takes its
>> course.
>>
>> That of course should not need to be done for TREE_READONLY bases
>> which cannot change contents.  The SRA code handled this situation in
>> one of two necessary places but only for DECL_IN_CONSTANT_POOL const
>> decls, this patch modifies both to check TREE_READONLY.
>>
>> Bootstrapped and tested on aarch64-linux, OK for trunk?
>>
>> Thanks,
>>
>> Martin
>>
>>
>>
>> gcc/ChangeLog:
>>
>> 2021-05-12  Martin Jambor  <mjambor@suse.cz>
>>
>>     PR tree-optimization/100453
>>     * tree-sra.c (sra_modify_assign): All const base accesses do not
>>     need refreshing, not just those from decl_pool.
>>     (sra_modify_assign): Do not refresh into a const base decl.
>>
>> gcc/testsuite/ChangeLog:
>>
>> 2021-05-12  Martin Jambor  <mjambor@suse.cz>
>>
>>     PR tree-optimization/100453
>>     * gcc.dg/tree-ssa/pr100453.c: New test.
> 
> OK
> 
> jeff
> 
> 

Looks like this caused:

                === acats tests ===
FAIL:   c41325a
FAIL:   c45347d
FAIL:   c74402a
FAIL:   c95085m
FAIL:   cc3601a

                === gnat tests ===

FAIL: gnat.dg/addr12.adb (test for excess errors)
UNRESOLVED: gnat.dg/addr12.adb compilation failed to produce executable
FAIL: gnat.dg/addr12_a.adb (test for excess errors)
FAIL: gnat.dg/bip_overlay.adb (test for excess errors)
FAIL: gnat.dg/global.adb (test for excess errors)
FAIL: gnat.dg/spark1.adb  (test for errors, line 8)
FAIL: gnat.dg/spark1.adb (test for excess errors)
FAIL: gnat.dg/sync2.adb (test for excess errors)
FAIL: gnat.dg/synchronized1.adb (test for excess errors)


Bernd.
Eric Botcazou May 14, 2021, 4:49 p.m. UTC | #3
> Looks like this caused:
> 
>                 === acats tests ===
> FAIL:   c41325a
> FAIL:   c45347d
> FAIL:   c74402a
> FAIL:   c95085m
> FAIL:   cc3601a
> 
>                 === gnat tests ===
> 
> FAIL: gnat.dg/addr12.adb (test for excess errors)
> UNRESOLVED: gnat.dg/addr12.adb compilation failed to produce executable
> FAIL: gnat.dg/addr12_a.adb (test for excess errors)
> FAIL: gnat.dg/bip_overlay.adb (test for excess errors)
> FAIL: gnat.dg/global.adb (test for excess errors)
> FAIL: gnat.dg/spark1.adb  (test for errors, line 8)
> FAIL: gnat.dg/spark1.adb (test for excess errors)
> FAIL: gnat.dg/sync2.adb (test for excess errors)
> FAIL: gnat.dg/synchronized1.adb (test for excess errors)

Yes, it did, as well as PR boostrap/100597 probably.
Martin Jambor May 15, 2021, 8:33 a.m. UTC | #4
Hi,

On Fri, May 14 2021, Eric Botcazou wrote:
>> Looks like this caused:
>> 
>>                 === acats tests ===
>> FAIL:   c41325a
>> FAIL:   c45347d
>> FAIL:   c74402a
>> FAIL:   c95085m
>> FAIL:   cc3601a
>> 
>>                 === gnat tests ===
>> 
>> FAIL: gnat.dg/addr12.adb (test for excess errors)
>> UNRESOLVED: gnat.dg/addr12.adb compilation failed to produce executable
>> FAIL: gnat.dg/addr12_a.adb (test for excess errors)
>> FAIL: gnat.dg/bip_overlay.adb (test for excess errors)
>> FAIL: gnat.dg/global.adb (test for excess errors)
>> FAIL: gnat.dg/spark1.adb  (test for errors, line 8)
>> FAIL: gnat.dg/spark1.adb (test for excess errors)
>> FAIL: gnat.dg/sync2.adb (test for excess errors)
>> FAIL: gnat.dg/synchronized1.adb (test for excess errors)
>
> Yes, it did, as well as PR boostrap/100597 probably.
>

Sorry, I forgot I do not test Ada on Aarch64 (where I chose to do the
testing because the architecture has DECL_IN_CONSTANT_POOL constnts).

I have reverted the commit until I figure out what is going on.

Sorry again,

Martin
Martin Jambor May 17, 2021, 5:22 p.m. UTC | #5
Hi Eric,

sorry for breaking Ada over the weekend, however...

On Fri, May 14 2021, Eric Botcazou wrote:
>> Looks like this caused:
>> 
>>                 === acats tests ===
>> FAIL:   c41325a

None of the non-ACATS tests fail for me without doing a bootstrap, but I
did manage to reproduce this one (by the way, there really should be a
documentation on how to run ACATS tests manually, I should not need to
spend an hour and half figuring it out).

The problem is that before (early) SRA, there is a TREE_READONLY decl
that is being written to and my patch eliminated those writes.
Specifically, I see:

  <bb 183> :
  var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[1]{lb: 1 sz: 1} = _877;
  _880 = report.ident_bool (1);

  <bb 184> :
  var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[2]{lb: 1 sz: 1} = _880;
  _883 = report.ident_bool (1);

  <bb 185> :
  var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[3]{lb: 1 sz: 1} = _883;
  _886 = report.ident_bool (1);

  <bb 186> :
  var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[4]{lb: 1 sz: 1} = _886;
  _889 = report.ident_bool (1);

[...and many more.]  Note that this is an -fdump-tree-all-uid dump, the
DECL that is being assigned to has DECL_UID 5012 and when I dump it:

DECL_UID of racc->base is: 5012
print_node (dump_file, "", racc->base, 0):
 <var_decl 0x7fc249fbc5a0 var_ara_5
    type <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD sizes-gimplified type_5 TI
        size <integer_cst 0x7fc24b13dc00 constant 128>
        unit-size <integer_cst 0x7fc24b13dc18 constant 16>
        align:64 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type 0x7fc249faf690
        fields <field_decl 0x7fc249fb02f8 F type <array_type 0x7fc249faf1f8 c41325a__p__array_5>
            decl_3 TI c41325a.adb:57:11
            size <integer_cst 0x7fc24b13dc00 constant 128>
            unit-size <integer_cst 0x7fc24b13dc18 constant 16>
            align:8 warn_if_not_align:0 offset_align 128
            offset <integer_cst 0x7fc24b13dbe8 constant 0>
            bit-offset <integer_cst 0x7fc24b13dc30 constant 0> context <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD>> context <function_decl 0x7fc249f89d00 c41325a>
        Ada size <integer_cst 0x7fc24b13dc00 constant 128>
        pointer_to_this <pointer_type 0x7fc249d4e7e0> chain <type_decl 0x7fc249fb0390 c41325a__p__p__obj_ara_5___PAD>>
--> readonly TI c41325a.adb:70:6      
    size <integer_cst 0x7fc24b13dc00 type <integer_type 0x7fc24b1520a8 bitsizetype> constant 128>
    unit-size <integer_cst 0x7fc24b13dc18 type <integer_type 0x7fc24b152000 sizetype> constant 16>
    align:64 warn_if_not_align:0 context <function_decl 0x7fc249f89d00 c41325a> chain <var_decl 0x7fc249fbc630 var_ara_6>>

I can see that base is TREE_READONLY.

Am I right that this is a bug happening at some point earlier in the Ada
compiler?

Would you please have a look at why this is so?  Otherwise I can modify
my patch to only consider TREE_READONLY meaningful for PARM_DECLs but
that does not seem right.

Thanks,

Martin


>> FAIL:   c45347d
>> FAIL:   c74402a
>> FAIL:   c95085m
>> FAIL:   cc3601a
>> 
>>                 === gnat tests ===
>> 
>> FAIL: gnat.dg/addr12.adb (test for excess errors)
>> UNRESOLVED: gnat.dg/addr12.adb compilation failed to produce executable
>> FAIL: gnat.dg/addr12_a.adb (test for excess errors)
>> FAIL: gnat.dg/bip_overlay.adb (test for excess errors)
>> FAIL: gnat.dg/global.adb (test for excess errors)
>> FAIL: gnat.dg/spark1.adb  (test for errors, line 8)
>> FAIL: gnat.dg/spark1.adb (test for excess errors)
>> FAIL: gnat.dg/sync2.adb (test for excess errors)
>> FAIL: gnat.dg/synchronized1.adb (test for excess errors)
>
> Yes, it did, as well as PR boostrap/100597 probably.
>
> -- 
> Eric Botcazou
Eric Botcazou May 17, 2021, 6:12 p.m. UTC | #6
> sorry for breaking Ada over the weekend, however...

No problem.

> None of the non-ACATS tests fail for me without doing a bootstrap, but I
> did manage to reproduce this one (by the way, there really should be a
> documentation on how to run ACATS tests manually, I should not need to
> spend an hour and half figuring it out).

https://gcc.gnu.org/wiki/DebuggingGCC

> The problem is that before (early) SRA, there is a TREE_READONLY decl
> that is being written to and my patch eliminated those writes.
> Specifically, I see:
> 
>   <bb 183> :
>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[1]{lb: 1 sz: 1} = _877;
>   _880 = report.ident_bool (1);
> 
>   <bb 184> :
>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[2]{lb: 1 sz: 1} = _880;
>   _883 = report.ident_bool (1);
> 
>   <bb 185> :
>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[3]{lb: 1 sz: 1} = _883;
>   _886 = report.ident_bool (1);
> 
>   <bb 186> :
>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[4]{lb: 1 sz: 1} = _886;
>   _889 = report.ident_bool (1);
> 
> [...and many more.]  Note that this is an -fdump-tree-all-uid dump, the
> DECL that is being assigned to has DECL_UID 5012 and when I dump it:
> 
> DECL_UID of racc->base is: 5012
> print_node (dump_file, "", racc->base, 0):
>  <var_decl 0x7fc249fbc5a0 var_ara_5
>     type <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD
> sizes-gimplified type_5 TI size <integer_cst 0x7fc24b13dc00 constant 128>
>         unit-size <integer_cst 0x7fc24b13dc18 constant 16>
>         align:64 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type
> 0x7fc249faf690 fields <field_decl 0x7fc249fb02f8 F type <array_type
> 0x7fc249faf1f8 c41325a__p__array_5> decl_3 TI c41325a.adb:57:11
>             size <integer_cst 0x7fc24b13dc00 constant 128>
>             unit-size <integer_cst 0x7fc24b13dc18 constant 16>
>             align:8 warn_if_not_align:0 offset_align 128
>             offset <integer_cst 0x7fc24b13dbe8 constant 0>
>             bit-offset <integer_cst 0x7fc24b13dc30 constant 0> context
> <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD>> context
> <function_decl 0x7fc249f89d00 c41325a> Ada size <integer_cst 0x7fc24b13dc00
> constant 128>
>         pointer_to_this <pointer_type 0x7fc249d4e7e0> chain <type_decl
> 0x7fc249fb0390 c41325a__p__p__obj_ara_5___PAD>> --> readonly TI
> c41325a.adb:70:6
>     size <integer_cst 0x7fc24b13dc00 type <integer_type 0x7fc24b1520a8
> bitsizetype> constant 128> unit-size <integer_cst 0x7fc24b13dc18 type
> <integer_type 0x7fc24b152000 sizetype> constant 16> align:64
> warn_if_not_align:0 context <function_decl 0x7fc249f89d00 c41325a> chain
> <var_decl 0x7fc249fbc630 var_ara_6>>
> 
> I can see that base is TREE_READONLY.
> 
> Am I right that this is a bug happening at some point earlier in the Ada
> compiler?

Yes, in the gimplifier apparently, so try with:

diff --git a/gcc/gimplify.c b/gcc/gimplify.c
index e790f08b23f..52cef6f8bff 100644
--- a/gcc/gimplify.c
+++ b/gcc/gimplify.c
@@ -1822,6 +1822,7 @@ gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
 	  if (!TREE_STATIC (decl))
 	    {
 	      DECL_INITIAL (decl) = NULL_TREE;
+	      TREE_READONLY (decl) = 0;
 	      init = build2 (INIT_EXPR, void_type_node, decl, init);
 	      gimplify_and_add (init, seq_p);
 	      ggc_free (init);
Martin Jambor May 21, 2021, 5:04 p.m. UTC | #7
Hi,

On Mon, May 17 2021, Eric Botcazou wrote:
>> sorry for breaking Ada over the weekend, however...
>
> No problem.
>
>> None of the non-ACATS tests fail for me without doing a bootstrap, but I
>> did manage to reproduce this one (by the way, there really should be a
>> documentation on how to run ACATS tests manually, I should not need to
>> spend an hour and half figuring it out).
>
> https://gcc.gnu.org/wiki/DebuggingGCC

I missed that.  I'll try to put the string ACATS there so that people
can search for it.

>
>> The problem is that before (early) SRA, there is a TREE_READONLY decl
>> that is being written to and my patch eliminated those writes.
>> Specifically, I see:
>> 
>>   <bb 183> :
>>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[1]{lb: 1 sz: 1} = _877;
>>   _880 = report.ident_bool (1);
>> 
>>   <bb 184> :
>>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[2]{lb: 1 sz: 1} = _880;
>>   _883 = report.ident_bool (1);
>> 
>>   <bb 185> :
>>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[3]{lb: 1 sz: 1} = _883;
>>   _886 = report.ident_bool (1);
>> 
>>   <bb 186> :
>>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[4]{lb: 1 sz: 1} = _886;
>>   _889 = report.ident_bool (1);
>> 
>> [...and many more.]  Note that this is an -fdump-tree-all-uid dump, the
>> DECL that is being assigned to has DECL_UID 5012 and when I dump it:
>> 
>> DECL_UID of racc->base is: 5012
>> print_node (dump_file, "", racc->base, 0):
>>  <var_decl 0x7fc249fbc5a0 var_ara_5
>>     type <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD
>> sizes-gimplified type_5 TI size <integer_cst 0x7fc24b13dc00 constant 128>
>>         unit-size <integer_cst 0x7fc24b13dc18 constant 16>
>>         align:64 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type
>> 0x7fc249faf690 fields <field_decl 0x7fc249fb02f8 F type <array_type
>> 0x7fc249faf1f8 c41325a__p__array_5> decl_3 TI c41325a.adb:57:11
>>             size <integer_cst 0x7fc24b13dc00 constant 128>
>>             unit-size <integer_cst 0x7fc24b13dc18 constant 16>
>>             align:8 warn_if_not_align:0 offset_align 128
>>             offset <integer_cst 0x7fc24b13dbe8 constant 0>
>>             bit-offset <integer_cst 0x7fc24b13dc30 constant 0> context
>> <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD>> context
>> <function_decl 0x7fc249f89d00 c41325a> Ada size <integer_cst 0x7fc24b13dc00
>> constant 128>
>>         pointer_to_this <pointer_type 0x7fc249d4e7e0> chain <type_decl
>> 0x7fc249fb0390 c41325a__p__p__obj_ara_5___PAD>> --> readonly TI
>> c41325a.adb:70:6
>>     size <integer_cst 0x7fc24b13dc00 type <integer_type 0x7fc24b1520a8
>> bitsizetype> constant 128> unit-size <integer_cst 0x7fc24b13dc18 type
>> <integer_type 0x7fc24b152000 sizetype> constant 16> align:64
>> warn_if_not_align:0 context <function_decl 0x7fc249f89d00 c41325a> chain
>> <var_decl 0x7fc249fbc630 var_ara_6>>
>> 
>> I can see that base is TREE_READONLY.
>> 
>> Am I right that this is a bug happening at some point earlier in the Ada
>> compiler?
>
> Yes, in the gimplifier apparently, so try with:
>
> diff --git a/gcc/gimplify.c b/gcc/gimplify.c
> index e790f08b23f..52cef6f8bff 100644
> --- a/gcc/gimplify.c
> +++ b/gcc/gimplify.c
> @@ -1822,6 +1822,7 @@ gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
>  	  if (!TREE_STATIC (decl))
>  	    {
>  	      DECL_INITIAL (decl) = NULL_TREE;
> +	      TREE_READONLY (decl) = 0;
>  	      init = build2 (INIT_EXPR, void_type_node, decl, init);
>  	      gimplify_and_add (init, seq_p);
>  	      ggc_free (init);

The problem with this patch is that it causes:

  FAIL: gnat.dg/opt94.adb scan-tree-dump-times optimized "worker" 1

which is exactly the testcase from the commit which caused the bug I am
trying to address.

I have given up attempting to clean IL we get from Ada testcases from
writes to TREE_READONLY decls, at least for now.  I spent a bigger part
of today trying to handle those cases gracefully in SRA but I still
cannot make it work, always some Ada testcase breaks.  I'll try again
next week.

Martin
Richard Biener May 25, 2021, 7:01 a.m. UTC | #8
On Fri, 21 May 2021, Martin Jambor wrote:

> Hi,
> 
> On Mon, May 17 2021, Eric Botcazou wrote:
> >> sorry for breaking Ada over the weekend, however...
> >
> > No problem.
> >
> >> None of the non-ACATS tests fail for me without doing a bootstrap, but I
> >> did manage to reproduce this one (by the way, there really should be a
> >> documentation on how to run ACATS tests manually, I should not need to
> >> spend an hour and half figuring it out).
> >
> > https://gcc.gnu.org/wiki/DebuggingGCC
> 
> I missed that.  I'll try to put the string ACATS there so that people
> can search for it.
> 
> >
> >> The problem is that before (early) SRA, there is a TREE_READONLY decl
> >> that is being written to and my patch eliminated those writes.
> >> Specifically, I see:
> >> 
> >>   <bb 183> :
> >>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[1]{lb: 1 sz: 1} = _877;
> >>   _880 = report.ident_bool (1);
> >> 
> >>   <bb 184> :
> >>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[2]{lb: 1 sz: 1} = _880;
> >>   _883 = report.ident_bool (1);
> >> 
> >>   <bb 185> :
> >>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[3]{lb: 1 sz: 1} = _883;
> >>   _886 = report.ident_bool (1);
> >> 
> >>   <bb 186> :
> >>   var_ara_5D.5012.FD.4868[1]{lb: 1 sz: 4}[4]{lb: 1 sz: 1} = _886;
> >>   _889 = report.ident_bool (1);
> >> 
> >> [...and many more.]  Note that this is an -fdump-tree-all-uid dump, the
> >> DECL that is being assigned to has DECL_UID 5012 and when I dump it:
> >> 
> >> DECL_UID of racc->base is: 5012
> >> print_node (dump_file, "", racc->base, 0):
> >>  <var_decl 0x7fc249fbc5a0 var_ara_5
> >>     type <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD
> >> sizes-gimplified type_5 TI size <integer_cst 0x7fc24b13dc00 constant 128>
> >>         unit-size <integer_cst 0x7fc24b13dc18 constant 16>
> >>         align:64 warn_if_not_align:0 symtab:0 alias-set -1 canonical-type
> >> 0x7fc249faf690 fields <field_decl 0x7fc249fb02f8 F type <array_type
> >> 0x7fc249faf1f8 c41325a__p__array_5> decl_3 TI c41325a.adb:57:11
> >>             size <integer_cst 0x7fc24b13dc00 constant 128>
> >>             unit-size <integer_cst 0x7fc24b13dc18 constant 16>
> >>             align:8 warn_if_not_align:0 offset_align 128
> >>             offset <integer_cst 0x7fc24b13dbe8 constant 0>
> >>             bit-offset <integer_cst 0x7fc24b13dc30 constant 0> context
> >> <record_type 0x7fc249faf690 c41325a__p__p__obj_ara_5___PAD>> context
> >> <function_decl 0x7fc249f89d00 c41325a> Ada size <integer_cst 0x7fc24b13dc00
> >> constant 128>
> >>         pointer_to_this <pointer_type 0x7fc249d4e7e0> chain <type_decl
> >> 0x7fc249fb0390 c41325a__p__p__obj_ara_5___PAD>> --> readonly TI
> >> c41325a.adb:70:6
> >>     size <integer_cst 0x7fc24b13dc00 type <integer_type 0x7fc24b1520a8
> >> bitsizetype> constant 128> unit-size <integer_cst 0x7fc24b13dc18 type
> >> <integer_type 0x7fc24b152000 sizetype> constant 16> align:64
> >> warn_if_not_align:0 context <function_decl 0x7fc249f89d00 c41325a> chain
> >> <var_decl 0x7fc249fbc630 var_ara_6>>
> >> 
> >> I can see that base is TREE_READONLY.
> >> 
> >> Am I right that this is a bug happening at some point earlier in the Ada
> >> compiler?
> >
> > Yes, in the gimplifier apparently, so try with:
> >
> > diff --git a/gcc/gimplify.c b/gcc/gimplify.c
> > index e790f08b23f..52cef6f8bff 100644
> > --- a/gcc/gimplify.c
> > +++ b/gcc/gimplify.c
> > @@ -1822,6 +1822,7 @@ gimplify_decl_expr (tree *stmt_p, gimple_seq *seq_p)
> >  	  if (!TREE_STATIC (decl))
> >  	    {
> >  	      DECL_INITIAL (decl) = NULL_TREE;
> > +	      TREE_READONLY (decl) = 0;
> >  	      init = build2 (INIT_EXPR, void_type_node, decl, init);
> >  	      gimplify_and_add (init, seq_p);
> >  	      ggc_free (init);
> 
> The problem with this patch is that it causes:
> 
>   FAIL: gnat.dg/opt94.adb scan-tree-dump-times optimized "worker" 1

But isn't it still a correct fix?  That said, I wonder how far we would
get with verifying that a TREE_READONLY (auto-)var is never stored to ...

> which is exactly the testcase from the commit which caused the bug I am
> trying to address.
> 
> I have given up attempting to clean IL we get from Ada testcases from
> writes to TREE_READONLY decls, at least for now.  I spent a bigger part
> of today trying to handle those cases gracefully in SRA but I still
> cannot make it work, always some Ada testcase breaks.  I'll try again
> next week.

So if the above doesn't work then I'd try to simply disqualifying
TREE_READONLY vars for SRA if they would be ever written to.

Richard.

> Martin
>
Eric Botcazou May 25, 2021, 9:03 a.m. UTC | #9
> The problem with this patch is that it causes:
> 
>   FAIL: gnat.dg/opt94.adb scan-tree-dump-times optimized "worker" 1
> 
> which is exactly the testcase from the commit which caused the bug I am
> trying to address.

Sorry about that, a thinko in the original change, I'm testing this fixlet.


	* gimplify.c (gimplify_decl_expr): Clear TREE_READONLY on the DECL
	when creating an initialization statement for it.
	* tree-inline.c (setup_one_parameter): Fix thinko in new condition.
Richard Biener May 25, 2021, 9:09 a.m. UTC | #10
On Tue, 25 May 2021, Eric Botcazou wrote:

> > The problem with this patch is that it causes:
> > 
> >   FAIL: gnat.dg/opt94.adb scan-tree-dump-times optimized "worker" 1
> > 
> > which is exactly the testcase from the commit which caused the bug I am
> > trying to address.
> 
> Sorry about that, a thinko in the original change, I'm testing this fixlet.

LGTM.

> 
> 	* gimplify.c (gimplify_decl_expr): Clear TREE_READONLY on the DECL
> 	when creating an initialization statement for it.
> 	* tree-inline.c (setup_one_parameter): Fix thinko in new condition.
>
Eric Botcazou May 25, 2021, 4:33 p.m. UTC | #11
> LGTM.

Thanks, but a bit too bold because gimplify_and_add can promote the non-static 
DECL to static memory and reinstate DECL_INITIAL, so first hunk adjusted.


	* gimplify.c (gimplify_decl_expr): Clear TREE_READONLY on the DECL
	when really creating an initialization statement for it.
Eric Botcazou May 26, 2021, 7:57 a.m. UTC | #12
> 	* gimplify.c (gimplify_decl_expr): Clear TREE_READONLY on the DECL
> 	when really creating an initialization statement for it.

Quite an interesting can of worms I have opened it seems. :-(  So the change  
apparently causes OMP lowering to create dangling references, I have applied 
the attached stopgap fix in order to buy me some time to properly debug it.


	* gimplify.c (gimplify_decl_expr): Do not clear TREE_READONLY on a
	DECL which is a reference for OMP.
diff mbox series

Patch

diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr100453.c b/gcc/testsuite/gcc.dg/tree-ssa/pr100453.c
new file mode 100644
index 00000000000..0cf0ad23815
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/tree-ssa/pr100453.c
@@ -0,0 +1,18 @@ 
+/* { dg-do run } */
+/* { dg-options "-O1" } */
+
+struct a {
+  int b : 4;
+} d;
+static int c, e;
+static const struct a f;
+static void g(const struct a h) {
+  for (; c < 1; c++)
+    d = h;
+  e = h.b;
+  c = h.b;
+}
+int main() {
+  g(f);
+  return 0;
+}
diff --git a/gcc/tree-sra.c b/gcc/tree-sra.c
index 8dfc923ed7e..186cd62b476 100644
--- a/gcc/tree-sra.c
+++ b/gcc/tree-sra.c
@@ -4244,7 +4244,7 @@  sra_modify_assign (gimple *stmt, gimple_stmt_iterator *gsi)
       || stmt_ends_bb_p (stmt))
     {
       /* No need to copy into a constant-pool, it comes pre-initialized.  */
-      if (access_has_children_p (racc) && !constant_decl_p (racc->base))
+      if (access_has_children_p (racc) && !TREE_READONLY (racc->base))
 	generate_subtree_copies (racc->first_child, rhs, racc->offset, 0, 0,
 				 gsi, false, false, loc);
       if (access_has_children_p (lacc))
@@ -4333,7 +4333,7 @@  sra_modify_assign (gimple *stmt, gimple_stmt_iterator *gsi)
 	    }
 	  /* Restore the aggregate RHS from its components so the
 	     prevailing aggregate copy does the right thing.  */
-	  if (access_has_children_p (racc))
+	  if (access_has_children_p (racc) && !TREE_READONLY (racc->base))
 	    generate_subtree_copies (racc->first_child, rhs, racc->offset, 0, 0,
 				     gsi, false, false, loc);
 	  /* Re-load the components of the aggregate copy destination.