1 //===- VCIXToLLVMIRTranslation.cpp - Translate VCIX to LLVM IR ------------===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a translation between the MLIR VCIX dialect and
12 //===----------------------------------------------------------------------===//
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"
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/IntrinsicsRISCV.h"
22 #include "llvm/Support/raw_ostream.h"
25 using namespace mlir::LLVM
;
26 using mlir::LLVM::detail::createIntrinsicCall
;
28 /// Infer XLen type from opcode's type. This is done to avoid passing target
30 static llvm::Type
*getXlenType(Attribute opcodeAttr
,
31 LLVM::ModuleTranslation
&moduleTranslation
) {
32 auto intAttr
= cast
<IntegerAttr
>(opcodeAttr
);
33 unsigned xlenWidth
= cast
<IntegerType
>(intAttr
.getType()).getWidth();
34 return llvm::Type::getIntNTy(moduleTranslation
.getLLVMContext(), xlenWidth
);
37 /// Return VL for VCIX intrinsic. If vl was previously set, return it,
38 /// otherwise construct a constant using fixed vector type.
39 static llvm::Value
*createVL(llvm::IRBuilderBase
&builder
, llvm::Value
*vl
,
40 VectorType vtype
, llvm::Type
*xlen
, Location loc
,
41 LLVM::ModuleTranslation
&moduleTranslation
) {
43 assert(vtype
.isScalable() &&
44 "vl parameter must be set for scalable vectors");
45 return builder
.CreateZExtOrTrunc(vl
, xlen
);
48 assert(vtype
.getRank() == 1 && "Only 1-d fixed vectors are supported");
49 return mlir::LLVM::detail::getLLVMConstant(
51 IntegerAttr::get(IntegerType::get(&moduleTranslation
.getContext(), 64),
53 loc
, moduleTranslation
);
57 /// Implementation of the dialect interface that converts operations belonging
58 /// to the VCIX dialect to LLVM IR.
59 class VCIXDialectLLVMIRTranslationInterface
60 : public LLVMTranslationDialectInterface
{
62 using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface
;
64 /// Translates the given operation to LLVM IR using the provided IR builder
65 /// and saving the state in `moduleTranslation`.
67 convertOperation(Operation
*op
, llvm::IRBuilderBase
&builder
,
68 LLVM::ModuleTranslation
&moduleTranslation
) const final
{
69 Operation
&opInst
= *op
;
70 #include "mlir/Dialect/LLVMIR/VCIXConversions.inc"
77 void mlir::registerVCIXDialectTranslation(DialectRegistry
®istry
) {
78 registry
.insert
<vcix::VCIXDialect
>();
79 registry
.addExtension(+[](MLIRContext
*ctx
, vcix::VCIXDialect
*dialect
) {
80 dialect
->addInterfaces
<VCIXDialectLLVMIRTranslationInterface
>();
84 void mlir::registerVCIXDialectTranslation(MLIRContext
&context
) {
85 DialectRegistry registry
;
86 registerVCIXDialectTranslation(registry
);
87 context
.appendDialectRegistry(registry
);