Skip to content

Commit 86c4dfa

Browse files
committed
[mlir][gpu] Add GPU target attribute interface.
**For an explanation of these patches see D154153.** Commit message: This patch adds the `GPUTargetAttrInterface` attribute interface, this interface is meant to be used as an opaque interface for serializing GPU modules into binary strings. Reviewed By: mehdi_amini, krzysz00 Differential Revision: https://reviews.llvm.org/D154104
1 parent c8e0364 commit 86c4dfa

File tree

6 files changed

+199
-0
lines changed

6 files changed

+199
-0
lines changed

mlir/include/mlir/Dialect/GPU/IR/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@ mlir_tablegen(GPUOpsEnums.h.inc -gen-enum-decls)
1616
mlir_tablegen(GPUOpsEnums.cpp.inc -gen-enum-defs)
1717
add_public_tablegen_target(MLIRGPUOpsEnumsGen)
1818

19+
set(LLVM_TARGET_DEFINITIONS CompilationAttrInterfaces.td)
20+
mlir_tablegen(CompilationAttrInterfaces.h.inc -gen-attr-interface-decls)
21+
mlir_tablegen(CompilationAttrInterfaces.cpp.inc -gen-attr-interface-defs)
22+
add_public_tablegen_target(MLIRGPUCompilationAttrInterfacesIncGen)
23+
1924
set(LLVM_TARGET_DEFINITIONS GPUOps.td)
2025
mlir_tablegen(GPUOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=gpu)
2126
mlir_tablegen(GPUOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=gpu)
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
//===-- CompilationAttrInterfaces.td - GPU compilation interfaces ---------===//
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 defines interfaces for GPU target attributes.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef GPU_COMPILATIONATTRINTERFACES
14+
#define GPU_COMPILATIONATTRINTERFACES
15+
16+
include "mlir/IR/AttrTypeBase.td"
17+
include "mlir/IR/OpBase.td"
18+
19+
//===----------------------------------------------------------------------===//
20+
// GPU target attribute interface.
21+
//===----------------------------------------------------------------------===//
22+
def GPUTargetAttrInterface : AttrInterface<"TargetAttrInterface"> {
23+
let description = [{
24+
Interface for GPU target attributes. Attributes implementing this interface
25+
compile GPU modules into binary objects, providing an opaque interface to
26+
hide implementation details.
27+
}];
28+
let cppNamespace = "::mlir::gpu";
29+
let methods = [
30+
InterfaceMethod<[{
31+
Serializes a GPU module to a string containing a representation of the
32+
module.
33+
34+
If serialization fails then the method should return `std::nullopt`.
35+
36+
The `module` argument must be a GPU Module Op. The `options` argument is
37+
meant to be used for passing additional options that are not in the
38+
attribute.
39+
}],
40+
"std::optional<SmallVector<char, 0>>", "serializeToObject",
41+
(ins "Operation*":$module, "const gpu::TargetOptions&":$options)>
42+
];
43+
}
44+
45+
def GPUTargetArrayAttr : TypedArrayAttrBase<GPUTargetAttrInterface,
46+
"array of GPU target attributes">;
47+
48+
def GPUNonEmptyTargetArrayAttr :
49+
ConfinedAttr<GPUTargetArrayAttr, [ArrayMinCount<1>]>;
50+
51+
#endif // GPU_COMPILATIONATTRINTERFACES
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
//===-- CompilationInterfaces.h - GPU compilation interfaces ---*- C++ -*-===//
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 defines interfaces for GPU compilation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
14+
#define MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
15+
16+
#include "mlir/IR/Attributes.h"
17+
18+
namespace mlir {
19+
namespace gpu {
20+
/// This class serves as an opaque interface for passing options to the
21+
/// `TargetAttrInterface` methods. Users of this class must implement the
22+
/// `classof` method as well as using the macros `MLIR_*_EXPLICIT_TYPE_ID` to
23+
/// ensure type safeness. Targets are free to ignore these options.
24+
class TargetOptions {
25+
public:
26+
/// The target representation of the compilation process.
27+
typedef enum {
28+
offload, /// The process should produce an offloading representation. For
29+
/// the NVVM & ROCDL targets this option produces LLVM IR.
30+
assembly, /// The process should produce assembly code.
31+
binary /// The process should produce a binary.
32+
} CompilationTarget;
33+
34+
/// Constructor initializing the toolkit path, the list of files to link to,
35+
/// extra command line options & the compilation target. The default
36+
/// compilation target is `binary`.
37+
TargetOptions(StringRef toolkitPath = {},
38+
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
39+
CompilationTarget compilationTarget = binary);
40+
41+
/// Returns the typeID.
42+
TypeID getTypeID() const;
43+
44+
/// Returns the toolkit path.
45+
StringRef getToolkitPath() const;
46+
47+
/// Returns the files to link to.
48+
ArrayRef<std::string> getLinkFiles() const;
49+
50+
/// Returns the command line options.
51+
StringRef getCmdOptions() const;
52+
53+
/// Returns a tokenization of the command line options.
54+
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
55+
tokenizeCmdOptions() const;
56+
57+
/// Returns the compilation target.
58+
CompilationTarget getCompilationTarget() const;
59+
60+
protected:
61+
/// Derived classes must use this constructor to initialize `typeID` to the
62+
/// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
63+
TargetOptions(TypeID typeID, StringRef toolkitPath = {},
64+
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
65+
CompilationTarget compilationTarget = binary);
66+
67+
/// Path to the target toolkit.
68+
std::string toolkitPath;
69+
70+
/// List of files to link with the LLVM module.
71+
SmallVector<std::string> linkFiles;
72+
73+
/// An optional set of command line options to be used by the compilation
74+
/// process.
75+
std::string cmdOptions;
76+
77+
/// Compilation process target representation.
78+
CompilationTarget compilationTarget;
79+
80+
private:
81+
TypeID typeID;
82+
};
83+
} // namespace gpu
84+
} // namespace mlir
85+
86+
MLIR_DECLARE_EXPLICIT_TYPE_ID(::mlir::gpu::TargetOptions)
87+
88+
#include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.h.inc"
89+
90+
#endif // MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "mlir/Bytecode/BytecodeOpInterface.h"
1818
#include "mlir/Dialect/DLTI/Traits.h"
19+
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
1920
#include "mlir/IR/Builders.h"
2021
#include "mlir/IR/BuiltinTypes.h"
2122
#include "mlir/IR/Dialect.h"

mlir/lib/Dialect/GPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ add_mlir_dialect_library(MLIRGPUDialect
3232
MLIRGPUOpsAttributesIncGen
3333
MLIRGPUOpsEnumsGen
3434
MLIRGPUOpInterfacesIncGen
35+
MLIRGPUCompilationAttrInterfacesIncGen
3536

3637
LINK_LIBS PUBLIC
3738
MLIRArithDialect

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

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@
2828
#include "mlir/Interfaces/SideEffectInterfaces.h"
2929
#include "mlir/Transforms/InliningUtils.h"
3030
#include "llvm/ADT/TypeSwitch.h"
31+
#include "llvm/Support/CommandLine.h"
3132
#include "llvm/Support/ErrorHandling.h"
33+
#include "llvm/Support/StringSaver.h"
3234

3335
using namespace mlir;
3436
using namespace mlir::gpu;
@@ -1811,6 +1813,53 @@ void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
18111813
results.add<SimplifyDimOfAllocOp>(context);
18121814
}
18131815

1816+
//===----------------------------------------------------------------------===//
1817+
// GPU target options
1818+
//===----------------------------------------------------------------------===//
1819+
1820+
TargetOptions::TargetOptions(StringRef toolkitPath,
1821+
ArrayRef<std::string> linkFiles,
1822+
StringRef cmdOptions,
1823+
CompilationTarget compilationTarget)
1824+
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, linkFiles,
1825+
cmdOptions, compilationTarget) {}
1826+
1827+
TargetOptions::TargetOptions(TypeID typeID, StringRef toolkitPath,
1828+
ArrayRef<std::string> linkFiles,
1829+
StringRef cmdOptions,
1830+
CompilationTarget compilationTarget)
1831+
: toolkitPath(toolkitPath.str()), linkFiles(linkFiles),
1832+
cmdOptions(cmdOptions.str()), compilationTarget(compilationTarget),
1833+
typeID(typeID) {}
1834+
1835+
TypeID TargetOptions::getTypeID() const { return typeID; }
1836+
1837+
StringRef TargetOptions::getToolkitPath() const { return toolkitPath; }
1838+
1839+
ArrayRef<std::string> TargetOptions::getLinkFiles() const { return linkFiles; }
1840+
1841+
StringRef TargetOptions::getCmdOptions() const { return cmdOptions; }
1842+
1843+
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
1844+
TargetOptions::tokenizeCmdOptions() const {
1845+
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options;
1846+
llvm::StringSaver stringSaver(options.first);
1847+
#ifdef _WIN32
1848+
llvm::cl::TokenizeWindowsCommandLine(cmdOptions, stringSaver, options.second,
1849+
/*MarkEOLs=*/false);
1850+
#else
1851+
llvm::cl::TokenizeGNUCommandLine(cmdOptions, stringSaver, options.second,
1852+
/*MarkEOLs=*/false);
1853+
#endif // _WIN32
1854+
return options;
1855+
}
1856+
1857+
TargetOptions::CompilationTarget TargetOptions::getCompilationTarget() const {
1858+
return compilationTarget;
1859+
}
1860+
1861+
MLIR_DEFINE_EXPLICIT_TYPE_ID(::mlir::gpu::TargetOptions)
1862+
18141863
#include "mlir/Dialect/GPU/IR/GPUOpInterfaces.cpp.inc"
18151864
#include "mlir/Dialect/GPU/IR/GPUOpsEnums.cpp.inc"
18161865

@@ -1819,3 +1868,5 @@ void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
18191868

18201869
#define GET_OP_CLASSES
18211870
#include "mlir/Dialect/GPU/IR/GPUOps.cpp.inc"
1871+
1872+
#include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.cpp.inc"

0 commit comments

Comments
 (0)