diff mbox series

nvptx: Support '--with-multilib-list' (was: Raise nvptx code generation to default PTX ISA 7.3, sm_52, therefore CUDA 11.3 (released 2021-04))

Message ID 87a5d913b9.fsf@euler.schwinge.ddns.net
State New
Headers show
Series nvptx: Support '--with-multilib-list' (was: Raise nvptx code generation to default PTX ISA 7.3, sm_52, therefore CUDA 11.3 (released 2021-04)) | expand

Commit Message

Thomas Schwinge Dec. 6, 2024, 11:03 a.m. UTC
Hi!

On 2024-09-24T18:09:48+0200, I wrote:
> [...] build sm_30 multilib variants (either
> (3a) by default or (3b) upon 'configure'-time request via an additional
> option: '--with-multilib-list=default,sm_30' or similar), and the user
> builds with '-foffload-options=nvptx-none=-march=sm_30': [...]
>
> [...] I'll be happy to
> implement (3b) if people think that's still helpful.
>
> In fact, (3b) can then generally support 'configure'-time selection of
> further multilib variants to be built (for example,
> '--with-multilib-list=default,sm_30,sm_89') -- but not use them as
> default '-march=[...]', in contrast to what '--with-arch=sm_30' or
> '--with-arch=sm_89' does, for example.  I'll look into that.

Pushed to trunk branch commit 86b3a7532d56f74fcd1c362f2da7f95e8cc4e4a6
"nvptx: Support '--with-multilib-list'", see attached.


Grüße
 Thomas
diff mbox series

Patch

From 86b3a7532d56f74fcd1c362f2da7f95e8cc4e4a6 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <tschwinge@baylibre.com>
Date: Fri, 27 Sep 2024 17:44:16 +0200
Subject: [PATCH] nvptx: Support '--with-multilib-list'

No change in behavior unless specifying it.

	gcc/
	* config.gcc: nvptx: Support '--with-multilib-list'.
	* config/nvptx/gen-multilib-matches.sh: Adjust.
	* configure.ac: Likewise.
	* configure: Regenerate.
	* doc/install.texi: Update.
	* doc/invoke.texi: Align.
	* config/nvptx/gen-multilib-matches-tests: Extend.
---
 gcc/config.gcc                              |  69 +++++++++--
 gcc/config/nvptx/gen-multilib-matches-tests | 121 +++++++++++++++++++-
 gcc/config/nvptx/gen-multilib-matches.sh    |  29 ++++-
 gcc/configure                               |   4 +-
 gcc/configure.ac                            |   2 +-
 gcc/doc/install.texi                        |  42 ++++++-
 gcc/doc/invoke.texi                         |  10 +-
 7 files changed, 242 insertions(+), 35 deletions(-)

diff --git a/gcc/config.gcc b/gcc/config.gcc
index f4ae14c6db2a..6381a5793194 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -5595,21 +5595,70 @@  case "${target}" in
 		;;
 	nvptx-*)
 		supported_defaults=arch
-		TM_MULTILIB_CONFIG=$with_arch
-		#TODO 'sm_[...]' list per 'nvptx-sm.def'.
-		case $with_arch in
-			sm_30 )
-				# OK; default.
+
+		nvptx_multilibs_default=sm_30
+
+		case "x${with_multilib_list}" in
+		x | xno)
+			nvptx_multilibs=
+			;;
+		xdefault | xyes)
+			nvptx_multilibs=default
+			;;
+		*)
+			nvptx_multilibs=$with_multilib_list
+			;;
+		esac
+		nvptx_multilibs=`echo $nvptx_multilibs | sed -e 's/,/ /g'`
+		# Expand 'default'.
+		nvptx_multilibs_expanded=
+		for nvptx_multilib in $nvptx_multilibs; do
+			case $nvptx_multilib in
+			default )
+				nvptx_multilibs_expanded="$nvptx_multilibs_expanded $nvptx_multilibs_default"
+				;;
+			* )
+				nvptx_multilibs_expanded="$nvptx_multilibs_expanded $nvptx_multilib"
 				;;
-			sm_35 | sm_53 | sm_70 | sm_75 | sm_80 )
-				# OK, but we'd like 'sm_30', too.
-				TM_MULTILIB_CONFIG="$TM_MULTILIB_CONFIG sm_30"
+			esac
+		done
+		# The '--with-arch=[...]' one comes first.
+		nvptx_multilibs=$with_arch$nvptx_multilibs_expanded
+		# Filter out any duplicates.
+		nvptx_multilibs_filtered=
+		for nvptx_multilib in $nvptx_multilibs; do
+			case " $nvptx_multilibs_filtered " in
+			*" $nvptx_multilib "* )
+				:
+				;;
+			* )
+				nvptx_multilibs_filtered="$nvptx_multilibs_filtered $nvptx_multilib"
+				;;
+			esac
+		done
+		nvptx_multilibs=$nvptx_multilibs_filtered
+		# Verify, and build 'TM_MULTILIB_CONFIG'.
+		TM_MULTILIB_CONFIG=
+		for nvptx_multilib in $nvptx_multilibs; do
+			case $nvptx_multilib in
+			#TODO 'sm_[...]' list per 'nvptx-sm.def'.
+			sm_30 | sm_35 \
+			| sm_53 \
+			| sm_70 | sm_75 \
+			| sm_80 )
+				TM_MULTILIB_CONFIG="$TM_MULTILIB_CONFIG $nvptx_multilib"
+				;;
+			$with_arch )
+				echo "Unknown arch used in --with-arch=$nvptx_multilib" 1>&2
+				exit 1
 				;;
 			* )
-				echo "Unknown arch used in --with-arch=$with_arch" 1>&2
+				echo "Unknown arch used in --with-multilib-list: $nvptx_multilib" 1>&2
 				exit 1
 				;;
-		esac
+			esac
+		done
+		TM_MULTILIB_CONFIG=`echo $TM_MULTILIB_CONFIG | sed 's/^ //'`
 		;;
 
 	powerpc*-*-* | rs6000-*-*)
diff --git a/gcc/config/nvptx/gen-multilib-matches-tests b/gcc/config/nvptx/gen-multilib-matches-tests
index c2775f268354..b93369149465 100644
--- a/gcc/config/nvptx/gen-multilib-matches-tests
+++ b/gcc/config/nvptx/gen-multilib-matches-tests
@@ -10,7 +10,7 @@ 
 # 'CMMC': compute "multilib matches" per the current settings, and compare to the expected.
 
 
-BEGIN '--with-arch=sm_30'
+BEGIN '--with-arch=sm_30', '--with-multilib-list=sm_30'
 SMOID sm_30
 SMOIL sm_30
 AEMM .=misa?sm_30
@@ -21,8 +21,35 @@  AEMM .=misa?sm_75
 AEMM .=misa?sm_80
 CMMC
 
+BEGIN '--with-arch=sm_30', '--with-multilib-list=sm_30,sm_80'
+SMOID sm_30
+SMOIL sm_30 sm_80
+AEMM .=misa?sm_30
+AEMM .=misa?sm_35
+AEMM .=misa?sm_53
+AEMM .=misa?sm_70
+AEMM .=misa?sm_75
+CMMC
+
+BEGIN '--with-arch=sm_30', '--with-multilib-list=sm_30,sm_35,sm_53,sm_70,sm_75,sm_80'
+SMOID sm_30
+SMOIL sm_30 sm_35 sm_53 sm_70 sm_75 sm_80
+AEMM .=misa?sm_30
+CMMC
+
+
+BEGIN '--with-arch=sm_35', '--with-multilib-list=sm_35'
+SMOID sm_35
+SMOIL sm_35
+AEMM .=misa?sm_30
+AEMM .=misa?sm_35
+AEMM .=misa?sm_53
+AEMM .=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
 
-BEGIN '--with-arch=sm_35'
+BEGIN '--with-arch=sm_35', '--with-multilib-list=sm_35,sm_30'
 SMOID sm_35
 SMOIL sm_35 sm_30
 AEMM .=misa?sm_35
@@ -33,7 +60,7 @@  AEMM .=misa?sm_80
 CMMC
 
 
-BEGIN '--with-arch=sm_53'
+BEGIN '--with-arch=sm_53', '--with-multilib-list=sm_53,sm_30'
 SMOID sm_53
 SMOIL sm_53 sm_30
 AEMM misa?sm_30=misa?sm_35
@@ -43,8 +70,25 @@  AEMM .=misa?sm_75
 AEMM .=misa?sm_80
 CMMC
 
+BEGIN '--with-arch=sm_53', '--with-multilib-list=sm_53,sm_30,sm_35,sm_70,sm_75,sm_80'
+SMOID sm_53
+SMOIL sm_53 sm_30 sm_35 sm_70 sm_75 sm_80
+AEMM .=misa?sm_53
+CMMC
+
 
-BEGIN '--with-arch=sm_70'
+BEGIN '--with-arch=sm_70', '--with-multilib-list=sm_70'
+SMOID sm_70
+SMOIL sm_70
+AEMM .=misa?sm_30
+AEMM .=misa?sm_35
+AEMM .=misa?sm_53
+AEMM .=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
+
+BEGIN '--with-arch=sm_70', '--with-multilib-list=sm_70,sm_30'
 SMOID sm_70
 SMOIL sm_70 sm_30
 AEMM misa?sm_30=misa?sm_35
@@ -54,8 +98,27 @@  AEMM .=misa?sm_75
 AEMM .=misa?sm_80
 CMMC
 
+BEGIN '--with-arch=sm_70', '--with-multilib-list=sm_70,sm_53'
+SMOID sm_70
+SMOIL sm_70 sm_53
+AEMM misa?sm_53=misa?sm_30
+AEMM misa?sm_53=misa?sm_35
+AEMM .=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
+
+BEGIN '--with-arch=sm_70', '--with-multilib-list=sm_70,sm_53,sm_30'
+SMOID sm_70
+SMOIL sm_70 sm_53 sm_30
+AEMM misa?sm_30=misa?sm_35
+AEMM .=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
 
-BEGIN '--with-arch=sm_75'
+
+BEGIN '--with-arch=sm_75', '--with-multilib-list=sm_75,sm_30'
 SMOID sm_75
 SMOIL sm_75 sm_30
 AEMM misa?sm_30=misa?sm_35
@@ -65,8 +128,38 @@  AEMM .=misa?sm_75
 AEMM .=misa?sm_80
 CMMC
 
+BEGIN '--with-arch=sm_75', '--with-multilib-list=sm_75,sm_53'
+SMOID sm_75
+SMOIL sm_75 sm_53
+AEMM misa?sm_53=misa?sm_30
+AEMM misa?sm_53=misa?sm_35
+AEMM misa?sm_53=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
+
+BEGIN '--with-arch=sm_75', '--with-multilib-list=sm_75,sm_30,sm_53'
+SMOID sm_75
+SMOIL sm_75 sm_30 sm_53
+AEMM misa?sm_30=misa?sm_35
+AEMM misa?sm_53=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
 
-BEGIN '--with-arch=sm_80'
+
+BEGIN '--with-arch=sm_80', '--with-multilib-list=sm_80'
+SMOID sm_80
+SMOIL sm_80
+AEMM .=misa?sm_30
+AEMM .=misa?sm_35
+AEMM .=misa?sm_53
+AEMM .=misa?sm_70
+AEMM .=misa?sm_75
+AEMM .=misa?sm_80
+CMMC
+
+BEGIN '--with-arch=sm_80', '--with-multilib-list=sm_80,sm_30'
 SMOID sm_80
 SMOIL sm_80 sm_30
 AEMM misa?sm_30=misa?sm_35
@@ -75,3 +168,19 @@  AEMM misa?sm_30=misa?sm_70
 AEMM misa?sm_30=misa?sm_75
 AEMM .=misa?sm_80
 CMMC
+
+BEGIN '--with-arch=sm_80', '--with-multilib-list=sm_80,sm_75'
+SMOID sm_80
+SMOIL sm_80 sm_75
+AEMM misa?sm_75=misa?sm_30
+AEMM misa?sm_75=misa?sm_35
+AEMM misa?sm_75=misa?sm_53
+AEMM misa?sm_75=misa?sm_70
+AEMM .=misa?sm_80
+CMMC
+
+BEGIN '--with-arch=sm_80', '--with-multilib-list=sm_80,sm_30,sm_35,sm_53,sm_70,sm_75'
+SMOID sm_80
+SMOIL sm_80 sm_30 sm_35 sm_53 sm_70 sm_75
+AEMM .=misa?sm_80
+CMMC
diff --git a/gcc/config/nvptx/gen-multilib-matches.sh b/gcc/config/nvptx/gen-multilib-matches.sh
index f6f2ed079f68..1c611ba4f820 100755
--- a/gcc/config/nvptx/gen-multilib-matches.sh
+++ b/gcc/config/nvptx/gen-multilib-matches.sh
@@ -34,8 +34,17 @@  sms=$(grep ^NVPTX_SM $nvptx_sm_def | sed 's/.*(//;s/,.*//')
 
 # Every variant in 'sms' has to either be remapped to the default variant
 # ('.', which is always built), or does get built as non-default variant
-# ('misa=sm_SM'; thus not remapped), or has to be remapped to the "next lower"
-# variant that does get built.
+# ('misa=sm_SM'; thus not remapped), or gets remapped to a suitable variant,
+# typically the "next lower" one that does get built.  If no "next lower" one
+# does get built, then remap to the "lowest" one that does get built.  This
+# increases chances that the linked code is compatible with more GPU hardware
+# (backward compatibility).  For example, for GCC built '--with-arch=sm_80',
+# '--with-multilib-list=sm_53', only 'sm_53' and 'sm_80' target libraries get
+# built.  If now requesting a '-march=[...]' where no corresponding or "next
+# lower" variant of the target libraries have been built, GCC's default
+# behavior is to link in the default variant, 'sm_80'.  However, if compiling
+# user code with '-march=sm_35', for example, linking in the 'sm_53' variant is
+# supposedly more useful in terms of compatibility with GPU hardware.
 
 print_multilib_matches() {
     local sms
@@ -52,8 +61,18 @@  print_multilib_matches() {
     local multilib_matches
     multilib_matches=
 
+    # Determine the "lowest" variant that does get built.
     local sm_next_lower
-    unset sm_next_lower
+    sm_next_lower=.
+    local sm
+    for sm in $sms; do
+	if [ x"sm_$sm" = x"$multilib_options_isa_default" ]; then
+	    continue
+	elif expr " $multilib_options_isa_list " : ".* sm_$sm " > /dev/null; then
+	    sm_next_lower=$sm
+	    break
+	fi
+    done
 
     local sm
     for sm in $sms; do
@@ -64,9 +83,7 @@  print_multilib_matches() {
 	elif expr " $multilib_options_isa_list " : ".* sm_$sm " > /dev/null; then
 	    sm_map=
 	else
-	    # Assert here that a "next lower" variant is available; the
-	    # "lowest" variant always does get built.
-	    sm_map=${sm_next_lower?}
+	    sm_map=$sm_next_lower
 	fi
 
 	if [ x"${sm_map?}" = x ]; then
diff --git a/gcc/configure b/gcc/configure
index 2754293d661c..0a55fa75573b 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -1883,8 +1883,8 @@  Optional Packages:
                           Root for documentation URLs
   --with-changes-root-url=URL
                           Root for GCC changes URLs
-  --with-multilib-list    select multilibs (AArch64, ARM, AVR, OR1K, RISC-V,
-                          SH and x86-64 only)
+  --with-multilib-list    select multilibs (AArch64, ARM, AVR, nvptx, OR1K,
+                          RISC-V, SH and x86-64 only)
   --with-multilib-generator
                           Multi-libs configuration string (RISC-V only)
   --with-zstd=PATH        specify prefix directory for installed zstd library.
diff --git a/gcc/configure.ac b/gcc/configure.ac
index ed8d9596668b..1a6f01e25675 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1218,7 +1218,7 @@  if test "x$enable_offload_defaulted" = xyes; then
 fi
 
 AC_ARG_WITH(multilib-list,
-[AS_HELP_STRING([--with-multilib-list], [select multilibs (AArch64, ARM, AVR, OR1K, RISC-V, SH and x86-64 only)])],
+[AS_HELP_STRING([--with-multilib-list], [select multilibs (AArch64, ARM, AVR, nvptx, OR1K, RISC-V, SH and x86-64 only)])],
 :,
 with_multilib_list=default)
 
diff --git a/gcc/doc/install.texi b/gcc/doc/install.texi
index 4107697f10cb..f3a0700ced43 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1275,13 +1275,13 @@  sysv, aix.
 
 @end table
 
-@item --with-multilib-list=@var{list}
+@item @anchor{with-multilib-list}--with-multilib-list=@var{list}
 @itemx --without-multilib-list
 Specify what multilibs to build.  @var{list} is a comma separated list of
 values, possibly consisting of a single value.  Currently only implemented
-for aarch64*-*-*, amdgcn*-*-*, arm*-*-*, loongarch*-*-*, riscv*-*-*, sh*-*-*
-and x86-64-*-linux*.  The accepted values and meaning for each target is given
-below.
+for aarch64*-*-*, amdgcn*-*-*, arm*-*-*, loongarch*-*-*, nvptx-*, riscv*-*-*,
+sh*-*-* and x86-64-*-linux*.
+The accepted values and meaning for each target is given below.
 
 @table @code
 @item aarch64*-*-*
@@ -1407,6 +1407,28 @@  If @var{list} is empty or @code{default}, or if @option{--with-multilib-list}
 is not specified, then only the default variant of the libraries are built,
 where the default ABI is implied by the configured target triplet.
 
+@item nvptx-*
+The target libraries corresponding to the default value of the
+@option{-march} option are always built,
+@ifnothtml
+@pxref{nvptx-x-none,,host/target specific installation notes}.
+@end ifnothtml
+@ifhtml
+see
+@uref{specific.html#nvptx-x-none,,host/target specific installation notes}.
+@end ifhtml
+If @var{list} is empty, @samp{no}, or @option{--without-multilib-list}
+is specified, then no additional multilibs are built.
+Otherwise, @var{list} is a comma separated list specifying which
+multilibs to build.
+List items are either @samp{sm_SM}, or @samp{default}, which is a
+placeholder specifying a default set of multilibs: @samp{sm_30}.
+Any duplicates are filtered out.
+If @option{--with-multilib-list} is not specified, then
+@option{--with-multilib-list=default} is assumed.
+For @samp{sm_30}, @samp{sm_35} target libraries, @option{-mptx-3.1}
+sub-variants are additionally built.
+
 @item riscv*-*-*
 @var{list} is a single ABI name.  The target architecture must be either
 @code{rv32gc} or @code{rv64gc}.  This will build a single multilib for the
@@ -4609,8 +4631,16 @@  corresponding target libraries.
 The default is @option{--with-arch=sm_30}.
 
 For example, if @option{--with-arch=sm_70} is specified,
-@option{-march=sm_30} and @option{-march=sm_70} target libraries are
-built, and code generation defaults to @option{-march=sm_70}.
+code generation defaults to @option{-march=sm_70} and
+corresponding target libraries are built, in addition to those
+mandated by @option{--with-multilib-list}, if any,
+@ifnothtml
+@pxref{with-multilib-list,,@option{--with-multilib-list}}.
+@end ifnothtml
+@ifhtml
+see
+@uref{configure.html#with-multilib-list,,@option{--with-multilib-list}}.
+@end ifhtml
 
 @html
 <hr />
diff --git a/gcc/doc/invoke.texi b/gcc/doc/invoke.texi
index 80eaeb967acb..33a1b6b7983a 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -30044,10 +30044,12 @@  supported.
 
 @opindex march
 @item -march=@var{architecture-string}
-Generate code for the specified PTX ISA target architecture
-(e.g.@: @samp{sm_35}).  Valid architecture strings are @samp{sm_30},
-@samp{sm_35}, @samp{sm_53}, @samp{sm_70}, @samp{sm_75} and
-@samp{sm_80}.
+Generate code for the specified PTX ISA target architecture.
+Valid architecture strings are
+@samp{sm_30}, @samp{sm_35},
+@samp{sm_53},
+@samp{sm_70}, @samp{sm_75},
+and @samp{sm_80}.
 The default depends on how the compiler has been configured, see
 @option{--with-arch}.
 
-- 
2.34.1