MLIR 23.0.0git
NVVMAttachTarget.cpp
Go to the documentation of this file.
1//===- NVVMAttachTarget.cpp - Attach an NVVM target -----------------------===//
2//
3// Part of the LLVM Project, 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 `GpuNVVMAttachTarget` pass, attaching `#nvvm.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_GPUNVVMATTACHTARGET
25#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
26} // namespace mlir
27
28using namespace mlir;
29using namespace mlir::NVVM;
30
31namespace {
32struct NVVMAttachTarget
33 : public impl::GpuNVVMAttachTargetBase<NVVMAttachTarget> {
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<NVVM::NVVMDialect>();
42 }
43};
44} // namespace
45
46DictionaryAttr NVVMAttachTarget::getFlags(OpBuilder &builder) const {
47 UnitAttr unitAttr = builder.getUnitAttr();
48 SmallVector<NamedAttribute, 3> flags;
49 auto addFlag = [&](StringRef flag) {
50 flags.push_back(builder.getNamedAttr(flag, unitAttr));
51 };
52 if (fastFlag)
53 addFlag("fast");
54 if (ftzFlag)
55 addFlag("ftz");
56 if (compilerDiagnosticsFlag)
57 addFlag("collect-compiler-diagnostics");
58
59 // Tokenize and set the optional command line options.
60 if (!cmdOptions.empty()) {
62 if (!options.second.empty()) {
63 llvm::SmallVector<mlir::Attribute> nvvmOptionAttrs;
64 for (const char *opt : options.second) {
65 nvvmOptionAttrs.emplace_back(
66 mlir::StringAttr::get(builder.getContext(), StringRef(opt)));
67 }
68 flags.push_back(builder.getNamedAttr(
69 "ptxas-cmd-options",
70 mlir::ArrayAttr::get(builder.getContext(), nvvmOptionAttrs)));
71 }
72 }
73
74 if (!flags.empty())
75 return builder.getDictionaryAttr(flags);
76 return nullptr;
77}
78
79void NVVMAttachTarget::runOnOperation() {
80 OpBuilder builder(&getContext());
81 ArrayRef<std::string> libs(linkLibs);
82 SmallVector<StringRef> filesToLink(libs);
83 auto target = builder.getAttr<NVVMTargetAttr>(
84 optLevel, triple, chip, features, getFlags(builder),
85 filesToLink.empty() ? nullptr : builder.getStrArrayAttr(filesToLink),
86 verifyTarget);
87 llvm::Regex matcher(moduleMatcher);
88 for (Region &region : getOperation()->getRegions())
89 for (Block &block : region.getBlocks())
90 for (auto module : block.getOps<gpu::GPUModuleOp>()) {
91 // Check if the name of the module matches.
92 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
93 continue;
94 // Create the target array.
95 SmallVector<Attribute> targets;
96 if (std::optional<ArrayAttr> attrs = module.getTargets())
97 targets.append(attrs->getValue().begin(), attrs->getValue().end());
98 targets.push_back(target);
99 // Remove any duplicate targets.
100 targets.erase(llvm::unique(targets), targets.end());
101 // Update the target attribute array.
102 module.setTargetsAttr(builder.getArrayAttr(targets));
103 }
104}
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
UnitAttr getUnitAttr()
Definition Builders.cpp:102
MLIRContext * getContext() const
Definition Builders.h:56
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
Definition Builders.cpp:108
NamedAttribute getNamedAttr(StringRef name, Attribute val)
Definition Builders.cpp:98
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Definition Builders.cpp:310
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
Definition Builders.h:100
This class helps build Operations.
Definition Builders.h:209
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeCmdOptions() const
Returns a tokenization of the command line options.
Include the generated interface declarations.