21#include "llvm/Support/Regex.h"
24#define GEN_PASS_DEF_GPUROCDLATTACHTARGET
25#include "mlir/Dialect/GPU/Transforms/ROCDL/Passes.h.inc"
32struct ROCDLAttachTarget
33 :
public impl::GpuROCDLAttachTargetBase<ROCDLAttachTarget> {
36 DictionaryAttr
getFlags(OpBuilder &builder,
bool isWave64)
const;
38 void runOnOperation()
override;
40 void getDependentDialects(DialectRegistry ®istry)
const override {
41 registry.
insert<ROCDL::ROCDLDialect>();
46DictionaryAttr ROCDLAttachTarget::getFlags(
OpBuilder &builder,
47 bool isWave64)
const {
49 SmallVector<NamedAttribute, 6> flags;
50 auto addFlag = [&](StringRef flag) {
60 addFlag(
"finite_only");
62 addFlag(
"unsafe_math");
64 addFlag(
"unsafe_sqrt");
70void ROCDLAttachTarget::runOnOperation() {
76 std::string resolvedTriple = triple;
77 std::string resolvedChip = chip;
78 std::string resolvedFeatures = features;
82 bool resolvedWave64 = waveSize != 32;
85 std::optional<ROCDL::TargetInfo> targetInfo;
87 std::optional<llvm::AMDGPU::TargetID>
id =
91 <<
"'" << arch <<
"' is not a valid AMDGPU architecture";
92 return signalPassFailure();
96 if (id->getGPUKind() == llvm::AMDGPU::GK_NONE) {
99 <<
"' names no GPU; a chip is required to attach a "
101 return signalPassFailure();
104 llvm::Triple parsed(id->getTargetTripleString());
106 StringRef archName = parsed.getArchName();
107 llvm::Triple::SubArchType subArch =
108 llvm::AMDGPU::getSubArch(id->getGPUKind());
109 if (StringRef subArchName = llvm::AMDGPU::getSubArchName(subArch);
110 !subArchName.empty())
111 archName = subArchName;
116 (parsed.getEnvironment() == llvm::Triple::UnknownEnvironment
117 ? llvm::Triple(archName, parsed.getVendorName(),
119 : llvm::Triple(archName, parsed.getVendorName(),
120 parsed.getOSName(), parsed.getEnvironmentName()))
122 resolvedChip = llvm::AMDGPU::getArchNameAMDGCN(id->getGPUKind()).str();
126 FailureOr<ROCDL::TargetInfo> info =
131 return signalPassFailure();
133 resolvedWave64 = info->getWavefrontSize() == 64;
138 if (info->supportsBothWavefrontSizes()) {
139 if (!resolvedFeatures.empty())
140 resolvedFeatures +=
",";
142 resolvedWave64 ?
"+wavefrontsize64" :
"+wavefrontsize32";
144 }
else if (waveSize != 0 && waveSize != 32 && waveSize != 64) {
146 <<
"wavefront size must be 32 or 64, got " << waveSize;
147 return signalPassFailure();
150 ArrayRef<std::string> libs(linkLibs);
151 SmallVector<StringRef> filesToLink(libs);
153 optLevel, resolvedTriple, resolvedChip, resolvedFeatures, abiVersion,
156 llvm::Regex matcher(moduleMatcher);
157 for (Region ®ion : getOperation()->getRegions())
158 for (
Block &block : region.getBlocks())
159 for (
auto module : block.getOps<gpu::GPUModuleOp>()) {
161 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
164 SmallVector<Attribute> targets;
165 if (std::optional<ArrayAttr> attrs = module.getTargets())
166 targets.append(attrs->getValue().begin(), attrs->getValue().end());
167 targets.push_back(
target);
169 targets.erase(llvm::unique(targets), targets.end());
171 module.setTargetsAttr(builder.getArrayAttr(targets));
173 targetInfo->migrateArchFeaturesToModuleFlags(module);
static arith::FastMathFlags getFlags(arith::FastMathFlagsAttr fastmath)
Returns the flags held by fastmath, or none if it is null.
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
NamedAttribute getNamedAttr(StringRef name, Attribute val)
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
This class helps build Operations.
static FailureOr< TargetInfo > get(StringRef arch, unsigned waveSize=0, function_ref< InFlightDiagnostic()> emitError=nullptr)
Resolves a target description.
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...
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.