| 1 | //===-- IntelGPUTargetParser.h - Parser for Intel GPU targets ---*- 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 names the Intel GPU a driver reports, and finds the Intel GPU a |
| 10 | // user names. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #ifndef LLVM_TARGETPARSER_INTELGPUTARGETPARSER_H |
| 15 | #define LLVM_TARGETPARSER_INTELGPUTARGETPARSER_H |
| 16 | |
| 17 | #include "llvm/ADT/STLForwardCompat.h" |
| 18 | #include "llvm/ADT/StringRef.h" |
| 19 | #include "llvm/Support/Compiler.h" |
| 20 | #include <cstdint> |
| 21 | #include <string> |
| 22 | |
| 23 | namespace llvm { |
| 24 | |
| 25 | namespace IntelGPU { |
| 26 | |
| 27 | /// Intel GPU architecture names, covering both physical devices and the |
| 28 | /// compatibility names that stand for a whole product line. |
| 29 | /// |
| 30 | /// The underlying type is fixed because clang/Basic/OffloadArch.h forward |
| 31 | /// declares this enumeration; the two declarations have to agree. |
| 32 | enum GPUKind : uint8_t { |
| 33 | GK_NONE = 0, |
| 34 | #define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \ |
| 35 | GK_##KIND, |
| 36 | #define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) GK_##KIND, |
| 37 | #include "llvm/TargetParser/IntelGPUTargetParser.def" |
| 38 | }; |
| 39 | |
| 40 | /// Represents an IGCA feature set, including whether or not it is exact. The |
| 41 | /// values are bitfields where the lowest bit represents exact. See IGCATarget |
| 42 | /// for the packed representation. |
| 43 | enum class IGCAFeatureSet : uint8_t { |
| 44 | Core = 0b000, |
| 45 | Compute = 0b010, |
| 46 | ComputeExact = 0b011, |
| 47 | Render = 0b100, |
| 48 | RenderExact = 0b101, |
| 49 | }; |
| 50 | |
| 51 | /// Wrapper around an IGCA target's uint32_t representation, packed using the |
| 52 | /// following format: |
| 53 | /// |
| 54 | /// 31 16 15 3 2 0 |
| 55 | /// +------------------+------------------------+---------------+ |
| 56 | /// | Target | Reserved | Feature set | |
| 57 | /// +------------------+------------------------+---------------+ |
| 58 | /// 16 bits 13 bits 3 bits |
| 59 | /// |
| 60 | /// Note that bit 0 is a flag that represents if the feature set is "exact". |
| 61 | /// Only IGCA targets that some device in IntelGPUTargetParser.def can run are |
| 62 | /// valid; any other value is stored as invalid(), whose packed representation |
| 63 | /// is 0. |
| 64 | class IGCATarget { |
| 65 | uint32_t V = 0; |
| 66 | |
| 67 | // Positions of the fields in V; see the bit layout above the class. |
| 68 | static constexpr uint32_t TargetShift = 16; |
| 69 | static constexpr uint32_t FeatureSetMask = 0x7; |
| 70 | static constexpr uint32_t IsExactMask = 0x1; |
| 71 | /// Bits 3-15, which no IGCA target uses. |
| 72 | static constexpr uint32_t ReservedMask = |
| 73 | ((uint32_t(1) << TargetShift) - 1) & ~FeatureSetMask; |
| 74 | |
| 75 | /// \return true if some device in IntelGPUTargetParser.def with the same |
| 76 | /// numeric IGCA target can run code built for the IGCA target in \p Packed. |
| 77 | LLVM_ABI static bool isKnownIGCATarget(uint32_t Packed); |
| 78 | |
| 79 | /// Creates an invalid IGCA target, whose packed representation is 0, for |
| 80 | /// invalid(). |
| 81 | constexpr IGCATarget() = default; |
| 82 | |
| 83 | explicit IGCATarget(uint32_t Packed) { |
| 84 | // IGCA targets that no device can run (including Target == 0) are |
| 85 | // invalid, so V remains 0. This keeps V == 0 as the only canonical invalid |
| 86 | // representation. |
| 87 | if (isKnownIGCATarget(Packed)) |
| 88 | V = Packed; |
| 89 | } |
| 90 | |
| 91 | public: |
| 92 | IGCATarget(uint16_t Target, IGCAFeatureSet FeatureSet) |
| 93 | : IGCATarget(uint32_t(Target) << TargetShift | |
| 94 | (llvm::to_underlying(E: FeatureSet) & FeatureSetMask)) {} |
| 95 | |
| 96 | /// \return an invalid IGCATarget (whose packed representation is 0). |
| 97 | static constexpr IGCATarget invalid() { return IGCATarget(); } |
| 98 | |
| 99 | /// \return the packed uint32_t representation of the IGCA target. |
| 100 | constexpr uint32_t pack() const { return V; } |
| 101 | /// Wrap a packed uint32_t IGCA target with an IGCATarget class. |
| 102 | static IGCATarget unpack(uint32_t V) { return IGCATarget(V); } |
| 103 | |
| 104 | constexpr uint16_t getTarget() const { return uint16_t(V >> TargetShift); } |
| 105 | /// \return the feature set, including whether it is exact. |
| 106 | /// |
| 107 | /// To check if a target has a feature set regardless of exactness, use |
| 108 | /// isAnyCompute() or isAnyRender() instead of raw comparison between |
| 109 | /// IGCAFeatureSet enums. |
| 110 | constexpr IGCAFeatureSet getFeatureSet() const { |
| 111 | return IGCAFeatureSet(V & FeatureSetMask); |
| 112 | } |
| 113 | constexpr bool isExact() const { return V & IsExactMask; } |
| 114 | constexpr bool isValid() const { return getTarget() != 0; } |
| 115 | constexpr explicit operator bool() const { return isValid(); } |
| 116 | |
| 117 | /// \return true if the target has the Core feature set. |
| 118 | constexpr bool isCore() const { |
| 119 | // invalid() has the same feature set bits as Core, so we check explicitly. |
| 120 | return isValid() && getFeatureSet() == IGCAFeatureSet::Core; |
| 121 | } |
| 122 | constexpr bool isComputeExact() const { |
| 123 | return getFeatureSet() == IGCAFeatureSet::ComputeExact; |
| 124 | } |
| 125 | constexpr bool isRenderExact() const { |
| 126 | return getFeatureSet() == IGCAFeatureSet::RenderExact; |
| 127 | } |
| 128 | /// \return true if target has either Compute or ComputeExact feature set. |
| 129 | constexpr bool isAnyCompute() const { |
| 130 | return getFeatureSet() == IGCAFeatureSet::Compute || |
| 131 | getFeatureSet() == IGCAFeatureSet::ComputeExact; |
| 132 | } |
| 133 | /// \return true if target has either Render or RenderExact feature set. |
| 134 | constexpr bool isAnyRender() const { |
| 135 | return getFeatureSet() == IGCAFeatureSet::Render || |
| 136 | getFeatureSet() == IGCAFeatureSet::RenderExact; |
| 137 | } |
| 138 | |
| 139 | friend constexpr bool operator==(IGCATarget A, IGCATarget B) { |
| 140 | return A.V == B.V; |
| 141 | } |
| 142 | friend constexpr bool operator!=(IGCATarget A, IGCATarget B) { |
| 143 | return !(A == B); |
| 144 | } |
| 145 | }; |
| 146 | |
| 147 | /// \return the device name that \p GPUIPVersion identifies, as reported by the |
| 148 | /// driver, e.g. "xe-pvc". If the table lists no such device, return an empty |
| 149 | /// string. Only the major and minor versions are looked at, so every revision |
| 150 | /// of a device resolves to the same name. |
| 151 | LLVM_ABI StringRef getArchName(uint32_t GPUIPVersion); |
| 152 | |
| 153 | /// \return the human-friendly name of \p Kind, e.g. "xe-pvc", or an empty |
| 154 | /// string for GK_NONE. |
| 155 | LLVM_ABI StringRef getArchName(GPUKind Kind); |
| 156 | |
| 157 | /// \return the numeric name of \p GPUIPVersion, e.g. "xe_35.11.0". |
| 158 | LLVM_ABI std::string getNumericArchName(uint32_t GPUIPVersion); |
| 159 | |
| 160 | /// \return the device \p Name denotes, or GK_NONE for a name this build does |
| 161 | /// not know. |
| 162 | /// |
| 163 | /// \p Name may be a human-friendly name ("xe-pvc"), a compatibility name |
| 164 | /// ("xe-dg2"), an alias ("xe-arl-u") or a numeric name ("xe_12.60.7"). A |
| 165 | /// numeric name must end in a revision, which may be any number and is ignored, |
| 166 | /// so "xe_12.60.0" and "xe_12.60.7" denote the same device. |
| 167 | LLVM_ABI GPUKind parseArch(StringRef Name); |
| 168 | |
| 169 | /// Parse an IGCA target string such as "igca_20ca". |
| 170 | /// |
| 171 | /// \return an invalid IGCATarget if no device in IntelGPUTargetParser.def can |
| 172 | /// run \p MaybeTarget, e.g. "igca_10c". |
| 173 | LLVM_ABI IGCATarget parseIGCATarget(StringRef MaybeTarget); |
| 174 | |
| 175 | /// \return the \p Target as a string, e.g. "igca_20ca", or an empty string if |
| 176 | /// \p Target is invalid. |
| 177 | LLVM_ABI StringRef getIGCATargetName(IGCATarget Target); |
| 178 | |
| 179 | /// \return the IGCA target of \p Kind, e.g. igca_20ca for xe-pvc, or an |
| 180 | /// invalid IGCATarget for GK_NONE. |
| 181 | LLVM_ABI IGCATarget getIGCATarget(GPUKind Kind); |
| 182 | |
| 183 | } // namespace IntelGPU |
| 184 | } // namespace llvm |
| 185 | |
| 186 | #endif // LLVM_TARGETPARSER_INTELGPUTARGETPARSER_H |
| 187 | |