[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
This commit is contained in:
Fabian Mora
2023-08-08 13:09:52 +00:00
parent c8e0364a43
commit 86c4dfa209
6 changed files with 199 additions and 0 deletions

View File

@@ -16,6 +16,11 @@ mlir_tablegen(GPUOpsEnums.h.inc -gen-enum-decls)
mlir_tablegen(GPUOpsEnums.cpp.inc -gen-enum-defs)
add_public_tablegen_target(MLIRGPUOpsEnumsGen)
set(LLVM_TARGET_DEFINITIONS CompilationAttrInterfaces.td)
mlir_tablegen(CompilationAttrInterfaces.h.inc -gen-attr-interface-decls)
mlir_tablegen(CompilationAttrInterfaces.cpp.inc -gen-attr-interface-defs)
add_public_tablegen_target(MLIRGPUCompilationAttrInterfacesIncGen)
set(LLVM_TARGET_DEFINITIONS GPUOps.td)
mlir_tablegen(GPUOpsAttributes.h.inc -gen-attrdef-decls -attrdefs-dialect=gpu)
mlir_tablegen(GPUOpsAttributes.cpp.inc -gen-attrdef-defs -attrdefs-dialect=gpu)

View File

@@ -0,0 +1,51 @@
//===-- CompilationAttrInterfaces.td - GPU compilation interfaces ---------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines interfaces for GPU target attributes.
//
//===----------------------------------------------------------------------===//
#ifndef GPU_COMPILATIONATTRINTERFACES
#define GPU_COMPILATIONATTRINTERFACES
include "mlir/IR/AttrTypeBase.td"
include "mlir/IR/OpBase.td"
//===----------------------------------------------------------------------===//
// GPU target attribute interface.
//===----------------------------------------------------------------------===//
def GPUTargetAttrInterface : AttrInterface<"TargetAttrInterface"> {
let description = [{
Interface for GPU target attributes. Attributes implementing this interface
compile GPU modules into binary objects, providing an opaque interface to
hide implementation details.
}];
let cppNamespace = "::mlir::gpu";
let methods = [
InterfaceMethod<[{
Serializes a GPU module to a string containing a representation of the
module.
If serialization fails then the method should return `std::nullopt`.
The `module` argument must be a GPU Module Op. The `options` argument is
meant to be used for passing additional options that are not in the
attribute.
}],
"std::optional<SmallVector<char, 0>>", "serializeToObject",
(ins "Operation*":$module, "const gpu::TargetOptions&":$options)>
];
}
def GPUTargetArrayAttr : TypedArrayAttrBase<GPUTargetAttrInterface,
"array of GPU target attributes">;
def GPUNonEmptyTargetArrayAttr :
ConfinedAttr<GPUTargetArrayAttr, [ArrayMinCount<1>]>;
#endif // GPU_COMPILATIONATTRINTERFACES

View File

@@ -0,0 +1,90 @@
//===-- CompilationInterfaces.h - GPU compilation interfaces ---*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// This file defines interfaces for GPU compilation.
//
//===----------------------------------------------------------------------===//
#ifndef MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
#define MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H
#include "mlir/IR/Attributes.h"
namespace mlir {
namespace gpu {
/// This class serves as an opaque interface for passing options to the
/// `TargetAttrInterface` methods. Users of this class must implement the
/// `classof` method as well as using the macros `MLIR_*_EXPLICIT_TYPE_ID` to
/// ensure type safeness. Targets are free to ignore these options.
class TargetOptions {
public:
/// The target representation of the compilation process.
typedef enum {
offload, /// The process should produce an offloading representation. For
/// the NVVM & ROCDL targets this option produces LLVM IR.
assembly, /// The process should produce assembly code.
binary /// The process should produce a binary.
} CompilationTarget;
/// Constructor initializing the toolkit path, the list of files to link to,
/// extra command line options & the compilation target. The default
/// compilation target is `binary`.
TargetOptions(StringRef toolkitPath = {},
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
CompilationTarget compilationTarget = binary);
/// Returns the typeID.
TypeID getTypeID() const;
/// Returns the toolkit path.
StringRef getToolkitPath() const;
/// Returns the files to link to.
ArrayRef<std::string> getLinkFiles() const;
/// Returns the command line options.
StringRef getCmdOptions() const;
/// Returns a tokenization of the command line options.
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
tokenizeCmdOptions() const;
/// Returns the compilation target.
CompilationTarget getCompilationTarget() const;
protected:
/// Derived classes must use this constructor to initialize `typeID` to the
/// appropiate value: ie. `TargetOptions(TypeID::get<DerivedClass>())`.
TargetOptions(TypeID typeID, StringRef toolkitPath = {},
ArrayRef<std::string> linkFiles = {}, StringRef cmdOptions = {},
CompilationTarget compilationTarget = binary);
/// Path to the target toolkit.
std::string toolkitPath;
/// List of files to link with the LLVM module.
SmallVector<std::string> linkFiles;
/// An optional set of command line options to be used by the compilation
/// process.
std::string cmdOptions;
/// Compilation process target representation.
CompilationTarget compilationTarget;
private:
TypeID typeID;
};
} // namespace gpu
} // namespace mlir
MLIR_DECLARE_EXPLICIT_TYPE_ID(::mlir::gpu::TargetOptions)
#include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.h.inc"
#endif // MLIR_DIALECT_GPU_IR_COMPILATIONINTERFACES_H

View File

@@ -16,6 +16,7 @@
#include "mlir/Bytecode/BytecodeOpInterface.h"
#include "mlir/Dialect/DLTI/Traits.h"
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"

View File

@@ -32,6 +32,7 @@ add_mlir_dialect_library(MLIRGPUDialect
MLIRGPUOpsAttributesIncGen
MLIRGPUOpsEnumsGen
MLIRGPUOpInterfacesIncGen
MLIRGPUCompilationAttrInterfacesIncGen
LINK_LIBS PUBLIC
MLIRArithDialect

View File

@@ -28,7 +28,9 @@
#include "mlir/Interfaces/SideEffectInterfaces.h"
#include "mlir/Transforms/InliningUtils.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/StringSaver.h"
using namespace mlir;
using namespace mlir::gpu;
@@ -1811,6 +1813,53 @@ void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
results.add<SimplifyDimOfAllocOp>(context);
}
//===----------------------------------------------------------------------===//
// GPU target options
//===----------------------------------------------------------------------===//
TargetOptions::TargetOptions(StringRef toolkitPath,
ArrayRef<std::string> linkFiles,
StringRef cmdOptions,
CompilationTarget compilationTarget)
: TargetOptions(TypeID::get<TargetOptions>(), toolkitPath, linkFiles,
cmdOptions, compilationTarget) {}
TargetOptions::TargetOptions(TypeID typeID, StringRef toolkitPath,
ArrayRef<std::string> linkFiles,
StringRef cmdOptions,
CompilationTarget compilationTarget)
: toolkitPath(toolkitPath.str()), linkFiles(linkFiles),
cmdOptions(cmdOptions.str()), compilationTarget(compilationTarget),
typeID(typeID) {}
TypeID TargetOptions::getTypeID() const { return typeID; }
StringRef TargetOptions::getToolkitPath() const { return toolkitPath; }
ArrayRef<std::string> TargetOptions::getLinkFiles() const { return linkFiles; }
StringRef TargetOptions::getCmdOptions() const { return cmdOptions; }
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>>
TargetOptions::tokenizeCmdOptions() const {
std::pair<llvm::BumpPtrAllocator, SmallVector<const char *>> options;
llvm::StringSaver stringSaver(options.first);
#ifdef _WIN32
llvm::cl::TokenizeWindowsCommandLine(cmdOptions, stringSaver, options.second,
/*MarkEOLs=*/false);
#else
llvm::cl::TokenizeGNUCommandLine(cmdOptions, stringSaver, options.second,
/*MarkEOLs=*/false);
#endif // _WIN32
return options;
}
TargetOptions::CompilationTarget TargetOptions::getCompilationTarget() const {
return compilationTarget;
}
MLIR_DEFINE_EXPLICIT_TYPE_ID(::mlir::gpu::TargetOptions)
#include "mlir/Dialect/GPU/IR/GPUOpInterfaces.cpp.inc"
#include "mlir/Dialect/GPU/IR/GPUOpsEnums.cpp.inc"
@@ -1819,3 +1868,5 @@ void AllocOp::getCanonicalizationPatterns(RewritePatternSet &results,
#define GET_OP_CLASSES
#include "mlir/Dialect/GPU/IR/GPUOps.cpp.inc"
#include "mlir/Dialect/GPU/IR/CompilationAttrInterfaces.cpp.inc"