xref: /freebsd/contrib/llvm-project/clang/lib/CIR/Lowering/DirectToLLVM/LowerToLLVMIR.cpp (revision 700637cbb5e582861067a11aaca4d053546871d2)
1*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
2*700637cbSDimitry Andric //
3*700637cbSDimitry Andric // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*700637cbSDimitry Andric // See https://llvm.org/LICENSE.txt for license information.
5*700637cbSDimitry Andric // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*700637cbSDimitry Andric //
7*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
8*700637cbSDimitry Andric //
9*700637cbSDimitry Andric // This file implements lowering of CIR attributes and operations directly to
10*700637cbSDimitry Andric // LLVMIR.
11*700637cbSDimitry Andric //
12*700637cbSDimitry Andric //===----------------------------------------------------------------------===//
13*700637cbSDimitry Andric 
14*700637cbSDimitry Andric #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
15*700637cbSDimitry Andric #include "mlir/IR/DialectRegistry.h"
16*700637cbSDimitry Andric #include "mlir/Target/LLVMIR/LLVMTranslationInterface.h"
17*700637cbSDimitry Andric #include "mlir/Target/LLVMIR/ModuleTranslation.h"
18*700637cbSDimitry Andric #include "clang/CIR/Dialect/IR/CIRAttrs.h"
19*700637cbSDimitry Andric #include "clang/CIR/Dialect/IR/CIRDialect.h"
20*700637cbSDimitry Andric #include "clang/CIR/MissingFeatures.h"
21*700637cbSDimitry Andric #include "llvm/ADT/ArrayRef.h"
22*700637cbSDimitry Andric #include "llvm/IR/Constant.h"
23*700637cbSDimitry Andric #include "llvm/IR/GlobalVariable.h"
24*700637cbSDimitry Andric 
25*700637cbSDimitry Andric using namespace llvm;
26*700637cbSDimitry Andric 
27*700637cbSDimitry Andric namespace cir {
28*700637cbSDimitry Andric namespace direct {
29*700637cbSDimitry Andric 
30*700637cbSDimitry Andric /// Implementation of the dialect interface that converts CIR attributes to LLVM
31*700637cbSDimitry Andric /// IR metadata.
32*700637cbSDimitry Andric class CIRDialectLLVMIRTranslationInterface
33*700637cbSDimitry Andric     : public mlir::LLVMTranslationDialectInterface {
34*700637cbSDimitry Andric public:
35*700637cbSDimitry Andric   using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface;
36*700637cbSDimitry Andric 
37*700637cbSDimitry Andric   /// Translates the given operation to LLVM IR using the provided IR builder
38*700637cbSDimitry Andric   /// and saving the state in `moduleTranslation`.
convertOperation(mlir::Operation * op,llvm::IRBuilderBase & builder,mlir::LLVM::ModuleTranslation & moduleTranslation) const39*700637cbSDimitry Andric   mlir::LogicalResult convertOperation(
40*700637cbSDimitry Andric       mlir::Operation *op, llvm::IRBuilderBase &builder,
41*700637cbSDimitry Andric       mlir::LLVM::ModuleTranslation &moduleTranslation) const final {
42*700637cbSDimitry Andric 
43*700637cbSDimitry Andric     if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op))
44*700637cbSDimitry Andric       moduleTranslation.mapValue(cirOp.getResult()) =
45*700637cbSDimitry Andric           llvm::Constant::getNullValue(
46*700637cbSDimitry Andric               moduleTranslation.convertType(cirOp.getType()));
47*700637cbSDimitry Andric 
48*700637cbSDimitry Andric     return mlir::success();
49*700637cbSDimitry Andric   }
50*700637cbSDimitry Andric };
51*700637cbSDimitry Andric 
registerCIRDialectTranslation(mlir::DialectRegistry & registry)52*700637cbSDimitry Andric void registerCIRDialectTranslation(mlir::DialectRegistry &registry) {
53*700637cbSDimitry Andric   registry.insert<cir::CIRDialect>();
54*700637cbSDimitry Andric   registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) {
55*700637cbSDimitry Andric     dialect->addInterfaces<CIRDialectLLVMIRTranslationInterface>();
56*700637cbSDimitry Andric   });
57*700637cbSDimitry Andric }
58*700637cbSDimitry Andric 
59*700637cbSDimitry Andric } // namespace direct
60*700637cbSDimitry Andric } // namespace cir
61*700637cbSDimitry Andric 
62*700637cbSDimitry Andric namespace mlir {
registerCIRDialectTranslation(mlir::MLIRContext & context)63*700637cbSDimitry Andric void registerCIRDialectTranslation(mlir::MLIRContext &context) {
64*700637cbSDimitry Andric   mlir::DialectRegistry registry;
65*700637cbSDimitry Andric   cir::direct::registerCIRDialectTranslation(registry);
66*700637cbSDimitry Andric   context.appendDialectRegistry(registry);
67*700637cbSDimitry Andric }
68*700637cbSDimitry Andric } // namespace mlir
69