21#include "llvm/Support/Regex.h"
24#define GEN_PASS_DEF_GPUNVVMATTACHTARGET
25#include "mlir/Dialect/GPU/Transforms/Passes.h.inc"
32struct NVVMAttachTarget
36 DictionaryAttr getFlags(OpBuilder &builder)
const;
38 void runOnOperation()
override;
40 void getDependentDialects(DialectRegistry ®istry)
const override {
41 registry.
insert<NVVM::NVVMDialect>();
46DictionaryAttr NVVMAttachTarget::getFlags(
OpBuilder &builder)
const {
48 SmallVector<NamedAttribute, 3> flags;
49 auto addFlag = [&](StringRef flag) {
56 if (compilerDiagnosticsFlag)
57 addFlag(
"collect-compiler-diagnostics");
60 if (!cmdOptions.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)));
70 mlir::ArrayAttr::get(builder.
getContext(), nvvmOptionAttrs)));
79void NVVMAttachTarget::runOnOperation() {
81 ArrayRef<std::string> libs(linkLibs);
82 SmallVector<StringRef> filesToLink(libs);
84 optLevel, triple, chip, features, getFlags(builder),
87 llvm::Regex matcher(moduleMatcher);
88 for (Region ®ion : getOperation()->getRegions())
89 for (
Block &block : region.getBlocks())
90 for (
auto module : block.getOps<gpu::GPUModuleOp>()) {
92 if (!moduleMatcher.empty() && !matcher.match(module.getName()))
95 SmallVector<Attribute> targets;
96 if (std::optional<ArrayAttr> attrs = module.getTargets())
97 targets.append(attrs->getValue().begin(), attrs->getValue().end());
100 targets.erase(llvm::unique(targets), targets.end());
102 module.setTargetsAttr(builder.getArrayAttr(targets));
static llvm::ManagedStatic< PassManagerOptions > options
MLIRContext * getContext() const
DictionaryAttr getDictionaryAttr(ArrayRef< NamedAttribute > value)
NamedAttribute getNamedAttr(StringRef name, Attribute val)
ArrayAttr getStrArrayAttr(ArrayRef< StringRef > values)
Attr getAttr(Args &&...args)
Get or construct an instance of the attribute Attr with provided arguments.
This class helps build Operations.
std::pair< llvm::BumpPtrAllocator, SmallVector< const char * > > tokenizeCmdOptions() const
Returns a tokenization of the command line options.
Include the generated interface declarations.