|
| 1 | +//===-- XeVMAttachTarget.cpp - DESC -----------------------------*- C++ -*-===// |
| 2 | +// |
| 3 | +// This file is licensed 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 the `GpuXeVMAttachTarget` pass, attaching `#xevm.target` |
| 10 | +// attributes to GPU modules. |
| 11 | +// |
| 12 | +//===----------------------------------------------------------------------===// |
| 13 | + |
| 14 | +#include "mlir/Dialect/GPU/Transforms/Passes.h" |
| 15 | + |
| 16 | +#include "mlir/Dialect/GPU/IR/GPUDialect.h" |
| 17 | +#include "mlir/Dialect/LLVMIR/XeVMDialect.h" |
| 18 | +#include "mlir/IR/Builders.h" |
| 19 | +#include "mlir/Pass/Pass.h" |
| 20 | +#include "llvm/Support/Regex.h" |
| 21 | + |
| 22 | +namespace mlir { |
| 23 | +#define GEN_PASS_DEF_GPUXEVMATTACHTARGET |
| 24 | +#include "mlir/Dialect/GPU/Transforms/Passes.h.inc" |
| 25 | +} // namespace mlir |
| 26 | + |
| 27 | +using namespace mlir; |
| 28 | +using namespace mlir::xevm; |
| 29 | + |
| 30 | +namespace { |
| 31 | +struct XeVMAttachTarget |
| 32 | + : public mlir::impl::GpuXeVMAttachTargetBase<XeVMAttachTarget> { |
| 33 | + using Base::Base; |
| 34 | + |
| 35 | + DictionaryAttr getFlags(OpBuilder &builder) const; |
| 36 | + |
| 37 | + void runOnOperation() override; |
| 38 | + |
| 39 | + void getDependentDialects(DialectRegistry ®istry) const override { |
| 40 | + registry.insert<mlir::xevm::XeVMDialect>(); |
| 41 | + } |
| 42 | +}; |
| 43 | +} // namespace |
| 44 | + |
| 45 | +DictionaryAttr XeVMAttachTarget::getFlags(OpBuilder &builder) const { |
| 46 | + SmallVector<NamedAttribute, 3> flags; |
| 47 | + // Tokenize and set the optional command line options. |
| 48 | + if (!cmdOptions.empty()) { |
| 49 | + auto options = gpu::TargetOptions::tokenizeCmdOptions(cmdOptions); |
| 50 | + if (!options.second.empty()) { |
| 51 | + llvm::SmallVector<mlir::Attribute> xevmOptionAttrs; |
| 52 | + for (const char *opt : options.second) { |
| 53 | + xevmOptionAttrs.emplace_back( |
| 54 | + mlir::StringAttr::get(builder.getContext(), StringRef(opt))); |
| 55 | + } |
| 56 | + flags.push_back(builder.getNamedAttr( |
| 57 | + "cmd-options", |
| 58 | + mlir::ArrayAttr::get(builder.getContext(), xevmOptionAttrs))); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + if (!flags.empty()) |
| 63 | + return builder.getDictionaryAttr(flags); |
| 64 | + return nullptr; |
| 65 | +} |
| 66 | + |
| 67 | +void XeVMAttachTarget::runOnOperation() { |
| 68 | + OpBuilder builder(&getContext()); |
| 69 | + ArrayRef<std::string> libs(linkLibs); |
| 70 | + SmallVector<StringRef> filesToLink(libs); |
| 71 | + auto target = builder.getAttr<mlir::xevm::XeVMTargetAttr>( |
| 72 | + optLevel, triple, chip, getFlags(builder), |
| 73 | + filesToLink.empty() ? nullptr : builder.getStrArrayAttr(filesToLink)); |
| 74 | + llvm::Regex matcher(moduleMatcher); |
| 75 | + // Check if the name of the module matches. |
| 76 | + auto gpuModule = cast<gpu::GPUModuleOp>(getOperation()); |
| 77 | + if (!moduleMatcher.empty() && !matcher.match(gpuModule.getName())) |
| 78 | + return; |
| 79 | + // Create the target array. |
| 80 | + SmallVector<Attribute> targets; |
| 81 | + if (std::optional<ArrayAttr> attrs = gpuModule.getTargets()) |
| 82 | + targets.append(attrs->getValue().begin(), attrs->getValue().end()); |
| 83 | + targets.push_back(target); |
| 84 | + // Remove any duplicate targets. |
| 85 | + targets.erase(llvm::unique(targets), targets.end()); |
| 86 | + // Update the target attribute array. |
| 87 | + gpuModule.setTargetsAttr(builder.getArrayAttr(targets)); |
| 88 | +} |
0 commit comments