Grok 10.0.5
targets.h
Go to the documentation of this file.
1// Copyright 2020 Google LLC
2// SPDX-License-Identifier: Apache-2.0
3//
4// Licensed under the Apache License, Version 2.0 (the "License");
5// you may not use this file except in compliance with the License.
6// You may obtain a copy of the License at
7//
8// http://www.apache.org/licenses/LICENSE-2.0
9//
10// Unless required by applicable law or agreed to in writing, software
11// distributed under the License is distributed on an "AS IS" BASIS,
12// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13// See the License for the specific language governing permissions and
14// limitations under the License.
15
16#ifndef HIGHWAY_HWY_TARGETS_H_
17#define HIGHWAY_HWY_TARGETS_H_
18
19// Allows opting out of C++ standard library usage, which is not available in
20// some Compiler Explorer environments.
21#ifndef HWY_NO_LIBCXX
22#include <vector>
23#endif
24
25// For SIMD module implementations and their callers. Defines which targets to
26// generate and call.
27
28#include "hwy/base.h"
29#include "hwy/detect_targets.h"
30#include "hwy/highway_export.h"
31
32#if !HWY_ARCH_RVV && !defined(HWY_NO_LIBCXX)
33#include <atomic>
34#endif
35
36namespace hwy {
37
38// Returns bitfield of enabled targets that are supported on this CPU; there is
39// always at least one such target, hence the return value is never 0. The
40// targets returned may change after calling DisableTargets. This function is
41// always defined, but the HWY_SUPPORTED_TARGETS wrapper may allow eliding
42// calls to it if there is only a single target enabled.
44
45// Evaluates to a function call, or literal if there is a single target.
46#if (HWY_TARGETS & (HWY_TARGETS - 1)) == 0
47#define HWY_SUPPORTED_TARGETS HWY_TARGETS
48#else
49#define HWY_SUPPORTED_TARGETS hwy::SupportedTargets()
50#endif
51
52// Subsequent SupportedTargets will not return targets whose bit(s) are set in
53// `disabled_targets`. Exception: if SupportedTargets would return 0, it will
54// instead return HWY_STATIC_TARGET (there must always be one target to call).
55//
56// This function is useful for disabling targets known to be buggy, or if the
57// best available target is undesirable (perhaps due to throttling or memory
58// bandwidth limitations). Use SetSupportedTargetsForTest instead of this
59// function for iteratively enabling specific targets for testing.
60HWY_DLLEXPORT void DisableTargets(int64_t disabled_targets);
61
62// Subsequent SupportedTargets will return the given set of targets, except
63// those disabled via DisableTargets. Call with a mask of 0 to disable the mock
64// and return to the normal SupportedTargets behavior. Used to run tests for
65// all targets.
67
68#ifndef HWY_NO_LIBCXX
69
70// Return the list of targets in HWY_TARGETS supported by the CPU as a list of
71// individual HWY_* target macros such as HWY_SCALAR or HWY_NEON. This list
72// is affected by the current SetSupportedTargetsForTest() mock if any.
74 std::vector<int64_t> ret;
75 for (int64_t targets = SupportedTargets() & HWY_TARGETS; targets != 0;
76 targets = targets & (targets - 1)) {
77 int64_t current_target = targets & ~(targets - 1);
78 ret.push_back(current_target);
79 }
80 return ret;
81}
82
83#endif // HWY_NO_LIBCXX
84
85static inline HWY_MAYBE_UNUSED const char* TargetName(int64_t target) {
86 switch (target) {
87#if HWY_ARCH_X86
88 case HWY_SSSE3:
89 return "SSSE3";
90 case HWY_SSE4:
91 return "SSE4";
92 case HWY_AVX2:
93 return "AVX2";
94 case HWY_AVX3:
95 return "AVX3";
96 case HWY_AVX3_DL:
97 return "AVX3_DL";
98#endif
99
100#if HWY_ARCH_ARM
101 case HWY_SVE2_128:
102 return "SVE2_128";
103 case HWY_SVE_256:
104 return "SVE_256";
105 case HWY_SVE2:
106 return "SVE2";
107 case HWY_SVE:
108 return "SVE";
109 case HWY_NEON:
110 return "NEON";
111#endif
112
113#if HWY_ARCH_PPC
114 case HWY_PPC8:
115 return "PPC8";
116#endif
117
118#if HWY_ARCH_WASM
119 case HWY_WASM:
120 return "WASM";
121 case HWY_WASM_EMU256:
122 return "WASM_EMU256";
123#endif
124
125#if HWY_ARCH_RVV
126 case HWY_RVV:
127 return "RVV";
128#endif
129
130 case HWY_EMU128:
131 return "EMU128";
132 case HWY_SCALAR:
133 return "SCALAR";
134
135 default:
136 return "Unknown"; // must satisfy gtest IsValidParamName()
137 }
138}
139
140// The maximum number of dynamic targets on any architecture is defined by
141// HWY_MAX_DYNAMIC_TARGETS and depends on the arch.
142
143// For the ChosenTarget mask and index we use a different bit arrangement than
144// in the HWY_TARGETS mask. Only the targets involved in the current
145// architecture are used in this mask, and therefore only the least significant
146// (HWY_MAX_DYNAMIC_TARGETS + 2) bits of the int64_t mask are used. The least
147// significant bit is set when the mask is not initialized, the next
148// HWY_MAX_DYNAMIC_TARGETS more significant bits are a range of bits from the
149// HWY_TARGETS or SupportedTargets() mask for the given architecture shifted to
150// that position and the next more significant bit is used for HWY_SCALAR (if
151// HWY_COMPILE_ONLY_SCALAR is defined) or HWY_EMU128. Because of this we need to
152// define equivalent values for HWY_TARGETS in this representation.
153// This mask representation allows to use ctz() on this mask and obtain a small
154// number that's used as an index of the table for dynamic dispatch. In this
155// way the first entry is used when the mask is uninitialized, the following
156// HWY_MAX_DYNAMIC_TARGETS are for dynamic dispatch and the last one is for
157// scalar.
158
159// The HWY_SCALAR/HWY_EMU128 bit in the ChosenTarget mask format.
160#define HWY_CHOSEN_TARGET_MASK_SCALAR (1LL << (HWY_MAX_DYNAMIC_TARGETS + 1))
161
162// Converts from a HWY_TARGETS mask to a ChosenTarget mask format for the
163// current architecture.
164#define HWY_CHOSEN_TARGET_SHIFT(X) \
165 ((((X) >> (HWY_HIGHEST_TARGET_BIT + 1 - HWY_MAX_DYNAMIC_TARGETS)) & \
166 ((1LL << HWY_MAX_DYNAMIC_TARGETS) - 1)) \
167 << 1)
168
169// The HWY_TARGETS mask in the ChosenTarget mask format.
170#define HWY_CHOSEN_TARGET_MASK_TARGETS \
171 (HWY_CHOSEN_TARGET_SHIFT(HWY_TARGETS) | HWY_CHOSEN_TARGET_MASK_SCALAR | 1LL)
172
173#if HWY_ARCH_X86
174// Maximum number of dynamic targets, changing this value is an ABI incompatible
175// change
176#define HWY_MAX_DYNAMIC_TARGETS 15
177#define HWY_HIGHEST_TARGET_BIT HWY_HIGHEST_TARGET_BIT_X86
178// These must match the order in which the HWY_TARGETS are defined
179// starting by the least significant (HWY_HIGHEST_TARGET_BIT + 1 -
180// HWY_MAX_DYNAMIC_TARGETS) bit. This list must contain exactly
181// HWY_MAX_DYNAMIC_TARGETS elements and does not include SCALAR. The first entry
182// corresponds to the best target. Don't include a "," at the end of the list.
183#define HWY_CHOOSE_TARGET_LIST(func_name) \
184 nullptr, /* reserved */ \
185 nullptr, /* reserved */ \
186 nullptr, /* reserved */ \
187 nullptr, /* reserved */ \
188 nullptr, /* reserved */ \
189 nullptr, /* reserved */ \
190 nullptr, /* reserved */ \
191 HWY_CHOOSE_AVX3_DL(func_name), /* AVX3_DL */ \
192 HWY_CHOOSE_AVX3(func_name), /* AVX3 */ \
193 HWY_CHOOSE_AVX2(func_name), /* AVX2 */ \
194 nullptr, /* AVX */ \
195 HWY_CHOOSE_SSE4(func_name), /* SSE4 */ \
196 HWY_CHOOSE_SSSE3(func_name), /* SSSE3 */ \
197 nullptr , /* reserved - SSE3? */ \
198 nullptr /* reserved - SSE2? */
199
200#elif HWY_ARCH_ARM
201// See HWY_ARCH_X86 above for details.
202#define HWY_MAX_DYNAMIC_TARGETS 15
203#define HWY_HIGHEST_TARGET_BIT HWY_HIGHEST_TARGET_BIT_ARM
204#define HWY_CHOOSE_TARGET_LIST(func_name) \
205 nullptr, /* reserved */ \
206 nullptr, /* reserved */ \
207 nullptr, /* reserved */ \
208 nullptr, /* reserved */ \
209 nullptr, /* reserved */ \
210 nullptr, /* reserved */ \
211 nullptr, /* reserved */ \
212 nullptr, /* reserved */ \
213 nullptr, /* reserved */ \
214 HWY_CHOOSE_SVE2_128(func_name), /* SVE2 128-bit */ \
215 HWY_CHOOSE_SVE_256(func_name), /* SVE 256-bit */ \
216 HWY_CHOOSE_SVE2(func_name), /* SVE2 */ \
217 HWY_CHOOSE_SVE(func_name), /* SVE */ \
218 HWY_CHOOSE_NEON(func_name), /* NEON */ \
219 nullptr /* reserved - Helium? */
220
221#elif HWY_ARCH_RVV
222// See HWY_ARCH_X86 above for details.
223#define HWY_MAX_DYNAMIC_TARGETS 9
224#define HWY_HIGHEST_TARGET_BIT HWY_HIGHEST_TARGET_BIT_RVV
225#define HWY_CHOOSE_TARGET_LIST(func_name) \
226 nullptr, /* reserved */ \
227 nullptr, /* reserved */ \
228 nullptr, /* reserved */ \
229 nullptr, /* reserved */ \
230 nullptr, /* reserved */ \
231 nullptr, /* reserved */ \
232 nullptr, /* reserved */ \
233 HWY_CHOOSE_RVV(func_name), /* RVV */ \
234 nullptr /* reserved */
235
236#elif HWY_ARCH_PPC
237// See HWY_ARCH_X86 above for details.
238#define HWY_MAX_DYNAMIC_TARGETS 9
239#define HWY_HIGHEST_TARGET_BIT HWY_HIGHEST_TARGET_BIT_PPC
240#define HWY_CHOOSE_TARGET_LIST(func_name) \
241 nullptr, /* reserved */ \
242 nullptr, /* reserved */ \
243 nullptr, /* reserved */ \
244 nullptr, /* reserved */ \
245 nullptr, /* reserved */ \
246 nullptr, /* reserved */ \
247 HWY_CHOOSE_PPC8(func_name), /* PPC8 */ \
248 nullptr, /* reserved (VSX or AltiVec) */ \
249 nullptr /* reserved (VSX or AltiVec) */
250
251#elif HWY_ARCH_WASM
252// See HWY_ARCH_X86 above for details.
253#define HWY_MAX_DYNAMIC_TARGETS 9
254#define HWY_HIGHEST_TARGET_BIT HWY_HIGHEST_TARGET_BIT_WASM
255#define HWY_CHOOSE_TARGET_LIST(func_name) \
256 nullptr, /* reserved */ \
257 nullptr, /* reserved */ \
258 nullptr, /* reserved */ \
259 nullptr, /* reserved */ \
260 nullptr, /* reserved */ \
261 nullptr, /* reserved */ \
262 HWY_CHOOSE_WASM_EMU256(func_name), /* WASM_EMU256 */ \
263 HWY_CHOOSE_WASM(func_name), /* WASM */ \
264 nullptr /* reserved */
265
266#else
267// Unknown architecture, will use HWY_SCALAR without dynamic dispatch, though
268// still creating single-entry tables in HWY_EXPORT to ensure portability.
269#define HWY_MAX_DYNAMIC_TARGETS 1
270#define HWY_HIGHEST_TARGET_BIT HWY_HIGHEST_TARGET_BIT_SCALAR
271#endif
272
273// Bitfield of supported and enabled targets. The format differs from that of
274// HWY_TARGETS; the lowest bit governs the first function pointer (which is
275// special in that it calls FunctionCache, then Update, then dispatches to the
276// actual implementation) in the tables created by HWY_EXPORT. Monostate (see
277// GetChosenTarget), thread-safe except on RVV.
279 public:
280 // Reset bits according to `targets` (typically the return value of
281 // SupportedTargets()). Postcondition: IsInitialized() == true.
282 void Update(int64_t targets) {
283 // These are `targets` shifted downwards, see above. Also include SCALAR
284 // (corresponds to the last entry in the function table) as fallback.
286 }
287
288 // Reset to the uninitialized state, so that FunctionCache will call Update
289 // during the next HWY_DYNAMIC_DISPATCH, and IsInitialized returns false.
290 void DeInit() { StoreMask(1); }
291
292 // Whether Update was called. This indicates whether any HWY_DYNAMIC_DISPATCH
293 // function was called, which we check in tests.
294 bool IsInitialized() const { return LoadMask() != 1; }
295
296 // Return the index in the dynamic dispatch table to be used by the current
297 // CPU. Note that this method must be in the header file so it uses the value
298 // of HWY_CHOSEN_TARGET_MASK_TARGETS defined in the translation unit that
299 // calls it, which may be different from others. This means we only enable
300 // those targets that were actually compiled in this module.
301 size_t HWY_INLINE GetIndex() const {
303 static_cast<uint64_t>(LoadMask() & HWY_CHOSEN_TARGET_MASK_TARGETS));
304 }
305
306 private:
307 // TODO(janwas): remove RVV once <atomic> is available
308#if HWY_ARCH_RVV || defined(HWY_NO_LIBCXX)
309 int64_t LoadMask() const { return mask_; }
310 void StoreMask(int64_t mask) { mask_ = mask; }
311
312 int64_t mask_{1}; // Initialized to 1 so GetIndex() returns 0.
313#else
314 int64_t LoadMask() const { return mask_.load(); }
315 void StoreMask(int64_t mask) { mask_.store(mask); }
316
317 std::atomic<int64_t> mask_{1}; // Initialized to 1 so GetIndex() returns 0.
318#endif // HWY_ARCH_RVV
319};
320
321// For internal use (e.g. by FunctionCache and DisableTargets).
323
324} // namespace hwy
325
326#endif // HIGHWAY_HWY_TARGETS_H_
#define HWY_INLINE
Definition base.h:70
#define HWY_MAYBE_UNUSED
Definition base.h:82
#define HWY_WASM_EMU256
Definition detect_targets.h:106
#define HWY_AVX3_DL
Definition detect_targets.h:65
#define HWY_NEON
Definition detect_targets.h:84
#define HWY_EMU128
Definition detect_targets.h:113
#define HWY_PPC8
Definition detect_targets.h:100
#define HWY_SVE2
Definition detect_targets.h:82
#define HWY_AVX3
Definition detect_targets.h:66
#define HWY_AVX2
Definition detect_targets.h:67
#define HWY_SCALAR
Definition detect_targets.h:115
#define HWY_SVE_256
Definition detect_targets.h:81
#define HWY_SVE2_128
Definition detect_targets.h:80
#define HWY_WASM
Definition detect_targets.h:107
#define HWY_SVE
Definition detect_targets.h:83
#define HWY_RVV
Definition detect_targets.h:90
#define HWY_TARGETS
Definition detect_targets.h:467
#define HWY_SSE4
Definition detect_targets.h:69
#define HWY_SSSE3
Definition detect_targets.h:70
#define HWY_DLLEXPORT
Definition highway_export.h:13
Definition aligned_allocator.h:27
HWY_API size_t Num0BitsBelowLS1Bit_Nonzero64(const uint64_t x)
Definition base.h:806
static HWY_MAYBE_UNUSED const char * TargetName(int64_t target)
Definition targets.h:85
HWY_DLLEXPORT ChosenTarget & GetChosenTarget()
HWY_DLLEXPORT void DisableTargets(int64_t disabled_targets)
HWY_INLINE std::vector< int64_t > SupportedAndGeneratedTargets()
Definition targets.h:73
HWY_DLLEXPORT int64_t SupportedTargets()
HWY_DLLEXPORT void SetSupportedTargetsForTest(int64_t targets)
Definition targets.h:278
bool IsInitialized() const
Definition targets.h:294
void StoreMask(int64_t mask)
Definition targets.h:315
size_t HWY_INLINE GetIndex() const
Definition targets.h:301
void DeInit()
Definition targets.h:290
void Update(int64_t targets)
Definition targets.h:282
int64_t LoadMask() const
Definition targets.h:314
std::atomic< int64_t > mask_
Definition targets.h:317
#define HWY_CHOSEN_TARGET_MASK_TARGETS
Definition targets.h:170
#define HWY_CHOSEN_TARGET_SHIFT(X)
Definition targets.h:164
#define HWY_CHOSEN_TARGET_MASK_SCALAR
Definition targets.h:160