public inbox for newlib@sourceware.org
 help / color / mirror / Atom feed
From: Tobias Burnus <tobias@codesourcery.com>
To: <newlib@sourceware.org>
Subject: [PATCH] amdgcn: Use __builtin_gcn_ in libc/machine/amdgcn/getreent.c
Date: Tue, 22 Nov 2022 17:59:38 +0100	[thread overview]
Message-ID: <3975025a-2aa8-910b-1a2e-575c047d5b46@codesourcery.com> (raw)

[-- Attachment #1: Type: text/plain, Size: 560 bytes --]

This patch replaces some more hard-coded register use and
coded internal implementation choices by compiler builtins –
added in this case of GCC 13 (mainline). #if conditions are
used to use the builtins if (and only if) they are available
in the used compiler.

Thanks,

Tobias
-----------------
Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955

[-- Attachment #2: 0001-amdgcn-Use-__builtin_gcn_-in-libc-machine-amdgcn-get.patch --]
[-- Type: text/x-patch, Size: 2773 bytes --]

From 611559a7939c29afeda6bb585fb6ba846e5a8b0e Mon Sep 17 00:00:00 2001
From: Tobias Burnus <tobias@codesourcery.com>
Date: Tue, 22 Nov 2022 14:53:48 +0100
Subject: [PATCH] amdgcn: Use __builtin_gcn_ in libc/machine/amdgcn/getreent.c

Call __builtin_gcn_get_stack_limit and __builtin_gcn_first_call_this_thread_p
to reduce dependency on some register/layout assumptions by using the new
GCC mainline (GCC 13) builtins, if they are available. If not, the existing
code is used.
---
 newlib/libc/machine/amdgcn/getreent.c | 38 ++++++++++++++++++++-------
 1 file changed, 29 insertions(+), 9 deletions(-)

diff --git a/newlib/libc/machine/amdgcn/getreent.c b/newlib/libc/machine/amdgcn/getreent.c
index be7d2edc9..ef731f649 100644
--- a/newlib/libc/machine/amdgcn/getreent.c
+++ b/newlib/libc/machine/amdgcn/getreent.c
@@ -29,22 +29,42 @@ typedef struct hsa_kernel_dispatch_packet_s {
 struct _reent *
 __getreent (void)
 {
-  /* Place the reent data at the top of the stack allocation.
-     s[0:1] contains a 48-bit private segment base address.
+  /* Place the reent data at the top of the stack allocation.  */
+  struct data {
+    int marker;
+    struct _reent reent;
+  } *data;
+
+#if defined(__has_builtin) \
+    && __has_builtin(__builtin_gcn_get_stack_limit) \
+    && __has_builtin(__builtin_gcn_first_call_this_thread_p)
+  unsigned long addr = (((unsigned long) __builtin_gcn_get_stack_limit()
+			 - sizeof(struct data)) & ~7);
+  data = (struct data *)addr;
+
+  register long sp asm("s16");
+
+  if (sp >= addr)
+    goto stackoverflow;
+  if (__builtin_gcn_first_call_this_thread_p())
+    {
+      data->marker = 12345;
+      __builtin_memset (&data->reent, 0, sizeof(struct _reent));
+      _REENT_INIT_PTR_ZEROED (&data->reent);
+    }
+  else if (data->marker != 12345)
+    goto stackoverflow;
+#else
+  /* s[0:1] contains a 48-bit private segment base address.
      s11 contains the offset to the base of the stack.
      s[4:5] contains the dispatch pointer.
-     
+
      WARNING: this code will break if s[0:1] is ever used for anything!  */
   const register unsigned long buffer_descriptor asm("s0");
   unsigned long private_segment = buffer_descriptor & 0x0000ffffffffffff;
   const register unsigned int stack_offset asm("s11");
   const register hsa_kernel_dispatch_packet_t *dispatch_ptr asm("s4");
 
-  struct data {
-    int marker;
-    struct _reent reent;
-  } *data;
-
   unsigned long stack_base = private_segment + stack_offset;
   unsigned long stack_end = stack_base + dispatch_ptr->private_segment_size * 64;
   unsigned long addr = (stack_end - sizeof(struct data)) & ~7;
@@ -69,7 +89,7 @@ __getreent (void)
     }
   else if (data->marker != 12345)
     goto stackoverflow;
-
+#endif
 
   return &data->reent;
 
-- 
2.25.1


             reply	other threads:[~2022-11-22 16:59 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-11-22 16:59 Tobias Burnus [this message]
2022-11-22 23:08 ` Jeff Johnston

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=3975025a-2aa8-910b-1a2e-575c047d5b46@codesourcery.com \
    --to=tobias@codesourcery.com \
    --cc=newlib@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).