Skip to content

Commit b599319

Browse files
[GPU] Add xevm.target definition and validation (#400)
1 parent 535f852 commit b599319

File tree

7 files changed

+163
-2
lines changed

7 files changed

+163
-2
lines changed

include/gc/Dialect/LLVMIR/XeVMOps.td

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,6 @@ class XeVM_Attr<string attrName, string attrMnemonic, list<Trait> traits = []>
2727
let mnemonic = attrMnemonic;
2828
}
2929

30-
def XeVM_TargettAttr : XeVM_Attr<"XeVMTarget", "target"> {}
31-
3230
class XeVM_Op<string mnemonic, list<Trait> traits = []> :
3331
Op<XeVM_Dialect, mnemonic, traits>;
3432

@@ -163,4 +161,37 @@ def XeVM_BlockStore2dOp : XeVM_Op<"blockstore2d">,
163161
let hasVerifier = 1;
164162
}
165163

164+
def XeVM_TargetAttr : XeVM_Attr<"XeVMTarget", "target"> {
165+
let description = [{
166+
GPU target attribute for controlling compilation of targets. All
167+
parameters decay into default values if not present.
168+
169+
Examples:
170+
171+
1. Target with default values.
172+
```
173+
gpu.module @mymodule [#xevm.target] attributes {...} {
174+
...
175+
}
176+
```
177+
}];
178+
let parameters = (ins
179+
DefaultValuedParameter<"int", "2", "Optimization level to apply.">:$O,
180+
StringRefParameter<"Target triple.", "\"spirv64-unknown-unknown\"">:$triple,
181+
StringRefParameter<"Target chip.", "\"pvc\"">:$chip
182+
);
183+
let assemblyFormat = [{
184+
(`<` struct($O, $triple, $chip)^ `>`)?
185+
}];
186+
let builders = [
187+
AttrBuilder<(ins CArg<"int", "2">:$optLevel,
188+
CArg<"StringRef", "\"spirv64-unknown-unknown\"">:$triple,
189+
CArg<"StringRef", "\"pvc\"">:$chip), [{
190+
return Base::get($_ctxt, optLevel, triple, chip);
191+
}]>
192+
];
193+
let skipDefaultBuilders = 1;
194+
let genVerifyDecl = 1;
195+
}
196+
166197
#endif // XEVMIR_OPS

include/gc/Target/LLVM/XeVM/Target.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//===-- Target.h - MLIR XeVM target registration ----------------*- C++ -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides registration calls for attaching the XeVM target interface.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_XEVM_TARGET_H
14+
#define MLIR_TARGET_XEVM_TARGET_H
15+
16+
namespace mlir {
17+
class DialectRegistry;
18+
class MLIRContext;
19+
namespace xevm {
20+
/// Registers the `TargetAttrInterface` for the `#xevm.target` attribute in
21+
/// the given registry.
22+
void registerXeVMTargetInterfaceExternalModels(DialectRegistry &registry);
23+
24+
/// Registers the `TargetAttrInterface` for the `#xevm.target` attribute in
25+
/// the registry associated with the given context.
26+
void registerXeVMTargetInterfaceExternalModels(MLIRContext &context);
27+
} // namespace xevm
28+
} // namespace mlir
29+
30+
#endif // MLIR_TARGET_XEVM_TARGET_H

lib/gc/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ add_subdirectory(Analysis)
22
add_subdirectory(CAPI)
33
add_subdirectory(Conversion)
44
add_subdirectory(Dialect)
5+
add_subdirectory(Target)
56
add_subdirectory(Transforms)
67
add_subdirectory(ExecutionEngine)

lib/gc/Dialect/LLVMIR/IR/XeVMDialect.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,6 +279,24 @@ LogicalResult BlockStore2dOp::verify() {
279279
return success();
280280
}
281281

282+
LogicalResult
283+
XeVMTargetAttr::verify(function_ref<InFlightDiagnostic()> emitError, int O,
284+
StringRef triple, StringRef chip) {
285+
if (O < 0 || O > 3) {
286+
emitError() << "The optimization level must be a number between 0 and 3.";
287+
return failure();
288+
}
289+
if (triple.empty()) {
290+
emitError() << "The target triple cannot be empty.";
291+
return failure();
292+
}
293+
if (chip.empty()) {
294+
emitError() << "The target chip cannot be empty.";
295+
return failure();
296+
}
297+
return success();
298+
}
299+
282300
void XeVMDialect::initialize() {
283301
// NOLINTBEGIN
284302
addOperations<

lib/gc/Target/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
add_subdirectory(LLVM)

lib/gc/Target/LLVM/CMakeLists.txt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
gc_add_mlir_dialect_library(MLIRXeVMTarget
2+
XeVM/Target.cpp
3+
4+
OBJECT
5+
6+
ADDITIONAL_HEADER_DIRS
7+
${MLIR_MAIN_INCLUDE_DIR}/mlir/Dialect/LLVMIR
8+
${PROJECT_SOURCE_DIR}/include/gc/Dialect/LLVMIR
9+
10+
LINK_LIBS PUBLIC
11+
MLIRIR
12+
MLIRExecutionEngineUtils
13+
MLIRSupport
14+
MLIRGPUDialect
15+
MLIRTargetLLVM
16+
GcInterface
17+
)

lib/gc/Target/LLVM/XeVM/Target.cpp

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
//===-- Target.cpp - MLIR LLVM XeVM target compilation ----------*- C++ -*-===//
2+
//
3+
// This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines XeVM target related functions including registration
10+
// calls for the `#xevm.target` compilation attribute.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "gc/Target/LLVM/XeVM/Target.h"
15+
16+
#include "gc/Dialect/LLVMIR/XeVMDialect.h"
17+
#include "mlir/Dialect/GPU/IR/CompilationInterfaces.h"
18+
#include "mlir/Dialect/GPU/IR/GPUDialect.h"
19+
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
20+
#include "mlir/IR/ExtensibleDialect.h"
21+
#include "mlir/Target/LLVMIR/Dialect/GPU/GPUToLLVMIRTranslation.h"
22+
#include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
23+
24+
#include "llvm/IR/LegacyPassManager.h"
25+
#include "llvm/Support/MemoryBuffer.h"
26+
#include "llvm/Support/TargetSelect.h"
27+
#include "llvm/Target/TargetMachine.h"
28+
29+
using namespace mlir;
30+
using namespace mlir::xevm;
31+
32+
namespace {
33+
// XeVM implementation of the gpu:TargetAttrInterface.
34+
class XeVMTargetAttrImpl
35+
: public gpu::TargetAttrInterface::FallbackModel<XeVMTargetAttrImpl> {
36+
public:
37+
std::optional<SmallVector<char, 0>>
38+
serializeToObject(Attribute attribute, Operation *module,
39+
const gpu::TargetOptions &options) const { /*TODO*/
40+
return {};
41+
}
42+
43+
Attribute createObject(Attribute attribute, Operation *module,
44+
const SmallVector<char, 0> &object,
45+
const gpu::TargetOptions &options) const { /*TODO*/
46+
return {};
47+
}
48+
};
49+
} // namespace
50+
51+
void mlir::xevm::registerXeVMTargetInterfaceExternalModels(
52+
DialectRegistry &registry) {
53+
registry.addExtension(+[](MLIRContext *ctx, xevm::XeVMDialect *dialect) {
54+
XeVMTargetAttr::attachInterface<XeVMTargetAttrImpl>(*ctx);
55+
});
56+
}
57+
58+
void mlir::xevm::registerXeVMTargetInterfaceExternalModels(
59+
MLIRContext &context) {
60+
DialectRegistry registry;
61+
registerXeVMTargetInterfaceExternalModels(registry);
62+
context.appendDialectRegistry(registry);
63+
}

0 commit comments

Comments
 (0)