* Question about creating global varaiable during IPA PASS.
@ 2023-12-13 9:04 Hanke Zhang
2023-12-15 17:15 ` Thomas Schwinge
0 siblings, 1 reply; 3+ messages in thread
From: Hanke Zhang @ 2023-12-13 9:04 UTC (permalink / raw)
To: gcc
Hi, I'm trying to create a global variable in my own PASS which
located at the LATE_IPA_PASSES. (I'm using GCC 10.3.0.)
And after creating it, I added the attributes like the following.
// 1. create the var
tree new_name = get_identifier (xx);
tree new_type = build_pointer_type (xx);
tree new_var = build_decl (UNKNOWN_LOCATION, VAR_DECL, new_name, new_type);
add_attributes (new_var);
static void
add_attributes (tree var)
{
DECL_ARTIFICIAL (var) = 1;
DECL_EXTERNAL (var) = 0;
TREE_STATIC (var) = 1;
TREE_PUBLIC (var) = 1;
TREE_USED (var) = 1;
DECL_CONTEXT (var) = NULL_TREE;
TREE_THIS_VOLATILE (var) = 0;
TREE_ADDRESSABLE (var) = 0;
TREE_READONLY (var) = 0;
if (is_global_var (var))
set_decl_tls_model (var, TLS_MODEL_NONE);
}
But when I try to compile some example files with -flto, error occurs.
/usr/bin/ld: xxx.ltrans0.ltrans.o: in function `xxx':
xxx.c: undefined reference to `glob_var'
xxx.c: undefined reference to `glob_var'
xxx.c: undefined reference to `glob_var'
Here `glob_var' is the global varaiable created in my PASS.
I would like to ask, am I using some attributes incorrectly?
Thanks
Hanke Zhang
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Question about creating global varaiable during IPA PASS.
2023-12-13 9:04 Question about creating global varaiable during IPA PASS Hanke Zhang
@ 2023-12-15 17:15 ` Thomas Schwinge
2023-12-21 7:27 ` Hanke Zhang
0 siblings, 1 reply; 3+ messages in thread
From: Thomas Schwinge @ 2023-12-15 17:15 UTC (permalink / raw)
To: Hanke Zhang; +Cc: gcc
Hi Hanke!
On 2023-12-13T17:04:57+0800, Hanke Zhang via Gcc <gcc@gcc.gnu.org> wrote:
> Hi, I'm trying to create a global variable in my own PASS which
> located at the LATE_IPA_PASSES. (I'm using GCC 10.3.0.)
I can't comment on IPA aspects, or whether something was different on
oldish GCC 10 (why using that one, by the way?), and I've not actually
verified what you're doing here:
> And after creating it, I added the attributes like the following.
>
> // 1. create the var
> tree new_name = get_identifier (xx);
> tree new_type = build_pointer_type (xx);
> tree new_var = build_decl (UNKNOWN_LOCATION, VAR_DECL, new_name, new_type);
> add_attributes (new_var);
>
> static void
> add_attributes (tree var)
> {
> DECL_ARTIFICIAL (var) = 1;
> DECL_EXTERNAL (var) = 0;
> TREE_STATIC (var) = 1;
> TREE_PUBLIC (var) = 1;
> TREE_USED (var) = 1;
> DECL_CONTEXT (var) = NULL_TREE;
> TREE_THIS_VOLATILE (var) = 0;
> TREE_ADDRESSABLE (var) = 0;
> TREE_READONLY (var) = 0;
> if (is_global_var (var))
> set_decl_tls_model (var, TLS_MODEL_NONE);
> }
>
> But when I try to compile some example files with -flto, error occurs.
>
> /usr/bin/ld: xxx.ltrans0.ltrans.o: in function `xxx':
> xxx.c: undefined reference to `glob_var'
> xxx.c: undefined reference to `glob_var'
> xxx.c: undefined reference to `glob_var'
>
> Here `glob_var' is the global varaiable created in my PASS.
>
> I would like to ask, am I using some attributes incorrectly?
..., but are you maybe simply missing to
'varpool_node::add (var);' or 'varpool_node::finalize_decl (var);' or
something like that? See other uses of those, and description in
'gcc/cgraph.h', 'struct [...] varpool_node':
/* Add the variable DECL to the varpool.
Unlike finalize_decl function is intended to be used
by middle end and allows insertion of new variable at arbitrary point
of compilation. */
static void add (tree decl);
/* Mark DECL as finalized. By finalizing the declaration, frontend instruct
the middle end to output the variable to asm file, if needed or externally
visible. */
static void finalize_decl (tree decl);
If that's not it, we'll have to look in more detail.
Grüße
Thomas
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Question about creating global varaiable during IPA PASS.
2023-12-15 17:15 ` Thomas Schwinge
@ 2023-12-21 7:27 ` Hanke Zhang
0 siblings, 0 replies; 3+ messages in thread
From: Hanke Zhang @ 2023-12-21 7:27 UTC (permalink / raw)
To: thomas; +Cc: gcc
Hi Thomas!
Thanks for your reply. That's exactly what I'm missing. When I add
varpool_node::finalize_decl() to my code, everything works fine!
Thomas Schwinge <thomas@schwinge.name> 于2023年12月16日周六 01:15写道:
>
> Hi Hanke!
>
> On 2023-12-13T17:04:57+0800, Hanke Zhang via Gcc <gcc@gcc.gnu.org> wrote:
> > Hi, I'm trying to create a global variable in my own PASS which
> > located at the LATE_IPA_PASSES. (I'm using GCC 10.3.0.)
>
> I can't comment on IPA aspects, or whether something was different on
> oldish GCC 10 (why using that one, by the way?), and I've not actually
> verified what you're doing here:
>
> > And after creating it, I added the attributes like the following.
> >
> > // 1. create the var
> > tree new_name = get_identifier (xx);
> > tree new_type = build_pointer_type (xx);
> > tree new_var = build_decl (UNKNOWN_LOCATION, VAR_DECL, new_name, new_type);
> > add_attributes (new_var);
> >
> > static void
> > add_attributes (tree var)
> > {
> > DECL_ARTIFICIAL (var) = 1;
> > DECL_EXTERNAL (var) = 0;
> > TREE_STATIC (var) = 1;
> > TREE_PUBLIC (var) = 1;
> > TREE_USED (var) = 1;
> > DECL_CONTEXT (var) = NULL_TREE;
> > TREE_THIS_VOLATILE (var) = 0;
> > TREE_ADDRESSABLE (var) = 0;
> > TREE_READONLY (var) = 0;
> > if (is_global_var (var))
> > set_decl_tls_model (var, TLS_MODEL_NONE);
> > }
> >
> > But when I try to compile some example files with -flto, error occurs.
> >
> > /usr/bin/ld: xxx.ltrans0.ltrans.o: in function `xxx':
> > xxx.c: undefined reference to `glob_var'
> > xxx.c: undefined reference to `glob_var'
> > xxx.c: undefined reference to `glob_var'
> >
> > Here `glob_var' is the global varaiable created in my PASS.
> >
> > I would like to ask, am I using some attributes incorrectly?
>
> ..., but are you maybe simply missing to
> 'varpool_node::add (var);' or 'varpool_node::finalize_decl (var);' or
> something like that? See other uses of those, and description in
> 'gcc/cgraph.h', 'struct [...] varpool_node':
>
> /* Add the variable DECL to the varpool.
> Unlike finalize_decl function is intended to be used
> by middle end and allows insertion of new variable at arbitrary point
> of compilation. */
> static void add (tree decl);
>
> /* Mark DECL as finalized. By finalizing the declaration, frontend instruct
> the middle end to output the variable to asm file, if needed or externally
> visible. */
> static void finalize_decl (tree decl);
>
> If that's not it, we'll have to look in more detail.
>
>
> Grüße
> Thomas
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2023-12-21 7:27 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-12-13 9:04 Question about creating global varaiable during IPA PASS Hanke Zhang
2023-12-15 17:15 ` Thomas Schwinge
2023-12-21 7:27 ` Hanke Zhang
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).