1//===- VCIXToLLVMIRTranslation.cpp - Translate VCIX to LLVM IR ------------===//
2//
3// Part of the LLVM Project, 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 VCIX dialect and
10// LLVM IR.
11//
12//===----------------------------------------------------------------------===//
13
14#include "mlir/Target/LLVMIR/Dialect/VCIX/VCIXToLLVMIRTranslation.h"
15#include "mlir/Dialect/LLVMIR/VCIXDialect.h"
16#include "mlir/IR/BuiltinAttributes.h"
17#include "mlir/IR/Operation.h"
18#include "mlir/Target/LLVMIR/ModuleTranslation.h"
19
20#include "llvm/IR/IRBuilder.h"
21#include "llvm/IR/IntrinsicsRISCV.h"
22
23using namespace mlir;
24using namespace mlir::LLVM;
25using mlir::LLVM::detail::createIntrinsicCall;
26
27/// Infer XLen type from opcode's type. This is done to avoid passing target
28/// option around.
29static llvm::Type *getXlenType(Attribute opcodeAttr,
30 LLVM::ModuleTranslation &moduleTranslation) {
31 auto intAttr = cast<IntegerAttr>(Val&: opcodeAttr);
32 unsigned xlenWidth = cast<IntegerType>(Val: intAttr.getType()).getWidth();
33 return llvm::Type::getIntNTy(C&: moduleTranslation.getLLVMContext(), N: xlenWidth);
34}
35
36/// Return VL for VCIX intrinsic. If vl was previously set, return it,
37/// otherwise construct a constant using fixed vector type.
38static llvm::Value *createVL(llvm::IRBuilderBase &builder, llvm::Value *vl,
39 VectorType vtype, llvm::Type *xlen, Location loc,
40 LLVM::ModuleTranslation &moduleTranslation) {
41 if (vl) {
42 assert(vtype.isScalable() &&
43 "vl parameter must be set for scalable vectors");
44 return builder.CreateZExtOrTrunc(V: vl, DestTy: xlen);
45 }
46
47 assert(vtype.getRank() == 1 && "Only 1-d fixed vectors are supported");
48 return mlir::LLVM::detail::getLLVMConstant(
49 llvmType: xlen,
50 attr: IntegerAttr::get(type: IntegerType::get(context: &moduleTranslation.getContext(), width: 64),
51 value: vtype.getShape()[0]),
52 loc, moduleTranslation);
53}
54
55namespace {
56/// Implementation of the dialect interface that converts operations belonging
57/// to the VCIX dialect to LLVM IR.
58class VCIXDialectLLVMIRTranslationInterface
59 : public LLVMTranslationDialectInterface {
60public:
61 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
62
63 /// Translates the given operation to LLVM IR using the provided IR builder
64 /// and saving the state in `moduleTranslation`.
65 LogicalResult
66 convertOperation(Operation *op, llvm::IRBuilderBase &builder,
67 LLVM::ModuleTranslation &moduleTranslation) const final {
68 Operation &opInst = *op;
69#include "mlir/Dialect/LLVMIR/VCIXConversions.inc"
70
71 return failure();
72 }
73};
74} // namespace
75
76void mlir::registerVCIXDialectTranslation(DialectRegistry &registry) {
77 registry.insert<vcix::VCIXDialect>();
78 registry.addExtension(extensionFn: +[](MLIRContext *ctx, vcix::VCIXDialect *dialect) {
79 dialect->addInterfaces<VCIXDialectLLVMIRTranslationInterface>();
80 });
81}
82
83void mlir::registerVCIXDialectTranslation(MLIRContext &context) {
84 DialectRegistry registry;
85 registerVCIXDialectTranslation(registry);
86 context.appendDialectRegistry(registry);
87}
88

source code of mlir/lib/Target/LLVMIR/Dialect/VCIX/VCIXToLLVMIRTranslation.cpp