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
19#include "mlir/IR/Builders.h"
20#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/NVVM/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();
49 auto addFlag = [&](StringRef flag) {
50 flags.push_back(builder.getNamedAttr(flag, unitAttr));
51 };
53 addFlag("fast");
55 addFlag("ftz");
57 addFlag("collect-compiler-diagnostics");
58
59 // Tokenize and set the optional command line options.
60 if (!cmdOptions.empty()) {
62 if (!options.second.empty()) {
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());
82 SmallVector<StringRef> filesToLink(libs);
83 // Build the target attribute, failing the pass if the options are invalid.
84 // `getChecked` takes its arguments by value and pass options are not
85 // copyable, hence the conversions.
86 auto emitError = [op = getOperation()]() { return op->emitError(); };
87 auto target = NVVMTargetAttr::getChecked(
88 emitError, &getContext(), static_cast<int>(optLevel), StringRef(triple),
89 StringRef(chip), StringRef(features), getFlags(builder),
90 filesToLink.empty() ? nullptr : builder.getStrArrayAttr(filesToLink),
91 static_cast<bool>(verifyTarget));
92 if (!target)
94 llvm::Regex matcher(moduleMatcher);
95 for (Region &region : getOperation()->getRegions())
96 for (Block &block : region.getBlocks())
97 for (auto module : block.getOps<gpu::GPUModuleOp>()) {
98 // Check if the name of the module matches.
99 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
100 continue;
101 // Create the target array.
103 if (std::optional<ArrayAttr> attrs = module.getTargets())
104 targets.append(attrs->getValue().begin(), attrs->getValue().end());
105 targets.push_back(target);
106 // Remove any duplicate targets.
107 targets.erase(llvm::unique(targets), targets.end());
108 // Update the target attribute array.
109 module.setTargetsAttr(builder.getArrayAttr(targets));
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.
Block represents an ordered list of Operations.
Definition Block.h:34
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
OpT getOperation()
Return the current operation being transformed.
Definition Pass.h:389
void signalPassFailure()
Signal that some invariant was broken when running.
Definition Pass.h:226
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:474
::mlir::Pass::Option< std::string > moduleMatcher
::mlir::Pass::Option< std::string > cmdOptions
::mlir::Pass::Option< bool > ftzFlag
::mlir::Pass::Option< bool > fastFlag
::mlir::Pass::ListOption< std::string > linkLibs
::mlir::Pass::Option< bool > compilerDiagnosticsFlag
Include the generated interface declarations.
InFlightDiagnostic emitError(Location loc)
Utility method to emit an error message using this location.