gcn/mkoffload.cc: Fix SRAM_ECC and XNACK handling [PR111966]

Some more '-g' fixes as the .mkoffload.dbg.o debug file's has elf flags
which did not match those generated for the compilation, leading to linker
errors.  For .mkoffload.dbg.o, the elf flags are generated by mkoffload
itself - while for the other .o files, that's done by the compiler via
setting default and mainly via the ASM_SPEC.

This is a follow up to r14-8332-g13127dac106724 which fixed an issue
caused by the default arch.  In this patch, it is mainly for gfx1100
and gfx1030 which always failed.  It also affects gfx906 and possibly
gfx900 but only when using the -mxnack/-msram-ecc flags explicitly.

What happens on the compiler side is mainly determined by gcn-hsa.h's
and otherwise by some default setting. In particular for xnack and
sram_ecc, there is:

For gfx1100 and gfx1030, neither xnack nor sram_ecc is set (only
'+wavefrontsize64'). 

For fiji, gfx900 and gfx906 there is always -mattr=-xnack and
no -msram-ecc - independent of what has been passed to the compiler.
On elf flags level, that's not quite true as for fiji those flags are
set to 0 because of HSACOv3 and for gfx900 there is a remark that even
if -msram-ecc were set (which it isn't), the elf flags would be still
zero because of a buggy assembler. See copy_early_debug_info.

For gfx90a, the -msram-ecc= and -mxnack= are passed on, or if not present,
...=any is passed on.  Note that this "any" is different from argument
nor present at elf flag level:
For XNACK: unset/unsupported is 0, any = 0x100, off = 0x200, on = 0x300.
For SRAMECC: unset/unsupported is 0, any = 0x400, off = 0x800, on = 0xc00.

The obstack_ptr_grow changes are more to avoid confusion than having an
actual effect as they would overwise be filtered out via the ASM_SPEC.

gcc/ChangeLog:

	PR other/111966
	* config/gcn/mkoffload.cc (SET_XNACK_UNSET, TEST_SRAM_ECC_UNSET): New.
	(SET_SRAM_ECC_UNSUPPORTED): Renamed to ...
	(SET_SRAM_ECC_UNSET): ... this.
	(main): Update SRAM_ECC and XNACK for the -march as done in gcn-hsa.h.

Signed-off-by: Tobias Burnus <tburnus@baylibre.com>


 gcc/config/gcn/mkoffload.cc | 38 ++++++++++++++++++++++++--------------
 1 file changed, 24 insertions(+), 14 deletions(-)

diff --git a/gcc/config/gcn/mkoffload.cc b/gcc/config/gcn/mkoffload.cc
index 0d0e7bac9b2..218ad888aea 100644
--- a/gcc/config/gcn/mkoffload.cc
+++ b/gcc/config/gcn/mkoffload.cc
@@ -80,6 +80,8 @@
 				  | EF_AMDGPU_FEATURE_XNACK_ANY_V4)
 #define SET_XNACK_OFF(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_XNACK_V4) \
 				  | EF_AMDGPU_FEATURE_XNACK_OFF_V4)
+#define SET_XNACK_UNSET(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_XNACK_V4) \
+				    | EF_AMDGPU_FEATURE_XNACK_UNSUPPORTED_V4)
 #define TEST_XNACK_ANY(VAR) ((VAR & EF_AMDGPU_FEATURE_XNACK_V4) \
 			     == EF_AMDGPU_FEATURE_XNACK_ANY_V4)
 #define TEST_XNACK_ON(VAR) ((VAR & EF_AMDGPU_FEATURE_XNACK_V4) \
@@ -94,13 +96,14 @@
 				     | EF_AMDGPU_FEATURE_SRAMECC_ANY_V4)
 #define SET_SRAM_ECC_OFF(VAR) VAR = ((VAR & ~EF_AMDGPU_FEATURE_SRAMECC_V4) \
 				     | EF_AMDGPU_FEATURE_SRAMECC_OFF_V4)
-#define SET_SRAM_ECC_UNSUPPORTED(VAR) \
+#define SET_SRAM_ECC_UNSET(VAR) \
   VAR = ((VAR & ~EF_AMDGPU_FEATURE_SRAMECC_V4) \
 	 | EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4)
 #define TEST_SRAM_ECC_ANY(VAR) ((VAR & EF_AMDGPU_FEATURE_SRAMECC_V4) \
 				== EF_AMDGPU_FEATURE_SRAMECC_ANY_V4)
 #define TEST_SRAM_ECC_ON(VAR) ((VAR & EF_AMDGPU_FEATURE_SRAMECC_V4) \
 			       == EF_AMDGPU_FEATURE_SRAMECC_ON_V4)
+#define TEST_SRAM_ECC_UNSET(VAR) ((VAR & EF_AMDGPU_FEATURE_SRAMECC_V4) == 0)
 
 #ifndef R_AMDGPU_NONE
 #define R_AMDGPU_NONE		0
@@ -125,7 +128,7 @@ static struct obstack files_to_cleanup;
 
 enum offload_abi offload_abi = OFFLOAD_ABI_UNSET;
 uint32_t elf_arch = EF_AMDGPU_MACH_AMDGCN_GFX900;  // Default GPU architecture.
-uint32_t elf_flags = EF_AMDGPU_FEATURE_SRAMECC_ANY_V4;
+uint32_t elf_flags = EF_AMDGPU_FEATURE_SRAMECC_UNSUPPORTED_V4;
 
 static int gcn_stack_size = 0;  /* Zero means use default.  */
 
@@ -1007,21 +1010,26 @@ main (int argc, char **argv)
       gcc_unreachable ();
     }
 
-  /* Disable XNACK mode on architectures where it doesn't work (well).
-     Set default to "any" otherwise.  */
+  /* This must match gcn-hsa.h's settings for NO_XNACK, NO_SRAM_ECC
+     and ASM_SPEC.  */
   switch (elf_arch)
     {
-    case EF_AMDGPU_MACH_AMDGCN_GFX803:
     case EF_AMDGPU_MACH_AMDGCN_GFX900:
     case EF_AMDGPU_MACH_AMDGCN_GFX906:
     case EF_AMDGPU_MACH_AMDGCN_GFX908:
+      SET_XNACK_OFF (elf_flags);
+      break;
+    case EF_AMDGPU_MACH_AMDGCN_GFX803:
     case EF_AMDGPU_MACH_AMDGCN_GFX1030:
     case EF_AMDGPU_MACH_AMDGCN_GFX1100:
-      SET_XNACK_OFF (elf_flags);
+      SET_XNACK_UNSET (elf_flags);
+      SET_SRAM_ECC_UNSET (elf_flags);
       break;
     case EF_AMDGPU_MACH_AMDGCN_GFX90a:
       if (TEST_XNACK_UNSET (elf_flags))
 	SET_XNACK_ANY (elf_flags);
+      if (TEST_SRAM_ECC_UNSET (elf_flags))
+	SET_SRAM_ECC_ANY (elf_flags);
       break;
     default:
       fatal_error (input_location, "unhandled architecture");
@@ -1145,14 +1153,16 @@ main (int argc, char **argv)
 	}
       obstack_ptr_grow (&ld_argv_obstack, gcn_s2_name);
       obstack_ptr_grow (&ld_argv_obstack, "-lgomp");
-      obstack_ptr_grow (&ld_argv_obstack,
-			(TEST_XNACK_ON (elf_flags) ? "-mxnack=on"
-			 : TEST_XNACK_ANY (elf_flags) ? "-mxnack=any"
-			 : "-mxnack=off"));
-      obstack_ptr_grow (&ld_argv_obstack,
-			(TEST_SRAM_ECC_ON (elf_flags) ? "-msram-ecc=on"
-			 : TEST_SRAM_ECC_ANY (elf_flags) ? "-msram-ecc=any"
-			 : "-msram-ecc=off"));
+      if (!TEST_XNACK_UNSET (elf_flags))
+	obstack_ptr_grow (&ld_argv_obstack,
+			  (TEST_XNACK_ON (elf_flags) ? "-mxnack=on"
+			   : TEST_XNACK_ANY (elf_flags) ? "-mxnack=any"
+			   : "-mxnack=off"));
+      if (!TEST_SRAM_ECC_UNSET (elf_flags))
+	obstack_ptr_grow (&ld_argv_obstack,
+			  (TEST_SRAM_ECC_ON (elf_flags) ? "-msram-ecc=on"
+			   : TEST_SRAM_ECC_ANY (elf_flags) ? "-msram-ecc=any"
+			   : "-msram-ecc=off"));
       if (verbose)
 	obstack_ptr_grow (&ld_argv_obstack, "-v");
 
