MLIR 24.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"
20#include "llvm/Support/Regex.h"
21
22namespace mlir {
23#define GEN_PASS_DEF_GPUNVVMATTACHTARGET
24#include "mlir/Dialect/GPU/Transforms/NVVM/Passes.h.inc"
25} // namespace mlir
26
27using namespace mlir;
28using namespace mlir::NVVM;
29
30namespace {
31struct NVVMAttachTarget
32 : public impl::GpuNVVMAttachTargetBase<NVVMAttachTarget> {
33 using Base::Base;
34
35 DictionaryAttr getFlags(OpBuilder &builder) const;
36
37 void runOnOperation() override;
38
39 void getDependentDialects(DialectRegistry &registry) const override {
40 registry.insert<NVVM::NVVMDialect>();
41 }
42};
43} // namespace
44
45DictionaryAttr NVVMAttachTarget::getFlags(OpBuilder &builder) const {
46 UnitAttr unitAttr = builder.getUnitAttr();
47 SmallVector<NamedAttribute, 3> flags;
48 auto addFlag = [&](StringRef flag) {
49 flags.push_back(builder.getNamedAttr(flag, unitAttr));
50 };
51 if (fastFlag)
52 addFlag("fast");
53 if (ftzFlag)
54 addFlag("ftz");
55 if (compilerDiagnosticsFlag)
56 addFlag("collect-compiler-diagnostics");
57
58 // Tokenize and set the optional command line options.
59 if (!cmdOptions.empty()) {
61 if (!options.second.empty()) {
62 llvm::SmallVector<mlir::Attribute> nvvmOptionAttrs;
63 for (const char *opt : options.second) {
64 nvvmOptionAttrs.emplace_back(
65 mlir::StringAttr::get(builder.getContext(), StringRef(opt)));
66 }
67 flags.push_back(builder.getNamedAttr(
68 "ptxas-cmd-options",
69 mlir::ArrayAttr::get(builder.getContext(), nvvmOptionAttrs)));
70 }
71 }
72
73 if (!flags.empty())
74 return builder.getDictionaryAttr(flags);
75 return nullptr;
76}
77
78void NVVMAttachTarget::runOnOperation() {
79 OpBuilder builder(&getContext());
80 ArrayRef<std::string> libs(linkLibs);
81 SmallVector<StringRef> filesToLink(libs);
82 // Build the target attribute, failing the pass if the options are invalid.
83 // `getChecked` takes its arguments by value and pass options are not
84 // copyable, hence the conversions.
85 auto emitError = [op = getOperation()]() { return op->emitError(); };
86 auto target = NVVMTargetAttr::getChecked(
87 emitError, &getContext(), static_cast<int>(optLevel), StringRef(triple),
88 StringRef(chip), StringRef(features), getFlags(builder),
89 filesToLink.empty() ? nullptr : builder.getStrArrayAttr(filesToLink),
90 static_cast<bool>(verifyTarget));
91 if (!target)
92 return signalPassFailure();
93 llvm::Regex matcher(moduleMatcher);
94 for (Region &region : getOperation()->getRegions())
95 for (Block &block : region.getBlocks())
96 for (auto module : block.getOps<gpu::GPUModuleOp>()) {
97 // Check if the name of the module matches.
98 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
99 continue;
100 // Create the target array.
101 SmallVector<Attribute> targets;
102 if (std::optional<ArrayAttr> attrs = module.getTargets())
103 targets.append(attrs->getValue().begin(), attrs->getValue().end());
104 targets.push_back(target);
105 // Remove any duplicate targets.
106 targets.erase(llvm::unique(targets), targets.end());
107 // Update the target attribute array.
108 module.setTargetsAttr(builder.getArrayAttr(targets));
109 }
110}
b getContext())
static llvm::ManagedStatic< PassManagerOptions > options
static arith::FastMathFlags getFlags(arith::FastMathFlagsAttr fastmath)
Returns the flags held by fastmath, or none if it is null.
UnitAttr getUnitAttr()
Definition Builders.cpp:106
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
This class helps build Operations.
Definition Builders.h:210
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeCmdOptions() const
Returns a tokenization of the command line options.
Definition GPUBase.cpp:472
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.