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 GPUInfo *Info = getAMDGPUInfo(AK);
533 return Info ? Info->MaxWavesPerEU : 10;
534}
535
539
541 assert(T.isAMDGPU());
542 auto ProcKind = T.isAMDGCN() ? parseArchAMDGCN(Arch) : parseArchR600(Arch);
543 if (ProcKind == GK_NONE)
544 return StringRef();
545
546 return T.isAMDGCN() ? getArchNameAMDGCN(ProcKind) : getArchNameR600(ProcKind);
547}
548
549// Capability features clang queries via the feature bitset but must not
550// serialize into the target-feature string.
551//
552// FIXME: This is hacky, we shouldn't have mismatches between the bitset and
553// feature string map.
555 FEAT_FAST_FMAF,
556 FEAT_FAST_DENORMAL_F32,
557 FEAT_SUPPORTS_WAVE32,
558 FEAT_SUPPORTS_WGP,
559 FEAT_XNACK_SUPPORT,
560 FEAT_SRAMECC_SUPPORT,
561 FEAT_XNACK_ON_OFF_MODES,
562 FEAT_APERTURE_REGS,
563 FEAT_GET_DOORBELL_ID,
564 FEAT_AGPR_ALLOC,
565 FEAT_1536_PHYSICAL_VGPRS,
566 FEAT_HALF_ADDRESSABLE_PHYSICAL_LOCAL_MEMORY,
567 FEAT_1024_ADDRESSABLE_VGPRS};
568
569// Add a GPU's features (minus the frontend-only ones) to \p Features. With \p
570// Overwrite false, existing entries are kept so user -mattr overrides win.
571static void addGPUFeatures(const GPUInfo &Info, bool Overwrite,
572 StringMap<bool> &Features) {
574 getFeatureNames(Info.Features & ~FrontendOnlyFeatures, Names);
575 for (StringRef Name : Names) {
576 if (Overwrite)
577 Features[Name] = true;
578 else
579 Features.insert({Name, true});
580 }
581}
582
583/// Add a GPU's default features to \p Features (preserving user overrides) and
584/// validate any requested wavesize.
585static std::pair<FeatureError, StringRef>
587 StringMap<bool> &Features) {
588 // With no explicit GPU, the triple's subarch identifies the target.
589 GPUKind Kind = GPU.empty() && T.getSubArch() != Triple::NoSubArch
590 ? getGPUKindFromSubArch(T.getSubArch())
591 : parseArchAMDGCN(GPU);
592 const GPUInfo *Info = getAMDGPUInfo(Kind);
593
594 // A bare subarch triple (no -target-cpu) still pins down the target, so it is
595 // not a null GPU. The target's native wavesize (if single-mode) is in the
596 // feature bitset; a dual-mode GPU has neither wave bit set.
597 const bool IsNullGPU = T.getSubArch() == Triple::NoSubArch && GPU.empty();
598 const bool TargetHasWave32 =
599 Info && Info->Features.test(FEAT_WAVEFRONTSIZE32);
600 const bool TargetHasWave64 =
601 Info && Info->Features.test(FEAT_WAVEFRONTSIZE64);
602
603 auto Wave32Itr = Features.find("wavefrontsize32");
604 auto Wave64Itr = Features.find("wavefrontsize64");
605 const bool EnableWave32 =
606 Wave32Itr != Features.end() && Wave32Itr->getValue();
607 const bool EnableWave64 =
608 Wave64Itr != Features.end() && Wave64Itr->getValue();
609 const bool DisableWave32 =
610 Wave32Itr != Features.end() && !Wave32Itr->getValue();
611 const bool DisableWave64 =
612 Wave64Itr != Features.end() && !Wave64Itr->getValue();
613
614 if (EnableWave32 && EnableWave64)
616 "'+wavefrontsize32' and '+wavefrontsize64' are mutually exclusive"};
617 if (DisableWave32 && DisableWave64)
619 "'-wavefrontsize32' and '-wavefrontsize64' are mutually exclusive"};
620
621 if (!IsNullGPU) {
622 if (TargetHasWave64) {
623 if (EnableWave32)
624 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "+wavefrontsize32"};
625 if (DisableWave64)
626 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "-wavefrontsize64"};
627 }
628
629 if (TargetHasWave32) {
630 if (EnableWave64)
631 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "+wavefrontsize64"};
632 if (DisableWave32)
633 return {AMDGPU::UNSUPPORTED_TARGET_FEATURE, "-wavefrontsize32"};
634 }
635 }
636
637 // Don't assume any wavesize with an unknown subtarget.
638 // Default to wave32 if target supports both.
639 if (!IsNullGPU && !EnableWave32 && !EnableWave64 && !TargetHasWave32 &&
640 !TargetHasWave64)
641 Features.insert({"wavefrontsize32", true});
642
643 // Merge the target defaults, keeping any user -mattr overrides.
644 if (Info)
645 addGPUFeatures(*Info, /*Overwrite=*/false, Features);
646
647 return {NO_ERROR, StringRef()};
648}
649
650/// Fills Features map with default values for given target GPU.
651/// \p Features contains overriding target features and this function returns
652/// default target features with entries overridden by \p Features.
653std::pair<FeatureError, StringRef>
655 StringMap<bool> &Features) {
656 // XXX - What does the member GPU mean if device name string passed here?
657 if (T.isSPIRV() && T.getOS() == Triple::OSType::AMDHSA) {
658 // AMDGCN SPIRV must support the union of all AMDGCN features.
661 for (StringRef G : GPUs)
662 if (const GPUInfo *Info = getAMDGPUInfo(parseArchAMDGCN(G)))
663 addGPUFeatures(*Info, /*Overwrite=*/true, Features);
664 Features["wavefrontsize32"] = true;
665 Features["wavefrontsize64"] = true;
666 } else if (T.isAMDGCN()) {
667 return fillAMDGCNFeatureMap(GPU, T, Features);
668 } else {
669 if (GPU.empty())
670 GPU = "r600";
671
672 switch (llvm::AMDGPU::parseArchR600(GPU)) {
673 case GK_CAYMAN:
674 case GK_CYPRESS:
675 case GK_RV770:
676 case GK_RV670:
677 // TODO: Add fp64 when implemented.
678 break;
679 case GK_TURKS:
680 case GK_CAICOS:
681 case GK_BARTS:
682 case GK_SUMO:
683 case GK_REDWOOD:
684 case GK_JUNIPER:
685 case GK_CEDAR:
686 case GK_RV730:
687 case GK_RV710:
688 case GK_RS880:
689 case GK_R630:
690 case GK_R600:
691 break;
692 default:
693 llvm_unreachable("Unhandled GPU!");
694 }
695 }
696 return {NO_ERROR, StringRef()};
697}
698
699TargetID::TargetID(GPUKind Arch, const Triple &TT, TargetIDSetting XnackSetting,
700 TargetIDSetting SramEccSetting)
701 : Arch(Arch),
702 TargetTripleString(TT.normalize(Triple::CanonicalForm::FOUR_IDENT)),
703 XnackSetting(XnackSetting), SramEccSetting(SramEccSetting),
704 IsAMDHSA(TT.getOS() == Triple::AMDHSA) {}
705
706// Parse a feature modifier sign ("+"/"-"). Returns "Unsupported" if \p Sign is
707// neither (i.e. the modifier is malformed).
709 if (Sign == "+")
710 return TargetIDSetting::On;
711 if (Sign == "-")
712 return TargetIDSetting::Off;
713
714 return TargetIDSetting::Unsupported;
715}
716
717// Derive the architecture from the processor name in \p TargetIDStr. "generic"
718// and the empty processor name act as a wildcard.
719static GPUKind getGPUKindFromTargetID(const Triple &TT, StringRef TargetIDStr) {
720 StringRef CPUName = TargetIDStr.split(':').first;
721 return (CPUName.empty() || CPUName == "generic")
722 ? getGPUKindFromSubArch(TT.getSubArch())
723 : parseArchAMDGCN(CPUName);
724}
725
726// Compute the default xnack/sramecc settings for processor \p Arch, before any
727// explicit feature modifiers are applied.
731 const AMDGPUFeatureBitset &Features = getFeatureBitset(Arch);
732 // xnack with on/off modes defaults to Any; supported without on/off modes is
733 // hardwired On (e.g. gfx1250); unsupported is Unsupported.
734 if (!Features.test(FEAT_XNACK_SUPPORT))
735 XnackSetting = TargetIDSetting::Unsupported;
736 else if (Features.test(FEAT_XNACK_ON_OFF_MODES))
737 XnackSetting = TargetIDSetting::Any;
738 else
739 XnackSetting = TargetIDSetting::On;
740 SramEccSetting = Features.test(FEAT_SRAMECC_SUPPORT)
741 ? TargetIDSetting::Any
742 : TargetIDSetting::Unsupported;
743}
744
745// Compute the xnack/sramecc settings for processor \p Arch from the
746// processor+features string \p TargetIDStr
747// (e.g. "gfx90a:xnack+:sramecc-"). Returns false if a modifier names an unknown
748// or repeated feature, names one the processor does not support, or has a
749// malformed sign.
750static bool computeTargetIDFeatures(GPUKind Arch, StringRef TargetIDStr,
753 const AMDGPUFeatureBitset &Features = getFeatureBitset(Arch);
755
756 // The first component is the processor; the rest are feature modifiers of the
757 // form "<feature><+|->".
759 TargetIDStr.split(Split, ':');
760 bool SeenXnack = false;
761 bool SeenSramEcc = false;
762 bool Valid = true;
763 for (unsigned I = 1, E = Split.size(); I != E; ++I) {
764 StringRef FeatureString = Split[I];
765 if (FeatureString.consume_front("xnack")) {
767 // An xnack modifier is only valid with on/off modes: rejected when xnack
768 // is unsupported or hardwired on (e.g. gfx1250).
769 if (SeenXnack || !Features.test(FEAT_XNACK_ON_OFF_MODES) ||
770 Sign == TargetIDSetting::Unsupported)
771 Valid = false;
772 else
773 XnackSetting = Sign;
774 SeenXnack = true;
775 } else if (FeatureString.consume_front("sramecc")) {
777 if (SeenSramEcc || SramEccSetting == TargetIDSetting::Unsupported ||
778 Sign == TargetIDSetting::Unsupported)
779 Valid = false;
780 else
781 SramEccSetting = Sign;
782 SeenSramEcc = true;
783 } else {
784 // Unknown feature name.
785 Valid = false;
786 }
787 }
788 return Valid;
789}
790
791TargetID::TargetID(const Triple &TT, StringRef TargetIDStr)
792 : TargetID(getGPUKindFromTargetID(TT, TargetIDStr), TT,
794 // Derive the feature settings from the string. Validity is not checked here;
795 // parseTargetIDString validates untrusted input.
796 computeTargetIDFeatures(Arch, TargetIDStr, XnackSetting, SramEccSetting);
797}
798
800 StringRef FeatureString) {
801 GPUKind Arch = parseArchAMDGCN(CPU);
802 TargetIDSetting XnackSetting, SramEccSetting;
803 getDefaultTargetIDFeatures(Arch, XnackSetting, SramEccSetting);
804
805 // Apply the +/-xnack and +/-sramecc modifiers from the feature string, only
806 // for targets that can toggle the corresponding mode.
807 bool XnackToggleable = XnackSetting == TargetIDSetting::Any;
808 bool SramEccToggleable = SramEccSetting == TargetIDSetting::Any;
810 FeatureString.split(Features, ',', /*MaxSplit=*/-1, /*KeepEmpty=*/false);
811 for (StringRef Feature : Features) {
812 TargetIDSetting Sign =
813 getTargetIDSettingFromFeatureString(Feature.take_front());
814 if (Sign == TargetIDSetting::Unsupported)
815 continue;
816 StringRef Name = Feature.drop_front();
817 if (Name == "xnack" && XnackToggleable)
818 XnackSetting = Sign;
819 else if (Name == "sramecc" && SramEccToggleable)
820 SramEccSetting = Sign;
821 }
822
823 return TargetID(Arch, TT, XnackSetting, SramEccSetting);
824}
825
826std::optional<TargetID> TargetID::parse(const Triple &TT,
827 StringRef ProcAndFeatures) {
828 if (!TT.isAMDGCN())
829 return std::nullopt;
830
831 // Filter out unrecognized subarch suffixes.
832 if (TT.getSubArch() == Triple::NoSubArch && TT.getArchName() != "amdgcn")
833 return std::nullopt;
834
835 // A named processor (i.e. not the empty/generic wildcard, which is resolved
836 // from the triple's subarch) must be a recognized GPU that is consistent with
837 // the triple's subarch.
838 StringRef CPUName = ProcAndFeatures.split(':').first;
839 if (!CPUName.empty() && CPUName != "generic" &&
840 !isCPUValidForSubArch(TT.getSubArch(), CPUName))
841 return std::nullopt;
842
843 // Parse the processor and its feature modifiers, then construct directly from
844 // the resulting fields.
845 GPUKind Arch = getGPUKindFromTargetID(TT, ProcAndFeatures);
846 TargetIDSetting XnackSetting, SramEccSetting;
847 if (!computeTargetIDFeatures(Arch, ProcAndFeatures, XnackSetting,
848 SramEccSetting))
849 return std::nullopt;
850
851 return TargetID(Arch, TT, XnackSetting, SramEccSetting);
852}
853
854std::optional<TargetID>
856 // Split on '-' to get arch-vendor-os-environment-processor:features. There is
857 // a single dash separator after the 4-component triple, so the
858 // processor+features field must be present (even if empty).
860 TargetIDDirective.split(Parts, '-', /*MaxSplit=*/4);
861 if (Parts.size() < 5)
862 return std::nullopt;
863
864 return parse(Triple(Parts[0], Parts[1], Parts[2], Parts[3]), Parts[4]);
865}
866
867// Returns true if \p Arch hardwires xnack on (supports xnack but has no on/off
868// modes, e.g. gfx1250), so xnack is not a selectable target-id modifier.
869static bool isXnackHardwiredOn(GPUKind Arch) {
870 const AMDGPUFeatureBitset &Features = getFeatureBitset(Arch);
871 return Features.test(FEAT_XNACK_SUPPORT) &&
872 !Features.test(FEAT_XNACK_ON_OFF_MODES);
873}
874
875// Append the explicit (On/Off) sramecc/xnack feature modifiers in canonical
876// order, e.g. ":sramecc-:xnack+". Xnack is never emitted for hardwired-on
877// targets.
879 TargetIDSetting Xnack,
880 bool XnackHardwiredOn) {
881 if (SramEcc == TargetIDSetting::Off)
882 OS << ":sramecc-";
883 else if (SramEcc == TargetIDSetting::On)
884 OS << ":sramecc+";
885
886 if (XnackHardwiredOn)
887 return;
888
889 if (Xnack == TargetIDSetting::Off)
890 OS << ":xnack-";
891 else if (Xnack == TargetIDSetting::On)
892 OS << ":xnack+";
893}
894
895void TargetID::print(raw_ostream &StreamRep) const {
896 StreamRep << TargetTripleString << '-' << getArchNameAMDGCN(Arch);
897
898 if (IsAMDHSA) {
900 isXnackHardwiredOn(Arch));
901 }
902}
903
904std::string TargetID::toString() const {
905 std::string Str;
906 raw_string_ostream OS(Str);
907 OS << *this;
908 return Str;
909}
910
916
918 std::string Str;
919 raw_string_ostream OS(Str);
921 return Str;
922}
923
925 return Arch == Other.Arch && XnackSetting == Other.XnackSetting &&
926 SramEccSetting == Other.SramEccSetting && IsAMDHSA == Other.IsAMDHSA &&
927 TargetTripleString == Other.TargetTripleString;
928}
929
931 TargetIDSetting Requested) {
932 return Provided == TargetIDSetting::Any ||
933 Provided == TargetIDSetting::Unsupported || Provided == Requested;
934}
935
937 // The processor and feature settings must match exactly
938 if (Arch != Other.Arch || XnackSetting != Other.XnackSetting ||
939 SramEccSetting != Other.SramEccSetting)
940 return false;
941
943 .isCompatibleWith(Triple(Other.getTargetTripleString()));
944}
945
947 // A major-family/generic processor (e.g. amdgpu9) provides for a specific
948 // member of its family (e.g. gfx900), but not the reverse. Otherwise the
949 // processors must match.
950 if (Arch != Other.Arch && Arch != GK_NONE && Other.Arch != GK_NONE) {
951 Triple::SubArchType ThisSubArch = getSubArch(Arch);
952 if (ThisSubArch != getMajorSubArch(ThisSubArch) ||
953 ThisSubArch != getMajorSubArch(getSubArch(Other.Arch)))
954 return false;
955 }
956
957 if (!featureProvidesFor(XnackSetting, Other.XnackSetting) ||
958 !featureProvidesFor(SramEccSetting, Other.SramEccSetting))
959 return false;
960
962 .isCompatibleWith(Triple(Other.getTargetTripleString()));
963}
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 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 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.