| 1 | //===-- IntelGPUTargetParser - Parser for Intel GPU targets ---------------===// |
| 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 for the Intel GPU list. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "llvm/TargetParser/IntelGPUTargetParser.h" |
| 14 | #include "llvm/ADT/STLExtras.h" |
| 15 | #include "llvm/ADT/StringExtras.h" |
| 16 | #include "llvm/ADT/StringSwitch.h" |
| 17 | #include "llvm/ADT/Twine.h" |
| 18 | #include "llvm/Support/ErrorHandling.h" |
| 19 | #include <cassert> |
| 20 | |
| 21 | using namespace llvm; |
| 22 | using namespace IntelGPU; |
| 23 | |
| 24 | // A GPU IP version packs four fields, from the most significant bit down: |
| 25 | // |
| 26 | // 31 22 21 14 13 6 5 0 |
| 27 | // +---------------+----------+----------+----------+ |
| 28 | // | major | minor | reserved | revision | |
| 29 | // +---------------+----------+----------+----------+ |
| 30 | // 10 bits 8 bits 8 bits 6 bits |
| 31 | // |
| 32 | // The reserved bits carry no information. |
| 33 | static constexpr uint32_t GPUIPMajorShift = 22; |
| 34 | static constexpr uint32_t GPUIPMinorShift = 14; |
| 35 | [[maybe_unused]] static constexpr uint32_t GPUIPMajorMask = 0x3ff; |
| 36 | static constexpr uint32_t GPUIPMinorMask = 0xff; |
| 37 | static constexpr uint32_t GPUIPRevisionMask = 0x3f; |
| 38 | |
| 39 | // The bits that identify a device: the major and the minor version. Neither the |
| 40 | // revision nor the reserved bits take part in the lookup, because every |
| 41 | // revision of a device is one device as far as the compiler is concerned. |
| 42 | static constexpr uint32_t GPUIPDeviceMask = ~0u << GPUIPMinorShift; |
| 43 | |
| 44 | // Pack a major and a minor version the way a GPU IP version does, so that a |
| 45 | // row of the table can be compared against a reported version as it is. A value |
| 46 | // too wide for its field would silently corrupt the fields above it, which |
| 47 | // would mean a typo in IntelGPUTargetParser.def going unnoticed. |
| 48 | static constexpr uint32_t packDevice(uint32_t Major, uint32_t Minor) { |
| 49 | assert((Major & ~GPUIPMajorMask) == 0 && "major version too wide" ); |
| 50 | assert((Minor & ~GPUIPMinorMask) == 0 && "minor version too wide" ); |
| 51 | return (Major << GPUIPMajorShift) | (Minor << GPUIPMinorShift); |
| 52 | } |
| 53 | |
| 54 | StringRef llvm::IntelGPU::getArchName(uint32_t GPUIPVersion) { |
| 55 | const uint32_t Device = GPUIPVersion & GPUIPDeviceMask; |
| 56 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 57 | if (Device == packDevice(MAJOR, MINOR)) \ |
| 58 | return NAME; |
| 59 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 60 | return "" ; |
| 61 | } |
| 62 | |
| 63 | std::string llvm::IntelGPU::getNumericArchName(uint32_t GPUIPVersion) { |
| 64 | const uint32_t Major = GPUIPVersion >> GPUIPMajorShift; |
| 65 | const uint32_t Minor = (GPUIPVersion >> GPUIPMinorShift) & GPUIPMinorMask; |
| 66 | const uint32_t Revision = GPUIPVersion & GPUIPRevisionMask; |
| 67 | return ("xe_" + Twine(Major) + "." + Twine(Minor) + "." + Twine(Revision)) |
| 68 | .str(); |
| 69 | } |
| 70 | |
| 71 | StringRef llvm::IntelGPU::getArchName(GPUKind Kind) { |
| 72 | switch (Kind) { |
| 73 | case GK_NONE: |
| 74 | return "" ; |
| 75 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 76 | case GK_##KIND: \ |
| 77 | return NAME; |
| 78 | #define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 79 | case GK_##KIND: \ |
| 80 | return NAME; |
| 81 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 82 | } |
| 83 | llvm_unreachable("invalid Intel GPU kind" ); |
| 84 | } |
| 85 | |
| 86 | GPUKind llvm::IntelGPU::parseArch(StringRef Name) { |
| 87 | // A numeric name ends in a revision, e.g. "xe_12.60.7". Only the major and |
| 88 | // minor versions name a device, so the revision may be any number and is not |
| 89 | // looked up. |
| 90 | if (Name.starts_with(Prefix: "xe_" )) { |
| 91 | auto [Device, Revision] = Name.rsplit(Separator: '.'); |
| 92 | if (Device.count(C: '.') != 1 || Revision.empty() || |
| 93 | !all_of(Range&: Revision, P: isDigit)) |
| 94 | return GK_NONE; |
| 95 | Name = Device; |
| 96 | } |
| 97 | |
| 98 | return StringSwitch<GPUKind>(Name) |
| 99 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 100 | .Case(NAME, GK_##KIND).Case("xe_" #MAJOR "." #MINOR, GK_##KIND) |
| 101 | #define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 102 | .Case(NAME, GK_##KIND) |
| 103 | #define INTEL_GPU_ALIAS(NAME, KIND) .Case(NAME, GK_##KIND) |
| 104 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 105 | .Default(Value: GK_NONE); |
| 106 | } |
| 107 | |
| 108 | namespace { |
| 109 | // The IGCA target of each row in IntelGPUTargetParser.def, e.g. {20, |
| 110 | // ComputeExact} for xe-pvc. Several rows can have the same IGCA target, e.g. |
| 111 | // the nine igca_10r devices, so entries repeat. |
| 112 | struct DeviceIGCATarget { |
| 113 | uint16_t Target; |
| 114 | IGCAFeatureSet FeatureSet; |
| 115 | }; |
| 116 | constexpr DeviceIGCATarget DeviceIGCATargets[] = { |
| 117 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 118 | {IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS}, |
| 119 | #define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 120 | {IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS}, |
| 121 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 122 | }; |
| 123 | |
| 124 | // All five possible names for each device's numeric IGCA target, e.g. igca_20, |
| 125 | // igca_20c, igca_20ca, igca_20r and igca_20ra for xe-pvc. Which of them are |
| 126 | // valid is decided by the IGCATarget constructor, so the rule is in one place. |
| 127 | // The names are string literals, so getIGCATargetName() can return them. |
| 128 | struct IGCATargetNameEntry { |
| 129 | const char *Name; |
| 130 | uint16_t Target; |
| 131 | IGCAFeatureSet FeatureSet; |
| 132 | }; |
| 133 | #define IGCA_TARGET_NAMES(TARGET) \ |
| 134 | {"igca_" #TARGET, TARGET, IGCAFeatureSet::Core}, \ |
| 135 | {"igca_" #TARGET "c", TARGET, IGCAFeatureSet::Compute}, \ |
| 136 | {"igca_" #TARGET "ca", TARGET, IGCAFeatureSet::ComputeExact}, \ |
| 137 | {"igca_" #TARGET "r", TARGET, IGCAFeatureSet::Render}, \ |
| 138 | {"igca_" #TARGET "ra", TARGET, IGCAFeatureSet::RenderExact}, |
| 139 | constexpr IGCATargetNameEntry IGCATargetNames[] = { |
| 140 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 141 | IGCA_TARGET_NAMES(IGCA_TARGET) |
| 142 | #define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 143 | IGCA_TARGET_NAMES(IGCA_TARGET) |
| 144 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 145 | }; |
| 146 | #undef IGCA_TARGET_NAMES |
| 147 | } // namespace |
| 148 | |
| 149 | /// \return true if a device with the IGCA feature set \p DeviceFeatureSet can |
| 150 | /// run code built for the IGCA feature set \p RequestedFeatureSet at the |
| 151 | /// device's own numeric IGCA target. For example, xe-pvc (igca_20ca, |
| 152 | /// ComputeExact) can run code built for igca_20ca, igca_20c and igca_20, but |
| 153 | /// not igca_20r. |
| 154 | static bool deviceCanRunIGCAFeatureSet(IGCAFeatureSet DeviceFeatureSet, |
| 155 | IGCAFeatureSet RequestedFeatureSet) { |
| 156 | // Every device runs code built for the Core IGCA feature set, e.g. igca_20. |
| 157 | if (RequestedFeatureSet == IGCAFeatureSet::Core) |
| 158 | return true; |
| 159 | // A device runs code built for its own IGCA feature set, e.g. igca_20ca. |
| 160 | if (RequestedFeatureSet == DeviceFeatureSet) |
| 161 | return true; |
| 162 | // An exact IGCA feature set also runs code built for its non-exact form, |
| 163 | // e.g. igca_20c. |
| 164 | if (DeviceFeatureSet == IGCAFeatureSet::ComputeExact) |
| 165 | return RequestedFeatureSet == IGCAFeatureSet::Compute; |
| 166 | if (DeviceFeatureSet == IGCAFeatureSet::RenderExact) |
| 167 | return RequestedFeatureSet == IGCAFeatureSet::Render; |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | bool llvm::IntelGPU::IGCATarget::isKnownIGCATarget(uint32_t Packed) { |
| 172 | // A value with any of bits 3-15 set is not an IGCA target, e.g. igca_20c |
| 173 | // (0x00140002) with bit 3 set. |
| 174 | if (Packed & ReservedMask) |
| 175 | return false; |
| 176 | // Split the value into its numeric IGCA target and feature set, e.g. 20 and |
| 177 | // Compute for igca_20c. |
| 178 | uint16_t RequestedTarget = uint16_t(Packed >> TargetShift); |
| 179 | auto RequestedFeatureSet = IGCAFeatureSet(Packed & FeatureSetMask); |
| 180 | for (const DeviceIGCATarget &D : DeviceIGCATargets) |
| 181 | if (D.Target == RequestedTarget && |
| 182 | deviceCanRunIGCAFeatureSet(DeviceFeatureSet: D.FeatureSet, RequestedFeatureSet)) |
| 183 | return true; |
| 184 | return false; |
| 185 | } |
| 186 | |
| 187 | IGCATarget llvm::IntelGPU::parseIGCATarget(StringRef MaybeTarget) { |
| 188 | for (const IGCATargetNameEntry &E : IGCATargetNames) |
| 189 | if (MaybeTarget == E.Name) |
| 190 | return IGCATarget(E.Target, E.FeatureSet); |
| 191 | return IGCATarget::invalid(); |
| 192 | } |
| 193 | |
| 194 | StringRef llvm::IntelGPU::getIGCATargetName(IGCATarget T) { |
| 195 | for (const IGCATargetNameEntry &E : IGCATargetNames) |
| 196 | if (E.Target == T.getTarget() && E.FeatureSet == T.getFeatureSet()) |
| 197 | return E.Name; |
| 198 | return "" ; |
| 199 | } |
| 200 | |
| 201 | IGCATarget llvm::IntelGPU::getIGCATarget(GPUKind Kind) { |
| 202 | switch (Kind) { |
| 203 | case GK_NONE: |
| 204 | return IGCATarget::invalid(); |
| 205 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 206 | case GK_##KIND: \ |
| 207 | return IGCATarget(IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS); |
| 208 | #define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 209 | case GK_##KIND: \ |
| 210 | return IGCATarget(IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS); |
| 211 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 212 | } |
| 213 | llvm_unreachable("invalid Intel GPU kind" ); |
| 214 | } |
| 215 | |