From db6fe8dcd45c06cad6f73ec3bcb99f2b79f72a86 Mon Sep 17 00:00:00 2001 From: Petr Kurapov Date: Tue, 29 Oct 2024 03:32:04 -0700 Subject: [PATCH] [GPU] Add xevm.target definition and validation --- include/gc/Dialect/LLVMIR/XeVMOps.td | 35 ++++++++++++- include/gc/Target/LLVM/XeVM/Target.h | 30 +++++++++++ lib/gc/CMakeLists.txt | 1 + lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp | 18 +++++++ lib/gc/Target/CMakeLists.txt | 1 + lib/gc/Target/LLVM/CMakeLists.txt | 17 +++++++ lib/gc/Target/LLVM/XeVM/Target.cpp | 63 ++++++++++++++++++++++++ 7 files changed, 163 insertions(+), 2 deletions(-) create mode 100644 include/gc/Target/LLVM/XeVM/Target.h create mode 100644 lib/gc/Target/CMakeLists.txt create mode 100644 lib/gc/Target/LLVM/CMakeLists.txt create mode 100644 lib/gc/Target/LLVM/XeVM/Target.cpp diff --git a/include/gc/Dialect/LLVMIR/XeVMOps.td b/include/gc/Dialect/LLVMIR/XeVMOps.td index b460a5d8b..efc337281 100644 --- a/include/gc/Dialect/LLVMIR/XeVMOps.td +++ b/include/gc/Dialect/LLVMIR/XeVMOps.td @@ -27,8 +27,6 @@ class XeVM_Attr traits = []> let mnemonic = attrMnemonic; } -def XeVM_TargettAttr : XeVM_Attr<"XeVMTarget", "target"> {} - class XeVM_Op traits = []> : Op; @@ -163,4 +161,37 @@ def XeVM_BlockStore2dOp : XeVM_Op<"blockstore2d">, let hasVerifier = 1; } +def XeVM_TargetAttr : XeVM_Attr<"XeVMTarget", "target"> { + let description = [{ + GPU target attribute for controlling compilation of targets. All + parameters decay into default values if not present. + + Examples: + + 1. Target with default values. + ``` + gpu.module @mymodule [#xevm.target] attributes {...} { + ... + } + ``` + }]; + let parameters = (ins + DefaultValuedParameter<"int", "2", "Optimization level to apply.">:$O, + StringRefParameter<"Target triple.", "\"spirv64-unknown-unknown\"">:$triple, + StringRefParameter<"Target chip.", "\"pvc\"">:$chip + ); + let assemblyFormat = [{ + (`<` struct($O, $triple, $chip)^ `>`)? + }]; + let builders = [ + AttrBuilder<(ins CArg<"int", "2">:$optLevel, + CArg<"StringRef", "\"spirv64-unknown-unknown\"">:$triple, + CArg<"StringRef", "\"pvc\"">:$chip), [{ + return Base::get($_ctxt, optLevel, triple, chip); + }]> + ]; + let skipDefaultBuilders = 1; + let genVerifyDecl = 1; +} + #endif // XEVMIR_OPS diff --git a/include/gc/Target/LLVM/XeVM/Target.h b/include/gc/Target/LLVM/XeVM/Target.h new file mode 100644 index 000000000..c0ecafd88 --- /dev/null +++ b/include/gc/Target/LLVM/XeVM/Target.h @@ -0,0 +1,30 @@ +//===-- Target.h - MLIR XeVM target registration ----------------*- 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 +// +//===----------------------------------------------------------------------===// +// +// This provides registration calls for attaching the XeVM target interface. +// +//===----------------------------------------------------------------------===// + +#ifndef MLIR_TARGET_XEVM_TARGET_H +#define MLIR_TARGET_XEVM_TARGET_H + +namespace mlir { +class DialectRegistry; +class MLIRContext; +namespace xevm { +/// Registers the `TargetAttrInterface` for the `#xevm.target` attribute in +/// the given registry. +void registerXeVMTargetInterfaceExternalModels(DialectRegistry ®istry); + +/// Registers the `TargetAttrInterface` for the `#xevm.target` attribute in +/// the registry associated with the given context. +void registerXeVMTargetInterfaceExternalModels(MLIRContext &context); +} // namespace xevm +} // namespace mlir + +#endif // MLIR_TARGET_XEVM_TARGET_H diff --git a/lib/gc/CMakeLists.txt b/lib/gc/CMakeLists.txt index ea396ecaa..e652a4a30 100644 --- a/lib/gc/CMakeLists.txt +++ b/lib/gc/CMakeLists.txt @@ -1,5 +1,6 @@ add_subdirectory(Analysis) add_subdirectory(CAPI) add_subdirectory(Dialect) +add_subdirectory(Target) add_subdirectory(Transforms) add_subdirectory(ExecutionEngine) diff --git a/lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp b/lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp index 025b7d95c..f7ca5a635 100644 --- a/lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp +++ b/lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp @@ -279,6 +279,24 @@ LogicalResult BlockStore2dOp::verify() { return success(); } +LogicalResult +XeVMTargetAttr::verify(function_ref emitError, int O, + StringRef triple, StringRef chip) { + if (O < 0 || O > 3) { + emitError() << "The optimization level must be a number between 0 and 3."; + return failure(); + } + if (triple.empty()) { + emitError() << "The target triple cannot be empty."; + return failure(); + } + if (chip.empty()) { + emitError() << "The target chip cannot be empty."; + return failure(); + } + return success(); +} + void XeVMDialect::initialize() { // NOLINTBEGIN addOperations< diff --git a/lib/gc/Target/CMakeLists.txt b/lib/gc/Target/CMakeLists.txt new file mode 100644 index 000000000..8bd310735 --- /dev/null +++ b/lib/gc/Target/CMakeLists.txt @@ -0,0 +1 @@ +add_subdirectory(LLVM) diff --git a/lib/gc/Target/LLVM/CMakeLists.txt b/lib/gc/Target/LLVM/CMakeLists.txt new file mode 100644 index 000000000..81d4d5630 --- /dev/null +++ b/lib/gc/Target/LLVM/CMakeLists.txt @@ -0,0 +1,17 @@ +gc_add_mlir_dialect_library(MLIRXeVMTarget + XeVM/Target.cpp + + OBJECT + + ADDITIONAL_HEADER_DIRS + ${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR + ${PROJECT_SOURCE_DIR}/include/gc/Dialect/LLVMIR + + LINK_LIBS PUBLIC + MLIRIR + MLIRExecutionEngineUtils + MLIRSupport + MLIRGPUDialect + MLIRTargetLLVM + GcInterface +) diff --git a/lib/gc/Target/LLVM/XeVM/Target.cpp b/lib/gc/Target/LLVM/XeVM/Target.cpp new file mode 100644 index 000000000..b1ead6b1c --- /dev/null +++ b/lib/gc/Target/LLVM/XeVM/Target.cpp @@ -0,0 +1,63 @@ +//===-- Target.cpp - MLIR LLVM XeVM target compilation ----------*- 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 +// +//===----------------------------------------------------------------------===// +// +// This file defines XeVM target related functions including registration +// calls for the `#xevm.target` compilation attribute. +// +//===----------------------------------------------------------------------===// + +#include "gc/Target/LLVM/XeVM/Target.h" + +#include "gc/Dialect/LLVMIR/XeVMDialect.h" +#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h" +#include "mlir/Dialect/GPU/IR/GPUDialect.h" +#include "mlir/Dialect/LLVMIR/LLVMDialect.h" +#include "mlir/IR/ExtensibleDialect.h" +#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h" +#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h" + +#include "llvm/IR/LegacyPassManager.h" +#include "llvm/Support/MemoryBuffer.h" +#include "llvm/Support/TargetSelect.h" +#include "llvm/Target/TargetMachine.h" + +using namespace mlir; +using namespace mlir::xevm; + +namespace { +// XeVM implementation of the gpu:TargetAttrInterface. +class XeVMTargetAttrImpl + : public gpu::TargetAttrInterface::FallbackModel { +public: + std::optional> + serializeToObject(Attribute attribute, Operation *module, + const gpu::TargetOptions &options) const { /*TODO*/ + return {}; + } + + Attribute createObject(Attribute attribute, Operation *module, + const SmallVector &object, + const gpu::TargetOptions &options) const { /*TODO*/ + return {}; + } +}; +} // namespace + +void mlir::xevm::registerXeVMTargetInterfaceExternalModels( + DialectRegistry ®istry) { + registry.addExtension(+[](MLIRContext *ctx, xevm::XeVMDialect *dialect) { + XeVMTargetAttr::attachInterface(*ctx); + }); +} + +void mlir::xevm::registerXeVMTargetInterfaceExternalModels( + MLIRContext &context) { + DialectRegistry registry; + registerXeVMTargetInterfaceExternalModels(registry); + context.appendDialectRegistry(registry); +}