LLVM 24.0.0git
AMDGPUTargetParser.cpp
Go to the documentation of this file.
1//===-- AMDGPUTargetParser - Parser for AMDGPU features ---------*- C++ -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements a target parser to recognise AMDGPU hardware features.
10//
11//===----------------------------------------------------------------------===//
12
16#include "llvm/ADT/Twine.h"
19#include <algorithm>
20#include <array>
21#include <cassert>
22
23using namespace llvm;
24using namespace AMDGPU;
25
26namespace {
27constexpr unsigned NumAMDGPUSubArches =
29
30// A legacy GPU name (e.g. "tahiti") mapped to the GPUKind it aliases.
31struct GPUNameAlias {
32 StringTable::Offset AltName;
33 GPUKind Kind;
34};
35
36// Per-GPU data for the AMDGCN GPUKinds, from the generated table below.
37struct GPUInfo {
38 StringTable::Offset Name;
39 Triple::SubArchType SubArch;
40 AMDGPUFeatureBitset Features;
41 IsaVersion Version;
42 StringTable::Offset FamilyName;
43 uint8_t MaxWavesPerEU;
44 uint32_t MaxHWAddressableLocalMemorySize;
45 uint8_t LDSBankCount;
46 uint8_t BufferResourceNumRecordsWidth;
47};
48
49// Per-GPU data for the R600 GPUKinds.
50struct R600Info {
51 StringTable::Offset Name;
52 R600FeatureBitset Features;
53};
54
55#define GET_AMDGPU_NAME_TABLE
56#define GET_AMDGPU_GPU_TABLE
57#define GET_AMDGPU_GPU_ALIAS_TABLE
58#define GET_AMDGPU_MAJOR_SUBARCH
59#define GET_AMDGPU_SUBARCH_NAME
60#define GET_AMDGPU_FEATURE_NAME_TABLE
61#include "llvm/TargetParser/AMDGPUTargetParserDef.inc"
62
63#define GET_R600_NAME_TABLE
64#define GET_R600_GPU_TABLE
65#define GET_R600_GPU_ALIAS_TABLE
66#define GET_R600_FEATURE_NAME_TABLE
67#include "llvm/TargetParser/R600TargetParserDef.inc"
68
69// The string tables holding GPU-name-derived strings as offsets. R600 and
70// AMDGPU come from separate generated headers, each with its own pool.
71constexpr StringTable AMDGPUNameStrTab = AMDGPUNameTable;
72constexpr StringTable R600NameStrTab = R600NameTable;
73
74// Look up the GPUInfo row for an AMDGCN GPUKind, or nullptr for GK_NONE / a
75// non-AMDGCN (R600) kind.
76const GPUInfo *getAMDGPUInfo(GPUKind AK) {
77 if (AK < AMDGPUFirstGPUKind)
78 return nullptr;
79 unsigned Idx = AK - AMDGPUFirstGPUKind;
80 if (Idx >= std::size(AMDGPUGPUTable))
81 return nullptr;
82 return &AMDGPUGPUTable[Idx];
83}
84
85// Look up the R600Info row for an R600 GPUKind, or nullptr for a non-R600 kind.
86const R600Info *getR600Info(GPUKind AK) {
87 if (AK < R600FirstGPUKind)
88 return nullptr;
89 unsigned Idx = AK - R600FirstGPUKind;
90 if (Idx >= std::size(R600GPUTable))
91 return nullptr;
92 return &R600GPUTable[Idx];
93}
94
95// Scan a name -> GPUKind table (canonical names, then aliases) for \p CPU.
96template <typename InfoT, size_t N, size_t M>
97GPUKind parseArchImpl(StringRef CPU, const InfoT (&Table)[N], GPUKind FirstKind,
98 const StringTable &StrTab,
99 const GPUNameAlias (&Aliases)[M]) {
100 for (unsigned I = 0; I != N; ++I) {
101 if (CPU == StrTab[Table[I].Name])
102 return static_cast<GPUKind>(FirstKind + I);
103 }
104
105 for (const GPUNameAlias &A : Aliases) {
106 if (CPU == StrTab[A.AltName])
107 return A.Kind;
108 }
109
110 return GK_NONE;
111}
112
113// Reverse map: SubArch -> GPUKind, indexed by (SubArch - FirstAMDGPUSubArch).
114// Subarches with no GPU (incl. the NoSubArch pseudo targets) map to GK_NONE.
115constexpr std::array<GPUKind, NumAMDGPUSubArches> AMDGPUSubArchToGPUKind = [] {
116 std::array<GPUKind, NumAMDGPUSubArches> Map{};
117
118 for (unsigned I = 0; I < std::size(AMDGPUGPUTable); ++I) {
119 Triple::SubArchType SubArch = AMDGPUGPUTable[I].SubArch;
120 if (SubArch != Triple::NoSubArch) {
122 static_cast<GPUKind>(AMDGPUFirstGPUKind + I);
123 }
124 }
125 return Map;
126}();
127
128/// SubArch -> major-family, indexed by (SubArch - FirstAMDGPUSubArch).
129constexpr std::array<Triple::SubArchType, NumAMDGPUSubArches>
130 AMDGPUMajorFamilies = [] {
131 std::array<Triple::SubArchType, NumAMDGPUSubArches> Map{};
132
133 for (unsigned I = 0; I < NumAMDGPUSubArches; ++I) {
134 Map[I] =
136 }
137
138 for (const AMDGPUMajorSubArchEntry &Entry : AMDGPUMajorSubArch)
139 Map[Entry.SubArch - Triple::FirstAMDGPUSubArch] = Entry.Major;
140 return Map;
141 }();
142
143// SubArch -> name-offset, indexed by (SubArch - FirstAMDGPUSubArch). Unmapped
144// subarches keep offset 0 (the empty string).
145constexpr std::array<StringTable::Offset, NumAMDGPUSubArches>
146 AMDGPUSubArchNameOffsets = [] {
147 std::array<StringTable::Offset, NumAMDGPUSubArches> Map{};
148 for (const AMDGPUSubArchNameEntry &Entry : AMDGPUSubArchNames)
149 Map[Entry.SubArch - Triple::FirstAMDGPUSubArch] = Entry.NameOffset;
150 return Map;
151 }();
152
153// SubArch -> triple-name-offset (e.g. "amdgpu9.00"), like
154// AMDGPUSubArchNameOffsets.
155constexpr std::array<StringTable::Offset, NumAMDGPUSubArches>
156 AMDGPUSubArchTripleNameOffsets = [] {
157 std::array<StringTable::Offset, NumAMDGPUSubArches> Map{};
158 for (const AMDGPUSubArchNameEntry &Entry : AMDGPUSubArchNames)
159 Map[Entry.SubArch - Triple::FirstAMDGPUSubArch] =
160 Entry.TripleNameOffset;
161 return Map;
162 }();
163} // namespace
164
166 const GPUInfo *Info = getAMDGPUInfo(AK);
167 return Info ? AMDGPUNameStrTab[Info->FamilyName] : "";
168}
169
171 const GPUInfo *Info = getAMDGPUInfo(AK);
172 return Info ? Info->SubArch : Triple::SubArchType::NoSubArch;
173}
174
178
181 if (SubArch < Triple::FirstAMDGPUSubArch ||
183 return GK_NONE;
184 return AMDGPUSubArchToGPUKind[SubArch - Triple::FirstAMDGPUSubArch];
185}
186
192
194 if (A == B || A == Triple::NoSubArch || B == Triple::NoSubArch)
195 return true;
196
199
200 // One side is the major-family subarch covering the other's family.
201 if (A == MajorA)
202 return MajorA == MajorB;
203 if (B == MajorB)
204 return MajorA == MajorB;
205
206 return false;
207}
208
210 // An unrecognized GPU is never valid.
211 if (AK == GK_NONE)
212 return false;
213 // A legacy triple without a subarch accepts any known GPU.
214 if (SubArch == Triple::NoSubArch)
215 return true;
216
217 // Reject the dummy "generic" targets
218 Triple::SubArchType GPUSubArch = getSubArch(AK);
219 if (GPUSubArch == Triple::NoSubArch)
220 return false;
221
222 return isSubArchCompatible(GPUSubArch, SubArch);
223}
224
228
230 const GPUInfo *Info = getAMDGPUInfo(AK);
231 return Info && Info->SubArch == Triple::NoSubArch;
232}
233
237
239 // Tolerate subarch mismatch if one entry is none. This is a hack for bitcode
240 // libraries.
241 // There's a missing enum entry for an unknown subarch. Make sure the
242 // subarch is really empty.
243 if (A.getSubArch() == Triple::NoSubArch)
244 return A.getArchName().size() == 6;
245
246 if (B.getSubArch() == Triple::NoSubArch)
247 return B.getArchName().size() == 6;
248
249 return isSubArchCompatible(A.getSubArch(), B.getSubArch());
250}
251
252std::string AMDGPU::mergeSubArch(const Triple &A, const Triple &B) {
253 if (A.getSubArch() == Triple::NoSubArch)
254 return B.str();
255 if (B.getSubArch() == Triple::NoSubArch)
256 return A.str();
257
258 Triple::SubArchType MajorA = AMDGPU::getMajorSubArch(A.getSubArch());
259 Triple::SubArchType MajorB = AMDGPU::getMajorSubArch(B.getSubArch());
260
261 // With a compatible major arch, return the specific subarch.
262 if (A.getSubArch() == MajorA) {
263 if (MajorA == MajorB)
264 return B.str();
265 }
266
267 if (B.getSubArch() == MajorB) {
268 if (MajorA == MajorB)
269 return A.str();
270 }
271
272 // Invalid case.
273 return B.str();
274}
275
277 const GPUInfo *Info = getAMDGPUInfo(AK);
278 return Info ? AMDGPUNameStrTab[Info->Name] : "";
279}
280
282 if (SubArch < Triple::FirstAMDGPUSubArch ||
284 return "";
285 return AMDGPUNameStrTab[AMDGPUSubArchNameOffsets[SubArch -
287}
288
290 if (SubArch == Triple::NoSubArch)
291 return AMDGPUNameStrTab[AMDGPUNoSubArchNameOffset];
292
294 SubArch <= Triple::LastAMDGPUSubArch &&
295 "expected an AMDGPU subarch or NoSubArch");
296 return AMDGPUNameStrTab
297 [AMDGPUSubArchTripleNameOffsets[SubArch - Triple::FirstAMDGPUSubArch]];
298}
299
301 const R600Info *Info = getR600Info(AK);
302 return Info ? R600NameStrTab[Info->Name] : "";
303}
304
306 return parseArchImpl(CPU, AMDGPUGPUTable, AMDGPUFirstGPUKind,
307 AMDGPUNameStrTab, AMDGPUGPUAliases);
308}
309
311 return parseArchImpl(CPU, R600GPUTable, R600FirstGPUKind, R600NameStrTab,
312 R600GPUAliases);
313}
314
316 static constexpr AMDGPUFeatureBitset Empty{};
317 const GPUInfo *Info = getAMDGPUInfo(AK);
318 return Info ? Info->Features : Empty;
319}
320
322 static constexpr R600FeatureBitset Empty{};
323 const R600Info *Info = getR600Info(AK);
324 return Info ? Info->Features : Empty;
325}
326
329 for (unsigned I = 0; I != NUM_FEATURES; ++I) {
330 if (Features.test(I))
331 Names.push_back(AMDGPUNameStrTab[AMDGPUFeatureNames[I]]);
332 }
333}
334
336 Triple::SubArchType SubArch) {
337 // XXX: Should this only report unique canonical names?
338 // An alias shares its GPU's GPUKind, so it is filtered alongside it.
339 for (unsigned I = 0; I != std::size(AMDGPUGPUTable); ++I) {
340 GPUKind Kind = static_cast<GPUKind>(AMDGPUFirstGPUKind + I);
341 if (AMDGPUGPUTable[I].SubArch != Triple::NoSubArch &&
342 isCPUValidForSubArch(SubArch, Kind))
343 Values.push_back(AMDGPUNameStrTab[AMDGPUGPUTable[I].Name]);
344 }
345
346 for (const GPUNameAlias &A : AMDGPUGPUAliases) {
347 if (isCPUValidForSubArch(SubArch, A.Kind))
348 Values.push_back(AMDGPUNameStrTab[A.AltName]);
349 }
350}
351
353 for (const R600Info &Info : R600GPUTable)
354 Values.push_back(R600NameStrTab[Info.Name]);
355 for (const GPUNameAlias &A : R600GPUAliases)
356 Values.push_back(R600NameStrTab[A.AltName]);
357}
358
360 const GPUInfo *Info = getAMDGPUInfo(parseArchAMDGCN(GPU));
361 return Info ? Info->Version : IsaVersion{0, 0, 0};
362}
363
365 const GPUInfo *Info = getAMDGPUInfo(getGPUKindFromSubArch(SubArch));
366 return Info ? Info->Version : IsaVersion{0, 0, 0};
367}
368
371 if (Version.Major >= 8)
372 return 800;
373 return 512;
374}
375
378 if (Version.Major >= 8)
379 return 800;
380 return 512;
381}
382
384 if (getFeatureBitset(AK).test(FEAT_SGPR_INIT_BUG))
386
388 if (Version.Major >= 10)
389 return 106;
390 if (Version.Major >= 8)
391 return 102;
392 return 104;
393}
394
396 if (getFeatureBitset(getGPUKindFromSubArch(SubArch)).test(FEAT_SGPR_INIT_BUG))
398
400 if (Version.Major >= 10)
401 return 106;
402 if (Version.Major >= 8)
403 return 102;
404 return 104;
405}
406
409 if (Version.Major >= 10)
410 return getAddressableNumSGPRs(AK);
411 if (Version.Major >= 8)
412 return 16;
413 return 8;
414}
415
418 if (Version.Major >= 10)
419 return getAddressableNumSGPRs(SubArch);
420 if (Version.Major >= 8)
421 return 16;
422 return 8;
423}
424
425unsigned AMDGPU::getVGPRAllocGranule(GPUKind AK, bool IsWave32) {
426 const AMDGPUFeatureBitset &Features = getFeatureBitset(AK);
427 if (Features.test(FEAT_GFX90A_INSTS))
428 return 8;
429 if (Features.test(FEAT_1536_PHYSICAL_VGPRS))
430 return IsWave32 ? 24 : 12;
431 if (Features.test(FEAT_GFX10_3_INSTS))
432 return IsWave32 ? 16 : 8;
433 return IsWave32 ? 8 : 4;
434}
435
437 bool IsWave32) {
438 return getVGPRAllocGranule(getGPUKindFromSubArch(SubArch), IsWave32);
439}
440
441unsigned AMDGPU::getTotalNumVGPRs(GPUKind AK, bool IsWave32) {
442 const AMDGPUFeatureBitset &Features = getFeatureBitset(AK);
443 if (Features.test(FEAT_GFX90A_INSTS))
444 return 512;
445 if (!Features.test(FEAT_GFX10_INSTS))
446 return 256;
447 if (Features.test(FEAT_1536_PHYSICAL_VGPRS))
448 return IsWave32 ? 1536 : 768;
449 return IsWave32 ? 1024 : 512;
450}
451
452unsigned AMDGPU::getTotalNumVGPRs(Triple::SubArchType SubArch, bool IsWave32) {
453 return getTotalNumVGPRs(getGPUKindFromSubArch(SubArch), IsWave32);
454}
455
456unsigned AMDGPU::getAddressableNumVGPRs(GPUKind AK, bool IsWave32) {
457 const AMDGPUFeatureBitset &Features = getFeatureBitset(AK);
458 // The unified register file makes the AGPRs addressable as VGPRs.
459 if (Features.test(FEAT_GFX90A_INSTS))
460 return 512;
461 if (Features.test(FEAT_1024_ADDRESSABLE_VGPRS))
462 return IsWave32 ? 1024 : 512;
463 return 256;
464}
465
467 bool IsWave32) {
468 return getAddressableNumVGPRs(getGPUKindFromSubArch(SubArch), IsWave32);
469}
470
472 const GPUInfo *Info = getAMDGPUInfo(AK);
473 return Info ? Info->MaxHWAddressableLocalMemorySize : 32768;
474}
475
476unsigned
480
481unsigned AMDGPU::getLocalMemorySize(GPUKind AK, bool FullSIMDMode) {
482 // gfx6 and gfx10/11/12 address half of the physical block.
484 if (getFeatureBitset(AK).test(FEAT_HALF_ADDRESSABLE_PHYSICAL_LOCAL_MEMORY))
485 Size *= 2;
486
487 // In half-SIMD mode the work-group reaches only half of the block.
488 if (!FullSIMDMode)
489 Size /= 2;
490
491 return Size;
492}
493
495 bool FullSIMDMode) {
496 return getLocalMemorySize(getGPUKindFromSubArch(SubArch), FullSIMDMode);
497}
498
499unsigned AMDGPU::getAddressableLocalMemorySize(GPUKind AK, bool FullSIMDMode) {
500 return std::min(getMaxHWAddressableLocalMemorySize(AK),
501 getLocalMemorySize(AK, FullSIMDMode));
502}
503
505 bool FullSIMDMode) {
507 FullSIMDMode);
508}
509
511 const GPUInfo *Info = getAMDGPUInfo(AK);
512 return Info ? Info->LDSBankCount : 32;
513}
514
518
520 const GPUInfo *Info = getAMDGPUInfo(AK);
521 if (!Info || Info->BufferResourceNumRecordsWidth == 0)
522 return std::nullopt;
523 return Info->BufferResourceNumRecordsWidth;
524}
525
526std::optional<unsigned>
530
532 const AMDGPUFeatureBitset &Features = getFeatureBitset(AK);
533 if (Features.none())
534 return 256;
535 assert((Features.test(FEAT_LDS_ALLOC_GRANULARITY_256) ||
536 Features.test(FEAT_LDS_ALLOC_GRANULARITY_512) ||
537 Features.test(FEAT_LDS_ALLOC_GRANULARITY_1024) ||
538 Features.test(FEAT_LDS_ALLOC_GRANULARITY_1280) ||
539 Features.test(FEAT_LDS_ALLOC_GRANULARITY_2048)) &&
540 "missing LDS allocation granularity feature");
541 if (Features.test(FEAT_LDS_ALLOC_GRANULARITY_256))
542 return 256;
543 if (Features.test(FEAT_LDS_ALLOC_GRANULARITY_512))
544 return 512;
545 if (Features.test(FEAT_LDS_ALLOC_GRANULARITY_1024))
546 return 1024;
547 if (Features.test(FEAT_LDS_ALLOC_GRANULARITY_1280))
548 return 1280;
549 if (Features.test(FEAT_LDS_ALLOC_GRANULARITY_2048))
550 return 2048;
551
552 return 256;
553}
554
558
560 const AMDGPUFeatureBitset &Features = getFeatureBitset(AK);
561 if (Features.test(FEAT_LDS_ENCODING_GRANULARITY_256))
562 return 256;
563 if (Features.test(FEAT_LDS_ENCODING_GRANULARITY_512))
564 return 512;
565 if (Features.test(FEAT_LDS_ENCODING_GRANULARITY_1024))
566 return 1024;
567 if (Features.test(FEAT_LDS_ENCODING_GRANULARITY_1280))
568 return 1280;
569 if (Features.test(FEAT_LDS_ENCODING_GRANULARITY_2048))
570 return 2048;
571
572 return 0;
573}
574
578
580 const GPUInfo *Info = getAMDGPUInfo(AK);
581 return Info ? Info->MaxWavesPerEU : 10;
582}
583
587
589 assert(T.isAMDGPU());
590 auto ProcKind = T.isAMDGCN() ? parseArchAMDGCN(Arch) : parseArchR600(Arch);
591 if (ProcKind == GK_NONE)
592 return StringRef();
593
594 return T.isAMDGCN() ? getArchNameAMDGCN(ProcKind) : getArchNameR600(ProcKind);
595}
596
597// Capability features clang queries via the feature bitset but must not
598// serialize into the target-feature string.
599//
600// FIXME: This is hacky, we shouldn't have mismatches between the bitset and
601// feature string map.
603 FEAT_FAST_FMAF,
604 FEAT_FAST_DENORMAL_F32,
605 FEAT_SUPPORTS_WAVE32,
606 FEAT_SUPPORTS_WGP,
607 FEAT_XNACK_SUPPORT,
608 FEAT_SRAMECC_SUPPORT,
609 FEAT_XNACK_ON_OFF_MODES,
610 FEAT_APERTURE_REGS,
611 FEAT_GET_DOORBELL_ID,
612 FEAT_AGPR_ALLOC,
613 FEAT_1536_PHYSICAL_VGPRS,
614 FEAT_HALF_ADDRESSABLE_PHYSICAL_LOCAL_MEMORY,
615 FEAT_1024_ADDRESSABLE_VGPRS,
616 FEAT_LDS_ALLOC_GRANULARITY_256,
617 FEAT_LDS_ALLOC_GRANULARITY_512,
618 FEAT_LDS_ALLOC_GRANULARITY_1024,
619 FEAT_LDS_ALLOC_GRANULARITY_1280,
620 FEAT_LDS_ALLOC_GRANULARITY_2048,
621 FEAT_LDS_ENCODING_GRANULARITY_256,
622 FEAT_LDS_ENCODING_GRANULARITY_512,
623 FEAT_LDS_ENCODING_GRANULARITY_1024,
624 FEAT_LDS_ENCODING_GRANULARITY_1280,
625 FEAT_LDS_ENCODING_GRANULARITY_2048};
626
627// Add a GPU's features (minus the frontend-only ones) to \p Features. With \p
628// Overwrite false, existing entries are kept so user -mattr overrides win.
629static void addGPUFeatures(const GPUInfo &Info, bool Overwrite,
630 StringMap<bool> &Features) {
632 getFeatureNames(Info.Features & ~FrontendOnlyFeatures, Names);
633 for (StringRef Name : Names) {
634 if (Overwrite)
635 Features[Name] = true;
636 else
637 Features.insert({Name, true});
638 }
639}
640
641/// Add a GPU's default features to \p Features (preserving user overrides) and
642/// validate any requested wavesize.
643static std::pair<FeatureError, StringRef>
645 StringMap<bool> &Features) {
646 // With no explicit GPU, the triple's subarch identifies the target.
647 GPUKind Kind = GPU.empty() && T.getSubArch() != Triple::NoSubArch
648 ? getGPUKindFromSubArch(T.getSubArch())
649 : parseArchAMDGCN(GPU);
650 const GPUInfo *Info = getAMDGPUInfo(Kind);
651
652 // A bare subarch triple (no -target-cpu) still pins down the target, so it is
653 // not a null GPU. The target's native wavesize (if single-mode) is in the
654 // feature bitset; a dual-mode GPU has neither wave bit set.
655 const bool IsNullGPU = T.getSubArch() == Triple::NoSubArch && GPU.empty();
656 const bool TargetHasWave32 =
657 Info && Info->Features.test(FEAT_WAVEFRONTSIZE32);
658 const bool TargetHasWave64 =
659 Info && Info->Features.test(FEAT_WAVEFRONTSIZE64);
660
661 auto Wave32Itr = Features.find("wavefrontsize32");
662 auto Wave64Itr = Features.find("wavefrontsize64");
663 const bool EnableWave32 =
664 Wave32Itr != Features.end() && Wave32Itr->getValue();
665 const bool EnableWave64 =
666 Wave64Itr != Features.end() && Wave64Itr->getValue();
667 const bool DisableWave32 =
668 Wave32Itr != Features.end() && !Wave32Itr->getValue();
669 const bool DisableWave64 =
670 Wave64Itr != Features.end() && !Wave64Itr->getValue();
671
672 if (EnableWave32 && EnableWave64)
674 "'+wavefrontsize32' and '+wavefrontsize64' are mutually exclusive"};
675 if (DisableWave32 && DisableWave64)
677 "'-wavefrontsize32' and '-wavefrontsize64' are mutually exclusive"};
678
679 if (!IsNullGPU) {
680 if (TargetHasWave64) {
681 if (EnableWave32)
682 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "+wavefrontsize32"};
683 if (DisableWave64)
684 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "-wavefrontsize64"};
685 }
686
687 if (TargetHasWave32) {
688 if (EnableWave64)
689 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "+wavefrontsize64"};
690 if (DisableWave32)
691 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "-wavefrontsize32"};
692 }
693 }
694
695 // Don't assume any wavesize with an unknown subtarget.
696 // Default to wave32 if target supports both.
697 if (!IsNullGPU && !EnableWave32 && !EnableWave64 && !TargetHasWave32 &&
698 !TargetHasWave64)
699 Features.insert({"wavefrontsize32", true});
700
701 // Merge the target defaults, keeping any user -mattr overrides.
702 if (Info)
703 addGPUFeatures(*Info, /*Overwrite=*/false, Features);
704
705 return {NO_ERROR, StringRef()};
706}
707
708/// Fills Features map with default values for given target GPU.
709/// \p Features contains overriding target features and this function returns
710/// default target features with entries overridden by \p Features.
711std::pair<FeatureError, StringRef>
713 StringMap<bool> &Features) {
714 // XXX - What does the member GPU mean if device name string passed here?
715 if (T.isSPIRV() && T.getOS() == Triple::OSType::AMDHSA) {
716 // AMDGCN SPIRV must support the union of all AMDGCN features.
719 for (StringRef G : GPUs)
720 if (const GPUInfo *Info = getAMDGPUInfo(parseArchAMDGCN(G)))
721 addGPUFeatures(*Info, /*Overwrite=*/true, Features);
722 Features["wavefrontsize32"] = true;
723 Features["wavefrontsize64"] = true;
724 } else if (T.isAMDGCN()) {
725 return fillAMDGCNFeatureMap(GPU, T, Features);
726 } else {
727 if (GPU.empty())
728 GPU = "r600";
729
730 switch (llvm::AMDGPU::parseArchR600(GPU)) {
731 case GK_CAYMAN:
732 case GK_CYPRESS:
733 case GK_RV770:
734 case GK_RV670:
735 // TODO: Add fp64 when implemented.
736 break;
737 case GK_TURKS:
738 case GK_CAICOS:
739 case GK_BARTS:
740 case GK_SUMO:
741 case GK_REDWOOD:
742 case GK_JUNIPER:
743 case GK_CEDAR:
744 case GK_RV730:
745 case GK_RV710:
746 case GK_RS880:
747 case GK_R630:
748 case GK_R600:
749 break;
750 default:
751 llvm_unreachable("Unhandled GPU!");
752 }
753 }
754 return {NO_ERROR, StringRef()};
755}
756
757TargetID::TargetID(GPUKind Arch, const Triple &TT, TargetIDSetting XnackSetting,
758 TargetIDSetting SramEccSetting)
759 : Arch(Arch),
760 TargetTripleString(TT.normalize(Triple::CanonicalForm::FOUR_IDENT)),
761 XnackSetting(XnackSetting), SramEccSetting(SramEccSetting),
762 IsAMDHSA(TT.getOS() == Triple::AMDHSA) {}
763
764// Parse a feature modifier sign ("+"/"-"). Returns "Unsupported" if \p Sign is
765// neither (i.e. the modifier is malformed).
767 if (Sign == "+")
768 return TargetIDSetting::On;
769 if (Sign == "-")
770 return TargetIDSetting::Off;
771
772 return TargetIDSetting::Unsupported;
773}
774
775// Derive the architecture from the processor name in \p TargetIDStr. "generic"
776// and the empty processor name act as a wildcard.
777static GPUKind getGPUKindFromTargetID(const Triple &TT, StringRef TargetIDStr) {
778 StringRef CPUName = TargetIDStr.split(':').first;
779 return (CPUName.empty() || CPUName == "generic")
780 ? getGPUKindFromSubArch(TT.getSubArch())
781 : parseArchAMDGCN(CPUName);
782}
783
784// Compute the default xnack/sramecc settings for processor \p Arch, before any
785// explicit feature modifiers are applied.
789 const AMDGPUFeatureBitset &Features = getFeatureBitset(Arch);
790 // xnack with on/off modes defaults to Any; supported without on/off modes is
791 // hardwired On (e.g. gfx1250); unsupported is Unsupported.
792 if (!Features.test(FEAT_XNACK_SUPPORT))
793 XnackSetting = TargetIDSetting::Unsupported;
794 else if (Features.test(FEAT_XNACK_ON_OFF_MODES))
795 XnackSetting = TargetIDSetting::Any;
796 else
797 XnackSetting = TargetIDSetting::On;
798 SramEccSetting = Features.test(FEAT_SRAMECC_SUPPORT)
799 ? TargetIDSetting::Any
800 : TargetIDSetting::Unsupported;
801}
802
803// Compute the xnack/sramecc settings for processor \p Arch from the
804// processor+features string \p TargetIDStr
805// (e.g. "gfx90a:xnack+:sramecc-"). Returns false if a modifier names an unknown
806// or repeated feature, names one the processor does not support, or has a
807// malformed sign.
808static bool computeTargetIDFeatures(GPUKind Arch, StringRef TargetIDStr,
811 const AMDGPUFeatureBitset &Features = getFeatureBitset(Arch);
813
814 // The first component is the processor; the rest are feature modifiers of the
815 // form "<feature><+|->".
817 TargetIDStr.split(Split, ':');
818 bool SeenXnack = false;
819 bool SeenSramEcc = false;
820 bool Valid = true;
821 for (unsigned I = 1, E = Split.size(); I != E; ++I) {
822 StringRef FeatureString = Split[I];
823 if (FeatureString.consume_front("xnack")) {
825 // An xnack modifier is only valid with on/off modes: rejected when xnack
826 // is unsupported or hardwired on (e.g. gfx1250).
827 if (SeenXnack || !Features.test(FEAT_XNACK_ON_OFF_MODES) ||
828 Sign == TargetIDSetting::Unsupported)
829 Valid = false;
830 else
831 XnackSetting = Sign;
832 SeenXnack = true;
833 } else if (FeatureString.consume_front("sramecc")) {
835 if (SeenSramEcc || SramEccSetting == TargetIDSetting::Unsupported ||
836 Sign == TargetIDSetting::Unsupported)
837 Valid = false;
838 else
839 SramEccSetting = Sign;
840 SeenSramEcc = true;
841 } else {
842 // Unknown feature name.
843 Valid = false;
844 }
845 }
846 return Valid;
847}
848
849TargetID::TargetID(const Triple &TT, StringRef TargetIDStr)
850 : TargetID(getGPUKindFromTargetID(TT, TargetIDStr), TT,
852 // Derive the feature settings from the string. Validity is not checked here;
853 // parseTargetIDString validates untrusted input.
854 computeTargetIDFeatures(Arch, TargetIDStr, XnackSetting, SramEccSetting);
855}
856
858 StringRef FeatureString) {
859 GPUKind Arch = parseArchAMDGCN(CPU);
860 TargetIDSetting XnackSetting, SramEccSetting;
861 getDefaultTargetIDFeatures(Arch, XnackSetting, SramEccSetting);
862
863 // Apply the +/-xnack and +/-sramecc modifiers from the feature string, only
864 // for targets that can toggle the corresponding mode.
865 bool XnackToggleable = XnackSetting == TargetIDSetting::Any;
866 bool SramEccToggleable = SramEccSetting == TargetIDSetting::Any;
868 FeatureString.split(Features, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
869 for (StringRef Feature : Features) {
870 TargetIDSetting Sign =
871 getTargetIDSettingFromFeatureString(Feature.take_front());
872 if (Sign == TargetIDSetting::Unsupported)
873 continue;
874 StringRef Name = Feature.drop_front();
875 if (Name == "xnack" && XnackToggleable)
876 XnackSetting = Sign;
877 else if (Name == "sramecc" && SramEccToggleable)
878 SramEccSetting = Sign;
879 }
880
881 return TargetID(Arch, TT, XnackSetting, SramEccSetting);
882}
883
884std::optional<TargetID> TargetID::parse(const Triple &TT,
885 StringRef ProcAndFeatures) {
886 if (!TT.isAMDGCN())
887 return std::nullopt;
888
889 // Filter out unrecognized subarch suffixes.
890 if (TT.getSubArch() == Triple::NoSubArch && TT.getArchName() != "amdgcn")
891 return std::nullopt;
892
893 // A named processor (i.e. not the empty/generic wildcard, which is resolved
894 // from the triple's subarch) must be a recognized GPU that is consistent with
895 // the triple's subarch.
896 StringRef CPUName = ProcAndFeatures.split(':').first;
897 if (!CPUName.empty() && CPUName != "generic" &&
898 !isCPUValidForSubArch(TT.getSubArch(), CPUName))
899 return std::nullopt;
900
901 // Parse the processor and its feature modifiers, then construct directly from
902 // the resulting fields.
903 GPUKind Arch = getGPUKindFromTargetID(TT, ProcAndFeatures);
904 TargetIDSetting XnackSetting, SramEccSetting;
905 if (!computeTargetIDFeatures(Arch, ProcAndFeatures, XnackSetting,
906 SramEccSetting))
907 return std::nullopt;
908
909 return TargetID(Arch, TT, XnackSetting, SramEccSetting);
910}
911
912std::optional<TargetID>
914 // Split on '-' to get arch-vendor-os-environment-processor:features. There is
915 // a single dash separator after the 4-component triple, so the
916 // processor+features field must be present (even if empty).
918 TargetIDDirective.split(Parts, '-', /*MaxSplit=*/4);
919 if (Parts.size() < 5)
920 return std::nullopt;
921
922 return parse(Triple(Parts[0], Parts[1], Parts[2], Parts[3]), Parts[4]);
923}
924
925// Returns true if \p Arch hardwires xnack on (supports xnack but has no on/off
926// modes, e.g. gfx1250), so xnack is not a selectable target-id modifier.
927static bool isXnackHardwiredOn(GPUKind Arch) {
928 const AMDGPUFeatureBitset &Features = getFeatureBitset(Arch);
929 return Features.test(FEAT_XNACK_SUPPORT) &&
930 !Features.test(FEAT_XNACK_ON_OFF_MODES);
931}
932
933// Append the explicit (On/Off) sramecc/xnack feature modifiers in canonical
934// order, e.g. ":sramecc-:xnack+". Xnack is never emitted for hardwired-on
935// targets.
937 TargetIDSetting Xnack,
938 bool XnackHardwiredOn) {
939 if (SramEcc == TargetIDSetting::Off)
940 OS << ":sramecc-";
941 else if (SramEcc == TargetIDSetting::On)
942 OS << ":sramecc+";
943
944 if (XnackHardwiredOn)
945 return;
946
947 if (Xnack == TargetIDSetting::Off)
948 OS << ":xnack-";
949 else if (Xnack == TargetIDSetting::On)
950 OS << ":xnack+";
951}
952
953void TargetID::print(raw_ostream &StreamRep) const {
954 StreamRep << TargetTripleString << '-' << getArchNameAMDGCN(Arch);
955
956 if (IsAMDHSA) {
958 isXnackHardwiredOn(Arch));
959 }
960}
961
962std::string TargetID::toString() const {
963 std::string Str;
964 raw_string_ostream OS(Str);
965 OS << *this;
966 return Str;
967}
968
974
976 std::string Str;
977 raw_string_ostream OS(Str);
979 return Str;
980}
981
983 return Arch == Other.Arch && XnackSetting == Other.XnackSetting &&
984 SramEccSetting == Other.SramEccSetting && IsAMDHSA == Other.IsAMDHSA &&
985 TargetTripleString == Other.TargetTripleString;
986}
987
989 TargetIDSetting Requested) {
990 return Provided == TargetIDSetting::Any ||
991 Provided == TargetIDSetting::Unsupported || Provided == Requested;
992}
993
995 // The processor and feature settings must match exactly
996 if (Arch != Other.Arch || XnackSetting != Other.XnackSetting ||
997 SramEccSetting != Other.SramEccSetting)
998 return false;
999
1001 .isCompatibleWith(Triple(Other.getTargetTripleString()));
1002}
1003
1005 // A major-family/generic processor (e.g. amdgpu9) provides for a specific
1006 // member of its family (e.g. gfx900), but not the reverse. Otherwise the
1007 // processors must match.
1008 if (Arch != Other.Arch && Arch != GK_NONE && Other.Arch != GK_NONE) {
1009 Triple::SubArchType ThisSubArch = getSubArch(Arch);
1010 if (ThisSubArch != getMajorSubArch(ThisSubArch) ||
1011 ThisSubArch != getMajorSubArch(getSubArch(Other.Arch)))
1012 return false;
1013 }
1014
1015 if (!featureProvidesFor(XnackSetting, Other.XnackSetting) ||
1016 !featureProvidesFor(SramEccSetting, Other.SramEccSetting))
1017 return false;
1018
1020 .isCompatibleWith(Triple(Other.getTargetTripleString()));
1021}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static cl::opt< bool > SramEccSetting("amdgpu-sramecc", cl::desc("Force amdgpu.sramecc for testing"), cl::ReallyHidden)
static cl::opt< bool > XnackSetting("amdgpu-xnack", cl::desc("Force amdgpu.xnack value for testing"), cl::ReallyHidden)
static GPUKind getGPUKindFromTargetID(const Triple &TT, StringRef TargetIDStr)
static std::pair< FeatureError, StringRef > fillAMDGCNFeatureMap(StringRef GPU, const Triple &T, StringMap< bool > &Features)
Add a GPU's default features to Features (preserving user overrides) and validate any requested waves...
static bool computeTargetIDFeatures(GPUKind Arch, StringRef TargetIDStr, TargetIDSetting &XnackSetting, TargetIDSetting &SramEccSetting)
static void getDefaultTargetIDFeatures(GPUKind Arch, TargetIDSetting &XnackSetting, TargetIDSetting &SramEccSetting)
static TargetIDSetting getTargetIDSettingFromFeatureString(StringRef Sign)
static bool featureProvidesFor(TargetIDSetting Provided, TargetIDSetting Requested)
static bool isXnackHardwiredOn(GPUKind Arch)
static void addGPUFeatures(const GPUInfo &Info, bool Overwrite, StringMap< bool > &Features)
static const AMDGPUFeatureBitset FrontendOnlyFeatures
static void printFeatureModifiers(raw_ostream &OS, TargetIDSetting SramEcc, TargetIDSetting Xnack, bool XnackHardwiredOn)
#define X(NUM, ENUM, NAME)
Definition ELF.h:857
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define T
modulo schedule test
This file defines the SmallVector class.
LocallyHashedType DenseMapInfo< LocallyHashedType >::Empty
static TargetID createFromSubtargetFeatures(const Triple &TT, StringRef CPU, StringRef FeatureString)
Construct a TargetID for triple TT and processor CPU, taking the xnack/sramecc modes from the subtarg...
void printCanonicalTargetIDString(raw_ostream &OS) const
Print the canonical processor name followed by any explicit xnack and sramecc feature modifiers (e....
static std::optional< TargetID > parseTargetIDString(StringRef TargetIDDirective)
Parse and validate a TargetID from a full "<triple>-<processor>:<features>" directive string.
void print(raw_ostream &OS) const
TargetIDSetting getXnackSetting() const
bool isEquivalent(const TargetID &Other) const
Returns true if Other denotes the same target as *this, i.e.
bool operator==(const TargetID &Other) const
bool providesFor(const TargetID &Other) const
Returns true if a device image for *this can provide the device code for a request for Other.
StringRef getTargetTripleString() const
std::string getCanonicalFeatureString() const
TargetID(GPUKind Arch, const Triple &TT, TargetIDSetting XnackSetting, TargetIDSetting SramEccSetting)
static std::optional< TargetID > parse(const Triple &TT, StringRef ProcAndFeatures)
Parse and validate a TargetID for triple TT from the processor+features string ProcAndFeatures (e....
std::string toString() const
TargetIDSetting getSramEccSetting() const
constexpr bool none() const
Definition Bitset.h:120
constexpr bool test(unsigned I) const
Definition Bitset.h:109
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringMap - This is an unconventional map that is specialized for handling keys that are "strings",...
Definition StringMap.h:129
iterator end()
Definition StringMap.h:214
iterator find(StringRef Key)
Definition StringMap.h:227
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
Definition StringMap.h:311
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition StringRef.h:736
constexpr bool empty() const
Check if the string is empty.
Definition StringRef.h:141
bool consume_front(char Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
Definition StringRef.h:661
A table of densely packed, null-terminated strings indexed by offset.
Definition StringTable.h:34
Triple - Helper class for working with autoconf configuration names.
Definition Triple.h:48
@ FirstAMDGPUSubArch
Definition Triple.h:278
@ LastAMDGPUSubArch
Definition Triple.h:279
LLVM_ABI bool isCompatibleWith(const Triple &Other) const
Test whether target triples are compatible.
Definition Triple.cpp:2274
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
A raw_ostream that writes to an std::string.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI StringRef getArchNameR600(GPUKind AK)
LLVM_ABI void fillValidArchListAMDGCN(SmallVectorImpl< StringRef > &Values, Triple::SubArchType SubArch=Triple::NoSubArch)
Append the valid AMDGCN GPU names to Values.
LLVM_ABI unsigned getLDSEncodingGranule(GPUKind AK)
LLVM_ABI unsigned getLDSAllocGranule(GPUKind AK)
LLVM_ABI unsigned getMaxWavesPerEU(GPUKind AK)
LLVM_ABI StringRef getCanonicalArchName(const Triple &T, StringRef Arch)
LLVM_ABI unsigned getAddressableLocalMemorySize(GPUKind AK, bool FullSIMDMode)
LLVM_ABI void fillValidArchListR600(SmallVectorImpl< StringRef > &Values)
LLVM_ABI std::string mergeSubArch(const Triple &A, const Triple &B)
Returns the effective triple appropriate to use when linking B into A by merging the subarches in cas...
LLVM_ABI bool isCPUValidForSubArch(Triple::SubArchType SubArch, GPUKind AK)
Return true if the GPU AK is usable with the triple subarch SubArch.
LLVM_ABI bool isSubArchCompatible(const Triple &A, const Triple &B)
Return true if subarch A is compatible with subarch B, i.e.
LLVM_ABI unsigned getLDSBankCount(GPUKind AK)
LLVM_ABI unsigned getMaxHWAddressableLocalMemorySize(GPUKind AK)
LDS size queries.
LLVM_ABI StringRef getArchFamilyNameAMDGCN(GPUKind AK)
LLVM_ABI StringRef getSubArchName(Triple::SubArchType SubArch)
Returns the triple subarch name for an AMDGPU subarch, e.g.
LLVM_ABI unsigned getAddressableNumSGPRs(GPUKind AK)
LLVM_ABI IsaVersion getIsaVersion(StringRef GPU)
LLVM_ABI unsigned getTotalNumVGPRs(GPUKind AK, bool IsWave32)
LLVM_ABI unsigned getTotalNumSGPRs(GPUKind AK)
LLVM_ABI std::optional< unsigned > getBufferResourceNumRecordsWidth(GPUKind AK)
GPUKind
GPU kinds supported by the AMDGPU target.
Bitset< NUM_FEATURES > AMDGPUFeatureBitset
LLVM_ABI Triple::SubArchType getSubArchFromGPUName(StringRef CPU)
Returns the preferred subarch for a GPU name CPU, or NoSubArch if unrecognized.
LLVM_ABI unsigned getLocalMemorySize(GPUKind AK, bool FullSIMDMode)
LLVM_ABI unsigned getSGPRAllocGranule(GPUKind AK)
LLVM_ABI Triple::SubArchType getSubArch(GPUKind AK)
LLVM_ABI StringRef getArchNameFromSubArch(Triple::SubArchType SubArch)
Returns the canonical GPU name for an AMDGPU subarch, e.g.
LLVM_ABI unsigned getVGPRAllocGranule(GPUKind AK, bool IsWave32)
LLVM_ABI GPUKind parseArchAMDGCN(StringRef CPU)
LLVM_ABI bool isPseudoTarget(GPUKind AK)
Return true if AK is a pseudo target (e.g.
LLVM_ABI GPUKind getGPUKindFromSubArch(Triple::SubArchType SubArch)
AMDGPU::TargetID TargetID
LLVM_ABI std::pair< FeatureError, StringRef > fillAMDGPUFeatureMap(StringRef GPU, const Triple &T, StringMap< bool > &Features)
Fills Features map with default values for given target GPU.
LLVM_ABI unsigned getAddressableNumVGPRs(GPUKind AK, bool IsWave32)
LLVM_ABI void getFeatureNames(const AMDGPUFeatureBitset &Features, SmallVectorImpl< StringRef > &Names)
Appends the feature name of each bit set in Features to Names.
LLVM_ABI StringRef getArchNameAMDGCN(GPUKind AK)
LLVM_ABI Triple::SubArchType getMajorSubArch(Triple::SubArchType SubArch)
LLVM_ABI const AMDGPUFeatureBitset & getFeatureBitset(GPUKind AK)
Returns AK's feature bitset, or an empty bitset if unknown.
LLVM_ABI const R600FeatureBitset & getFeatureBitsetR600(GPUKind AK)
Returns R600 GPU AK's feature bitset, or an empty bitset if unknown.
Bitset< R600_NUM_FEATURES > R600FeatureBitset
LLVM_ABI GPUKind parseArchR600(StringRef CPU)
This is an optimization pass for GlobalISel generic memory operations.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
#define N
Instruction set architecture version.