
This patch adds the `#gpu.kernel_metadata` and `#gpu.kernel_table` attributes. The `#gpu.kernel_metadata` attribute allows storing metadata related to a compiled kernel, for example, the number of scalar registers used by the kernel. The attribute only has 2 required parameters, the name and function type. It also has 2 optional parameters, the arguments attributes and generic dictionary for storing all other metadata. The `#gpu.kernel_table` stores a table of `#gpu.kernel_metadata`, mapping the name of the kernel to the metadata. Finally, the function `ROCDL::getAMDHSAKernelsELFMetadata` was added to collect ELF metadata from a binary, and to test the class methods in both attributes. Example: ```mlir gpu.binary @binary [#gpu.object<#rocdl.target<chip = "gfx900">, kernels = #gpu.kernel_table<[ #gpu.kernel_metadata<"kernel0", (i32) -> (), metadata = {sgpr_count = 255}>, #gpu.kernel_metadata<"kernel1", (i32, f32) -> (), arg_attrs = [{llvm.read_only}, {}]> ]> , bin = "BLOB">] ``` The motivation behind these attributes is to provide useful information for things like tunning. --------- Co-authored-by: Mehdi Amini <joker.eph@gmail.com>
150 lines
5.3 KiB
C++
150 lines
5.3 KiB
C++
//===- SerializeToLLVMBitcode.cpp -------------------------------*- C++ -*-===//
|
|
//
|
|
// This file is licensed 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
|
|
#include "mlir/IR/BuiltinDialect.h"
|
|
#include "mlir/IR/BuiltinOps.h"
|
|
#include "mlir/IR/MLIRContext.h"
|
|
#include "mlir/Parser/Parser.h"
|
|
#include "mlir/Target/LLVM/ModuleToObject.h"
|
|
#include "mlir/Target/LLVMIR/Dialect/Builtin/BuiltinToLLVMIRTranslation.h"
|
|
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
|
|
|
|
#include "llvm/IRReader/IRReader.h"
|
|
#include "llvm/Support/MemoryBufferRef.h"
|
|
#include "llvm/Support/TargetSelect.h"
|
|
#include "llvm/Support/raw_ostream.h"
|
|
#include "llvm/TargetParser/Host.h"
|
|
|
|
#include "gmock/gmock.h"
|
|
|
|
using namespace mlir;
|
|
|
|
// Skip the test if the native target was not built.
|
|
#if LLVM_NATIVE_TARGET_TEST_ENABLED == 0
|
|
#define SKIP_WITHOUT_NATIVE(x) DISABLED_##x
|
|
#else
|
|
#define SKIP_WITHOUT_NATIVE(x) x
|
|
#endif
|
|
|
|
namespace {
|
|
// Dummy interface for testing.
|
|
class TargetAttrImpl
|
|
: public gpu::TargetAttrInterface::FallbackModel<TargetAttrImpl> {
|
|
public:
|
|
std::optional<SmallVector<char, 0>>
|
|
serializeToObject(Attribute attribute, Operation *module,
|
|
const gpu::TargetOptions &options) const;
|
|
|
|
Attribute createObject(Attribute attribute, Operation *module,
|
|
const SmallVector<char, 0> &object,
|
|
const gpu::TargetOptions &options) const;
|
|
};
|
|
} // namespace
|
|
|
|
class MLIRTargetLLVM : public ::testing::Test {
|
|
protected:
|
|
void SetUp() override {
|
|
llvm::InitializeNativeTarget();
|
|
llvm::InitializeNativeTargetAsmPrinter();
|
|
registry.addExtension(+[](MLIRContext *ctx, BuiltinDialect *dialect) {
|
|
IntegerAttr::attachInterface<TargetAttrImpl>(*ctx);
|
|
});
|
|
registerBuiltinDialectTranslation(registry);
|
|
registerLLVMDialectTranslation(registry);
|
|
registry.insert<gpu::GPUDialect>();
|
|
}
|
|
|
|
// Dialect registry.
|
|
DialectRegistry registry;
|
|
|
|
// MLIR module used for the tests.
|
|
std::string moduleStr = R"mlir(
|
|
llvm.func @foo(%arg0 : i32) {
|
|
llvm.return
|
|
}
|
|
)mlir";
|
|
};
|
|
|
|
TEST_F(MLIRTargetLLVM, SKIP_WITHOUT_NATIVE(SerializeToLLVMBitcode)) {
|
|
MLIRContext context(registry);
|
|
|
|
OwningOpRef<ModuleOp> module =
|
|
parseSourceString<ModuleOp>(moduleStr, &context);
|
|
ASSERT_TRUE(!!module);
|
|
|
|
// Serialize the module.
|
|
std::string targetTriple = llvm::sys::getProcessTriple();
|
|
LLVM::ModuleToObject serializer(*(module->getOperation()), targetTriple, "",
|
|
"");
|
|
std::optional<SmallVector<char, 0>> serializedModule = serializer.run();
|
|
ASSERT_TRUE(!!serializedModule);
|
|
ASSERT_TRUE(!serializedModule->empty());
|
|
|
|
// Read the serialized module.
|
|
llvm::MemoryBufferRef buffer(
|
|
StringRef(serializedModule->data(), serializedModule->size()), "module");
|
|
llvm::LLVMContext llvmContext;
|
|
llvm::Expected<std::unique_ptr<llvm::Module>> llvmModule =
|
|
llvm::getLazyBitcodeModule(buffer, llvmContext);
|
|
ASSERT_TRUE(!!llvmModule);
|
|
ASSERT_TRUE(!!*llvmModule);
|
|
|
|
// Check that it has a function named `foo`.
|
|
ASSERT_TRUE((*llvmModule)->getFunction("foo") != nullptr);
|
|
}
|
|
|
|
std::optional<SmallVector<char, 0>>
|
|
TargetAttrImpl::serializeToObject(Attribute attribute, Operation *module,
|
|
const gpu::TargetOptions &options) const {
|
|
module->setAttr("serialize_attr", UnitAttr::get(module->getContext()));
|
|
std::string targetTriple = llvm::sys::getProcessTriple();
|
|
LLVM::ModuleToObject serializer(*module, targetTriple, "", "");
|
|
return serializer.run();
|
|
}
|
|
|
|
Attribute
|
|
TargetAttrImpl::createObject(Attribute attribute, Operation *module,
|
|
const SmallVector<char, 0> &object,
|
|
const gpu::TargetOptions &options) const {
|
|
return gpu::ObjectAttr::get(
|
|
module->getContext(), attribute, gpu::CompilationTarget::Offload,
|
|
StringAttr::get(module->getContext(),
|
|
StringRef(object.data(), object.size())),
|
|
module->getAttrDictionary(), /*kernels=*/nullptr);
|
|
}
|
|
|
|
TEST_F(MLIRTargetLLVM, SKIP_WITHOUT_NATIVE(TargetAttrAPI)) {
|
|
MLIRContext context(registry);
|
|
context.loadAllAvailableDialects();
|
|
|
|
OwningOpRef<ModuleOp> module =
|
|
parseSourceString<ModuleOp>(moduleStr, &context);
|
|
ASSERT_TRUE(!!module);
|
|
Builder builder(&context);
|
|
IntegerAttr target = builder.getI32IntegerAttr(0);
|
|
auto targetAttr = dyn_cast<gpu::TargetAttrInterface>(target);
|
|
// Check the attribute holds the interface.
|
|
ASSERT_TRUE(!!targetAttr);
|
|
gpu::TargetOptions opts;
|
|
std::optional<SmallVector<char, 0>> serializedBinary =
|
|
targetAttr.serializeToObject(*module, opts);
|
|
// Check the serialized string.
|
|
ASSERT_TRUE(!!serializedBinary);
|
|
ASSERT_TRUE(!serializedBinary->empty());
|
|
// Create the object attribute.
|
|
auto object = cast<gpu::ObjectAttr>(
|
|
targetAttr.createObject(*module, *serializedBinary, opts));
|
|
// Check the object has properties.
|
|
DictionaryAttr properties = object.getProperties();
|
|
ASSERT_TRUE(!!properties);
|
|
// Check that it contains the attribute added to the module in
|
|
// `serializeToObject`.
|
|
ASSERT_TRUE(properties.contains("serialize_attr"));
|
|
}
|