LLVM 24.0.0git
IntelGPUTargetParser.cpp
Go to the documentation of this file.
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
14#include "llvm/ADT/Enum.h"
15#include "llvm/ADT/STLExtras.h"
18#include "llvm/ADT/Twine.h"
20#include <cassert>
21
22using namespace llvm;
23using namespace IntelGPU;
24
25// A GPU IP version packs four fields, from the most significant bit down:
26//
27// 31 22 21 14 13 6 5 0
28// +---------------+----------+----------+----------+
29// | major | minor | reserved | revision |
30// +---------------+----------+----------+----------+
31// 10 bits 8 bits 8 bits 6 bits
32//
33// The reserved bits carry no information.
34static constexpr uint32_t GPUIPMajorShift = 22;
35static constexpr uint32_t GPUIPMinorShift = 14;
36[[maybe_unused]] static constexpr uint32_t GPUIPMajorMask = 0x3ff;
37static constexpr uint32_t GPUIPMinorMask = 0xff;
38static constexpr uint32_t GPUIPRevisionMask = 0x3f;
39
40// The bits that identify a device: the major and the minor version. Neither the
41// revision nor the reserved bits take part in the lookup, because every
42// revision of a device is one device as far as the compiler is concerned.
43static constexpr uint32_t GPUIPDeviceMask = ~0u << GPUIPMinorShift;
44
45// Pack a major and a minor version the way a GPU IP version does, so that a
46// row of the table can be compared against a reported version as it is. A value
47// too wide for its field would silently corrupt the fields above it, which
48// would mean a typo in IntelGPUTargetParser.def going unnoticed.
49static constexpr uint32_t packDevice(uint32_t Major, uint32_t Minor) {
50 assert((Major & ~GPUIPMajorMask) == 0 && "major version too wide");
51 assert((Minor & ~GPUIPMinorMask) == 0 && "minor version too wide");
52 return (Major << GPUIPMajorShift) | (Minor << GPUIPMinorShift);
53}
54
56 const uint32_t Device = GPUIPVersion & GPUIPDeviceMask;
57#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \
58 if (Device == packDevice(MAJOR, MINOR)) \
59 return NAME;
60#include "llvm/TargetParser/IntelGPUTargetParser.def"
61 return "";
62}
63
65 const uint32_t Major = GPUIPVersion >> GPUIPMajorShift;
66 const uint32_t Minor = (GPUIPVersion >> GPUIPMinorShift) & GPUIPMinorMask;
67 const uint32_t Revision = GPUIPVersion & GPUIPRevisionMask;
68 return ("xe_" + Twine(Major) + "." + Twine(Minor) + "." + Twine(Revision))
69 .str();
70}
71
73 switch (Kind) {
74 case GK_NONE:
75 return "";
76#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \
77 case GK_##KIND: \
78 return NAME;
79#define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \
80 case GK_##KIND: \
81 return NAME;
82#include "llvm/TargetParser/IntelGPUTargetParser.def"
83 }
84 llvm_unreachable("invalid Intel GPU kind");
85}
86
88 // A numeric name ends in a revision, e.g. "xe_12.60.7". Only the major and
89 // minor versions name a device, so the revision may be any number and is not
90 // looked up.
91 if (Name.starts_with("xe_")) {
92 auto [Device, Revision] = Name.rsplit('.');
93 if (Device.count('.') != 1 || Revision.empty() ||
94 !all_of(Revision, isDigit))
95 return GK_NONE;
96 Name = Device;
97 }
98
99 return StringSwitch<GPUKind>(Name)
100#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \
101 .Case(NAME, GK_##KIND).Case("xe_" #MAJOR "." #MINOR, GK_##KIND)
102#define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \
103 .Case(NAME, GK_##KIND)
104#define INTEL_GPU_ALIAS(NAME, KIND) .Case(NAME, GK_##KIND)
105#include "llvm/TargetParser/IntelGPUTargetParser.def"
107}
108
109namespace {
110// The IGCA target of each row in IntelGPUTargetParser.def, e.g. {20,
111// ComputeExact} for xe-pvc. Several rows can have the same IGCA target, e.g.
112// the nine igca_10r devices, so entries repeat.
113struct DeviceIGCATarget {
114 uint16_t Target;
115 IGCAFeatureSet FeatureSet;
116};
117constexpr DeviceIGCATarget DeviceIGCATargets[] = {
118#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \
119 {IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS},
120#define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \
121 {IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS},
122#include "llvm/TargetParser/IntelGPUTargetParser.def"
123};
124
125// All five possible names for each device's numeric IGCA target, e.g. igca_20,
126// igca_20c, igca_20ca, igca_20r and igca_20ra for xe-pvc. Which of them are
127// valid is decided by the IGCATarget constructor, so the rule is in one place.
128// The names are string literals, so getIGCATargetName() can return them.
129#define IGCA_TARGET_NAMES(TARGET) \
130 {{"igca_" #TARGET}, {TARGET, IGCAFeatureSet::Core}}, \
131 {{"igca_" #TARGET "c"}, {TARGET, IGCAFeatureSet::Compute}}, \
132 {{"igca_" #TARGET "ca"}, {TARGET, IGCAFeatureSet::ComputeExact}}, \
133 {{"igca_" #TARGET "r"}, {TARGET, IGCAFeatureSet::Render}}, \
134 {{"igca_" #TARGET "ra"}, {TARGET, IGCAFeatureSet::RenderExact}},
135constexpr EnumStringDef<DeviceIGCATarget> IGCATargetNameDefs[] = {
136#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \
137 IGCA_TARGET_NAMES(IGCA_TARGET)
138#define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \
139 IGCA_TARGET_NAMES(IGCA_TARGET)
140#include "llvm/TargetParser/IntelGPUTargetParser.def"
141};
142#undef IGCA_TARGET_NAMES
143constexpr auto IGCATargetNames = BUILD_ENUM_STRINGS(IGCATargetNameDefs);
144} // namespace
145
146/// \return true if a device with the IGCA feature set \p DeviceFeatureSet can
147/// run code built for the IGCA feature set \p RequestedFeatureSet at the
148/// device's own numeric IGCA target. For example, xe-pvc (igca_20ca,
149/// ComputeExact) can run code built for igca_20ca, igca_20c and igca_20, but
150/// not igca_20r.
151static bool deviceCanRunIGCAFeatureSet(IGCAFeatureSet DeviceFeatureSet,
152 IGCAFeatureSet RequestedFeatureSet) {
153 // Every device runs code built for the Core IGCA feature set, e.g. igca_20.
154 if (RequestedFeatureSet == IGCAFeatureSet::Core)
155 return true;
156 // A device runs code built for its own IGCA feature set, e.g. igca_20ca.
157 if (RequestedFeatureSet == DeviceFeatureSet)
158 return true;
159 // An exact IGCA feature set also runs code built for its non-exact form,
160 // e.g. igca_20c.
161 if (DeviceFeatureSet == IGCAFeatureSet::ComputeExact)
162 return RequestedFeatureSet == IGCAFeatureSet::Compute;
163 if (DeviceFeatureSet == IGCAFeatureSet::RenderExact)
164 return RequestedFeatureSet == IGCAFeatureSet::Render;
165 return false;
166}
167
168bool llvm::IntelGPU::IGCATarget::isKnownIGCATarget(uint32_t Packed) {
169 // A value with any of bits 3-15 set is not an IGCA target, e.g. igca_20c
170 // (0x00140002) with bit 3 set.
171 if (Packed & ReservedMask)
172 return false;
173 // Split the value into its numeric IGCA target and feature set, e.g. 20 and
174 // Compute for igca_20c.
175 uint16_t RequestedTarget = uint16_t(Packed >> TargetShift);
176 auto RequestedFeatureSet = IGCAFeatureSet(Packed & FeatureSetMask);
177 for (const DeviceIGCATarget &D : DeviceIGCATargets)
178 if (D.Target == RequestedTarget &&
179 deviceCanRunIGCAFeatureSet(D.FeatureSet, RequestedFeatureSet))
180 return true;
181 return false;
182}
183
185 for (const auto &E : IGCATargetNames)
186 if (MaybeTarget == E.name())
187 return IGCATarget(E.value().Target, E.value().FeatureSet);
188 return IGCATarget::invalid();
189}
190
192 for (const auto &E : IGCATargetNames)
193 if (E.value().Target == T.getTarget() &&
194 E.value().FeatureSet == T.getFeatureSet())
195 return E.name();
196 return "";
197}
198
200 switch (Kind) {
201 case GK_NONE:
202 return IGCATarget::invalid();
203#define INTEL_GPU(NAME, KIND, MAJOR, MINOR, IGCA_TARGET, IGCA_FEATURE_SETS) \
204 case GK_##KIND: \
205 return IGCATarget(IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS);
206#define INTEL_GPU_COMPAT(NAME, KIND, IGCA_TARGET, IGCA_FEATURE_SETS) \
207 case GK_##KIND: \
208 return IGCATarget(IGCA_TARGET, IGCAFeatureSet::IGCA_FEATURE_SETS);
209#include "llvm/TargetParser/IntelGPUTargetParser.def"
210 }
211 llvm_unreachable("invalid Intel GPU kind");
212}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
#define BUILD_ENUM_STRINGS(Tab)
Definition Enum.h:120
static constexpr uint32_t GPUIPMajorMask
static constexpr uint32_t GPUIPMajorShift
static constexpr uint32_t GPUIPRevisionMask
static constexpr uint32_t GPUIPMinorMask
static constexpr uint32_t packDevice(uint32_t Major, uint32_t Minor)
static constexpr uint32_t GPUIPMinorShift
static bool deviceCanRunIGCAFeatureSet(IGCAFeatureSet DeviceFeatureSet, IGCAFeatureSet RequestedFeatureSet)
static constexpr uint32_t GPUIPDeviceMask
#define T
This file contains some templates that are useful if you are working with the STL at all.
This file contains some functions that are useful when dealing with strings.
This file implements the StringSwitch template, which mimics a switch() statement whose cases are str...
Wrapper around an IGCA target's uint32_t representation, packed using the following format:
static constexpr IGCATarget invalid()
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
A switch()-like statement whose cases are string literals.
Target - Wrapper for Target specific information.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition Twine.h:82
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
LLVM_ABI GPUKind parseArch(StringRef Name)
LLVM_ABI std::string getNumericArchName(uint32_t GPUIPVersion)
LLVM_ABI IGCATarget getIGCATarget(GPUKind Kind)
IGCAFeatureSet
Represents an IGCA feature set, including whether or not it is exact.
LLVM_ABI IGCATarget parseIGCATarget(StringRef MaybeTarget)
Parse an IGCA target string such as "igca_20ca".
LLVM_ABI StringRef getIGCATargetName(IGCATarget Target)
LLVM_ABI StringRef getArchName(uint32_t GPUIPVersion)
GPUKind
Intel GPU architecture names, covering both physical devices and the compatibility names that stand f...
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1755
bool isDigit(char C)
Checks if character C is one of the 10 decimal digits.
Compile-time data representation of enum entries.
Definition Enum.h:47