Skip to content

Commit 0e0db64

Browse files
committed
[mlir][nvvm_target] Add nvvm options into NVVMAttachTarget pass
1 parent aebe6c5 commit 0e0db64

File tree

5 files changed

+42
-2
lines changed

5 files changed

+42
-2
lines changed

mlir/include/mlir/Dialect/GPU/IR/CompilationInterfaces.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,10 @@ class TargetOptions {
108108
/// Returns the default compilation target: `CompilationTarget::Fatbin`.
109109
static CompilationTarget getDefaultCompilationTarget();
110110

111+
/// Returns a tokenization of the command line options.
112+
static std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
113+
tokenizeCmdOptions(const std::string &cmdOptions);
114+
111115
protected:
112116
/// Derived classes must use this constructor to initialize `typeID` to the
113117
/// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.

mlir/include/mlir/Dialect/GPU/Transforms/Passes.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,9 @@ def GpuNVVMAttachTarget: Pass<"nvvm-attach-target", ""> {
141141
Option<"ftzFlag", "ftz", "bool",
142142
/*default=*/"false",
143143
"Enable flush to zero for denormals.">,
144+
Option<"nvvmOptions", "nvvm-options", "std::string",
145+
/*default=*/ [{""}],
146+
"Options passed to libnvvm">,
144147
ListOption<"linkLibs", "l", "std::string",
145148
"Extra bitcode libraries paths to link to.">,
146149
];

mlir/lib/Dialect/GPU/IR/GPUDialect.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2564,7 +2564,7 @@ CompilationTarget TargetOptions::getDefaultCompilationTarget() {
25642564
}
25652565

25662566
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
2567-
TargetOptions::tokenizeCmdOptions() const {
2567+
TargetOptions::tokenizeCmdOptions(const std::string &cmdOptions) {
25682568
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options;
25692569
llvm::StringSaver stringSaver(options.first);
25702570
StringRef opts = cmdOptions;
@@ -2586,6 +2586,11 @@ TargetOptions::tokenizeCmdOptions() const {
25862586
return options;
25872587
}
25882588

2589+
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
2590+
TargetOptions::tokenizeCmdOptions() const {
2591+
return tokenizeCmdOptions(cmdOptions);
2592+
}
2593+
25892594
MLIR_DEFINE_EXPLICIT_TYPE_ID(::mlir::gpu::TargetOptions)
25902595

25912596
#include "mlir/Dialect/GPU/IR/GPUOpInterfaces.cpp.inc"

mlir/lib/Dialect/GPU/Transforms/NVVMAttachTarget.cpp

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,27 @@ struct NVVMAttachTarget
4545

4646
DictionaryAttr NVVMAttachTarget::getFlags(OpBuilder &builder) const {
4747
UnitAttr unitAttr = builder.getUnitAttr();
48-
SmallVector<NamedAttribute, 2> flags;
48+
SmallVector<NamedAttribute, 3> flags;
4949
auto addFlag = [&](StringRef flag) {
5050
flags.push_back(builder.getNamedAttr(flag, unitAttr));
5151
};
5252
if (fastFlag)
5353
addFlag("fast");
5454
if (ftzFlag)
5555
addFlag("ftz");
56+
if (!nvvmOptions.empty()) {
57+
auto options = gpu::TargetOptions::tokenizeCmdOptions(nvvmOptions);
58+
if (!options.second.empty()) {
59+
llvm::SmallVector<mlir::Attribute> nvvmOptionAttrs;
60+
for (const char *opt : options.second) {
61+
nvvmOptionAttrs.emplace_back(
62+
mlir::StringAttr::get(builder.getContext(), StringRef(opt)));
63+
}
64+
flags.push_back(builder.getNamedAttr(
65+
"libNVVMOptions",
66+
mlir::ArrayAttr::get(builder.getContext(), nvvmOptionAttrs)));
67+
}
68+
}
5669
if (!flags.empty())
5770
return builder.getDictionaryAttr(flags);
5871
return nullptr;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
// RUN: mlir-opt %s --nvvm-attach-target="" | FileCheck %s
2+
// RUN: mlir-opt %s --nvvm-attach-target="-opt=1" | FileCheck %s -check-prefix=CHECK-OPTIONS
3+
4+
module attributes {gpu.container_module} {
5+
// CHECK-LABEL:gpu.binary @kernel_module1
6+
// CHECK:gpu.module @kernel_module1 [#nvvm.target]
7+
// CHECK-OPTIONS:gpu.module @kernel_module1 [#nvvm.target<flags = {libNVVMOptions = ["-opt=1"]}>]
8+
gpu.module @kernel_module1 {
9+
llvm.func @kernel(%arg0: i32, %arg1: !llvm.ptr,
10+
%arg2: !llvm.ptr, %arg3: i64, %arg4: i64,
11+
%arg5: i64) attributes {gpu.kernel} {
12+
llvm.return
13+
}
14+
}
15+
}

0 commit comments

Comments
 (0)