MLIR 24.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"
20#include "llvm/Support/Regex.h"
21
22namespace mlir {
23#define GEN_PASS_DEF_GPUXEVMATTACHTARGET
24#include "mlir/Dialect/GPU/Transforms/XeVM/Passes.h.inc"
25} // namespace mlir
26
27using namespace mlir;
28using namespace mlir::xevm;
29
30namespace {
31struct XeVMAttachTarget
32 : public mlir::impl::GpuXeVMAttachTargetBase<XeVMAttachTarget> {
33 using Base::Base;
34
35 DictionaryAttr getFlags(OpBuilder &builder) const;
36
37 void runOnOperation() override;
39 void getDependentDialects(DialectRegistry &registry) const override {
40 registry.insert<xevm::XeVMDialect>();
41 }
42};
43} // namespace
45DictionaryAttr XeVMAttachTarget::getFlags(OpBuilder &builder) const {
47 // Tokenize and set the optional command line options.
48 if (!cmdOptions.empty()) {
49 std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options =
53 for (const char *opt : options.second) {
54 xevmOptionAttrs.emplace_back(
55 mlir::StringAttr::get(builder.getContext(), StringRef(opt)));
56 }
57 flags.push_back(builder.getNamedAttr(
58 "cmd-options",
59 mlir::ArrayAttr::get(builder.getContext(), xevmOptionAttrs)));
60 }
61 }
62
63 if (!flags.empty())
64 return builder.getDictionaryAttr(flags);
65 return nullptr;
66}
67
68void XeVMAttachTarget::runOnOperation() {
69 OpBuilder builder(&getContext());
71 SmallVector<StringRef> filesToLink(libs);
72 auto target = builder.getAttr<xevm::XeVMTargetAttr>(
73 optLevel, triple, chip, getFlags(builder),
74 filesToLink.empty() ? nullptr : builder.getStrArrayAttr(filesToLink));
75 llvm::Regex matcher(moduleMatcher);
76 for (Region &region : getOperation()->getRegions())
77 for (Block &block : region.getBlocks())
78 for (auto module : block.getOps<gpu::GPUModuleOp>()) {
79 // Check if the name of the module matches.
80 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
81 continue;
82 // Create the target array.
84 if (std::optional<ArrayAttr> attrs = module.getTargets())
85 targets.append(attrs->getValue().begin(), attrs->getValue().end());
86 targets.push_back(target);
87 // Remove any duplicate targets.
88 targets.erase(llvm::unique(targets), targets.end());
89 // Update the target attribute array.
90 module.setTargetsAttr(builder.getArrayAttr(targets));
91 }
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
Block represents an ordered list of Operations.
Definition Block.h:34
MLIRContext * getContext() const
Definition Builders.h:56
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:112
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition Builders.cpp:102
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition Builders.cpp:315
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition Builders.h:101
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
This class helps build Operations.
Definition Builders.h:210
OpT getOperation()
Return the current operation being transformed.
Definition Pass.h:389
This class contains a list of basic blocks and a link to the parent operation it is attached to.
Definition Region.h:26
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeCmdOptions() const
Returns a tokenization of the command line options.
Definition GPUBase.cpp:472
::mlir::Pass::ListOption< std::string > linkLibs
::mlir::Pass::Option< unsigned > optLevel
::mlir::Pass::Option< std::string > cmdOptions
::mlir::Pass::Option< std::string > moduleMatcher
::mlir::Pass::Option< std::string > chip
void getDependentDialects(::mlir::DialectRegistry &registry) const override
Return the dialect that must be loaded in the context before this pass.
::mlir::Pass::Option< std::string > triple
Include the generated interface declarations.