From 8343b7d0ed96cf5d45c12b36ea34a156229b8745 Mon Sep 17 00:00:00 2001
From: Thomas Schwinge <thomas@codesourcery.com>
Date: Sat, 11 Jun 2022 19:37:10 +0200
Subject: [PATCH 3/3] nvptx: Allow '--with-arch' to override the default
'-misa'
---
gcc/config.gcc | 5 ++
gcc/config/nvptx/gen-multilib-matches.sh | 60 ++++++++++++++++++++++++
gcc/config/nvptx/t-nvptx | 21 +++++++--
gcc/doc/install.texi | 9 ++++
gcc/doc/invoke.texi | 4 +-
5 files changed, 93 insertions(+), 6 deletions(-)
create mode 100755 gcc/config/nvptx/gen-multilib-matches.sh
@@ -5442,10 +5442,15 @@ 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.
;;
+ 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"
+ ;;
* )
echo "Unknown arch used in --with-arch=$with_arch" 1>&2
exit 1
new file mode 100755
@@ -0,0 +1,60 @@
+#!/bin/sh
+
+# Print nvptx 'MULTILIB_MATCHES'
+
+# Copyright (C) 2022 Free Software Foundation, Inc.
+#
+# This file is part of GCC.
+#
+# GCC is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3, or (at your option)
+# any later version.
+#
+# GCC is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with GCC; see the file COPYING3. If not see
+# <http://www.gnu.org/licenses/>.
+
+set -e
+
+nvptx_sm_def="$1/nvptx-sm.def"
+multilib_options_isa_default=$2
+multilib_options_isa_list=$3
+
+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.
+
+# The "lowest" variant has to be built.
+sm_next_lower=INVALID
+
+for sm in $sms; do
+ if [ x"sm_$sm" = x"$multilib_options_isa_default" ]; then
+ sm_map=.
+ elif expr " $multilib_options_isa_list " : ".* sm_$sm " > /dev/null; then
+ sm_map=
+ else
+ sm_map=$sm_next_lower
+ fi
+
+ if [ x"$sm_map" = x ]; then
+ sm_next_lower=$sm
+ else
+ # Output format as required for 'MULTILIB_MATCHES'.
+ if [ x"$sm_map" = x. ]; then
+ echo ".=misa?sm_$sm"
+ else
+ echo "misa?sm_$sm_map=misa?sm_$sm"
+ fi
+
+ sm_next_lower=$sm_map
+ fi
+done
@@ -42,11 +42,22 @@ MULTILIB_OPTIONS += mgomp
multilib_options_isa_list := $(TM_MULTILIB_CONFIG)
multilib_options_isa_default := $(word 1,$(multilib_options_isa_list))
-# Add the default '-misa' as a multilib option:
-MULTILIB_OPTIONS += misa=$(multilib_options_isa_default)
-# ..., but don't handle it specially (remap to default):
-MULTILIB_MATCHES += .=misa?$(multilib_options_isa_default)
-# ..., and don't actually build it:
+multilib_options_misa_list := $(addprefix misa=,$(multilib_options_isa_list))
+# Add the requested '-misa' variants as a multilib option ('misa=VAR1/misa=VAR2/misa=VAR3' etc.):
+empty :=
+space := $(empty) $(empty)
+MULTILIB_OPTIONS += $(subst $(space),/,$(multilib_options_misa_list))
+# ..., and remap '-misa' variants as appropriate:
+multilib_matches := $(shell $(srcdir)/config/nvptx/gen-multilib-matches.sh $(srcdir)/config/nvptx $(multilib_options_isa_default) "$(multilib_options_isa_list)")
+MULTILIB_MATCHES += $(multilib_matches)
+# ..., and don't actually build what's the default '-misa':
MULTILIB_EXCEPTIONS += *misa=$(multilib_options_isa_default)*
MULTILIB_OPTIONS += mptx=3.1
+# Filter out invalid '-misa'/'-mptx=3.1' combinations; per 'nvptx-sm.def',
+# 'nvptx.opt:ptx_version', 'nvptx.cc:first_ptx_version_supporting_sm'
+# (that is, '-mptx=3.1' only for sm_30, sm_35 variants):
+MULTILIB_EXCEPTIONS += $(foreach misa,$(filter-out %=sm_30 %=sm_35,$(multilib_options_misa_list)),*$(misa)/mptx=3.1)
+# ..., and special care has to be taken if '-mptx=3.1' is invalid for the
+# default variant:
+MULTILIB_EXCEPTIONS += $(if $(filter-out sm_30 sm_35,$(multilib_options_isa_default)),mgomp/mptx=3.1 mptx=3.1)
@@ -4617,6 +4617,15 @@ the GCC sources.
Use the @option{--disable-sjlj-exceptions} and
@option{--enable-newlib-io-long-long} options when configuring.
+The @option{--with-arch} option may be specified to override the
+default value for the @option{-march} option, and to also build
+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}.
+
@html
<hr />
@end html
@@ -27549,7 +27549,9 @@ supported.
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}. The default target architecture is sm_30.
+@samp{sm_80}.
+The default depends on how the compiler has been configured, see
+@option{--with-arch}.
This option sets the value of the preprocessor macro
@code{__PTX_SM__}; for instance, for @samp{sm_35}, it has the value
--
2.25.1