|
| 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 ®istry) { |
| 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 | +} |
0 commit comments