| 1 |
//===----------------------------------------------------------------------===// |
| 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 lowering of CIR attributes and operations directly to |
| 10 |
// LLVMIR. |
| 11 |
// |
| 12 |
//===----------------------------------------------------------------------===// |
| 13 |
|
| 14 |
#include "mlir/Dialect/LLVMIR/LLVMDialect.h" |
| 15 |
#include "mlir/IR/DialectRegistry.h" |
| 16 |
#include "mlir/Target/LLVMIR/LLVMTranslationInterface.h" |
| 17 |
#include "mlir/Target/LLVMIR/ModuleTranslation.h" |
| 18 |
#include "clang/CIR/Dialect/IR/CIRAttrs.h" |
| 19 |
#include "clang/CIR/Dialect/IR/CIRDialect.h" |
| 20 |
#include "clang/CIR/MissingFeatures.h" |
| 21 |
#include "llvm/ADT/ArrayRef.h" |
| 22 |
#include "llvm/IR/Constant.h" |
| 23 |
#include "llvm/IR/GlobalVariable.h" |
| 24 |
|
| 25 |
using namespace llvm; |
| 26 |
|
| 27 |
namespace cir { |
| 28 |
namespace direct { |
| 29 |
|
| 30 |
/// Implementation of the dialect interface that converts CIR attributes to LLVM |
| 31 |
/// IR metadata. |
| 32 |
class CIRDialectLLVMIRTranslationInterface |
| 33 |
: public mlir::LLVMTranslationDialectInterface { |
| 34 |
public: |
| 35 |
using LLVMTranslationDialectInterface::LLVMTranslationDialectInterface; |
| 36 |
|
| 37 |
/// Translates the given operation to LLVM IR using the provided IR builder |
| 38 |
/// and saving the state in `moduleTranslation`. |
| 39 |
mlir::LogicalResult convertOperation( |
| 40 |
mlir::Operation *op, llvm::IRBuilderBase &builder, |
| 41 |
mlir::LLVM::ModuleTranslation &moduleTranslation) const final { |
| 42 |
|
| 43 |
if (auto cirOp = llvm::dyn_cast<mlir::LLVM::ZeroOp>(op)) |
| 44 |
moduleTranslation.mapValue(cirOp.getResult()) = |
| 45 |
llvm::Constant::getNullValue( |
| 46 |
moduleTranslation.convertType(cirOp.getType())); |
| 47 |
|
| 48 |
return mlir::success(); |
| 49 |
} |
| 50 |
}; |
| 51 |
|
| 52 |
void registerCIRDialectTranslation(mlir::DialectRegistry ®istry) { |
| 53 |
registry.insert<cir::CIRDialect>(); |
| 54 |
registry.addExtension(+[](mlir::MLIRContext *ctx, cir::CIRDialect *dialect) { |
| 55 |
dialect->addInterfaces<CIRDialectLLVMIRTranslationInterface>(); |
| 56 |
}); |
| 57 |
} |
| 58 |
|
| 59 |
} // namespace direct |
| 60 |
} // namespace cir |
| 61 |
|
| 62 |
namespace mlir { |
| 63 |
void registerCIRDialectTranslation(mlir::MLIRContext &context) { |
| 64 |
mlir::DialectRegistry registry; |
| 65 |
cir::direct::registerCIRDialectTranslation(registry); |
| 66 |
context.appendDialectRegistry(registry); |
| 67 |
} |
| 68 |
} // namespace mlir |