Debian Codegraph @

gcc/config/nvptx/gen-multilib-matches.sh

raw
1 #!/bin/sh
2
3 # Print nvptx 'MULTILIB_MATCHES'
4
5 # Copyright (C) 2022-2025 Free Software Foundation, Inc.
6 #
7 # This file is part of GCC.
8 #
9 # GCC is free software; you can redistribute it and/or modify
10 # it under the terms of the GNU General Public License as published by
11 # the Free Software Foundation; either version 3, or (at your option)
12 # any later version.
13 #
14 # GCC is distributed in the hope that it will be useful,
15 # but WITHOUT ANY WARRANTY; without even the implied warranty of
16 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 # GNU General Public License for more details.
18 #
19 # You should have received a copy of the GNU General Public License
20 # along with GCC; see the file COPYING3. If not see
21 # <http://www.gnu.org/licenses/>.
22
23 set -e
24
25
26 nvptx_dir=$(dirname "$0")
27
28
29 nvptx_sm_def="$nvptx_dir/nvptx-sm.def"
30 gen_multilib_matches_tests="$nvptx_dir/gen-multilib-matches-tests"
31
32 sms=$(grep ^NVPTX_SM $nvptx_sm_def | sed 's/.*(//;s/,.*//')
33
34
35 # Every variant in 'sms' has to either be remapped to the default variant
36 # ('.', which is always built), or does get built as non-default variant
37 # ('misa=sm_SM'; thus not remapped), or gets remapped to a suitable variant,
38 # typically the "next lower" one that does get built. If no "next lower" one
39 # does get built, then remap to the "lowest" one that does get built. This
40 # increases chances that the linked code is compatible with more GPU hardware
41 # (backward compatibility). For example, for GCC built '--with-arch=sm_80',
42 # '--with-multilib-list=sm_53', only 'sm_53' and 'sm_80' target libraries get
43 # built. If now requesting a '-march=[...]' where no corresponding or "next
44 # lower" variant of the target libraries have been built, GCC's default
45 # behavior is to link in the default variant, 'sm_80'. However, if compiling
46 # user code with '-march=sm_35', for example, linking in the 'sm_53' variant is
47 # supposedly more useful in terms of compatibility with GPU hardware.
48
49 print_multilib_matches() {
50 local sms
51 sms=${1?}
52 shift
53 local multilib_options_isa_default
54 multilib_options_isa_default=${1?}
55 shift
56 local multilib_options_isa_list
57 multilib_options_isa_list=${1?}
58 shift
59 [ $# = 0 ]
60
61 local multilib_matches
62 multilib_matches=
63
64 # Determine the "lowest" variant that does get built.
65 local sm_next_lower
66 sm_next_lower=.
67 local sm
68 for sm in $sms; do
69 if [ x"sm_$sm" = x"$multilib_options_isa_default" ]; then
70 continue
71 elif expr " $multilib_options_isa_list " : ".* sm_$sm " > /dev/null; then
72 sm_next_lower=$sm
73 break
74 fi
75 done
76
77 local sm
78 for sm in $sms; do
79 local sm_map
80 unset sm_map
81 if [ x"sm_$sm" = x"$multilib_options_isa_default" ]; then
82 sm_map=.
83 elif expr " $multilib_options_isa_list " : ".* sm_$sm " > /dev/null; then
84 sm_map=
85 else
86 sm_map=$sm_next_lower
87 fi
88
89 if [ x"${sm_map?}" = x ]; then
90 sm_next_lower=$sm
91 else
92 local multilib_matches_sm
93 unset multilib_matches_sm
94 # Output format as required for 'MULTILIB_MATCHES'.
95 if [ x"$sm_map" = x. ]; then
96 multilib_matches_sm=".=misa?sm_$sm"
97 else
98 multilib_matches_sm="misa?sm_$sm_map=misa?sm_$sm"
99 fi
100 multilib_matches="$multilib_matches ${multilib_matches_sm?}"
101
102 sm_next_lower=$sm_map
103 fi
104 done
105
106 echo "$multilib_matches"
107 }
108
109
110 selftest() {
111 [ $# = 0 ]
112
113 local sms_default
114 sms_default=$sms
115
116 local name
117 unset name
118 local sms
119 unset sms
120 local multilib_options_isa_default
121 unset multilib_options_isa_default
122 local multilib_options_isa_list
123 unset multilib_options_isa_list
124 local multilib_matches_expected
125 unset multilib_matches_expected
126
127 local line
128 line=0
129 local f1 f2
130 unset f1 f2
131 while read -r f1 f2; do
132 line=$((line + 1))
133 case "$f1 $f2" in
134 ' ' | '#'* )
135 :
136 ;;
137 'BEGIN '* )
138 name=$f2
139 sms=$sms_default
140 unset multilib_options_isa_default
141 unset multilib_options_isa_list
142 unset multilib_matches_expected
143 ;;
144 'SSMS '* )
145 sms=$f2
146 ;;
147 'SMOID '* )
148 multilib_options_isa_default=$f2
149 ;;
150 'SMOIL '* )
151 multilib_options_isa_list=$f2
152 ;;
153 'AEMM '* )
154 multilib_matches_expected="$multilib_matches_expected $f2"
155 ;;
156 'CMMC ' )
157 local multilib_matches
158 multilib_matches=$(print_multilib_matches "${sms?}" "${multilib_options_isa_default?}" "${multilib_options_isa_list?}")
159 if [ "$multilib_matches" = "$multilib_matches_expected" ]; then
160 echo >&2 "$0": selftest PASS "${name?}" at "$gen_multilib_matches_tests:$line"
161 else
162 echo >&2 "$0": selftest FAIL "${name?}" at "$gen_multilib_matches_tests:$line"
163 echo >&2 expected:"$multilib_matches_expected"
164 echo >&2 actual:"$multilib_matches"
165 exit 1
166 fi
167 ;;
168 * )
169 echo >&2 "$0": selftest ERROR at "$gen_multilib_matches_tests:$line"
170 echo >&2 invalid directive: "$f1 $f2"
171 exit 1
172 ;;
173 esac
174 done < "$gen_multilib_matches_tests"
175 }
176
177
178 case "${1?}" in
179 --selftest )
180 shift
181 selftest "$@"
182 :;;
183 * )
184 multilib_matches=$(print_multilib_matches "$sms" "$@")
185 echo "multilib_matches := $multilib_matches"
186 ;;
187 esac