public inbox for jit@gcc.gnu.org
 help / color / mirror / Atom feed
* Global register variables
@ 2021-08-31  6:39 Marc Nieper-Wißkirchen
  2021-09-02 14:33 ` David Malcolm
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Nieper-Wißkirchen @ 2021-08-31  6:39 UTC (permalink / raw)
  To: jit

Does libgccjit support GCC's global register variables ([1])?

If not, could we expect them to be included?  One major use case of
libgccjit seems to be JIT compilation of some virtual machine bytecode and
virtual machines usually need a number of global variables (like some
virtual stack pointer or heap pointer, etc.).  It makes a lot of sense to
hold them in global (callee-saved) registers.

Marc

--

[1]
https://gcc.gnu.org/onlinedocs/gcc/Global-Register-Variables.html#Global-Register-Variables

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

* Re: Global register variables
  2021-08-31  6:39 Global register variables Marc Nieper-Wißkirchen
@ 2021-09-02 14:33 ` David Malcolm
  2021-09-02 16:05   ` Sv: " Petter Tomner
  0 siblings, 1 reply; 8+ messages in thread
From: David Malcolm @ 2021-09-02 14:33 UTC (permalink / raw)
  To: Marc Nieper-Wißkirchen, jit

On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
wrote:
> Does libgccjit support GCC's global register variables ([1])?

Not currently.

> 
> If not, could we expect them to be included?  

> One major use case of
> libgccjit seems to be JIT compilation of some virtual machine bytecode
> and
> virtual machines usually need a number of global variables (like some
> virtual stack pointer or heap pointer, etc.).  It makes a lot of sense
> to
> hold them in global (callee-saved) registers.

Sounds like a useful feature; if someone wants to cook up a patch I can
review it.

Dave


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

* Sv: Global register variables
  2021-09-02 14:33 ` David Malcolm
@ 2021-09-02 16:05   ` Petter Tomner
  2021-09-02 16:33     ` Marc Nieper-Wißkirchen
  2021-09-02 16:44     ` Sv: " David Malcolm
  0 siblings, 2 replies; 8+ messages in thread
From: Petter Tomner @ 2021-09-02 16:05 UTC (permalink / raw)
  To: David Malcolm, Marc Nieper-Wißkirchen, jit

Hi! Conceptually is seems quite straight forward if you want to try it on some code you have. 
See the attached code that hack any identifier starting with reg_* to  a "global register" named *.

It seems to work with e.g "reg_r14" and "reg_r15" on my machine with some silly getter and 
setter functions for those registers but there is an array "global_regs" in reginfo.c that is not
reset after each run so only run it once. And as EXPORTED global type.

I guess error handling and eg. having not to specify register explicitly for portability
would be the interesting and little bit harder part. 

Regards

From f45d0e328b21b2a4b23021c099f1f3b0ad6e6002 Mon Sep 17 00:00:00 2001
From: Petter Tomner <tomner@kth.se>
Date: Thu, 2 Sep 2021 17:23:24 +0200
Subject: [PATCH] Hack reg_* identifiers to global register

---
 gcc/jit/jit-playback.c | 15 ++++++++++++++-
 1 file changed, 14 insertions(+), 1 deletion(-)

diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
index 79ac525e5df..fa011ed4c0c 100644
--- a/gcc/jit/jit-playback.c
+++ b/gcc/jit/jit-playback.c
@@ -40,7 +40,7 @@ along with GCC; see the file COPYING3.  If not see
 #include "gcc.h"
 #include "diagnostic.h"
 #include "stmt.h"
-
+#include "varasm.h"
 #include <pthread.h>
 
 #include "jit-playback.h"
@@ -567,6 +567,19 @@ global_new_decl (location *loc,
       break;
     }
 
+  { /* Filescope register variables on identifiers reg_REGNUM */
+    const char *key = "reg_";
+
+    for (int i = 0; i < 4; i++)
+      if (name[i] != key[i]) goto bail;
+    if (!name[4]) goto bail;
+
+    DECL_REGISTER (inner) = 1;
+    set_user_assembler_name (inner, name + 4);
+    DECL_HARD_REGISTER (inner) = 1;
+  }
+bail:
+
   if (loc)
     set_tree_location (inner, loc);
 
-- 
2.20.1

-----Ursprungligt meddelande-----
Från: Jit <jit-bounces+tomner=kth.se@gcc.gnu.org> För David Malcolm via Jit
Skickat: den 2 september 2021 16:34
Till: Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>; jit@gcc.gnu.org
Ämne: Re: Global register variables

On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
wrote:
> Does libgccjit support GCC's global register variables ([1])?

Not currently.

> 
> If not, could we expect them to be included?

> One major use case of
> libgccjit seems to be JIT compilation of some virtual machine bytecode 
> and virtual machines usually need a number of global variables (like 
> some virtual stack pointer or heap pointer, etc.).  It makes a lot of 
> sense to hold them in global (callee-saved) registers.

Sounds like a useful feature; if someone wants to cook up a patch I can review it.

Dave


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

* Re: Global register variables
  2021-09-02 16:05   ` Sv: " Petter Tomner
@ 2021-09-02 16:33     ` Marc Nieper-Wißkirchen
  2021-09-02 16:44     ` Sv: " David Malcolm
  1 sibling, 0 replies; 8+ messages in thread
From: Marc Nieper-Wißkirchen @ 2021-09-02 16:33 UTC (permalink / raw)
  To: Petter Tomner; +Cc: David Malcolm, Marc Nieper-Wißkirchen, jit

Thank you very much for this proof of concept!

Am Do., 2. Sept. 2021 um 18:05 Uhr schrieb Petter Tomner <tomner@kth.se>:

> Hi! Conceptually is seems quite straight forward if you want to try it on
> some code you have.
> See the attached code that hack any identifier starting with reg_* to  a
> "global register" named *.
>
> It seems to work with e.g "reg_r14" and "reg_r15" on my machine with some
> silly getter and
> setter functions for those registers but there is an array "global_regs"
> in reginfo.c that is not
> reset after each run so only run it once. And as EXPORTED global type.
>
> I guess error handling and eg. having not to specify register explicitly
> for portability
> would be the interesting and little bit harder part.
>

I think specifying a register explicitly is not that bad.  This is what
would be done in C code as well. Depending on the target system, the
consumer of the libgccjit API would choose such registers.  I don't see a
good way to have the registers chosen automatically.  For example, on a
target with a lot of registers, I may want to pin a lot; while on a target
with only a few, I may want to pin just one for the most important global.
Or would you see libgccjit to do such heuristics?

Marc


> Regards
>
> From f45d0e328b21b2a4b23021c099f1f3b0ad6e6002 Mon Sep 17 00:00:00 2001
> From: Petter Tomner <tomner@kth.se>
> Date: Thu, 2 Sep 2021 17:23:24 +0200
> Subject: [PATCH] Hack reg_* identifiers to global register
>
> ---
>  gcc/jit/jit-playback.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
>
> diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
> index 79ac525e5df..fa011ed4c0c 100644
> --- a/gcc/jit/jit-playback.c
> +++ b/gcc/jit/jit-playback.c
> @@ -40,7 +40,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "gcc.h"
>  #include "diagnostic.h"
>  #include "stmt.h"
> -
> +#include "varasm.h"
>  #include <pthread.h>
>
>  #include "jit-playback.h"
> @@ -567,6 +567,19 @@ global_new_decl (location *loc,
>        break;
>      }
>
> +  { /* Filescope register variables on identifiers reg_REGNUM */
> +    const char *key = "reg_";
> +
> +    for (int i = 0; i < 4; i++)
> +      if (name[i] != key[i]) goto bail;
> +    if (!name[4]) goto bail;
> +
> +    DECL_REGISTER (inner) = 1;
> +    set_user_assembler_name (inner, name + 4);
> +    DECL_HARD_REGISTER (inner) = 1;
> +  }
> +bail:
> +
>    if (loc)
>      set_tree_location (inner, loc);
>
> --
> 2.20.1
>
> -----Ursprungligt meddelande-----
> Från: Jit <jit-bounces+tomner=kth.se@gcc.gnu.org> För David Malcolm via
> Jit
> Skickat: den 2 september 2021 16:34
> Till: Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>; jit@gcc.gnu.org
> Ämne: Re: Global register variables
>
> On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
> wrote:
> > Does libgccjit support GCC's global register variables ([1])?
>
> Not currently.
>
> >
> > If not, could we expect them to be included?
>
> > One major use case of
> > libgccjit seems to be JIT compilation of some virtual machine bytecode
> > and virtual machines usually need a number of global variables (like
> > some virtual stack pointer or heap pointer, etc.).  It makes a lot of
> > sense to hold them in global (callee-saved) registers.
>
> Sounds like a useful feature; if someone wants to cook up a patch I can
> review it.
>
> Dave
>
>

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

* Re: Sv: Global register variables
  2021-09-02 16:05   ` Sv: " Petter Tomner
  2021-09-02 16:33     ` Marc Nieper-Wißkirchen
@ 2021-09-02 16:44     ` David Malcolm
  2021-09-02 17:06       ` Sv: " Petter Tomner
  1 sibling, 1 reply; 8+ messages in thread
From: David Malcolm @ 2021-09-02 16:44 UTC (permalink / raw)
  To: Petter Tomner, Marc Nieper-Wißkirchen, jit

On Thu, 2021-09-02 at 16:05 +0000, Petter Tomner wrote:
> Hi! Conceptually is seems quite straight forward if you want to try it
> on some code you have. 
> See the attached code that hack any identifier starting with reg_* to 
> a "global register" named *.
> 
> It seems to work with e.g "reg_r14" and "reg_r15" on my machine with
> some silly getter and 
> setter functions for those registers

I don't like the idea of "magic" variable names, since it can change
the meaning of existing client code.  I was thinking of an API, maybe
something like:

extern gcc_jit_lvalue *
gcc_jit_global_set_register (gcc_jit_lvalue *global,
                             const char *regname);

(taking inspiration from gcc_jit_global_set_initializer) ?

>  but there is an array "global_regs" in reginfo.c that is not
> reset after each run so only run it once. 

Good catch.  Sounds like we'd need a reginfo_c_finalize, to be called
from toplev::finalize in toplev.c


> And as EXPORTED global type.
> 
> I guess error handling and eg. having not to specify register
> explicitly for portability
> would be the interesting and little bit harder part.

Is the name actually optional from the perspective of GCC's backend?

Maybe allow NULL for the regname param if it is?

Hope this is constructive
Dave

>  
> 
> Regards
> 
> From f45d0e328b21b2a4b23021c099f1f3b0ad6e6002 Mon Sep 17 00:00:00
> 2001
> From: Petter Tomner <tomner@kth.se>
> Date: Thu, 2 Sep 2021 17:23:24 +0200
> Subject: [PATCH] Hack reg_* identifiers to global register
> 
> ---
>  gcc/jit/jit-playback.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c
> index 79ac525e5df..fa011ed4c0c 100644
> --- a/gcc/jit/jit-playback.c
> +++ b/gcc/jit/jit-playback.c
> @@ -40,7 +40,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "gcc.h"
>  #include "diagnostic.h"
>  #include "stmt.h"
> -
> +#include "varasm.h"
>  #include <pthread.h>
>  
>  #include "jit-playback.h"
> @@ -567,6 +567,19 @@ global_new_decl (location *loc,
>        break;
>      }
>  
> +  { /* Filescope register variables on identifiers reg_REGNUM */
> +    const char *key = "reg_";
> +
> +    for (int i = 0; i < 4; i++)
> +      if (name[i] != key[i]) goto bail;
> +    if (!name[4]) goto bail;
> +
> +    DECL_REGISTER (inner) = 1;
> +    set_user_assembler_name (inner, name + 4);
> +    DECL_HARD_REGISTER (inner) = 1;
> +  }
> +bail:
> +
>    if (loc)
>      set_tree_location (inner, loc);
>  
> -- 
> 2.20.1
> 
> -----Ursprungligt meddelande-----
> Från: Jit <jit-bounces+tomner=kth.se@gcc.gnu.org> För David Malcolm
> via Jit
> Skickat: den 2 september 2021 16:34
> Till: Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>;   
> jit@gcc.gnu.org
> Ämne: Re: Global register variables
> 
> On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
> wrote:
> > Does libgccjit support GCC's global register variables ([1])?
> 
> Not currently.
> 
> > 
> > If not, could we expect them to be included?
> 
> > One major use case of
> > libgccjit seems to be JIT compilation of some virtual machine
> > bytecode 
> > and virtual machines usually need a number of global variables
> > (like 
> > some virtual stack pointer or heap pointer, etc.).  It makes a lot
> > of 
> > sense to hold them in global (callee-saved) registers.
> 
> Sounds like a useful feature; if someone wants to cook up a patch I
> can review it.
> 
> Dave
> 



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

* Sv: Sv: Global register variables
  2021-09-02 16:44     ` Sv: " David Malcolm
@ 2021-09-02 17:06       ` Petter Tomner
  2021-09-02 17:09         ` Marc Nieper-Wißkirchen
  0 siblings, 1 reply; 8+ messages in thread
From: Petter Tomner @ 2021-09-02 17:06 UTC (permalink / raw)
  To: David Malcolm, Marc Nieper-Wißkirchen, jit

> I don't like the idea of "magic" variable names, since it can change the meaning of existing client code. 
> I was thinking of an API, maybe something like:

Sorry, I thought it looked bad enough. I should have been clearer that it was no serious patch and meant
for Marc Nieper-Wißkirchen. I couldn't bother adding stuff all the call chain up to test it.

> Is the name actually optional from the perspective of GCC's backend?
> Maybe allow NULL for the regname param if it is?

No it seems to be no support for no name. I guess any solution would have to poll for a register name first
and then set the asm name.

Regards

-----Ursprungligt meddelande-----
Från: David Malcolm <dmalcolm@redhat.com> 
Skickat: den 2 september 2021 18:45
Till: Petter Tomner <tomner@kth.se>; Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>; jit@gcc.gnu.org
Ämne: Re: Sv: Global register variables

On Thu, 2021-09-02 at 16:05 +0000, Petter Tomner wrote:
> Hi! Conceptually is seems quite straight forward if you want to try it 
> on some code you have.
> See the attached code that hack any identifier starting with reg_* to 
> a "global register" named *.
> 
> It seems to work with e.g "reg_r14" and "reg_r15" on my machine with 
> some silly getter and setter functions for those registers

I don't like the idea of "magic" variable names, since it can change the meaning of existing client code.  I was thinking of an API, maybe something like:

extern gcc_jit_lvalue *
gcc_jit_global_set_register (gcc_jit_lvalue *global,
                             const char *regname);

(taking inspiration from gcc_jit_global_set_initializer) ?

>  but there is an array "global_regs" in reginfo.c that is not reset 
> after each run so only run it once.

Good catch.  Sounds like we'd need a reginfo_c_finalize, to be called from toplev::finalize in toplev.c


> And as EXPORTED global type.
> 
> I guess error handling and eg. having not to specify register 
> explicitly for portability would be the interesting and little bit 
> harder part.

Is the name actually optional from the perspective of GCC's backend?

Maybe allow NULL for the regname param if it is?

Hope this is constructive
Dave

>  
> 
> Regards
> 
> From f45d0e328b21b2a4b23021c099f1f3b0ad6e6002 Mon Sep 17 00:00:00
> 2001
> From: Petter Tomner <tomner@kth.se>
> Date: Thu, 2 Sep 2021 17:23:24 +0200
> Subject: [PATCH] Hack reg_* identifiers to global register
> 
> ---
>  gcc/jit/jit-playback.c | 15 ++++++++++++++-
>  1 file changed, 14 insertions(+), 1 deletion(-)
> 
> diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c index 
> 79ac525e5df..fa011ed4c0c 100644
> --- a/gcc/jit/jit-playback.c
> +++ b/gcc/jit/jit-playback.c
> @@ -40,7 +40,7 @@ along with GCC; see the file COPYING3.  If not see
>  #include "gcc.h"
>  #include "diagnostic.h"
>  #include "stmt.h"
> -
> +#include "varasm.h"
>  #include <pthread.h>
>  
>  #include "jit-playback.h"
> @@ -567,6 +567,19 @@ global_new_decl (location *loc,
>        break;
>      }
>  
> +  { /* Filescope register variables on identifiers reg_REGNUM */
> +    const char *key = "reg_";
> +
> +    for (int i = 0; i < 4; i++)
> +      if (name[i] != key[i]) goto bail;
> +    if (!name[4]) goto bail;
> +
> +    DECL_REGISTER (inner) = 1;
> +    set_user_assembler_name (inner, name + 4);
> +    DECL_HARD_REGISTER (inner) = 1;
> +  }
> +bail:
> +
>    if (loc)
>      set_tree_location (inner, loc);
>  
> --
> 2.20.1
> 
> -----Ursprungligt meddelande-----
> Från: Jit <jit-bounces+tomner=kth.se@gcc.gnu.org> För David Malcolm 
> via Jit
> Skickat: den 2 september 2021 16:34
> Till: Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>;   
> jit@gcc.gnu.org
> Ämne: Re: Global register variables
> 
> On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
> wrote:
> > Does libgccjit support GCC's global register variables ([1])?
> 
> Not currently.
> 
> > 
> > If not, could we expect them to be included?
> 
> > One major use case of
> > libgccjit seems to be JIT compilation of some virtual machine 
> > bytecode and virtual machines usually need a number of global 
> > variables (like some virtual stack pointer or heap pointer, etc.).  
> > It makes a lot of sense to hold them in global (callee-saved) 
> > registers.
> 
> Sounds like a useful feature; if someone wants to cook up a patch I 
> can review it.
> 
> Dave
> 



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

* Re: Sv: Global register variables
  2021-09-02 17:06       ` Sv: " Petter Tomner
@ 2021-09-02 17:09         ` Marc Nieper-Wißkirchen
  2021-12-07  9:07           ` Marc Nieper-Wißkirchen
  0 siblings, 1 reply; 8+ messages in thread
From: Marc Nieper-Wißkirchen @ 2021-09-02 17:09 UTC (permalink / raw)
  To: Petter Tomner; +Cc: David Malcolm, Marc Nieper-Wißkirchen, jit

Am Do., 2. Sept. 2021 um 19:06 Uhr schrieb Petter Tomner <tomner@kth.se>:

> > I don't like the idea of "magic" variable names, since it can change the
> meaning of existing client code.
> > I was thinking of an API, maybe something like:
>
> Sorry, I thought it looked bad enough. I should have been clearer that it
> was no serious patch and meant
> for Marc Nieper-Wißkirchen. I couldn't bother adding stuff all the call
> chain up to test it.
>

Yes, thanks, that was a fine proof-of-concept.  And looked bad enough for
me. ;)

The actual suggestion of "gcc_jit_global_set_register" looks good to me.
Marc


> > Is the name actually optional from the perspective of GCC's backend?
> > Maybe allow NULL for the regname param if it is?
>
> No it seems to be no support for no name. I guess any solution would have
> to poll for a register name first
> and then set the asm name.
>
> Regards
>
> -----Ursprungligt meddelande-----
> Från: David Malcolm <dmalcolm@redhat.com>
> Skickat: den 2 september 2021 18:45
> Till: Petter Tomner <tomner@kth.se>; Marc Nieper-Wißkirchen <
> marc.nieper+gnu@gmail.com>; jit@gcc.gnu.org
> Ämne: Re: Sv: Global register variables
>
> On Thu, 2021-09-02 at 16:05 +0000, Petter Tomner wrote:
> > Hi! Conceptually is seems quite straight forward if you want to try it
> > on some code you have.
> > See the attached code that hack any identifier starting with reg_* to
> > a "global register" named *.
> >
> > It seems to work with e.g "reg_r14" and "reg_r15" on my machine with
> > some silly getter and setter functions for those registers
>
> I don't like the idea of "magic" variable names, since it can change the
> meaning of existing client code.  I was thinking of an API, maybe something
> like:
>
> extern gcc_jit_lvalue *
> gcc_jit_global_set_register (gcc_jit_lvalue *global,
>                              const char *regname);
>
> (taking inspiration from gcc_jit_global_set_initializer) ?
>
> >  but there is an array "global_regs" in reginfo.c that is not reset
> > after each run so only run it once.
>
> Good catch.  Sounds like we'd need a reginfo_c_finalize, to be called from
> toplev::finalize in toplev.c
>
>
> > And as EXPORTED global type.
> >
> > I guess error handling and eg. having not to specify register
> > explicitly for portability would be the interesting and little bit
> > harder part.
>
> Is the name actually optional from the perspective of GCC's backend?
>
> Maybe allow NULL for the regname param if it is?
>
> Hope this is constructive
> Dave
>
> >
> >
> > Regards
> >
> > From f45d0e328b21b2a4b23021c099f1f3b0ad6e6002 Mon Sep 17 00:00:00
> > 2001
> > From: Petter Tomner <tomner@kth.se>
> > Date: Thu, 2 Sep 2021 17:23:24 +0200
> > Subject: [PATCH] Hack reg_* identifiers to global register
> >
> > ---
> >  gcc/jit/jit-playback.c | 15 ++++++++++++++-
> >  1 file changed, 14 insertions(+), 1 deletion(-)
> >
> > diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c index
> > 79ac525e5df..fa011ed4c0c 100644
> > --- a/gcc/jit/jit-playback.c
> > +++ b/gcc/jit/jit-playback.c
> > @@ -40,7 +40,7 @@ along with GCC; see the file COPYING3.  If not see
> >  #include "gcc.h"
> >  #include "diagnostic.h"
> >  #include "stmt.h"
> > -
> > +#include "varasm.h"
> >  #include <pthread.h>
> >
> >  #include "jit-playback.h"
> > @@ -567,6 +567,19 @@ global_new_decl (location *loc,
> >        break;
> >      }
> >
> > +  { /* Filescope register variables on identifiers reg_REGNUM */
> > +    const char *key = "reg_";
> > +
> > +    for (int i = 0; i < 4; i++)
> > +      if (name[i] != key[i]) goto bail;
> > +    if (!name[4]) goto bail;
> > +
> > +    DECL_REGISTER (inner) = 1;
> > +    set_user_assembler_name (inner, name + 4);
> > +    DECL_HARD_REGISTER (inner) = 1;
> > +  }
> > +bail:
> > +
> >    if (loc)
> >      set_tree_location (inner, loc);
> >
> > --
> > 2.20.1
> >
> > -----Ursprungligt meddelande-----
> > Från: Jit <jit-bounces+tomner=kth.se@gcc.gnu.org> För David Malcolm
> > via Jit
> > Skickat: den 2 september 2021 16:34
> > Till: Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>;
> > jit@gcc.gnu.org
> > Ämne: Re: Global register variables
> >
> > On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
> > wrote:
> > > Does libgccjit support GCC's global register variables ([1])?
> >
> > Not currently.
> >
> > >
> > > If not, could we expect them to be included?
> >
> > > One major use case of
> > > libgccjit seems to be JIT compilation of some virtual machine
> > > bytecode and virtual machines usually need a number of global
> > > variables (like some virtual stack pointer or heap pointer, etc.).
> > > It makes a lot of sense to hold them in global (callee-saved)
> > > registers.
> >
> > Sounds like a useful feature; if someone wants to cook up a patch I
> > can review it.
> >
> > Dave
> >
>
>
>

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

* Re: Sv: Global register variables
  2021-09-02 17:09         ` Marc Nieper-Wißkirchen
@ 2021-12-07  9:07           ` Marc Nieper-Wißkirchen
  0 siblings, 0 replies; 8+ messages in thread
From: Marc Nieper-Wißkirchen @ 2021-12-07  9:07 UTC (permalink / raw)
  To: Marc Nieper-Wißkirchen; +Cc: Petter Tomner, David Malcolm, jit

I have been thinking about this a bit more.

An alternative approach, which has the advantage that no specific knowledge
about registers on each supported architecture would be needed, would be to
introduce a general new calling convention using fewer (or none)
callee-save registers and more registers for arguments. What would have
been "globals variables stored in registers" before, would now be extra
arguments to functions and the new calling convention would make sure that
these arguments are passed along the calling chain in registers.

To make this work, one would need two things, I guess:

(1) Define a new function attribute "regcall", say, for the GCC targets to
be supported.

(2) Add two new procedures to libgccjit:

void gcc_jit_function_add_attribute (gcc_jit_function *func, const char*
attr)
void gcc_jit_function_type_add_attribute (gcc_jit_function_type, const
char* attr)

Implementing the latter is, of course, also of interest without (1) to
support existing function attributes like "noreturn", for example.

Are there any obstacles to implementing (2) in libgccjit?

Thanks,

Marc

Am Do., 2. Sept. 2021 um 19:09 Uhr schrieb Marc Nieper-Wißkirchen <
marc.nieper+gnu@gmail.com>:

> Am Do., 2. Sept. 2021 um 19:06 Uhr schrieb Petter Tomner <tomner@kth.se>:
>
>> > I don't like the idea of "magic" variable names, since it can change
>> the meaning of existing client code.
>> > I was thinking of an API, maybe something like:
>>
>> Sorry, I thought it looked bad enough. I should have been clearer that it
>> was no serious patch and meant
>> for Marc Nieper-Wißkirchen. I couldn't bother adding stuff all the call
>> chain up to test it.
>>
>
> Yes, thanks, that was a fine proof-of-concept.  And looked bad enough for
> me. ;)
>
> The actual suggestion of "gcc_jit_global_set_register" looks good to me.
> Marc
>
>
>> > Is the name actually optional from the perspective of GCC's backend?
>> > Maybe allow NULL for the regname param if it is?
>>
>> No it seems to be no support for no name. I guess any solution would have
>> to poll for a register name first
>> and then set the asm name.
>>
>> Regards
>>
>> -----Ursprungligt meddelande-----
>> Från: David Malcolm <dmalcolm@redhat.com>
>> Skickat: den 2 september 2021 18:45
>> Till: Petter Tomner <tomner@kth.se>; Marc Nieper-Wißkirchen <
>> marc.nieper+gnu@gmail.com>; jit@gcc.gnu.org
>> Ämne: Re: Sv: Global register variables
>>
>> On Thu, 2021-09-02 at 16:05 +0000, Petter Tomner wrote:
>> > Hi! Conceptually is seems quite straight forward if you want to try it
>> > on some code you have.
>> > See the attached code that hack any identifier starting with reg_* to
>> > a "global register" named *.
>> >
>> > It seems to work with e.g "reg_r14" and "reg_r15" on my machine with
>> > some silly getter and setter functions for those registers
>>
>> I don't like the idea of "magic" variable names, since it can change the
>> meaning of existing client code.  I was thinking of an API, maybe something
>> like:
>>
>> extern gcc_jit_lvalue *
>> gcc_jit_global_set_register (gcc_jit_lvalue *global,
>>                              const char *regname);
>>
>> (taking inspiration from gcc_jit_global_set_initializer) ?
>>
>> >  but there is an array "global_regs" in reginfo.c that is not reset
>> > after each run so only run it once.
>>
>> Good catch.  Sounds like we'd need a reginfo_c_finalize, to be called
>> from toplev::finalize in toplev.c
>>
>>
>> > And as EXPORTED global type.
>> >
>> > I guess error handling and eg. having not to specify register
>> > explicitly for portability would be the interesting and little bit
>> > harder part.
>>
>> Is the name actually optional from the perspective of GCC's backend?
>>
>> Maybe allow NULL for the regname param if it is?
>>
>> Hope this is constructive
>> Dave
>>
>> >
>> >
>> > Regards
>> >
>> > From f45d0e328b21b2a4b23021c099f1f3b0ad6e6002 Mon Sep 17 00:00:00
>> > 2001
>> > From: Petter Tomner <tomner@kth.se>
>> > Date: Thu, 2 Sep 2021 17:23:24 +0200
>> > Subject: [PATCH] Hack reg_* identifiers to global register
>> >
>> > ---
>> >  gcc/jit/jit-playback.c | 15 ++++++++++++++-
>> >  1 file changed, 14 insertions(+), 1 deletion(-)
>> >
>> > diff --git a/gcc/jit/jit-playback.c b/gcc/jit/jit-playback.c index
>> > 79ac525e5df..fa011ed4c0c 100644
>> > --- a/gcc/jit/jit-playback.c
>> > +++ b/gcc/jit/jit-playback.c
>> > @@ -40,7 +40,7 @@ along with GCC; see the file COPYING3.  If not see
>> >  #include "gcc.h"
>> >  #include "diagnostic.h"
>> >  #include "stmt.h"
>> > -
>> > +#include "varasm.h"
>> >  #include <pthread.h>
>> >
>> >  #include "jit-playback.h"
>> > @@ -567,6 +567,19 @@ global_new_decl (location *loc,
>> >        break;
>> >      }
>> >
>> > +  { /* Filescope register variables on identifiers reg_REGNUM */
>> > +    const char *key = "reg_";
>> > +
>> > +    for (int i = 0; i < 4; i++)
>> > +      if (name[i] != key[i]) goto bail;
>> > +    if (!name[4]) goto bail;
>> > +
>> > +    DECL_REGISTER (inner) = 1;
>> > +    set_user_assembler_name (inner, name + 4);
>> > +    DECL_HARD_REGISTER (inner) = 1;
>> > +  }
>> > +bail:
>> > +
>> >    if (loc)
>> >      set_tree_location (inner, loc);
>> >
>> > --
>> > 2.20.1
>> >
>> > -----Ursprungligt meddelande-----
>> > Från: Jit <jit-bounces+tomner=kth.se@gcc.gnu.org> För David Malcolm
>> > via Jit
>> > Skickat: den 2 september 2021 16:34
>> > Till: Marc Nieper-Wißkirchen <marc.nieper+gnu@gmail.com>;
>> > jit@gcc.gnu.org
>> > Ämne: Re: Global register variables
>> >
>> > On Tue, 2021-08-31 at 08:39 +0200, Marc Nieper-Wißkirchen via Jit
>> > wrote:
>> > > Does libgccjit support GCC's global register variables ([1])?
>> >
>> > Not currently.
>> >
>> > >
>> > > If not, could we expect them to be included?
>> >
>> > > One major use case of
>> > > libgccjit seems to be JIT compilation of some virtual machine
>> > > bytecode and virtual machines usually need a number of global
>> > > variables (like some virtual stack pointer or heap pointer, etc.).
>> > > It makes a lot of sense to hold them in global (callee-saved)
>> > > registers.
>> >
>> > Sounds like a useful feature; if someone wants to cook up a patch I
>> > can review it.
>> >
>> > Dave
>> >
>>
>>
>>

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

end of thread, other threads:[~2021-12-07  9:07 UTC | newest]

Thread overview: 8+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-08-31  6:39 Global register variables Marc Nieper-Wißkirchen
2021-09-02 14:33 ` David Malcolm
2021-09-02 16:05   ` Sv: " Petter Tomner
2021-09-02 16:33     ` Marc Nieper-Wißkirchen
2021-09-02 16:44     ` Sv: " David Malcolm
2021-09-02 17:06       ` Sv: " Petter Tomner
2021-09-02 17:09         ` Marc Nieper-Wißkirchen
2021-12-07  9:07           ` Marc Nieper-Wißkirchen

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