MLIR  22.0.0git
VCIXToLLVMIRTranslation.cpp
Go to the documentation of this file.
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 
17 #include "mlir/IR/Operation.h"
19 
20 #include "llvm/IR/IRBuilder.h"
21 #include "llvm/IR/IntrinsicsRISCV.h"
22 
23 using namespace mlir;
24 using namespace mlir::LLVM;
26 
27 /// Infer XLen type from opcode's type. This is done to avoid passing target
28 /// option around.
29 static llvm::Type *getXlenType(Attribute opcodeAttr,
30  LLVM::ModuleTranslation &moduleTranslation) {
31  auto intAttr = cast<IntegerAttr>(opcodeAttr);
32  unsigned xlenWidth = cast<IntegerType>(intAttr.getType()).getWidth();
33  return llvm::Type::getIntNTy(moduleTranslation.getLLVMContext(), 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.
38 static 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(vl, xlen);
45  }
46 
47  assert(vtype.getRank() == 1 && "Only 1-d fixed vectors are supported");
49  xlen,
50  IntegerAttr::get(IntegerType::get(&moduleTranslation.getContext(), 64),
51  vtype.getShape()[0]),
52  loc, moduleTranslation);
53 }
54 
55 namespace {
56 /// Implementation of the dialect interface that converts operations belonging
57 /// to the VCIX dialect to LLVM IR.
58 class VCIXDialectLLVMIRTranslationInterface
60 public:
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 
77  registry.insert<vcix::VCIXDialect>();
78  registry.addExtension(+[](MLIRContext *ctx, vcix::VCIXDialect *dialect) {
79  dialect->addInterfaces<VCIXDialectLLVMIRTranslationInterface>();
80  });
81 }
82 
84  DialectRegistry registry;
86  context.appendDialectRegistry(registry);
87 }
static llvm::Value * createVL(llvm::IRBuilderBase &builder, llvm::Value *vl, VectorType vtype, llvm::Type *xlen, Location loc, LLVM::ModuleTranslation &moduleTranslation)
Return VL for VCIX intrinsic.
static llvm::Type * getXlenType(Attribute opcodeAttr, LLVM::ModuleTranslation &moduleTranslation)
Infer XLen type from opcode's type.
Attributes are known-constant values of operations.
Definition: Attributes.h:25
The DialectRegistry maps a dialect namespace to a constructor for the matching dialect.
bool addExtension(TypeID extensionID, std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Base class for dialect interfaces providing translation to LLVM IR.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:76
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:63
void appendDialectRegistry(const DialectRegistry &registry)
Append the contents of the given dialect registry to the registry associated with this context.
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
llvm::CallInst * createIntrinsicCall(llvm::IRBuilderBase &builder, llvm::Intrinsic::ID intrinsic, ArrayRef< llvm::Value * > args={}, ArrayRef< llvm::Type * > tys={})
Creates a call to an LLVM IR intrinsic function with the given arguments.
llvm::Constant * getLLVMConstant(llvm::Type *llvmType, Attribute attr, Location loc, const ModuleTranslation &moduleTranslation)
Create an LLVM IR constant of llvmType from the MLIR attribute attr.
Include the generated interface declarations.
void registerVCIXDialectTranslation(DialectRegistry &registry)
Register the VCIX dialect and the translation from it to the LLVM IR in the given registry.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...