public inbox for gcc@gcc.gnu.org
 help / color / mirror / Atom feed
* Cloning functions
@ 2012-03-20  3:07 Matt Davis
  2012-03-20 10:30 ` Martin Jambor
  2012-03-21 14:22 ` Jan Hubicka
  0 siblings, 2 replies; 5+ messages in thread
From: Matt Davis @ 2012-03-20  3:07 UTC (permalink / raw)
  To: gcc

Hello,
In my transformation of an input program, I need to clone functions
and the callee functions in each clone.  To clone a function, or
create a duplicate, I use "cgraph_function_versioning()"  This works
perfectly well for the parent function.  I then go through the
statements in the parent and look for any function calls (callees).
If I find a function call, I clone that function and update the call
site using "gimple_call_set_fn()"  Now, when I dump the gimple via
"debug_function()" I see everything as I expect (parent-clone calls
all the callee-clones).  The parent and all of its callees are the
clones I want.  However, when GCC finishes compiling things, the
callee clones are no where to be found.  And the original (non-clone)
calleess are being used.  The parent-clone is there but all of the
callsites are using the original callees and not the clones.  I know
there must be some update routine, (rebuild_cgraph_edges() did not
help) to glue the callee clones in place so that they do not revert
back to the original callee.

I hope I haven't been too confusing, I do appreciate any help if possible.

-Matt

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

* Re: Cloning functions
  2012-03-20  3:07 Cloning functions Matt Davis
@ 2012-03-20 10:30 ` Martin Jambor
  2012-03-20 11:58   ` Matt Davis
  2012-03-21 14:22 ` Jan Hubicka
  1 sibling, 1 reply; 5+ messages in thread
From: Martin Jambor @ 2012-03-20 10:30 UTC (permalink / raw)
  To: Matt Davis; +Cc: gcc

Hi,

On Tue, Mar 20, 2012 at 02:07:17PM +1100, Matt Davis wrote:
> Hello,
> In my transformation of an input program, I need to clone functions
> and the callee functions in each clone.  To clone a function, or
> create a duplicate, I use "cgraph_function_versioning()"  This works
> perfectly well for the parent function.  I then go through the
> statements in the parent and look for any function calls (callees).
> If I find a function call, I clone that function and update the call
> site using "gimple_call_set_fn()"  Now, when I dump the gimple via
> "debug_function()" I see everything as I expect (parent-clone calls
> all the callee-clones).  The parent and all of its callees are the
> clones I want.  However, when GCC finishes compiling things, the
> callee clones are no where to be found.  

And do you change the calls in the callers of the "parent function?"
This is exactly what you would see if you don't.  See convert_callers
and convert_callers_for_node in tree-sra.c (ipa_modify_call_arguments
is probably equivalent to gimple_call_set_fndecl for your purposes).

cgraph_function_versioning only updates the call graph edges, not the
associated statements which are key in non-IPA stages of compilation
(yes, that makes the redirect_callers parameter quite misleading and
semi-irrelevant but well...).

> And the original (non-clone)
> calleess are being used.  The parent-clone is there but all of the
> callsites are using the original callees and not the clones.  I know
> there must be some update routine, (rebuild_cgraph_edges() did not
> help) to glue the callee clones in place so that they do not revert
> back to the original callee.
> 
> I hope I haven't been too confusing, I do appreciate any help if possible.

I am confused by the term parent function.  Do you mean a parent of
nested functions or something else?  If so, at what stage of
compilation do you clone functions then?  I'm afraid that
cgraph_function_lowering was not written with un-lowered nested
functions in mind and is only usable once they are lowered (because I
just had a quick look and the relevant cgraph_node fields are not
dealt with).  But at the same time I think it is unlikely your pass
runs that early.

Martin

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

* Re: Cloning functions
  2012-03-20 10:30 ` Martin Jambor
@ 2012-03-20 11:58   ` Matt Davis
  2012-03-21 14:26     ` Jan Hubicka
  0 siblings, 1 reply; 5+ messages in thread
From: Matt Davis @ 2012-03-20 11:58 UTC (permalink / raw)
  To: Matt Davis, gcc

Hi Martin, thanks very much for the information!

On Tue, Mar 20, 2012 at 9:29 PM, Martin Jambor <mjambor@suse.cz> wrote:
> Hi,
>
> On Tue, Mar 20, 2012 at 02:07:17PM +1100, Matt Davis wrote:
>> Hello,
>> In my transformation of an input program, I need to clone functions
>> and the callee functions in each clone.  To clone a function, or
>> create a duplicate, I use "cgraph_function_versioning()"  This works
>> perfectly well for the parent function.  I then go through the
>> statements in the parent and look for any function calls (callees).
>> If I find a function call, I clone that function and update the call
>> site using "gimple_call_set_fn()"  Now, when I dump the gimple via
>> "debug_function()" I see everything as I expect (parent-clone calls
>> all the callee-clones).  The parent and all of its callees are the
>> clones I want.  However, when GCC finishes compiling things, the
>> callee clones are no where to be found.
>
> And do you change the calls in the callers of the "parent function?"
> This is exactly what you would see if you don't.  See convert_callers
> and convert_callers_for_node in tree-sra.c (ipa_modify_call_arguments
> is probably equivalent to gimple_call_set_fndecl for your purposes).

Actually, I do change the calls in the parent function.  What I had to
do was set the 'cfun' to the parent function, and then run
'rebuild_cgraph_edges()' and 'cleanup_tree_cfg()'

> cgraph_function_versioning only updates the call graph edges, not the
> associated statements which are key in non-IPA stages of compilation
> (yes, that makes the redirect_callers parameter quite misleading and
> semi-irrelevant but well...).
>
>> And the original (non-clone)
>> calleess are being used.  The parent-clone is there but all of the
>> callsites are using the original callees and not the clones.  I know
>> there must be some update routine, (rebuild_cgraph_edges() did not
>> help) to glue the callee clones in place so that they do not revert
>> back to the original callee.
>>
>> I hope I haven't been too confusing, I do appreciate any help if possible.
>
> I am confused by the term parent function.  Do you mean a parent of
> nested functions or something else?

Yep, you got it.

> If so, at what stage of
> compilation do you clone functions then?  I'm afraid that
> cgraph_function_lowering was not written with un-lowered nested
> functions in mind and is only usable once they are lowered (because I
> just had a quick look and the relevant cgraph_node fields are not
> dealt with).  But at the same time I think it is unlikely your pass
> runs that early.

Yes, my pass is really late, after all IPA passes have complete.  Once
again, thank you for your insight!

-Matt

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

* Re: Cloning functions
  2012-03-20  3:07 Cloning functions Matt Davis
  2012-03-20 10:30 ` Martin Jambor
@ 2012-03-21 14:22 ` Jan Hubicka
  1 sibling, 0 replies; 5+ messages in thread
From: Jan Hubicka @ 2012-03-21 14:22 UTC (permalink / raw)
  To: Matt Davis; +Cc: gcc

> Hello,
> In my transformation of an input program, I need to clone functions
> and the callee functions in each clone.  To clone a function, or
> create a duplicate, I use "cgraph_function_versioning()"  This works
> perfectly well for the parent function.  I then go through the
> statements in the parent and look for any function calls (callees).
> If I find a function call, I clone that function and update the call
> site using "gimple_call_set_fn()"  Now, when I dump the gimple via
> "debug_function()" I see everything as I expect (parent-clone calls
> all the callee-clones).  The parent and all of its callees are the
> clones I want.  However, when GCC finishes compiling things, the
> callee clones are no where to be found.  And the original (non-clone)
> calleess are being used.  The parent-clone is there but all of the
> callsites are using the original callees and not the clones.  I know
> there must be some update routine, (rebuild_cgraph_edges() did not
> help) to glue the callee clones in place so that they do not revert
> back to the original callee.

Hi,
it seems that you are not redirecting the calls to the newly formed clone,
so the clones gets optimized out. 
You can look at cgraph_redirect_edge_callee uses in ipa-cp to see how this
is done.

Note that there are two mechanizms for clonning functions, the real
clones you use and virtual clones. It depends on what you are trying to
do on what is better for you. Can you elaborate more on your pass?

Honza
> 
> I hope I haven't been too confusing, I do appreciate any help if possible.
> 
> -Matt

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

* Re: Cloning functions
  2012-03-20 11:58   ` Matt Davis
@ 2012-03-21 14:26     ` Jan Hubicka
  0 siblings, 0 replies; 5+ messages in thread
From: Jan Hubicka @ 2012-03-21 14:26 UTC (permalink / raw)
  To: Matt Davis; +Cc: gcc

> Hi Martin, thanks very much for the information!
> 
> On Tue, Mar 20, 2012 at 9:29 PM, Martin Jambor <mjambor@suse.cz> wrote:
> > Hi,
> >
> > On Tue, Mar 20, 2012 at 02:07:17PM +1100, Matt Davis wrote:
> >> Hello,
> >> In my transformation of an input program, I need to clone functions
> >> and the callee functions in each clone.  To clone a function, or
> >> create a duplicate, I use "cgraph_function_versioning()"  This works
> >> perfectly well for the parent function.  I then go through the
> >> statements in the parent and look for any function calls (callees).
> >> If I find a function call, I clone that function and update the call
> >> site using "gimple_call_set_fn()"  Now, when I dump the gimple via
> >> "debug_function()" I see everything as I expect (parent-clone calls
> >> all the callee-clones).  The parent and all of its callees are the
> >> clones I want.  However, when GCC finishes compiling things, the
> >> callee clones are no where to be found.
> >
> > And do you change the calls in the callers of the "parent function?"
> > This is exactly what you would see if you don't.  See convert_callers
> > and convert_callers_for_node in tree-sra.c (ipa_modify_call_arguments
> > is probably equivalent to gimple_call_set_fndecl for your purposes).
> 
> Actually, I do change the calls in the parent function.  What I had to
> do was set the 'cfun' to the parent function, and then run
> 'rebuild_cgraph_edges()' and 'cleanup_tree_cfg()'

rebuidl_cgraph_edges just looks at the function body and makes edges
accordingly. It is not doing eny redirection.

The way redirection is usually done is that you call cgraph_redirect_edge_callee
and redirect edge to your clone.  Updating of statements is done alter, at
the end of IPA stage via cgraph_redirect_edge_call_stmt_to_callee
> 
> Yes, my pass is really late, after all IPA passes have complete.  Once
> again, thank you for your insight!

This will be probably the problem, since then the redirection is not done.
You can try to simply call cgraph_redirect_edge_call_stmt_to_callee after
each redirection. But why you need to do IPA pass so late?

Honza
> 
> -Matt

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

end of thread, other threads:[~2012-03-21 14:26 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-03-20  3:07 Cloning functions Matt Davis
2012-03-20 10:30 ` Martin Jambor
2012-03-20 11:58   ` Matt Davis
2012-03-21 14:26     ` Jan Hubicka
2012-03-21 14:22 ` Jan Hubicka

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).