diff mbox series

GCN – call assembler with -mattr=-code-object-v3 (PR93409)

Message ID 5a2d66cd-5206-fb0a-21f9-c7a6f281282d@codesourcery.com
State New
Headers show
Series GCN – call assembler with -mattr=-code-object-v3 (PR93409) | expand

Commit Message

Tobias Burnus Jan. 29, 2020, 12:53 p.m. UTC
Cf. PR93409 comments 4 and later. The comments 1–3 of the PR are covered 
by patch https://gcc.gnu.org/ml/gcc-patches/2020-01/msg01663.html (skip 
building libgomp's HSA/GCN plugin with -mx32).

For AMDGCN, the LLVM assembler is used. While for LLVM 7+8, GCC 
generates code which is the same as LLVM, for LLVM 9 (and later), the 
LLVM switched from Code Object V2 to V3.

With LLVM 9, the old variant is only accepted when also passing 
"-mattr=-code-object-v3" to the compiler; that's a"-" after the "=" to 
disable V3. (Cf. also 
https://sources.debian.org/src/gcc-10/10-20200117-2/debian/patches/gcc-amdgcn-fix.diff/).

My initial idea was to add a configure check – which I also include as 
attachment. However, it turned out that at last the assembler of LLVM 
version 7.0.1 already supports this flag. Hence, we can go for the 
simpler solution and use the assembler flag unconditionally.

OK for the trunk?

Tobias

Comments

Andrew Stubbs Jan. 29, 2020, 1:01 p.m. UTC | #1
On 29/01/2020 12:53, Tobias Burnus wrote:
> Cf. PR93409 comments 4 and later. The comments 1–3 of the PR are covered 
> by patch https://gcc.gnu.org/ml/gcc-patches/2020-01/msg01663.html (skip 
> building libgomp's HSA/GCN plugin with -mx32).
> 
> For AMDGCN, the LLVM assembler is used. While for LLVM 7+8, GCC 
> generates code which is the same as LLVM, for LLVM 9 (and later), the 
> LLVM switched from Code Object V2 to V3.
> 
> With LLVM 9, the old variant is only accepted when also passing 
> "-mattr=-code-object-v3" to the compiler; that's a"-" after the "=" to 
> disable V3. (Cf. also 
> https://sources.debian.org/src/gcc-10/10-20200117-2/debian/patches/gcc-amdgcn-fix.diff/). 
> 
> 
> My initial idea was to add a configure check – which I also include as 
> attachment. However, it turned out that at last the assembler of LLVM 
> version 7.0.1 already supports this flag. Hence, we can go for the 
> simpler solution and use the assembler flag unconditionally.
> 
> OK for the trunk?

What testing have you done?

I've been holding off submitting what is essentially this patch myself 
until I can run a full test with LLVM 8/9. It just hasn't got to the top 
of my list to set that up yet.

It looks right, of course, but I don't want to just assume so.

Andrew
Tobias Burnus Jan. 29, 2020, 3:40 p.m. UTC | #2
Hi Andrew,

On 1/29/20 2:01 PM, Andrew Stubbs wrote:
> On 29/01/2020 12:53, Tobias Burnus wrote:
>> With LLVM 9, the old variant is only accepted when also passing 
>> "-mattr=-code-object-v3" to the compiler; that's a"-" after the "=" 
>> to disable V3. […]
>>
>> it turned out that at last the assembler of LLVM version 7.0.1 
>> already supports this flag. Hence, we can go for the simpler solution 
>> and use the assembler flag unconditionally.
>>
>> OK for the trunk?
> What testing have you done?

I tested building GCC both as amdgcn target compiler and as 
x86-64-gnu-linux compiler with GCN offloading using LLVM 7.0.1. And I 
tested building GCC for GCN as target compiler using LLVM 9.0.1 – I 
also  did (start) run the test suite for GCN as target compiler, only 
(both LLVM 7 + 9) and got many PASS.

Cheers,

Tobias
Andrew Stubbs Jan. 29, 2020, 4:42 p.m. UTC | #3
On 29/01/2020 15:40, Tobias Burnus wrote:
> Hi Andrew,
> 
> On 1/29/20 2:01 PM, Andrew Stubbs wrote:
>> On 29/01/2020 12:53, Tobias Burnus wrote:
>>> With LLVM 9, the old variant is only accepted when also passing 
>>> "-mattr=-code-object-v3" to the compiler; that's a"-" after the "=" 
>>> to disable V3. […]
>>>
>>> it turned out that at last the assembler of LLVM version 7.0.1 
>>> already supports this flag. Hence, we can go for the simpler solution 
>>> and use the assembler flag unconditionally.
>>>
>>> OK for the trunk?
>> What testing have you done?
> 
> I tested building GCC both as amdgcn target compiler and as 
> x86-64-gnu-linux compiler with GCN offloading using LLVM 7.0.1. And I 
> tested building GCC for GCN as target compiler using LLVM 9.0.1 – I 
> also  did (start) run the test suite for GCN as target compiler, only 
> (both LLVM 7 + 9) and got many PASS.

OK, in that case let's go ahead.

Any problems that do show in a full test run are unlikely to negate this 
patch, given it's not exploded instantly.

Andrew
diff mbox series

Patch

* IGNORE *
THIS PATCH IS NOT NEEDED AS LLVM 7's ASSEMBLER ALREADY SUPPORTS THAT OPTION.
ONLY THE DEFAULT CHANGED WITH LLVM 9

* gcc/configure.ac: Add -mattr=-code-object-v3 check.
* gcc/config/gcn/gcn-hsa.h (ASM_SPEC): use it

 gcc/config.in            | 12 +++++++
 gcc/config/gcn/gcn-hsa.h |  7 ++++
 gcc/configure            | 91 +++++++++++++++++++++++++++++++++++++++++++-----
 gcc/configure.ac         | 33 ++++++++++++++++++
 4 files changed, 135 insertions(+), 8 deletions(-)

diff --git a/gcc/config.in b/gcc/config.in
index ec5c46abdb5..05dc9499f22 100644
--- a/gcc/config.in
+++ b/gcc/config.in
@@ -546,6 +546,12 @@ 
 #endif
 
 
+/* Define if your assembler supports -mattr=-code-object-v3. */
+#ifndef USED_FOR_TARGET
+#undef HAVE_AS_MATTR_CODE_OBJECT_V3
+#endif
+
+
 /* Define if your assembler supports mfcr field. */
 #ifndef USED_FOR_TARGET
 #undef HAVE_AS_MFCRF
@@ -2185,6 +2191,12 @@ 
 #endif
 
 
+/* Define if your target C Library properly handles PT_GNU_STACK */
+#ifndef USED_FOR_TARGET
+#undef TARGET_LIBC_GNUSTACK
+#endif
+
+
 /* Define if your target C Library provides the AT_HWCAP value in the TCB */
 #ifndef USED_FOR_TARGET
 #undef TARGET_LIBC_PROVIDES_HWCAP_IN_TCB
diff --git a/gcc/config/gcn/gcn-hsa.h b/gcc/config/gcn/gcn-hsa.h
index cefe67217e7..9ba2b543519 100644
--- a/gcc/config/gcn/gcn-hsa.h
+++ b/gcc/config/gcn/gcn-hsa.h
@@ -76,9 +76,16 @@  extern unsigned int gcn_local_sym_hash (const char *name);
 #define GOMP_SELF_SPECS ""
 
 /* Use LLVM assembler and linker options.  */
+#if HAVE_AS_MATTR_CODE_OBJECT_V3
+#define ASM_SPEC  "-triple=amdgcn--amdhsa -mattr=-code-object-v3 "  \
+		  "%:last_arg(%{march=*:-mcpu=%*}) " \
+		  "-filetype=obj"
+#else
 #define ASM_SPEC  "-triple=amdgcn--amdhsa "	     \
 		  "%:last_arg(%{march=*:-mcpu=%*}) " \
 		  "-filetype=obj"
+#endif
+
 /* Add -mlocal-symbol-id=<source-file-basename> unless the user (or mkoffload)
    passes the option explicitly on the command line.  The option also causes
    several dump-matching tests to fail in the testsuite, so the option is not
diff --git a/gcc/configure b/gcc/configure
index 490fe6ac8e8..0b6a8cb23a4 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -974,6 +974,7 @@  with_zstd_include
 with_zstd_lib
 enable_rpath
 with_libiconv_prefix
+with_libiconv_type
 enable_sjlj_exceptions
 with_gcc_major_version_only
 enable_secureplt
@@ -1811,6 +1812,7 @@  Optional Packages:
   --with-gnu-ld           assume the C compiler uses GNU ld default=no
   --with-libiconv-prefix[=DIR]  search for libiconv in DIR/include and DIR/lib
   --without-libiconv-prefix     don't search for libiconv in includedir and libdir
+  --with-libiconv-type=TYPE     type of library to search for (auto/static/shared)
   --with-gcc-major-version-only
                           use only GCC major number in filesystem paths
   --with-pic              try to use only PIC/non-PIC objects [default=use
@@ -10730,6 +10732,16 @@  if test "${with_libiconv_prefix+set}" = set; then :
 
 fi
 
+
+# Check whether --with-libiconv-type was given.
+if test "${with_libiconv_type+set}" = set; then :
+  withval=$with_libiconv_type;  with_libiconv_type=$withval
+else
+   with_libiconv_type=auto
+fi
+
+  lib_type=`eval echo \$with_libiconv_type`
+
       LIBICONV=
   LTLIBICONV=
   INCICONV=
@@ -10767,13 +10779,13 @@  fi
           found_so=
           found_a=
           if test $use_additional = yes; then
-            if test -n "$shlibext" && test -f "$additional_libdir/lib$name.$shlibext"; then
+            if test -n "$shlibext" && test -f "$additional_libdir/lib$name.$shlibext" && test x$lib_type != xstatic; then
               found_dir="$additional_libdir"
               found_so="$additional_libdir/lib$name.$shlibext"
               if test -f "$additional_libdir/lib$name.la"; then
                 found_la="$additional_libdir/lib$name.la"
               fi
-            else
+            elif test x$lib_type != xshared; then
               if test -f "$additional_libdir/lib$name.$libext"; then
                 found_dir="$additional_libdir"
                 found_a="$additional_libdir/lib$name.$libext"
@@ -10797,13 +10809,13 @@  fi
               case "$x" in
                 -L*)
                   dir=`echo "X$x" | sed -e 's/^X-L//'`
-                  if test -n "$shlibext" && test -f "$dir/lib$name.$shlibext"; then
+                  if test -n "$shlibext" && test -f "$dir/lib$name.$shlibext" && test x$lib_type != xstatic; then
                     found_dir="$dir"
                     found_so="$dir/lib$name.$shlibext"
                     if test -f "$dir/lib$name.la"; then
                       found_la="$dir/lib$name.la"
                     fi
-                  else
+                  elif test x$lib_type != xshared; then
                     if test -f "$dir/lib$name.$libext"; then
                       found_dir="$dir"
                       found_a="$dir/lib$name.$libext"
@@ -11031,8 +11043,13 @@  fi
               done
             fi
           else
-                                                            LIBICONV="${LIBICONV}${LIBICONV:+ }-l$name"
-            LTLIBICONV="${LTLIBICONV}${LTLIBICONV:+ }-l$name"
+                                                            if x$lib_type = xauto || x$lib_type = xshared; then
+              LIBICONV="${LIBICONV}${LIBICONV:+ }-l$name"
+              LTLIBICONV="${LTLIBICONV}${LTLIBICONV:+ }-l$name"
+            else
+              LIBICONV="${LIBICONV}${LIBICONV:+ }-l:lib$name.$libext"
+              LTLIBICONV="${LTLIBICONV}${LTLIBICONV:+ }-l:lib$name.$libext"
+            fi
           fi
         fi
       fi
@@ -18957,7 +18974,7 @@  else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 18960 "configure"
+#line 18977 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -19063,7 +19080,7 @@  else
   lt_dlunknown=0; lt_dlno_uscore=1; lt_dlneed_uscore=2
   lt_status=$lt_dlunknown
   cat > conftest.$ac_ext <<_LT_EOF
-#line 19066 "configure"
+#line 19083 "configure"
 #include "confdefs.h"
 
 #if HAVE_DLFCN_H
@@ -28158,6 +28175,47 @@  case "$target" in
     ;;
 esac
 
+# AMGCGN: Check whether the assembler supports -mattr=-code-object-v3
+case "$target" in
+  amdgcn-* | gcn-*)
+    { $as_echo "$as_me:${as_lineno-$LINENO}: checking assembler support for -mattr=-code-object-v3" >&5
+$as_echo_n "checking assembler support for -mattr=-code-object-v3... " >&6; }
+    gcc_cv_as_mattr_code_object_v3=no
+    if test x$gcc_cv_as != x; then
+      cat > conftest.s <<EOF
+        .text
+        .hsa_code_object_version 2,0
+        .hsa_code_object_isa
+        .section        .AMDGPU.config
+        .text
+        .section        .text
+        .globl  a
+        .data
+        .align  16
+        .type   a, @object
+        .size   a, 4
+EOF
+      if { ac_try='$gcc_cv_as -triple=amdgcn--amdhsa -filetype=obj -mattr=-code-object-v3 conftest.s -o conftest.o'
+  { { eval echo "\"\$as_me\":${as_lineno-$LINENO}: \"$ac_try\""; } >&5
+  (eval $ac_try) 2>&5
+  ac_status=$?
+  $as_echo "$as_me:${as_lineno-$LINENO}: \$? = $ac_status" >&5
+  test $ac_status = 0; }; }
+      then
+        gcc_cv_as_mattr_code_object_v3=yes
+      fi
+      rm -f conftest.s conftest.o conftest
+    fi
+    if test x$gcc_cv_as_mattr_code_object_v3 = xyes; then
+
+$as_echo "#define HAVE_AS_MATTR_CODE_OBJECT_V3 1" >>confdefs.h
+
+    fi
+    { $as_echo "$as_me:${as_lineno-$LINENO}: result: $gcc_cv_as_mattr_code_object_v3" >&5
+$as_echo "$gcc_cv_as_mattr_code_object_v3" >&6; }
+    ;;
+esac
+
 # ??? Not all targets support dwarf2 debug_line, even within a version
 # of gas.  Moreover, we need to emit a valid instruction to trigger any
 # info to the output file.  So, as supported targets are added to gas 2.11,
@@ -29800,6 +29858,23 @@  $as_echo "#define TARGET_LIBC_PROVIDES_HWCAP_IN_TCB 1" >>confdefs.h
 
 fi
 
+# Check if the target LIBC handles PT_GNU_STACK.
+gcc_cv_libc_gnustack=unknown
+case "$target" in
+  mips*-*-linux*)
+
+if test $glibc_version_major -gt 2 \
+  || ( test $glibc_version_major -eq 2 && test $glibc_version_minor -ge 31 ); then :
+  gcc_cv_libc_gnustack=yes
+fi
+    ;;
+esac
+if test x$gcc_cv_libc_gnustack = xyes; then
+
+$as_echo "#define TARGET_LIBC_GNUSTACK 1" >>confdefs.h
+
+fi
+
 { $as_echo "$as_me:${as_lineno-$LINENO}: checking dl_iterate_phdr in target C library" >&5
 $as_echo_n "checking dl_iterate_phdr in target C library... " >&6; }
 gcc_cv_target_dl_iterate_phdr=unknown
diff --git a/gcc/configure.ac b/gcc/configure.ac
index a7521ee9943..22357ff1aac 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -5084,6 +5084,39 @@  case "$target" in
     ;;
 esac
 
+# AMGCGN: Check whether the assembler supports -mattr=-code-object-v3
+case "$target" in
+  amdgcn-* | gcn-*)
+    AC_MSG_CHECKING(assembler support for -mattr=-code-object-v3)
+    gcc_cv_as_mattr_code_object_v3=no
+    if test x$gcc_cv_as != x; then
+      cat > conftest.s <<EOF
+        .text
+        .hsa_code_object_version 2,0
+        .hsa_code_object_isa
+        .section        .AMDGPU.config
+        .text
+        .section        .text
+        .globl  a
+        .data
+        .align  16
+        .type   a, @object
+        .size   a, 4
+EOF
+      if AC_TRY_COMMAND([$gcc_cv_as -triple=amdgcn--amdhsa -filetype=obj -mattr=-code-object-v3 conftest.s -o conftest.o])
+      then
+        gcc_cv_as_mattr_code_object_v3=yes
+      fi
+      rm -f conftest.s conftest.o conftest
+    fi
+    if test x$gcc_cv_as_mattr_code_object_v3 = xyes; then
+      AC_DEFINE(HAVE_AS_MATTR_CODE_OBJECT_V3, 1,
+		[Define if your assembler supports -mattr=-code-object-v3.])
+    fi
+    AC_MSG_RESULT($gcc_cv_as_mattr_code_object_v3)
+    ;;
+esac
+
 # ??? Not all targets support dwarf2 debug_line, even within a version
 # of gas.  Moreover, we need to emit a valid instruction to trigger any
 # info to the output file.  So, as supported targets are added to gas 2.11,