https://gcc.gnu.org/g:ed9bc1f5d9f7bce0c8836db7029434f174a5a0d3

commit ed9bc1f5d9f7bce0c8836db7029434f174a5a0d3
Author: Thomas Schwinge <tschwi...@baylibre.com>
Date:   Fri Sep 27 17:44:16 2024 +0200

    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.
    
    (cherry picked from commit 86b3a7532d56f74fcd1c362f2da7f95e8cc4e4a6)

Diff:
---
 gcc/ChangeLog.omp                           |  11 +++
 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 ++-
 8 files changed, 253 insertions(+), 35 deletions(-)

diff --git a/gcc/ChangeLog.omp b/gcc/ChangeLog.omp
index 81cffa1a2758..86d14fa31087 100644
--- a/gcc/ChangeLog.omp
+++ b/gcc/ChangeLog.omp
@@ -3,6 +3,17 @@
        Backported from trunk:
        2024-12-06  Thomas Schwinge  <tschwi...@baylibre.com>
 
+       * 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.
+
+       Backported from trunk:
+       2024-12-06  Thomas Schwinge  <tschwi...@baylibre.com>
+
        * config/nvptx/gen-multilib-matches.sh: Support '--selftest'.
        * config/nvptx/t-nvptx (t-nvptx-gen-multilib-matches:): Invoke it.
        * config/nvptx/gen-multilib-matches-tests: New.
diff --git a/gcc/config.gcc b/gcc/config.gcc
index a3566f5c77da..e9f776e5cc79 100644
--- a/gcc/config.gcc
+++ b/gcc/config.gcc
@@ -5561,21 +5561,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"
                                ;;
-                       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"
+                       * )
+                               
nvptx_multilibs_expanded="$nvptx_multilibs_expanded $nvptx_multilib"
+                               ;;
+                       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 "* )
+                               :
                                ;;
                        * )
-                               echo "Unknown arch used in 
--with-arch=$with_arch" 1>&2
+                               
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
                                ;;
-               esac
+                       * )
+                               echo "Unknown arch used in 
--with-multilib-list: $nvptx_multilib" 1>&2
+                               exit 1
+                               ;;
+                       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 abc8bfdc2443..892e9eda963e 100755
--- a/gcc/configure
+++ b/gcc/configure
@@ -1878,8 +1878,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 1d2cefa3f6a3..79e58f26eda4 100644
--- a/gcc/configure.ac
+++ b/gcc/configure.ac
@@ -1206,7 +1206,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 60c76f30d737..4b755deffa0c 100644
--- a/gcc/doc/install.texi
+++ b/gcc/doc/install.texi
@@ -1250,13 +1250,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*-*-*
@@ -1382,6 +1382,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
@@ -4716,8 +4738,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 5a23f644da5d..74aeb5481d67 100644
--- a/gcc/doc/invoke.texi
+++ b/gcc/doc/invoke.texi
@@ -30024,10 +30024,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}.

Reply via email to