MLIR  19.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 #include "llvm/Support/raw_ostream.h"
23 
24 using namespace mlir;
25 using namespace mlir::LLVM;
27 
28 /// Infer XLen type from opcode's type. This is done to avoid passing target
29 /// option around.
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);
35 }
36 
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) {
42  if (vl) {
43  assert(vtype.isScalable() &&
44  "vl parameter must be set for scalable vectors");
45  return builder.CreateZExtOrTrunc(vl, xlen);
46  }
47 
48  assert(vtype.getRank() == 1 && "Only 1-d fixed vectors are supported");
50  xlen,
51  IntegerAttr::get(IntegerType::get(&moduleTranslation.getContext(), 64),
52  vtype.getShape()[0]),
53  loc, moduleTranslation);
54 }
55 
56 namespace {
57 /// Implementation of the dialect interface that converts operations belonging
58 /// to the VCIX dialect to LLVM IR.
59 class VCIXDialectLLVMIRTranslationInterface
61 public:
63 
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"
71 
72  return failure();
73  }
74 };
75 } // namespace
76 
78  registry.insert<vcix::VCIXDialect>();
79  registry.addExtension(+[](MLIRContext *ctx, vcix::VCIXDialect *dialect) {
80  dialect->addInterfaces<VCIXDialectLLVMIRTranslationInterface>();
81  });
82 }
83 
85  DialectRegistry registry;
87  context.appendDialectRegistry(registry);
88 }
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.
void addExtension(std::unique_ptr< DialectExtensionBase > extension)
Add the given extension to the registry.
Base class for dialect interfaces providing translation to LLVM IR.
Implementation class for module translation.
llvm::LLVMContext & getLLVMContext() const
Returns the LLVM context in which the IR is being constructed.
MLIRContext & getContext()
Returns the MLIR context of the module being translated.
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Definition: Location.h:63
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
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.
LogicalResult failure(bool isFailure=true)
Utility function to generate a LogicalResult.
Definition: LogicalResult.h:62
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...
This class represents an efficient way to signal success or failure.
Definition: LogicalResult.h:26