Hi!

On 2021-07-19T10:46:35+0200, I wrote:
> | On 7/16/21 11:42 AM, Thomas Schwinge wrote:
> |> <https://gcc.gnu.org/bugzilla/show_bug.cgi?id=101374#c16>, similar
> |> problem with GCN target libgomp build:
> |> 
> |>      In function ‘gcn_thrs’,
> |>          inlined from ‘gomp_thread’ at 
> [...]/source-gcc/libgomp/libgomp.h:803:10,
> |>          inlined from ‘GOMP_barrier’ at 
> [...]/source-gcc/libgomp/barrier.c:34:29:
> |>      [...]/source-gcc/libgomp/libgomp.h:792:10: error: array subscript 0 
> is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ 
> [-Werror=array-bounds]
> |>        792 |   return *thrs;
> |>            |          ^~~~~
> |> 
> |>      gcc/config/gcn/gcn.h:  c_register_addr_space ("__lds", 
> ADDR_SPACE_LDS);                   \
> |> 
> |>      libgomp/libgomp.h-static inline struct gomp_thread *gcn_thrs (void)
> |>      libgomp/libgomp.h-{
> |>      libgomp/libgomp.h-  /* The value is at the bottom of LDS.  */
> |>      libgomp/libgomp.h:  struct gomp_thread * __lds *thrs = (struct 
> gomp_thread * __lds *)4;
> |>      libgomp/libgomp.h-  return *thrs;
> |>      libgomp/libgomp.h-}
> |> 
> |> ..., plus a few more.  [...]

> [...] I've
> thus pushed "[gcn] Work-around libgomp 'error: array subscript 0 is
> outside array bounds of ‘__lds struct gomp_thread * __lds[0]’
> [-Werror=array-bounds]' [PR101484]" [...]

> Now: "Awaiting a different solution, of course."  ;-)

Well, I just stumbled over that one; please have a look at the attached
"GCN: Implement 'TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID' [PR101484]".

I'm not very familiar with the GCN named address spaces -- in addition to
'__lds', which ones to include in 'TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID',
and/or instead of 'as == ADDR_SPACE_LDS', which 'AS_[...]_P' conditional
to use?

'gcc/config/gcn/gcn.h':

    /* Address spaces.  */
    enum gcn_address_spaces
    {
      ADDR_SPACE_DEFAULT = 0,
      ADDR_SPACE_FLAT,
      ADDR_SPACE_SCALAR_FLAT,
      ADDR_SPACE_FLAT_SCRATCH,
      ADDR_SPACE_LDS,
      ADDR_SPACE_GDS,
      ADDR_SPACE_SCRATCH,
      ADDR_SPACE_GLOBAL
    };
    #define REGISTER_TARGET_PRAGMAS() do {                               \
      c_register_addr_space ("__flat", ADDR_SPACE_FLAT);                 \
      c_register_addr_space ("__flat_scratch", ADDR_SPACE_FLAT_SCRATCH); \
      c_register_addr_space ("__scalar_flat", ADDR_SPACE_SCALAR_FLAT);   \
      c_register_addr_space ("__lds", ADDR_SPACE_LDS);                   \
      c_register_addr_space ("__gds", ADDR_SPACE_GDS);                   \
      c_register_addr_space ("__global", ADDR_SPACE_GLOBAL);             \
    } while (0);
    
    #define STACK_ADDR_SPACE ADDR_SPACE_GLOBAL
    #define DEFAULT_ADDR_SPACE \
      ((cfun && cfun->machine && !cfun->machine->use_flat_addressing) \
       ? ADDR_SPACE_GLOBAL : ADDR_SPACE_FLAT)
    #define AS_SCALAR_FLAT_P(AS)   ((AS) == ADDR_SPACE_SCALAR_FLAT)
    #define AS_FLAT_SCRATCH_P(AS)  ((AS) == ADDR_SPACE_FLAT_SCRATCH)
    #define AS_FLAT_P(AS)              ((AS) == ADDR_SPACE_FLAT \
                                || ((AS) == ADDR_SPACE_DEFAULT \
                                    && DEFAULT_ADDR_SPACE == ADDR_SPACE_FLAT))
    #define AS_LDS_P(AS)               ((AS) == ADDR_SPACE_LDS)
    #define AS_GDS_P(AS)               ((AS) == ADDR_SPACE_GDS)
    #define AS_SCRATCH_P(AS)       ((AS) == ADDR_SPACE_SCRATCH)
    #define AS_GLOBAL_P(AS)        ((AS) == ADDR_SPACE_GLOBAL \
                                || ((AS) == ADDR_SPACE_DEFAULT \
                                    && DEFAULT_ADDR_SPACE == ADDR_SPACE_GLOBAL))
    #define AS_ANY_FLAT_P(AS)      (AS_FLAT_SCRATCH_P (AS) || AS_FLAT_P (AS))
    #define AS_ANY_DS_P(AS)            (AS_LDS_P (AS) || AS_GDS_P (AS))


Additionally:

On 2021-07-19T12:10:59+0100, Andrew Stubbs <[email protected]> wrote:
> On 19/07/2021 09:46, Thomas Schwinge wrote:
>>> GCN already uses address 4 for this value because address 0 caused
>>> problems with null-pointer checks.
>> 
>> Ugh.  How much wasted bytes per what is that?  (I haven't looked yet;
>> hopefully not per GPU thread?)  Because:
>
> It's 4 bytes per gang. And that pointer is the only 8 bytes in the whole 
> of LDS (OpenMP mostly uses stack and heap), so it's not so bad, but still.
>
> I did investigate the target macro that lets you control null pointer 
> behaviour, but it didn't just work, and it wasn't important enough for 
> me to spend more time on it so I let it go.

Maybe what we get per the above ('TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID')
is already sufficient to also get rid of having to use "address 4 for
this value because address 0 caused problems with null-pointer checks"?
(I've not yet tried.)


Grüße
 Thomas


>From 50fa0cc04cef6b5c471e380d42cd3d4e3ec4a965 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <[email protected]>
Date: Thu, 11 Jun 2026 21:41:10 +0200
Subject: [PATCH] GCN: Implement 'TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID'
 [PR101484]
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

This is the proper fix to replace
commit 9f2bc5077debef2b046b6c10d38591ac324ad8b5
"[gcn] Work-around libgomp 'error: array subscript 0 is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ [-Werror=array-bounds]' [PR101484]",
commit 8168338684fc2bed576bb09202c63b3e9e678d92
"[gcn] Work-around libgomp 'error: array subscript 0 is outside array bounds of ‘__lds struct gomp_thread * __lds[0]’ [-Werror=array-bounds]' some more [PR101484]".

	PR target/101484
	gcc/
	* config/gcn/gcn.cc (TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID): Implement.
	libgomp/
	* configure.tgt [GCN] (XCFLAGS): Don't add '-Wno-error=array-bounds'.
---
 gcc/config/gcn/gcn.cc | 10 ++++++++++
 libgomp/configure.tgt |  3 ---
 2 files changed, 10 insertions(+), 3 deletions(-)

diff --git a/gcc/config/gcn/gcn.cc b/gcc/config/gcn/gcn.cc
index 18995d61e7b..ed27acfb8a1 100644
--- a/gcc/config/gcn/gcn.cc
+++ b/gcc/config/gcn/gcn.cc
@@ -1874,6 +1874,14 @@ gcn_addr_space_subset_p (addr_space_t subset, addr_space_t superset)
   return false;
 }
 
+static bool
+gcn_addr_space_zero_address_valid (addr_space_t as)
+{
+  if (as == ADDR_SPACE_LDS)
+    return true;
+  return false;
+}
+
 static addr_space_t
 gcn_addr_space_resolve_default (addr_space_t as)
 {
@@ -8085,6 +8093,8 @@ gcn_dwarf_register_span (rtx rtl)
 #define TARGET_ADDR_SPACE_POINTER_MODE gcn_addr_space_pointer_mode
 #undef  TARGET_ADDR_SPACE_SUBSET_P
 #define TARGET_ADDR_SPACE_SUBSET_P gcn_addr_space_subset_p
+#undef  TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID
+#define TARGET_ADDR_SPACE_ZERO_ADDRESS_VALID gcn_addr_space_zero_address_valid
 #undef  TARGET_ADDR_SPACE_CONVERT
 #define TARGET_ADDR_SPACE_CONVERT gcn_addr_space_convert
 #undef  TARGET_ARG_PARTIAL_BYTES
diff --git a/libgomp/configure.tgt b/libgomp/configure.tgt
index 46af75f978f..a74e966fdfd 100644
--- a/libgomp/configure.tgt
+++ b/libgomp/configure.tgt
@@ -181,9 +181,6 @@ case "${target}" in
 
   amdgcn*-*-*)
 	config_path="gcn accel"
-
-	#TODO PR101484
-	XCFLAGS="$XCFLAGS -Wno-error=array-bounds"
 	;;
 
   *)
-- 
2.34.1

Reply via email to