Bug 60449 - Merging function DECLs discards leaf attribute which causes cfg verifier to fail
Summary: Merging function DECLs discards leaf attribute which causes cfg verifier to fail
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: lto (show other bugs)
Version: 4.9.0
: P3 normal
Target Milestone: 4.9.3
Assignee: Not yet assigned to anyone
URL:
Keywords:
: 60721 (view as bug list)
Depends on:
Blocks:
 
Reported: 2014-03-06 17:03 UTC by Martin Jambor
Modified: 2014-11-25 15:21 UTC (History)
5 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2014-03-06 00:00:00


Attachments
Part one of a testcase (235 bytes, text/plain)
2014-03-06 17:04 UTC, Martin Jambor
Details
Part two of a testcase (406 bytes, text/plain)
2014-03-06 17:04 UTC, Martin Jambor
Details
Untested proposed fix (1.27 KB, patch)
2014-03-06 17:46 UTC, Martin Jambor
Details | Diff
RFC patch (4.37 KB, patch)
2014-06-24 14:40 UTC, Martin Jambor
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Martin Jambor 2014-03-06 17:03:10 UTC
I've come across this verify_flow_info failure when attempting to LTO
build Firefox.  I'm attaching a small testcase (I'm using trunk
revision 208276 on x86-64-linux), to reproduce do:

$ ~/gcc/trunk/inst/bin/gcc -c -O2 -flto gtod1.c 
$ ~/gcc/trunk/inst/bin/gcc -c -O2 -flto gtod2.c
$ ~/gcc/trunk/inst/bin/gcc -O2 -flto gtod1.o gtod2.o 
gtod2.c: In function ‘main’:
gtod2.c:41:5: error: control flow in the middle of basic block 6
 int main (void)
     ^
gtod2.c:41:5: error: control flow in the middle of basic block 6
gtod2.c:41:5: error: control flow in the middle of basic block 6
gtod2.c:41:5: internal compiler error: verify_flow_info failed

The order of .o files in the linking stage is important.

My artificial testcase declares gettimeofday itself, twice, each time
with different attributes on purpose.  When building FF, all
declarations claim to come from /usr/include/sys/time.h, I do not know
why some of them lost or did not get their attributes.

The problem with loosing the leaf attribute is that calls from
function that calls setjmp are considered control flow statements
unless they are without side effects or leaf (see stmt_ends_bb_p,
is_ctrl_altering_stmt and call_can_make_abnormal_goto).  Therefore, if
a leaf attribute is lost, calls to that functions suddenly may need to
break BBs or face the verifiers wrath.
Comment 1 Martin Jambor 2014-03-06 17:04:12 UTC
Created attachment 32289 [details]
Part one of a testcase
Comment 2 Martin Jambor 2014-03-06 17:04:33 UTC
Created attachment 32290 [details]
Part two of a testcase
Comment 3 Markus Trippelsdorf 2014-03-06 17:42:42 UTC
Confirmed.
Comment 4 Martin Jambor 2014-03-06 17:46:57 UTC
Created attachment 32291 [details]
Untested proposed fix

Untested proposed fix
Comment 5 Richard Biener 2014-03-07 08:55:24 UTC
Shouldn't fixup_cfg fix this up?  Simply prefering the decl with attributes
isn't enough if you attach an urelated attribute to both fns as well.
Comment 6 Richard Biener 2014-03-07 08:56:42 UTC
OTOH, why do we have to merge the decls/cgraph nodes at all?  Can't we simply
make them aliases if tree merging decides the decls are not equal?
Comment 7 Martin Jambor 2014-03-07 10:07:31 UTC
(In reply to Richard Biener from comment #5)
> Shouldn't fixup_cfg fix this up?  Simply prefering the decl with attributes
> isn't enough if you attach an urelated attribute to both fns as well.

Yes, I know.  I did not really mean to call it a "proposed" fix, did
that somewhat automatically.  On the other hand, I am able to slim-LTO
build Firefox with the patch though.

(In reply to Richard Biener from comment #6)
> OTOH, why do we have to merge the decls/cgraph nodes at all?  Can't we simply
> make them aliases if tree merging decides the decls are not equal?

Good idea, this might indeed be the best way to fix this.  (Although I
do not really know what needs to be done to turn a previously proper
decl and its symtab node into an alias.  Honza, do you think it would
be difficult?).
Comment 8 Richard Biener 2014-04-01 12:29:00 UTC
*** Bug 60721 has been marked as a duplicate of this bug. ***
Comment 9 Richard Biener 2014-04-01 12:29:55 UTC
(In reply to Richard Biener from comment #8)
> *** Bug 60721 has been marked as a duplicate of this bug. ***

See there for another proposed fix to retain per-call 'leaf' (well,
no-abnormal-goto) status.
Comment 10 Richard Biener 2014-04-01 12:36:27 UTC
(In reply to Richard Biener from comment #9)
> (In reply to Richard Biener from comment #8)
> > *** Bug 60721 has been marked as a duplicate of this bug. ***
> 
> See there for another proposed fix to retain per-call 'leaf' (well,
> no-abnormal-goto) status.

Btw, similar to this flag a flag for a per-call 'noreturn' status should
be added as well, getting rid of that MODIFIED_NORETURN_CALLS red herring
on the way (basically defer committing 'noreturn' from a decl to all callers
until a suitable time).  I suppose different 'noreturn' status in LTO units
can also cause a similar issue where a (no longer after merging) noreturn
call ending BB fails to have a fallthru edge.
Comment 11 Jan Hubicka 2014-04-02 16:29:51 UTC
> OTOH, why do we have to merge the decls/cgraph nodes at all?  Can't we simply
> make them aliases if tree merging decides the decls are not equal?

If we do so, we would never merge external declaration from one unit with definition from other, so everything would go through aliases effectively doubling the symtab. 

Also aliases currently ar different symbols, not different tree representation of the same symbol, so one would need to rename the declaration and make it to bubble all the way into the assembler file.

We can resort to not merging in selected cases, but I would prefer to do that only when we have very good reason why we want to do so.
Comment 12 Jakub Jelinek 2014-04-22 11:35:07 UTC
GCC 4.9.0 has been released
Comment 13 Martin Liška 2014-06-12 08:09:27 UTC
Same problem can be seen in Chromium. The final binary contains about ~3500 usages of a function with different DECL attributes. Problematic is just one function:

sigaction/538949 (sigaction) @0x7f0fb8916450
 DECL_ATTR:
sigaction/718813 (sigaction) @0x7f0fb7db58a0
 DECL_ATTR: __leaf__ , __nothrow__ 

Thus, preserving both variants of the function would not increase symtab significantly.
Comment 14 Martin Jambor 2014-06-24 14:40:53 UTC
Created attachment 32997 [details]
RFC patch

After playing around with aliases (of extern symbols) for a while I
decided that any such fix to this problem would be too invasive and
generally terrible.  Therefore, I set out to prepare a patch that
would put the leaf flag into individual gimple call statements and
that would also do what Richi suggested in comment #10, ie. remove
MODIFIED_NORETURN_CALLS by having a noreturn flag in calls too.

I may not get back to this for a while so I'm posting what I've got,
inviting comments on the general direction of the patch, even though
it is not at the submission level yet.  It bootstraps but it
introduces one (yet totally unexamined) asan test failure.  I'm also
worried about the occasional discrepancy between the decl and the call
noreturn flags, it would be nice to make it somehow clearer when to
use what.

I've also started to think that setting both the two new flags lazily
in the cfg cleanup was probably a mistake.  Verifier requires special
treatment when noreturn IS set and when leaf IS NOT set, which means
that the cfg cleanup sometimes must be run even though the cfg has not
actually changed (but a call was re-built) which is weird.
So in the next iteration I'll probably try to set the leaf flag when
building the statement and maintain it from that point on.

But that may not happen until late July.  Meanwhile, almost anyone
bumping into this problem can use the first provisional fix.
Comment 15 Jakub Jelinek 2014-07-16 13:26:15 UTC
GCC 4.9.1 has been released.
Comment 16 wmi 2014-08-20 16:47:45 UTC
Author: wmi
Date: Wed Aug 20 16:47:12 2014
New Revision: 214233

URL: https://gcc.gnu.org/viewcvs?rev=214233&root=gcc&view=rev
Log:
2014-08-20  Martin Jambor  <mjambor@suse.cz>
	    Wei Mi  <wmi@google.com>

	PR ipa/60449
	PR middle-end/61776
	* tree-ssa-operands.c (update_stmt_operands): Remove
	MODIFIED_NORETURN_CALLS.
	* tree-cfgcleanup.c (cleanup_call_ctrl_altering_flag): New func.
	(cleanup_control_flow_bb): Use cleanup_call_ctrl_altering_flag.
	(split_bb_on_noreturn_calls): Renamed from split_bbs_on_noreturn_calls.
	(cleanup_tree_cfg_1): Use split_bb_on_noreturn_calls.
	* tree-ssanames.h: Remove MODIFIED_NORETURN_CALLS.
	* gimple.h (enum gf_mask): Add GF_CALL_CTRL_ALTERING.
	(gimple_call_set_ctrl_altering): New func.
	(gimple_call_ctrl_altering_p): Ditto.
	* tree-cfg.c (gimple_call_initialize_ctrl_altering): Ditto.
	(make_blocks): Use gimple_call_initialize_ctrl_altering.
	(is_ctrl_altering_stmt): Use gimple_call_ctrl_altering_p.
	(execute_fixup_cfg): Use gimple_call_ctrl_altering_p and
	remove MODIFIED_NORETURN_CALLS.

2014-08-20  Martin Jambor  <mjambor@suse.cz>
	    Wei Mi  <wmi@google.com>

	PR ipa/60449
	PR middle-end/61776
	* testsuite/gcc.dg/lto/pr60449_1.c: New test.
	* testsuite/gcc.dg/lto/pr60449_0.c: New test.
	* testsuite/gcc.dg/pr61776.c: New test.


Modified:
    trunk/gcc/ChangeLog
    trunk/gcc/gimple.h
    trunk/gcc/testsuite/ChangeLog
    trunk/gcc/tree-cfg.c
    trunk/gcc/tree-cfgcleanup.c
    trunk/gcc/tree-ssa-operands.c
    trunk/gcc/tree-ssanames.h
Comment 17 wmi 2014-08-20 17:09:57 UTC
Author: wmi
Date: Wed Aug 20 17:09:25 2014
New Revision: 214237

URL: https://gcc.gnu.org/viewcvs?rev=214237&root=gcc&view=rev
Log:
2014-08-20  Martin Jambor  <mjambor@suse.cz>
	    Wei Mi  <wmi@google.com>

	PR ipa/60449
	PR middle-end/61776
	* tree-ssa-operands.c (update_stmt_operands): Remove
	MODIFIED_NORETURN_CALLS.
	* tree-cfgcleanup.c (cleanup_call_ctrl_altering_flag): New func.
	(cleanup_control_flow_bb): Use cleanup_call_ctrl_altering_flag.
	(split_bb_on_noreturn_calls): Renamed from split_bbs_on_noreturn_calls.
	(cleanup_tree_cfg_1): Use split_bb_on_noreturn_calls.
	* tree-ssanames.h: Remove MODIFIED_NORETURN_CALLS.
	* gimple.h (enum gf_mask): Add GF_CALL_CTRL_ALTERING.
	(gimple_call_set_ctrl_altering): New func.
	(gimple_call_ctrl_altering_p): Ditto.
	* tree-cfg.c (gimple_call_initialize_ctrl_altering): Ditto.
	(make_blocks): Use gimple_call_initialize_ctrl_altering.
	(is_ctrl_altering_stmt): Use gimple_call_ctrl_altering_p.
	(execute_fixup_cfg): Use gimple_call_ctrl_altering_p and
	remove MODIFIED_NORETURN_CALLS.

2014-08-20  Martin Jambor  <mjambor@suse.cz>
	    Wei Mi  <wmi@google.com>

	PR ipa/60449
	PR middle-end/61776
	* testsuite/gcc.dg/lto/pr60449_1.c: New test.
	* testsuite/gcc.dg/lto/pr60449_0.c: New test.
	* testsuite/gcc.dg/pr61776.c: New test.


Added:
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/lto/pr60449_0.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/lto/pr60449_1.c
    branches/gcc-4_9-branch/gcc/testsuite/gcc.dg/pr61776.c
Modified:
    branches/gcc-4_9-branch/gcc/ChangeLog
    branches/gcc-4_9-branch/gcc/gimple.h
    branches/gcc-4_9-branch/gcc/testsuite/ChangeLog
    branches/gcc-4_9-branch/gcc/tree-cfg.c
    branches/gcc-4_9-branch/gcc/tree-cfgcleanup.c
    branches/gcc-4_9-branch/gcc/tree-ssa-operands.c
    branches/gcc-4_9-branch/gcc/tree-ssanames.h
Comment 18 Jakub Jelinek 2014-10-30 10:36:15 UTC
GCC 4.9.2 has been released.
Comment 19 Martin Jambor 2014-11-25 15:21:32 UTC
I believe this has been fixed.  Thanks a lot by the way.