13#include "llvm/ADT/SmallVector.h"
14#include "llvm/ADT/StringRef.h"
15#include "llvm/ADT/Twine.h"
20namespace AMDGPU = ::llvm::AMDGPU;
26 const Twine &message) {
35 arch.split(parts,
'-', 4);
36 if (parts.size() == 5)
37 return AMDGPU::TargetID::parseTargetIDString(arch);
40 Triple triple(Triple::normalize(arch));
41 if (triple.isAMDGCN())
42 return AMDGPU::TargetID::parse(triple,
"");
46 return AMDGPU::TargetID::parse(Triple(
"amdgcn-amd-amdhsa"), arch);
55 bool targetWave32 = bits.test(AMDGPU::FEAT_WAVEFRONTSIZE32);
56 bool targetWave64 = bits.test(AMDGPU::FEAT_WAVEFRONTSIZE64);
61 if (!targetWave32 && !targetWave64)
62 bits.set(AMDGPU::FEAT_WAVEFRONTSIZE32);
66 return fail(
emitError,
"target only supports a wavefront size of 64");
67 bits.set(AMDGPU::FEAT_WAVEFRONTSIZE32);
71 return fail(
emitError,
"target only supports a wavefront size of 32");
72 bits.set(AMDGPU::FEAT_WAVEFRONTSIZE64);
76 "wavefront size must be 32 or 64, got " + Twine(waveSize));
84 return fail(
emitError,
"target architecture cannot be empty");
89 "' is not a valid AMDGPU architecture: expected "
90 "a GPU name, a triple, or a target ID");
93 info.kind =
id->getGPUKind();
94 info.subArch = AMDGPU::getSubArch(info.kind);
95 info.featureBits = AMDGPU::getFeatureBitset(info.kind);
96 info.xnackSetting =
id->getXnackSetting();
97 info.sramEccSetting =
id->getSramEccSetting();
101 info.dualWavefrontSize =
103 !info.featureBits.test(AMDGPU::FEAT_WAVEFRONTSIZE32) &&
104 !info.featureBits.test(AMDGPU::FEAT_WAVEFRONTSIZE64);
106 if (info.
isUnknown() && waveSize != 0 && waveSize != 32 && waveSize != 64)
108 "wavefront size must be 32 or 64, got " + Twine(waveSize));
117 StringRef deprecatedAlias) {
118 if (!arch.empty() && arch !=
"invalid")
120 return deprecatedAlias.empty() ? arch : deprecatedAlias;
128 auto hasGen = [&](
unsigned gen) {
131 return has(AMDGPU::FEAT_CI_INSTS);
133 return has(AMDGPU::FEAT_GFX8_INSTS);
135 return has(AMDGPU::FEAT_GFX9_INSTS);
137 return has(AMDGPU::FEAT_GFX10_INSTS);
139 return has(AMDGPU::FEAT_GFX11_INSTS);
141 return has(AMDGPU::FEAT_GFX12_INSTS);
143 return has(AMDGPU::FEAT_GFX13_INSTS);
154 return hasGen(major) && !hasGen(major + 1);
158 return AMDGPU::getBufferResourceNumRecordsWidth(kind);
164 return AMDGPU::getMaxHWAddressableLocalMemorySize(kind);
170 return AMDGPU::getTotalNumSGPRs(kind);
176 return AMDGPU::getAddressableNumSGPRs(kind);
182 return AMDGPU::getSGPRAllocGranule(kind);
190 return AMDGPU::getVGPRAllocGranule(kind, *waveSize == 32);
196 return AMDGPU::getLDSBankCount(kind);
202 return AMDGPU::getMaxWavesPerEU(kind);
206 if (
has(AMDGPU::FEAT_WAVEFRONTSIZE64))
208 if (
has(AMDGPU::FEAT_WAVEFRONTSIZE32))
215 "xnack and sramecc describe a whole code object, so they can only be "
216 "recorded on a module");
217 ROCDLDialect *dialect =
221 auto migrate = [&](AMDGPU::TargetIDSetting setting,
auto helper) {
222 if (setting != AMDGPU::TargetIDSetting::On &&
223 setting != AMDGPU::TargetIDSetting::Off)
226 builder.getBoolAttr(setting == AMDGPU::TargetIDSetting::On));
228 migrate(xnackSetting, dialect->getXnackAttrHelper());
229 migrate(sramEccSetting, dialect->getSrameccAttrHelper());
233 return AMDGPU::getIsaVersion(subArch);
237 return AMDGPU::getArchNameAMDGCN(kind);
241 return !
isUnknown() && AMDGPU::getMajorSubArch(subArch) == subArch;
static LogicalResult fail(function_ref< InFlightDiagnostic()> emitError, const Twine &message)
Reports message through emitError if it is non-null, and returns failure.
static LogicalResult resolveWavefrontSize(AMDGPU::AMDGPUFeatureBitset &bits, unsigned waveSize, function_ref< InFlightDiagnostic()> emitError)
Pins the wavefront size in bits, mirroring the policy LLVM applies in fillAMDGCNFeatureMap: a target ...
This class is a general helper class for creating context-global objects like types,...
This class represents a diagnostic that is inflight and set to be reported.
T * getOrLoadDialect()
Get (or create) a dialect for the given derived dialect type.
Operation is the basic unit of execution within MLIR.
MLIRContext * getContext()
Return the context this operation is associated with.
bool has(Feature feature) const
Returns whether the target has feature.
std::optional< unsigned > getTotalNumSGPRs() const
Returns the total number of SGPRs, or nullopt for an unknown target.
std::optional< unsigned > getSGPRAllocGranule() const
Returns the SGPR allocation granularity in registers, or nullopt for an unknown target.
std::optional< unsigned > getAddressableNumSGPRs() const
Returns the number of SGPRs addressable by a kernel, or nullopt for an unknown target.
bool isGeneric() const
Returns whether this is a "gfxN-generic" target, which carries only the features common to every GPU ...
::llvm::AMDGPU::IsaVersion getIsaVersion() const
Returns the ISA version.
bool isGeneration(unsigned major) const
Returns whether the target belongs to gfx generation major (9 for any gfx9xx, 12 for any gfx12xx,...
StringRef getArchName() const
Returns the canonical GPU name ("gfx942", "gfx9-4-generic"), or "" if the target is unknown.
std::optional< unsigned > getBufferResourceNumRecordsWidth() const
Returns the width in bits of the num_records field of the buffer resource (V#), or nullopt for an unk...
bool isUnknown() const
Returns whether no GPU was identified, in which case every feature query answers false.
std::optional< unsigned > getLDSBankCount() const
Returns the number of LDS banks per compute unit, or nullopt for an unknown target.
void migrateArchFeaturesToModuleFlags(Operation *op) const
Records the xnack and sramecc settings this target's ID pinned onto the module op,...
std::optional< unsigned > getWavefrontSize() const
Returns the wavefront size, or nullopt for an unknown target.
static FailureOr< TargetInfo > get(StringRef arch, unsigned waveSize=0, function_ref< InFlightDiagnostic()> emitError=nullptr)
Resolves a target description.
std::optional< unsigned > getMaxAddressableLocalMemorySize() const
Returns the maximum LDS in bytes a single workgroup can address, or nullopt for an unknown target.
std::optional< unsigned > getMaxWavesPerEU() const
Returns the maximum number of waves per execution unit, ignoring any limits a particular kernel impos...
TargetInfo()=default
Constructs an unknown target: no subarch, and every feature query answers false.
std::optional< unsigned > getVGPRAllocGranule() const
Returns the VGPR allocation granularity in registers, or nullopt for an unknown target.
static std::optional<::llvm::AMDGPU::TargetID > parseTargetID(StringRef arch)
Parses arch into a target ID, accepting the spellings get() documents, or returns nullopt if it names...
bool satisfiesLLVMModule(Operation *op)
LLVM requires some operations to be inside of a Module operation.
StringRef resolveArchOption(StringRef arch, StringRef deprecatedAlias)
Returns the target architecture that a pass should parse, given its arch option and the value of the ...
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.
llvm::function_ref< Fn > function_ref