MLIR 22.0.0git
XeVMAttachTarget.cpp
Go to the documentation of this file.
1//===-- XeVMAttachTarget.cpp - Attach an XeVM target ----------------------===//
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
15
18#include "mlir/IR/Builders.h"
19#include "mlir/Pass/Pass.h"
21#include "llvm/Support/Regex.h"
22
23namespace mlir {
24#define GEN_PASS_DEF_GPUXEVMATTACHTARGET
25#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
26} // namespace mlir
27
28using namespace mlir;
29using namespace mlir::xevm;
30
31namespace {
32struct XeVMAttachTarget
33 : public mlir::impl::GpuXeVMAttachTargetBase<XeVMAttachTarget> {
34 using Base::Base;
35
36 DictionaryAttr getFlags(OpBuilder &builder) const;
37
38 void runOnOperation() override;
39
40 void getDependentDialects(DialectRegistry &registry) const override {
41 registry.insert<xevm::XeVMDialect>();
42 }
43};
44} // namespace
45
46DictionaryAttr XeVMAttachTarget::getFlags(OpBuilder &builder) const {
47 SmallVector<NamedAttribute, 3> flags;
48 // Tokenize and set the optional command line options.
49 if (!cmdOptions.empty()) {
50 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options =
52 if (!options.second.empty()) {
53 llvm::SmallVector<mlir::Attribute> xevmOptionAttrs;
54 for (const char *opt : options.second) {
55 xevmOptionAttrs.emplace_back(
56 mlir::StringAttr::get(builder.getContext(), StringRef(opt)));
57 }
58 flags.push_back(builder.getNamedAttr(
59 "cmd-options",
60 mlir::ArrayAttr::get(builder.getContext(), xevmOptionAttrs)));
61 }
62 }
63
64 if (!flags.empty())
65 return builder.getDictionaryAttr(flags);
66 return nullptr;
67}
68
69void XeVMAttachTarget::runOnOperation() {
70 OpBuilder builder(&getContext());
71 ArrayRef<std::string> libs(linkLibs);
72 SmallVector<StringRef> filesToLink(libs);
73 auto target = builder.getAttr<xevm::XeVMTargetAttr>(
74 optLevel, triple, chip, getFlags(builder),
75 filesToLink.empty() ? nullptr : builder.getStrArrayAttr(filesToLink));
76 llvm::Regex matcher(moduleMatcher);
77 for (Region &region : getOperation()->getRegions())
78 for (Block &block : region.getBlocks())
79 for (auto module : block.getOps<gpu::GPUModuleOp>()) {
80 // Check if the name of the module matches.
81 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
82 continue;
83 // Create the target array.
84 SmallVector<Attribute> targets;
85 if (std::optional<ArrayAttr> attrs = module.getTargets())
86 targets.append(attrs->getValue().begin(), attrs->getValue().end());
87 targets.push_back(target);
88 // Remove any duplicate targets.
89 targets.erase(llvm::unique(targets), targets.end());
90 // Update the target attribute array.
91 module.setTargetsAttr(builder.getArrayAttr(targets));
92 }
93}
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
MLIRContext * getContext() const
Definition Builders.h:56
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:104
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition Builders.cpp:94
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition Builders.cpp:306
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition Builders.h:98
This class helps build Operations.
Definition Builders.h:207
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeCmdOptions() const
Returns a tokenization of the command line options.
Include the generated interface declarations.