Skip to content

Commit 5ae79ba

Browse files
authored
[MLIR][XeVM] Add XeVM to LLVMIR translation. (#150696)
Add XeVM dialect to LLVMIR translation. Currently no ops are translated. Only xevm.DecorationCacheControl are translated to metadata for spirv decoration - !spirv.DecorationCacheControlINTEL. Co-authored-by: Artem Kroviakov [email protected]
1 parent 6a22580 commit 5ae79ba

File tree

7 files changed

+181
-0
lines changed

7 files changed

+181
-0
lines changed

mlir/include/mlir/Target/LLVMIR/Dialect/All.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "mlir/Target/LLVMIR/Dialect/ROCDL/ROCDLToLLVMIRTranslation.h"
2929
#include "mlir/Target/LLVMIR/Dialect/SPIRV/SPIRVToLLVMIRTranslation.h"
3030
#include "mlir/Target/LLVMIR/Dialect/VCIX/VCIXToLLVMIRTranslation.h"
31+
#include "mlir/Target/LLVMIR/Dialect/XeVM/XeVMToLLVMIRTranslation.h"
3132

3233
namespace mlir {
3334
class DialectRegistry;
@@ -47,6 +48,7 @@ static inline void registerAllToLLVMIRTranslations(DialectRegistry &registry) {
4748
registerROCDLDialectTranslation(registry);
4849
registerSPIRVDialectTranslation(registry);
4950
registerVCIXDialectTranslation(registry);
51+
registerXeVMDialectTranslation(registry);
5052

5153
// Extension required for translating GPU offloading Ops.
5254
gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(registry);
@@ -63,6 +65,7 @@ registerAllGPUToLLVMIRTranslations(DialectRegistry &registry) {
6365
registerNVVMDialectTranslation(registry);
6466
registerROCDLDialectTranslation(registry);
6567
registerSPIRVDialectTranslation(registry);
68+
registerXeVMDialectTranslation(registry);
6669

6770
// Extension required for translating GPU offloading Ops.
6871
gpu::registerOffloadingLLVMTranslationInterfaceExternalModels(registry);
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===-- XeVMToLLVMIRTranslation.h - XeVM to LLVM IR -------------*- 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 XeVM dialect to LLVM IR translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_LLVMIR_DIALECT_XEVM_XEVMTOLLVMIRTRANSLATION_H
14+
#define MLIR_TARGET_LLVMIR_DIALECT_XEVM_XEVMTOLLVMIRTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Register the XeVM dialect and the translation from it to the LLVM IR in the
22+
/// given registry;
23+
void registerXeVMDialectTranslation(mlir::DialectRegistry &registry);
24+
25+
/// Register the XeVM dialect and the translation from it in the registry
26+
/// associated with the given context.
27+
void registerXeVMDialectTranslation(mlir::MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_LLVMIR_DIALECT_XEVM_XEVMTOLLVMIRTRANSLATION_H

mlir/lib/Target/LLVMIR/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,7 @@ add_mlir_translation_library(MLIRToLLVMIRTranslationRegistration
6060
MLIRROCDLToLLVMIRTranslation
6161
MLIRSPIRVToLLVMIRTranslation
6262
MLIRVCIXToLLVMIRTranslation
63+
MLIRXeVMToLLVMIRTranslation
6364
)
6465

6566
add_mlir_translation_library(MLIRTargetLLVMIRImport

mlir/lib/Target/LLVMIR/Dialect/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,4 @@ add_subdirectory(OpenMP)
1010
add_subdirectory(ROCDL)
1111
add_subdirectory(SPIRV)
1212
add_subdirectory(VCIX)
13+
add_subdirectory(XeVM)
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
set(LLVM_OPTIONAL_SOURCES
2+
XeVMToLLVMIRTranslation.cpp
3+
)
4+
5+
add_mlir_translation_library(MLIRXeVMToLLVMIRTranslation
6+
XeVMToLLVMIRTranslation.cpp
7+
8+
DEPENDS
9+
MLIRXeVMConversionsIncGen
10+
11+
LINK_COMPONENTS
12+
Core
13+
14+
LINK_LIBS PUBLIC
15+
MLIRDialectUtils
16+
MLIRIR
17+
MLIRLLVMDialect
18+
MLIRXeVMDialect
19+
MLIRSupport
20+
MLIRTargetLLVMIRExport
21+
)
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//===-- XeVMToLLVMIRTranslation.cpp - Translate XeVM to LLVM IR -*- 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 implements a translation between the MLIR XeVM dialect and
10+
// LLVM IR.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "mlir/Target/LLVMIR/Dialect/XeVM/XeVMToLLVMIRTranslation.h"
15+
#include "mlir/Dialect/LLVMIR/XeVMDialect.h"
16+
#include "mlir/IR/BuiltinAttributes.h"
17+
#include "mlir/IR/Operation.h"
18+
#include "mlir/Target/LLVMIR/ModuleTranslation.h"
19+
20+
#include "llvm/ADT/TypeSwitch.h"
21+
#include "llvm/IR/Constants.h"
22+
#include "llvm/IR/LLVMContext.h"
23+
#include "llvm/IR/Metadata.h"
24+
25+
#include "llvm/IR/ConstantRange.h"
26+
#include "llvm/IR/IRBuilder.h"
27+
#include "llvm/Support/raw_ostream.h"
28+
29+
using namespace mlir;
30+
using namespace mlir::LLVM;
31+
32+
namespace {
33+
/// Implementation of the dialect interface that converts operations belonging
34+
/// to the XeVM dialect to LLVM IR.
35+
class XeVMDialectLLVMIRTranslationInterface
36+
: public LLVMTranslationDialectInterface {
37+
public:
38+
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
39+
40+
/// Attaches module-level metadata for functions marked as kernels.
41+
LogicalResult
42+
amendOperation(Operation *op, ArrayRef<llvm::Instruction *> instructions,
43+
NamedAttribute attribute,
44+
LLVM::ModuleTranslation &moduleTranslation) const final {
45+
StringRef attrName = attribute.getName().getValue();
46+
if (attrName == mlir::xevm::XeVMDialect::getCacheControlsAttrName()) {
47+
auto cacheControlsArray = dyn_cast<ArrayAttr>(attribute.getValue());
48+
if (cacheControlsArray.size() != 2) {
49+
return op->emitOpError(
50+
"Expected both L1 and L3 cache control attributes!");
51+
}
52+
if (instructions.size() != 1) {
53+
return op->emitOpError("Expecting a single instruction");
54+
}
55+
return handleDecorationCacheControl(instructions.front(),
56+
cacheControlsArray.getValue());
57+
}
58+
auto func = dyn_cast<LLVM::LLVMFuncOp>(op);
59+
if (!func)
60+
return failure();
61+
62+
return success();
63+
}
64+
65+
private:
66+
static LogicalResult handleDecorationCacheControl(llvm::Instruction *inst,
67+
ArrayRef<Attribute> attrs) {
68+
SmallVector<llvm::Metadata *> decorations;
69+
llvm::LLVMContext &ctx = inst->getContext();
70+
llvm::Type *i32Ty = llvm::IntegerType::getInt32Ty(ctx);
71+
llvm::transform(
72+
attrs, std::back_inserter(decorations),
73+
[&ctx, i32Ty](Attribute attr) -> llvm::Metadata * {
74+
auto valuesArray = dyn_cast<ArrayAttr>(attr).getValue();
75+
std::array<llvm::Metadata *, 4> metadata;
76+
llvm::transform(
77+
valuesArray, metadata.begin(), [i32Ty](Attribute valueAttr) {
78+
return llvm::ConstantAsMetadata::get(llvm::ConstantInt::get(
79+
i32Ty, cast<IntegerAttr>(valueAttr).getValue()));
80+
});
81+
return llvm::MDNode::get(ctx, metadata);
82+
});
83+
constexpr llvm::StringLiteral decorationCacheControlMDName =
84+
"spirv.DecorationCacheControlINTEL";
85+
inst->setMetadata(decorationCacheControlMDName,
86+
llvm::MDNode::get(ctx, decorations));
87+
return success();
88+
}
89+
};
90+
} // namespace
91+
92+
void mlir::registerXeVMDialectTranslation(::mlir::DialectRegistry &registry) {
93+
registry.insert<xevm::XeVMDialect>();
94+
registry.addExtension(+[](MLIRContext *ctx, xevm::XeVMDialect *dialect) {
95+
dialect->addInterfaces<XeVMDialectLLVMIRTranslationInterface>();
96+
});
97+
}
98+
99+
void mlir::registerXeVMDialectTranslation(::mlir::MLIRContext &context) {
100+
DialectRegistry registry;
101+
registerXeVMDialectTranslation(registry);
102+
context.appendDialectRegistry(registry);
103+
}

mlir/test/Target/LLVMIR/xevm.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: mlir-translate --split-input-file -mlir-to-llvmir %s | FileCheck %s
2+
3+
module {
4+
llvm.func spir_funccc @_Z8prefetchPU3AS1Kcm(!llvm.ptr<1>, i64)
5+
llvm.func @prefetch(%arg0: !llvm.ptr<1>) {
6+
%0 = llvm.mlir.constant(1 : i64) : i64
7+
// CHECK-LABEL: call spir_func void @_Z8prefetchPU3AS1Kcm
8+
// CHECK-SAME: !spirv.DecorationCacheControlINTEL ![[DECO1:.*]]
9+
llvm.call spir_funccc @_Z8prefetchPU3AS1Kcm(%arg0, %0)
10+
{function_type = !llvm.func<void (ptr<1>, i64)>, linkage = #llvm.linkage<external>,
11+
no_unwind, sym_name = "_Z8prefetchPU3AS1Kcm", visibility_ = 0 : i64,
12+
xevm.DecorationCacheControl = [[6442 : i32, 0 : i32, 1 : i32, 0 : i32], [6442 : i32, 1 : i32, 1 : i32, 0 : i32]]}
13+
: (!llvm.ptr<1>, i64) -> ()
14+
llvm.return
15+
}
16+
}
17+
18+
// CHECK: ![[DECO1]] = !{![[DECO2:.*]], ![[DECO3:.*]]}
19+
// CHECK: ![[DECO2]] = !{i32 6442, i32 0, i32 1, i32 0}
20+
// CHECK: ![[DECO3]] = !{i32 6442, i32 1, i32 1, i32 0}
21+

0 commit comments

Comments
 (0)