-
Notifications
You must be signed in to change notification settings - Fork 13.4k
[mlir][gpu] Pass GPU module to TargetAttrInterface::createObject
.
#94910
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,6 +6,8 @@ | |
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#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" | ||
|
@@ -30,24 +32,46 @@ using namespace mlir; | |
#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>(); | ||
} | ||
}; | ||
|
||
TEST_F(MLIRTargetLLVM, SKIP_WITHOUT_NATIVE(SerializeToLLVMBitcode)) { | ||
// Dialect registry. | ||
DialectRegistry registry; | ||
|
||
// MLIR module used for the tests. | ||
std::string moduleStr = R"mlir( | ||
llvm.func @foo(%arg0 : i32) { | ||
llvm.return | ||
} | ||
)mlir"; | ||
}; | ||
|
||
DialectRegistry registry; | ||
registerBuiltinDialectTranslation(registry); | ||
registerLLVMDialectTranslation(registry); | ||
TEST_F(MLIRTargetLLVM, SKIP_WITHOUT_NATIVE(SerializeToLLVMBitcode)) { | ||
MLIRContext context(registry); | ||
|
||
OwningOpRef<ModuleOp> module = | ||
|
@@ -74,3 +98,52 @@ TEST_F(MLIRTargetLLVM, SKIP_WITHOUT_NATIVE(SerializeToLLVMBitcode)) { | |
// 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()); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can you also add more documentation to the tests, out of the context of this PR it wouldn't be straightforward to understand the intent of all this I believe. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, wrt docs everywhere, would you prefer a fresh patch, or can I bundle the changes into this patch #95292 ? (they are related) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. You're right, I'm touching |
||
|
||
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")); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We're missing documentation here!
(also should it be
GPUModuleOp
instead ofOperation*
)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The
GPUModuleOp
vsOperation*
goes back to the original introduction of these attributes. If you see, all other methods also useOperation*
. Back then, there was a build dependency that required the interfaces to be generated before the dialect, hence the types were not available (I'm not sure this is needed anymore, I'll check). Then a second reason, was to make the header independent ofGPUDialect.h