1 //===- LLVMIRToNVVMTranslation.cpp - Translate LLVM IR to NVVM dialect ----===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements a translation between LLVM IR and the MLIR NVVM dialect.
11 //===----------------------------------------------------------------------===//
13 #include "mlir/Target/LLVMIR/Dialect/NVVM/LLVMIRToNVVMTranslation.h"
14 #include "mlir/Dialect/LLVMIR/NVVMDialect.h"
15 #include "mlir/Target/LLVMIR/ModuleImport.h"
17 #include "llvm/IR/IntrinsicsNVPTX.h"
20 using namespace mlir::NVVM
;
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"
28 return convertibleIntrinsics
.contains(id
);
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"
37 return convertibleIntrinsics
;
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
,
44 LLVM::ModuleImport
&moduleImport
) {
45 llvm::Intrinsic::ID intrinsicID
= inst
->getIntrinsicID();
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(intrinsicID
)) {
50 SmallVector
<llvm::Value
*> args(inst
->args());
51 ArrayRef
<llvm::Value
*> llvmOperands(args
);
52 #include "mlir/Dialect/LLVMIR/NVVMFromLLVMIRConversions.inc"
60 /// Implementation of the dialect interface that converts operations belonging
61 /// to the NVVM dialect.
62 class NVVMDialectLLVMIRImportInterface
: public LLVMImportDialectInterface
{
64 using LLVMImportDialectInterface::LLVMImportDialectInterface
;
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(builder
, inst
, moduleImport
);
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();
82 void mlir::registerNVVMDialectImport(DialectRegistry
®istry
) {
83 registry
.insert
<NVVM::NVVMDialect
>();
84 registry
.addExtension(+[](MLIRContext
*ctx
, NVVM::NVVMDialect
*dialect
) {
85 dialect
->addInterfaces
<NVVMDialectLLVMIRImportInterface
>();
89 void mlir::registerNVVMDialectImport(MLIRContext
&context
) {
90 DialectRegistry registry
;
91 registerNVVMDialectImport(registry
);
92 context
.appendDialectRegistry(registry
);