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
14#include "mlir/Dialect/GPU/Transforms/NVVM/Passes.h"
15
16#include "mlir/Dialect/GPU/IR/GPUDialect.h"
17#include "mlir/Dialect/LLVMIR/NVVMAttributes.h"
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(Elt: builder.getNamedAttr(name: flag, val: 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()) {
60 auto options = gpu::TargetOptions::tokenizeCmdOptions(cmdOptions);
61 if (!options.second.empty()) {
62 llvm::SmallVector<mlir::Attribute> nvvmOptionAttrs;
63 for (const char *opt : options.second) {
64 nvvmOptionAttrs.emplace_back(
65 Args: mlir::StringAttr::get(context: builder.getContext(), bytes: StringRef(opt)));
66 }
67 flags.push_back(Elt: builder.getNamedAttr(
68 name: "ptxas-cmd-options",
69 val: mlir::ArrayAttr::get(context: builder.getContext(), value: nvvmOptionAttrs)));
70 }
71 }
72
73 if (!flags.empty())
74 return builder.getDictionaryAttr(value: 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, context: &getContext(), optLevel: static_cast<int>(optLevel), triple: StringRef(triple),
88 chip: StringRef(chip), features: StringRef(features), targetFlags: getFlags(builder),
89 linkFiles: filesToLink.empty() ? nullptr : builder.getStrArrayAttr(values: filesToLink),
90 verifyTarget: 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(String: module.getName()))
99 continue;
100 // Create the target array.
101 SmallVector<Attribute> targets;
102 if (std::optional<ArrayAttr> attrs = module.getTargets())
103 targets.append(in_start: attrs->getValue().begin(), in_end: attrs->getValue().end());
104 targets.push_back(Elt: target);
105 // Remove any duplicate targets.
106 targets.erase(CS: llvm::unique(R&: targets), CE: targets.end());
107 // Update the target attribute array.
108 module.setTargetsAttr(builder.getArrayAttr(value: targets));
109 }
110}
111