Skip to content

[GPU] Add xevm.target definition and validation #400

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

Merged
merged 1 commit into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 33 additions & 2 deletions include/gc/Dialect/LLVMIR/XeVMOps.td
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,6 @@ class XeVM_Attr<string attrName, string attrMnemonic, list<Trait> traits = []>
let mnemonic = attrMnemonic;
}

def XeVM_TargettAttr : XeVM_Attr<"XeVMTarget", "target"> {}

class XeVM_Op<string mnemonic, list<Trait> traits = []> :
Op<XeVM_Dialect, mnemonic, traits>;

Expand Down Expand Up @@ -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
30 changes: 30 additions & 0 deletions include/gc/Target/LLVM/XeVM/Target.h
Original file line number Diff line number Diff line change
@@ -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 &registry);

/// 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
1 change: 1 addition & 0 deletions lib/gc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_subdirectory(Analysis)
add_subdirectory(CAPI)
add_subdirectory(Dialect)
add_subdirectory(Target)
add_subdirectory(Transforms)
add_subdirectory(ExecutionEngine)
18 changes: 18 additions & 0 deletions lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,24 @@ LogicalResult BlockStore2dOp::verify() {
return success();
}

LogicalResult
XeVMTargetAttr::verify(function_ref<InFlightDiagnostic()> 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<
Expand Down
1 change: 1 addition & 0 deletions lib/gc/Target/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(LLVM)
17 changes: 17 additions & 0 deletions lib/gc/Target/LLVM/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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
)
63 changes: 63 additions & 0 deletions lib/gc/Target/LLVM/XeVM/Target.cpp
Original file line number Diff line number Diff line change
@@ -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<XeVMTargetAttrImpl> {
public:
std::optional<SmallVector<char, 0>>
serializeToObject(Attribute attribute, Operation *module,
const gpu::TargetOptions &options) const { /*TODO*/
return {};
}

Attribute createObject(Attribute attribute, Operation *module,
const SmallVector<char, 0> &object,
const gpu::TargetOptions &options) const { /*TODO*/
return {};
}
};
} // namespace

void mlir::xevm::registerXeVMTargetInterfaceExternalModels(
DialectRegistry &registry) {
registry.addExtension(+[](MLIRContext *ctx, xevm::XeVMDialect *dialect) {
XeVMTargetAttr::attachInterface<XeVMTargetAttrImpl>(*ctx);
});
}

void mlir::xevm::registerXeVMTargetInterfaceExternalModels(
MLIRContext &context) {
DialectRegistry registry;
registerXeVMTargetInterfaceExternalModels(registry);
context.appendDialectRegistry(registry);
}