public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [PATCH] RISC-V: Implment __builtin_thread_pointer
@ 2020-07-07  9:52 Kito Cheng
  2020-07-08 19:59 ` Jim Wilson
  0 siblings, 1 reply; 6+ messages in thread
From: Kito Cheng @ 2020-07-07  9:52 UTC (permalink / raw)
  To: gcc-patches, kito.cheng, jimw; +Cc: Kito Cheng

RISC-V has a dedicate register for thread pointer which is specified in psABI
doc, so we could support __builtin_thread_pointer in straightforward way.

Note: clang/llvm was supported __builtin_thread_pointer for RISC-V port
recently.
- https://reviews.llvm.org/rGaabc24acf0d5f8677bd22fe9c108581e07c3e180

gcc/ChangeLog:

	* gcc/config/riscv/riscv.md (): New.
	(TP_REGNUM): Ditto.
	* doc/extend.texi (Target Builtins): Add RISC-V built-in section.
	Document __builtin_thread_pointer.

gcc/testsuite/ChangeLog:

	* gcc.target/riscv/read-thread-pointer.c: New.
---
 gcc/config/riscv/riscv.md                            |  8 ++++++++
 gcc/doc/extend.texi                                  | 11 +++++++++++
 gcc/testsuite/gcc.target/riscv/read-thread-pointer.c |  7 +++++++
 3 files changed, 26 insertions(+)
 create mode 100644 gcc/testsuite/gcc.target/riscv/read-thread-pointer.c

diff --git a/gcc/config/riscv/riscv.md b/gcc/config/riscv/riscv.md
index 36012ad1f778..95a02ecaa34b 100644
--- a/gcc/config/riscv/riscv.md
+++ b/gcc/config/riscv/riscv.md
@@ -70,6 +70,7 @@
 (define_constants
   [(RETURN_ADDR_REGNUM		1)
    (GP_REGNUM 			3)
+   (TP_REGNUM			4)
    (T0_REGNUM			5)
    (T1_REGNUM			6)
    (S0_REGNUM			8)
@@ -2515,6 +2516,13 @@
   DONE;
 })
 
+;; Named pattern for expanding thread pointer reference.
+(define_expand "get_thread_pointer<mode>"
+  [(set (match_operand:P 0 "register_operand" "=r")
+	(reg:P TP_REGNUM))]
+  ""
+{})
+
 (include "sync.md")
 (include "peephole.md")
 (include "pic.md")
diff --git a/gcc/doc/extend.texi b/gcc/doc/extend.texi
index ecd3661d2571..556c98f46911 100644
--- a/gcc/doc/extend.texi
+++ b/gcc/doc/extend.texi
@@ -13859,6 +13859,7 @@ instructions, but allow the compiler to schedule those calls.
 * PowerPC Hardware Transactional Memory Built-in Functions::
 * PowerPC Atomic Memory Operation Functions::
 * PowerPC Matrix-Multiply Assist Built-in Functions::
+* RISC-V Built-in Functions::
 * RX Built-in Functions::
 * S/390 System z Built-in Functions::
 * SH Built-in Functions::
@@ -21461,6 +21462,16 @@ vec_t __builtin_vsx_xvcvspbf16 (vec_t);
 vec_t __builtin_vsx_xvcvbf16sp (vec_t);
 @end smallexample
 
+@node RISC-V Built-in Functions
+@subsection RISC-V Built-in Functions
+
+These built-in functions are available for the RISC-V family of
+processors.
+
+@deftypefn {Built-in Function} {void *} __builtin_thread_pointer (void)
+Returns the value that is currently set in the @samp{tp} register.
+@end deftypefn
+
 @node RX Built-in Functions
 @subsection RX Built-in Functions
 GCC supports some of the RX instructions which cannot be expressed in
diff --git a/gcc/testsuite/gcc.target/riscv/read-thread-pointer.c b/gcc/testsuite/gcc.target/riscv/read-thread-pointer.c
new file mode 100644
index 000000000000..760f8eafb406
--- /dev/null
+++ b/gcc/testsuite/gcc.target/riscv/read-thread-pointer.c
@@ -0,0 +1,7 @@
+/* { dg-do compile } */
+
+void *get_tp()
+{
+    return __builtin_thread_pointer ();
+}
+/* { dg-final { scan-assembler "mv\[ \t\]*[at][0-9]+,tp" } } */
-- 
2.27.0


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

* Re: [PATCH] RISC-V: Implment __builtin_thread_pointer
  2020-07-07  9:52 [PATCH] RISC-V: Implment __builtin_thread_pointer Kito Cheng
@ 2020-07-08 19:59 ` Jim Wilson
  2020-07-09  6:48   ` Kito Cheng
  2021-03-01  8:17   ` Matthias Klose
  0 siblings, 2 replies; 6+ messages in thread
From: Jim Wilson @ 2020-07-08 19:59 UTC (permalink / raw)
  To: Kito Cheng; +Cc: GCC Patches, Kito Cheng

On Tue, Jul 7, 2020 at 2:52 AM Kito Cheng <kito.cheng@sifive.com> wrote:
> gcc/ChangeLog:
>         * gcc/config/riscv/riscv.md (): New.
>         (TP_REGNUM): Ditto.
>         * doc/extend.texi (Target Builtins): Add RISC-V built-in section.
>         Document __builtin_thread_pointer.
> gcc/testsuite/ChangeLog:
>         * gcc.target/riscv/read-thread-pointer.c: New.

It looks OK to me in general.

You added builtin_thread_pointer but not builtin_set_thread_pointer.
Maybe we should implement both as long as we are implementing one?  If
clang only implements one, maybe it should implement the other also?
This doesn't have to be part of this patch.  This could be a separate
issue.

The builtin_thread_pointer docs looks out-of-date.  It is documented
for alpha and SH, but it is implemented in gcc/builtins.c not in the
backends.  A scan of md files show that quite a few targets support it
but don't document it.  I think it should be documented in the generic
builtins section not in the target dependent builtins sections with
some language that says not all targets support it.  This doesn't have
to be part of this patch.  This could be a separate issue.

We have two existing undocumented builtins.  __builtin_riscv_fsflags
and __builtin_riscv_frflags for setting or reading the FP flags.  I
don't know if anyone uses them though.  newlib and glbic both use
extended asms for these operations.  This doesn't have to be part of
this patch.  This could be a separate issue.

There is a document https://github.com/riscv/riscv-c-api-doc for
coordinating gcc and llvm work that has an empty list of builtin
functions.  I'm not sure if this document is still useful.  If this is
a RISC-V specific builtin then it should be listed here, but I don't
think it should be considered a RISC-V specific builtin.  There is an
unresolved pull request for the frflags and fsflags builtins.  I guess
I forgot about that.

Jim

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

* Re: [PATCH] RISC-V: Implment __builtin_thread_pointer
  2020-07-08 19:59 ` Jim Wilson
@ 2020-07-09  6:48   ` Kito Cheng
  2020-07-09  6:52     ` Kito Cheng
  2021-03-01  8:17   ` Matthias Klose
  1 sibling, 1 reply; 6+ messages in thread
From: Kito Cheng @ 2020-07-09  6:48 UTC (permalink / raw)
  To: Jim Wilson; +Cc: Kito Cheng, GCC Patches

On Thu, Jul 9, 2020 at 4:03 AM Jim Wilson <jimw@sifive.com> wrote:
>
> On Tue, Jul 7, 2020 at 2:52 AM Kito Cheng <kito.cheng@sifive.com> wrote:
> > gcc/ChangeLog:
> >         * gcc/config/riscv/riscv.md (): New.
> >         (TP_REGNUM): Ditto.
> >         * doc/extend.texi (Target Builtins): Add RISC-V built-in section.
> >         Document __builtin_thread_pointer.
> > gcc/testsuite/ChangeLog:
> >         * gcc.target/riscv/read-thread-pointer.c: New.
>
> It looks OK to me in general.
>
> You added builtin_thread_pointer but not builtin_set_thread_pointer.
> Maybe we should implement both as long as we are implementing one?  If
> clang only implements one, maybe it should implement the other also?
> This doesn't have to be part of this patch.  This could be a separate
> issue.

LLVM side only implements builtin_thread_pointer at this moment,
and I also saw the gcc and only half target are implemented both,
so I only implement builtin_thread_pointer first, but I think it's good idea
to implement both.

> The builtin_thread_pointer docs looks out-of-date.  It is documented
> for alpha and SH, but it is implemented in gcc/builtins.c not in the
> backends.  A scan of md files show that quite a few targets support it
> but don't document it.  I think it should be documented in the generic
> builtins section not in the target dependent builtins sections with
> some language that says not all targets support it.  This doesn't have
> to be part of this patch.  This could be a separate issue.

Yeah, I've found the issue when I seeking the writing material for the doc :P


> We have two existing undocumented builtins.  __builtin_riscv_fsflags
> and __builtin_riscv_frflags for setting or reading the FP flags.  I
> don't know if anyone uses them though.  newlib and glbic both use
> extended asms for these operations.  This doesn't have to be part of
> this patch.  This could be a separate issue.

Got it.

> There is a document https://github.com/riscv/riscv-c-api-doc for
> coordinating gcc and llvm work that has an empty list of builtin
> functions.  I'm not sure if this document is still useful.  If this is
> a RISC-V specific builtin then it should be listed here, but I don't
> think it should be considered a RISC-V specific builtin.  There is an
> unresolved pull request for the frflags and fsflags builtins.  I guess
> I forgot about that.

Oh...I even forgot the PR was opened by me...

>
> Jim

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

* Re: [PATCH] RISC-V: Implment __builtin_thread_pointer
  2020-07-09  6:48   ` Kito Cheng
@ 2020-07-09  6:52     ` Kito Cheng
  0 siblings, 0 replies; 6+ messages in thread
From: Kito Cheng @ 2020-07-09  6:52 UTC (permalink / raw)
  To: Jim Wilson; +Cc: Kito Cheng, GCC Patches

Committed with typo fix (on the title) and ChangeLog fix.

On Thu, Jul 9, 2020 at 2:48 PM Kito Cheng <kito.cheng@gmail.com> wrote:
>
> On Thu, Jul 9, 2020 at 4:03 AM Jim Wilson <jimw@sifive.com> wrote:
> >
> > On Tue, Jul 7, 2020 at 2:52 AM Kito Cheng <kito.cheng@sifive.com> wrote:
> > > gcc/ChangeLog:
> > >         * gcc/config/riscv/riscv.md (): New.
> > >         (TP_REGNUM): Ditto.
> > >         * doc/extend.texi (Target Builtins): Add RISC-V built-in section.
> > >         Document __builtin_thread_pointer.
> > > gcc/testsuite/ChangeLog:
> > >         * gcc.target/riscv/read-thread-pointer.c: New.
> >
> > It looks OK to me in general.
> >
> > You added builtin_thread_pointer but not builtin_set_thread_pointer.
> > Maybe we should implement both as long as we are implementing one?  If
> > clang only implements one, maybe it should implement the other also?
> > This doesn't have to be part of this patch.  This could be a separate
> > issue.
>
> LLVM side only implements builtin_thread_pointer at this moment,
> and I also saw the gcc and only half target are implemented both,
> so I only implement builtin_thread_pointer first, but I think it's good idea
> to implement both.
>
> > The builtin_thread_pointer docs looks out-of-date.  It is documented
> > for alpha and SH, but it is implemented in gcc/builtins.c not in the
> > backends.  A scan of md files show that quite a few targets support it
> > but don't document it.  I think it should be documented in the generic
> > builtins section not in the target dependent builtins sections with
> > some language that says not all targets support it.  This doesn't have
> > to be part of this patch.  This could be a separate issue.
>
> Yeah, I've found the issue when I seeking the writing material for the doc :P
>
>
> > We have two existing undocumented builtins.  __builtin_riscv_fsflags
> > and __builtin_riscv_frflags for setting or reading the FP flags.  I
> > don't know if anyone uses them though.  newlib and glbic both use
> > extended asms for these operations.  This doesn't have to be part of
> > this patch.  This could be a separate issue.
>
> Got it.
>
> > There is a document https://github.com/riscv/riscv-c-api-doc for
> > coordinating gcc and llvm work that has an empty list of builtin
> > functions.  I'm not sure if this document is still useful.  If this is
> > a RISC-V specific builtin then it should be listed here, but I don't
> > think it should be considered a RISC-V specific builtin.  There is an
> > unresolved pull request for the frflags and fsflags builtins.  I guess
> > I forgot about that.
>
> Oh...I even forgot the PR was opened by me...
>
> >
> > Jim

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

* Re: [PATCH] RISC-V: Implment __builtin_thread_pointer
  2020-07-08 19:59 ` Jim Wilson
  2020-07-09  6:48   ` Kito Cheng
@ 2021-03-01  8:17   ` Matthias Klose
  2021-03-02  9:57     ` Kito Cheng
  1 sibling, 1 reply; 6+ messages in thread
From: Matthias Klose @ 2021-03-01  8:17 UTC (permalink / raw)
  To: Jim Wilson, Kito Cheng; +Cc: GCC Patches, Kito Cheng

On 7/8/20 9:59 PM, Jim Wilson wrote:
> On Tue, Jul 7, 2020 at 2:52 AM Kito Cheng <kito.cheng@sifive.com> wrote:
>> gcc/ChangeLog:
>>         * gcc/config/riscv/riscv.md (): New.
>>         (TP_REGNUM): Ditto.
>>         * doc/extend.texi (Target Builtins): Add RISC-V built-in section.
>>         Document __builtin_thread_pointer.
>> gcc/testsuite/ChangeLog:
>>         * gcc.target/riscv/read-thread-pointer.c: New.
> 
> It looks OK to me in general.
> 
> You added builtin_thread_pointer but not builtin_set_thread_pointer.
> Maybe we should implement both as long as we are implementing one?  If
> clang only implements one, maybe it should implement the other also?
> This doesn't have to be part of this patch.  This could be a separate
> issue.
> 
> The builtin_thread_pointer docs looks out-of-date.  It is documented
> for alpha and SH, but it is implemented in gcc/builtins.c not in the
> backends.  A scan of md files show that quite a few targets support it
> but don't document it.  I think it should be documented in the generic
> builtins section not in the target dependent builtins sections with
> some language that says not all targets support it.  This doesn't have
> to be part of this patch.  This could be a separate issue.
> 
> We have two existing undocumented builtins.  __builtin_riscv_fsflags
> and __builtin_riscv_frflags for setting or reading the FP flags.  I
> don't know if anyone uses them though.  newlib and glbic both use
> extended asms for these operations.  This doesn't have to be part of
> this patch.  This could be a separate issue.
> 
> There is a document https://github.com/riscv/riscv-c-api-doc for
> coordinating gcc and llvm work that has an empty list of builtin
> functions.  I'm not sure if this document is still useful.  If this is
> a RISC-V specific builtin then it should be listed here, but I don't
> think it should be considered a RISC-V specific builtin.  There is an
> unresolved pull request for the frflags and fsflags builtins.  I guess
> I forgot about that.
> 
> Jim

LLVM 12 now uses __builtin_thread_pointer, and fails to build with GCC 10 (but
builds with GCC 11).  Could you consider backporting this one to GCC 10?

Thanks, Matthias

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

* Re: [PATCH] RISC-V: Implment __builtin_thread_pointer
  2021-03-01  8:17   ` Matthias Klose
@ 2021-03-02  9:57     ` Kito Cheng
  0 siblings, 0 replies; 6+ messages in thread
From: Kito Cheng @ 2021-03-02  9:57 UTC (permalink / raw)
  To: Matthias Klose; +Cc: Jim Wilson, Kito Cheng, GCC Patches

Hi Matthias:

Sure, backported to gcc 10 branch, let's wait 10.3 :)

On Mon, Mar 1, 2021 at 4:17 PM Matthias Klose <doko@ubuntu.com> wrote:
>
> On 7/8/20 9:59 PM, Jim Wilson wrote:
> > On Tue, Jul 7, 2020 at 2:52 AM Kito Cheng <kito.cheng@sifive.com> wrote:
> >> gcc/ChangeLog:
> >>         * gcc/config/riscv/riscv.md (): New.
> >>         (TP_REGNUM): Ditto.
> >>         * doc/extend.texi (Target Builtins): Add RISC-V built-in section.
> >>         Document __builtin_thread_pointer.
> >> gcc/testsuite/ChangeLog:
> >>         * gcc.target/riscv/read-thread-pointer.c: New.
> >
> > It looks OK to me in general.
> >
> > You added builtin_thread_pointer but not builtin_set_thread_pointer.
> > Maybe we should implement both as long as we are implementing one?  If
> > clang only implements one, maybe it should implement the other also?
> > This doesn't have to be part of this patch.  This could be a separate
> > issue.
> >
> > The builtin_thread_pointer docs looks out-of-date.  It is documented
> > for alpha and SH, but it is implemented in gcc/builtins.c not in the
> > backends.  A scan of md files show that quite a few targets support it
> > but don't document it.  I think it should be documented in the generic
> > builtins section not in the target dependent builtins sections with
> > some language that says not all targets support it.  This doesn't have
> > to be part of this patch.  This could be a separate issue.
> >
> > We have two existing undocumented builtins.  __builtin_riscv_fsflags
> > and __builtin_riscv_frflags for setting or reading the FP flags.  I
> > don't know if anyone uses them though.  newlib and glbic both use
> > extended asms for these operations.  This doesn't have to be part of
> > this patch.  This could be a separate issue.
> >
> > There is a document https://github.com/riscv/riscv-c-api-doc for
> > coordinating gcc and llvm work that has an empty list of builtin
> > functions.  I'm not sure if this document is still useful.  If this is
> > a RISC-V specific builtin then it should be listed here, but I don't
> > think it should be considered a RISC-V specific builtin.  There is an
> > unresolved pull request for the frflags and fsflags builtins.  I guess
> > I forgot about that.
> >
> > Jim
>
> LLVM 12 now uses __builtin_thread_pointer, and fails to build with GCC 10 (but
> builds with GCC 11).  Could you consider backporting this one to GCC 10?
>
> Thanks, Matthias

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

end of thread, other threads:[~2021-03-02  9:57 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-07-07  9:52 [PATCH] RISC-V: Implment __builtin_thread_pointer Kito Cheng
2020-07-08 19:59 ` Jim Wilson
2020-07-09  6:48   ` Kito Cheng
2020-07-09  6:52     ` Kito Cheng
2021-03-01  8:17   ` Matthias Klose
2021-03-02  9:57     ` Kito Cheng

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