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/IntrinsicsNVPTX.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(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 | #include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc" |
53 | } |
54 | |
55 | return failure(); |
56 | } |
57 | |
58 | namespace { |
59 | |
60 | /// Implementation of the dialect interface that converts operations belonging |
61 | /// to the NVVM dialect. |
62 | class NVVMDialectLLVMIRImportInterface : public LLVMImportDialectInterface { |
63 | public: |
64 | using LLVMImportDialectInterface::LLVMImportDialectInterface; |
65 | |
66 | /// Converts the LLVM intrinsic to an MLIR NVVM dialect operation if a |
67 | /// conversion exits. Returns failure otherwise. |
68 | LogicalResult convertIntrinsic(OpBuilder &builder, llvm::CallInst *inst, |
69 | LLVM::ModuleImport &moduleImport) const final { |
70 | return convertIntrinsicImpl(odsBuilder&: builder, inst, moduleImport); |
71 | } |
72 | |
73 | /// Returns the list of LLVM IR intrinsic identifiers that are convertible to |
74 | /// MLIR NVVM dialect intrinsics. |
75 | ArrayRef<unsigned> getSupportedIntrinsics() const final { |
76 | return getSupportedIntrinsicsImpl(); |
77 | } |
78 | }; |
79 | |
80 | } // namespace |
81 | |
82 | void mlir::registerNVVMDialectImport(DialectRegistry ®istry) { |
83 | registry.insert<NVVM::NVVMDialect>(); |
84 | registry.addExtension(extensionFn: +[](MLIRContext *ctx, NVVM::NVVMDialect *dialect) { |
85 | dialect->addInterfaces<NVVMDialectLLVMIRImportInterface>(); |
86 | }); |
87 | } |
88 | |
89 | void mlir::registerNVVMDialectImport(MLIRContext &context) { |
90 | DialectRegistry registry; |
91 | registerNVVMDialectImport(registry); |
92 | context.appendDialectRegistry(registry); |
93 | } |
94 | |