| 1 | //===- LLVMIRToNVVMTranslation.cpp - Translate LLVM IR to NVVM dialect ----===// |
|---|---|
| 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 LLVM IR and the MLIR NVVM dialect. |
| 10 | // |
| 11 | //===----------------------------------------------------------------------===// |
| 12 | |
| 13 | #include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h" |
| 14 | #include "mlir/Dialect/LLVMIR/NVVMDialect.h" |
| 15 | #include "mlir/Target/LLVMIR/ModuleImport.h" |
| 16 | |
| 17 | #include "llvm/IR/ConstantRange.h" |
| 18 | |
| 19 | using namespace mlir; |
| 20 | using namespace mlir::NVVM; |
| 21 | |
| 22 | /// Returns true if the LLVM IR intrinsic is convertible to an MLIR NVVM dialect |
| 23 | /// intrinsic. Returns false otherwise. |
| 24 | static bool isConvertibleIntrinsic(llvm::Intrinsic::ID id) { |
| 25 | static const DenseSet<unsigned> convertibleIntrinsics = { |
| 26 | #include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc" |
| 27 | }; |
| 28 | return convertibleIntrinsics.contains(V: id); |
| 29 | } |
| 30 | |
| 31 | /// Returns the list of LLVM IR intrinsic identifiers that are convertible to |
| 32 | /// MLIR NVVM dialect intrinsics. |
| 33 | static ArrayRef<unsigned> getSupportedIntrinsicsImpl() { |
| 34 | static const SmallVector<unsigned> convertibleIntrinsics = { |
| 35 | #include "mlir/Dialect/LLVMIR/NVVMConvertibleLLVMIRIntrinsics.inc" |
| 36 | }; |
| 37 | return convertibleIntrinsics; |
| 38 | } |
| 39 | |
| 40 | /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a |
| 41 | /// conversion exits. Returns failure otherwise. |
| 42 | static LogicalResult convertIntrinsicImpl(OpBuilder &odsBuilder, |
| 43 | llvm::CallInst *inst, |
| 44 | LLVM::ModuleImport &moduleImport) { |
| 45 | llvm::Intrinsic::ID intrinsicID = inst->getIntrinsicID(); |
| 46 | |
| 47 | // Check if the intrinsic is convertible to an MLIR dialect counterpart and |
| 48 | // copy the arguments to an an LLVM operands array reference for conversion. |
| 49 | if (isConvertibleIntrinsic(id: intrinsicID)) { |
| 50 | SmallVector<llvm::Value *> args(inst->args()); |
| 51 | ArrayRef<llvm::Value *> llvmOperands(args); |
| 52 | |
| 53 | SmallVector<llvm::OperandBundleUse> llvmOpBundles; |
| 54 | llvmOpBundles.reserve(N: inst->getNumOperandBundles()); |
| 55 | for (unsigned i = 0; i < inst->getNumOperandBundles(); ++i) |
| 56 | llvmOpBundles.push_back(Elt: inst->getOperandBundleAt(Index: i)); |
| 57 | |
| 58 | #include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc" |
| 59 | } |
| 60 | |
| 61 | return failure(); |
| 62 | } |
| 63 | |
| 64 | namespace { |
| 65 | |
| 66 | /// Implementation of the dialect interface that converts operations belonging |
| 67 | /// to the NVVM dialect. |
| 68 | class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface { |
| 69 | public: |
| 70 | using LLVMImportDialectInterface::LLVMImportDialectInterface; |
| 71 | |
| 72 | /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a |
| 73 | /// conversion exits. Returns failure otherwise. |
| 74 | LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, |
| 75 | LLVM::ModuleImport &moduleImport) const final { |
| 76 | return convertIntrinsicImpl(odsBuilder&: builder, inst, moduleImport); |
| 77 | } |
| 78 | |
| 79 | /// Returns the list of LLVM IR intrinsic identifiers that are convertible to |
| 80 | /// MLIR NVVM dialect intrinsics. |
| 81 | ArrayRef<unsigned> getSupportedIntrinsics() const final { |
| 82 | return getSupportedIntrinsicsImpl(); |
| 83 | } |
| 84 | }; |
| 85 | |
| 86 | } // namespace |
| 87 | |
| 88 | void mlir::registerNVVMDialectImport(DialectRegistry ®istry) { |
| 89 | registry.insert<NVVM::NVVMDialect>(); |
| 90 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) { |
| 91 | dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>(); |
| 92 | }); |
| 93 | } |
| 94 | |
| 95 | void mlir::registerNVVMDialectImport(MLIRContext &context) { |
| 96 | DialectRegistry registry; |
| 97 | registerNVVMDialectImport(registry); |
| 98 | context.appendDialectRegistry(registry); |
| 99 | } |
| 100 |
